SigTerm说: “闻起来就像一个糟糕的代码.squareOne..squareSix应该是数组。而不是char常量,应该使用枚举或整数......” 如果有人可以建议我必须使用的代码来让用户输入游戏的X和O的行和列,我如何使用此代码为董事会。
void showboard(char &squareOne, char &squareTwo, char &squareThree, char &squareFour, char &squareFive, char &squareSix, char &squareSeven,
char &squareEight, char &squareNine)
{
cout << squareOne << "|" << squareTwo << "|" << squareThree << endl
<< "-+-+-"<< endl
<< squareFour << "|" << squareFive << "|" << squareSix << endl
<< "-+-+-"<< endl
<< squareSeven << "|" << squareEight << "|" << squareNine << endl;
我遇到的问题是将此代码转换为函数。 下面列出的是我想要成为函数的代码。紧随其后的是“检查板”功能,我试图将其上方的代码转换为函数
bGameOver = false;
bool bWinGame = true;
// Check for end of game conditions
if (squareOne != '1') {
if (squareTwo == squareOne && squareThree == squareOne) {
bGameOver = true;
}
if (squareFour == squareOne && squareSeven == squareOne) {
bGameOver = true;
}
}
if (squareFive != '5')
{
if (squareOne == squareFive && squareNine == squareFive)
{
bGameOver = true;
}
if (squareTwo == squareFive && squareEight == squareFive)
{
bGameOver = true;
}
if (squareFour == squareFive && squareSix == squareFive)
{
bGameOver = true;
}
if (squareThree == squareFive && squareSeven == squareFive)
{
bGameOver = true;
}
}
if (squareNine != '9')
{
if (squareThree == squareNine && squareSix == squareNine)
{
bGameOver = true;
}
if (squareSeven == squareNine && squareEight == squareNine)
{
bGameOver = true;
}
}
/* Need to check the board full (no-win condition*/)
if (squareOne != '1' && squareTwo != '2' && squareThree != '3' &&
squareFour != '4' && squareFive != '5' && squareSix != '6' &&
squareSeven != '7' && squareEight != '8' && squareNine != '9' && !bGameOver)
{
bGameOver = true;
bWinGame = false;
}
system("cls");
if (bGameOver)
{
if (bWinGame)
{
cout << "Player " << currentPlayer << " wins!" << endl;
}
// Print ending board
cout << squareOne << "|" << squareTwo << "|" << squareThree << endl;
cout << "-+-+-"<< endl;
cout << squareFour << "|" << squareFive << "|" << squareSix << endl;
cout << "-+-+-"<< endl;
cout << squareSeven << "|" << squareEight << "|" << squareNine << endl;
cout << "Game Over!" << endl;
cout << "Play again (y/n)?" << endl;
char cPlayAgain;
cin >> cPlayAgain;
if (cPlayAgain == 'y')
{
bGameOver = false;
/* Clear the board*/
squareOne = '1';
squareTwo = '2';
squareThree = '3';
squareFour = '4';
squareFive = '5';
squareSix = '6';
squareSeven = '7';
squareEight = '8';
squareNine = '9';
}
currentPlayer[100] = 1;
}
} while (!bGameOver);
void checkboard(char** pCurrentPlayer, char &squareOne, char &squareTwo, char &squareThree, char &squareFour, char &squareFive, char &squareSix, char &squareSeven,
char &squareEight, char &squareNine, char &playerMarker, bool bGameOver)
{
bGameOver = false;
bool bWinGame = true;
// Check for end of game conditions
if (squareOne != '1') {
if (squareTwo == squareOne && squareThree == squareOne) {
bGameOver = true;
}
if (squareFour == squareOne && squareSeven == squareOne) {
bGameOver = true;
}
}
if (squareFive != '5')
{
if (squareOne == squareFive && squareNine == squareFive)
{
bGameOver = true;
}
if (squareTwo == squareFive && squareEight == squareFive)
{
bGameOver = true;
}
if (squareFour == squareFive && squareSix == squareFive)
{
bGameOver = true;
}
if (squareThree == squareFive && squareSeven == squareFive)
{
bGameOver = true;
}
}
if (squareNine != '9')
{
if (squareThree == squareNine && squareSix == squareNine)
{
bGameOver = true;
}
if (squareSeven == squareNine && squareEight == squareNine)
{
bGameOver = true;
}
}
// Need to check the board full (no-win condition)
if (squareOne != '1' && squareTwo != '2' && squareThree != '3' &&
squareFour != '4' && squareFive != '5' && squareSix != '6' &&
squareSeven != '7' && squareEight != '8' && squareNine != '9' && !bGameOver)
{
bGameOver = true;
bWinGame = false;
}
system("cls");
if (bGameOver)
{
if (bWinGame)
{
cout << "Player " << pCurrentPlayer << " wins!" << endl;
}
// Print ending board
cout << squareOne << "|" << squareTwo << "|" << squareThree << endl;
cout << "-+-+-"<< endl;
cout << squareFour << "|" << squareFive << "|" << squareSix << endl;
cout << "-+-+-"<< endl;
cout << squareSeven << "|" << squareEight << "|" << squareNine << endl;
cout << "Game Over!" << endl;
cout << "Play again (y/n)?" << endl;
char cPlayAgain;
cin >> cPlayAgain;
if (cPlayAgain == 'y')
{
bGameOver = false;
// Clear the board
squareOne = '1';
squareTwo = '2';
squareThree = '3';
squareFour = '4';
squareFive = '5';
squareSix = '6';
squareSeven = '7';
squareEight = '8';
squareNine = '9';
}
pCurrentPlayer = "1";
}
}
答案 0 :(得分:1)
在PlayerMove()
中,您将pCurrentPlayer作为char **
传递,这是指向指针的指针。将其更改为char *
然后它应该在函数内正确显示内容。您没有更改该函数中指针的值,因此它不需要额外的间接。
从相应的函数调用参数中删除运算符(&amp;)的地址。
答案 1 :(得分:0)
这一行:
currentPlayer[100] = 1;
非常有问题。
currentPlayer
应该指向playerOne
或playerTwo
。
我不知道你要用这条线做什么,但显然是错的。
答案 2 :(得分:0)
http://alongbutsimplenameformyblog.blogspot.com/2010/08/tic-tac-toe.html
在这里,我尽力编写紧凑但易读的代码,并牢记性能。我得到的程序是32行代码。我唯一不做的就是让游戏“可玩”。
如果查看我的代码,您将能够看到将游戏组织成多维数组的方法。你读它的方式(假设你不知道,对不起,如果我告诉你你知道的事情)是:
arrayName[rowNumber][columnNumber];
所以,为了将tic tac toe的右上方框改为'x',你会说:
arrayName[0][2] = 'x';
请记住,当你使用数组时,它从元素0开始,而不是元素1.这是我在开始时忘记的东西,至少。
所以,希望阅读我的代码,你将能够在一个函数中看到如何相当容易地检查胜利条件。如果您对我的代码如何工作有疑问,或者对如何实现此代码有任何疑问,请随意。我会定期回来。此外,当学年开始于秋季时,我应该写博客更多关于这样的事情,当我在我的课程中帮助一年级和二年级学生时。所以请随时添加书签或订阅。
#include < iostream >
const char grid[3][3] = {'x', ' ', 'o', 'o','x', 'o', 'x', 'x', 'o'};
inline const bool checkLine(char x, char y, char z) {
return (x == y && y == z && x != ' ');
}
inline const bool checkWin(){
bool a = 0, b = 0;
for(unsigned char i = 0; i < 3; ++i)
if(checkLine(grid[i][0], grid[i][1], grid[i][2]) || checkLine(grid[0][i], grid[1][i], grid[2][i]))
return true;
for(unsigned char i = 0; i < 2; ++i)
if(checkLine(grid[i == 0 ? 0 : 2][0], grid[1][1], grid[i == 0 ? 2 : 0][2]))
return true;
return false;
}
int main(){
for (unsigned char i = 0; i < 3; ++i) {
for (unsigned char j = 0; j < 3; ++j)
std::cout << grid[i][j];
std::cout << "\n";
}
if (checkWin())
std::cout << "WINNER\n";
return 0;
}