我的主要问题是,如何让令牌(X和O)出现在游戏板上?我必须向用户询问行号和列号,并且令牌将放置在此处。如您所知,我是编程的新手。我已经呆了几个小时了,我只是想做。在这里问是我的最后一个手段。如果任何代码可以做出不同或更有效或不需要的代码,或者我做错了什么,请告诉我。我没有想法。我只是想完成这个程序,所以您越详细,越好。我非常感谢。我很困惑,真的需要建议来完成此操作。
到目前为止我的代码:
#include <iostream>
using namespace std;
void createBoard(int board[][3], char charBoard[][3]);
void printBoard(char charBoard[][3]);
int checkWinner (int board[][3], int player);
bool checkMove(int board[][3], int row, int column);
void resetBoard(int board[][3]);
int main ()
{
int board[3][3];
char charBoard[3][3];
int player;
int row, column;
bool win = false;
char again = 'y';
while (win == false)
{
createBoard(board, charBoard);
printBoard(charBoard);
cout << "Player O's turn!" << endl;
cout << "Enter row number: "; cin >> row;
cout << "Enter column number: "; cin >> column;
checkMove(board, row, column);
//update board
win = checkWinner(board, -1);
if (checkWinner (board, -1) == true)
{
cout << "Player O wins!" << endl;
break;
}
//set flag to denote it's x's turn
printBoard(charBoard);
cout << "Player X's turn!" << endl;
cout << "Enter row number: "; cin >> row;
cout << "Enter column number: "; cin >> column;
checkMove(board, row, column);
//update board
win = checkWinner (board, 1);
if (checkWinner (board, 1) == true)
{
cout << "Player X wins!" << endl;
break;
}
cout << endl << endl;
cout << "Would you like to play again? (y/n): ";
cin >> again;
}
cout << endl << endl;
cout << "Good bye!" << endl;
return 0;
}
void createBoard(int board[][3], char charBoard[][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
// if game board has a -1, put an "O" in the character board
if (board[i][j]== -1)
{
charBoard[i][j] = 'O';
}
// if game board has a 1, put an "X" in the character board
else if (board[i][j]== 1)
{
charBoard[i][j] = 'X';
}
// if game board has a 0, put an " " (blank space) in the character board
else
{
charBoard[i][j] = ' ';
}
}
}
}
void printBoard(char charBoard[][3])
{
cout << " Tic Tac Toe!" << endl << endl;
cout << " Columnn";
cout << " 0 1 2" << endl << endl;
cout << "R 0 " << charBoard[0][0] << " | " << charBoard[0][1] << " | " << charBoard[0][2] << endl;
cout << " --------------------" << endl;
cout << "O 1 " << charBoard[1][0] << " | " << charBoard[1][1] << " | " << charBoard[1][2] << endl;
cout << " --------------------" << endl;
cout << "W 2 " << charBoard[2][0] << " | " << charBoard[2][1] << " | " << charBoard[2][2] << endl << endl;
}
int checkWinner (int board[][3], int player)
{
int sum;
// Check each column for a winner
for (int i = 0; i < 3; i++)
{
sum = board[i][0] + board[i][1] + board[i][2];
// if O is a winner
if (sum == 3*player)
{
return true;
}
}
// Check each row for a winner
for (int j = 0; j < 3; j++)
{
sum = board[0][j] + board[1][j] + board[2][j];
// if O is a winner
if (sum == 3*player)
{
return true;
}
}
// Check the back and front diagonal
sum = board[0][0] + board[1][1] + board[2][2];
if (sum == 3*player)
{
return true;
}
sum = board[0][2] + board[1][1] + board[2][0];
if (sum == 3*player)
{
return true;
}
return false;
}
bool checkMove(int board[][3], int row, int column)
{
if ((row < 0) || (row > 2))
{
return false;
}
else if ((column < 0) || (column > 2))
{
return false;
}
else if (board[row][column] == 0)
{
return true;
}
else
{
return false;
}
}
void resetBoard(int board[][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = 0;
}
}
}
看起来您应该在'checkMove'函数中插入适当的值:
else if (board[row][column] == 0)
{
board[row][column] = value; // value -1 or 1
return true;
}
其中"值"为-1或1,具体取决于您是否插入X或O。传递"值"的功能。