게임은 매트릭스를 표시하지만 현재 이동을 업데이트하지 않습니다. 어떻게하면됩니까?C++ Tic-Tac-Toe 클래스 사용
나는 지금까지 내가 가지고있는 코드를 보여줄 것이다. 보드에 대한 매트릭스를 정의합니다. 함수는 보드를 그릴 것이고, 플레이어로부터 움직일 것이며, 회전을 토글 할 것입니다. 보드를 그리는 내 기능이 행렬을 표시하지만 움직임을 업데이트하지 않습니다 : 여기에
#ifndef TICTACTOE_H
#define TICTACTOE_H
class TicTacToe
{
private:
char board[3][3];
public:
void DrawBoard();
void PrintBoard();
void GetMove(int move);
void TogglePlayer(char player);
bool DetermineDraw();
};
#endif
는 구현 파일입니다.
#include <iostream>
#include "TicTacToe.h"
using namespace std;
void TicTacToe::DrawBoard()
{
system("cls");
cout <<"\tWelcome to the Classes Tic Tac Toe! \n";
char board[3][3] =
{
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
};
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
cout << board[i][j] << " ";
}
cout << endl;
}
}
void TicTacToe::GetMove(int move)
{
char player = 'X';
cout <<"\nEnter the number of the field you would like to move:\n";
cin >> move;
if(move == 1)
{
board[0][0] = player;
}
else if(move == 2)
{
board[0][1] = player;
}
else if(move == 3)
{
board[0][2] = player;
}
else if(move == 4)
{
board[1][0] = player;
}
else if(move == 5)
{
board[1][1] = player;
}
else if(move == 6)
{
board[1][2] = player;
}
else if(move == 7)
{
board[2][0] = player;
}
else if(move == 8)
{
board[2][1] = player;
}
else if(move == 9)
{
board[2][2] = player;
}
}
void TicTacToe::TogglePlayer(char player)
{
if (player == 'X')
player = 'O';
else if(player == 'O')
player = 'X';
}
bool TicTacToe::DetermineDraw()
{
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
if(board[i][j] == 'X' && board[i][j] == 'O')
return true;
else
return false;
}
}
}
여기 주 파일이 있는데, 나는 추첨이 아닌 동안 계속 게임을 반복합니다. 이사회에 이사가 표시되지 않은 이유를 모르겠습니다. 이 로컬 변수 정의 board
사용하므로
#include <iostream>
#include "TicTacToe.h"
using namespace std;
int main()
{
TicTacToe game;
char player = 'X';
while(game.DetermineDraw() == false)
{
game.DrawBoard();
game.GetMove(player);
game.TogglePlayer(player);
}
system("pause");
}
문제를 일으키는 디버거를 사용할 때? –
'board [move/3] [move % 3] = player' :'if' 문을 제거하십시오. –
'TogglePlayer' 함수는'player' 매개 변수의 ** copy **를 변경 한 다음 종료합니다. 원래의'player' 변수는 변경되지 않았습니다. 참조로 전달 하시겠습니까? 아니면'TicTacToe' 클래스에'player' 변수를 넣을 계획입니까? –