gpt4 book ai didi

c++ - C++ 中的 Tic-Tac-Toe 帮助,如何制作循环以便 Tic Tac Toe 游戏每次都会重复棋盘

转载 作者:行者123 更新时间:2023-11-30 03:13:32 27 4
gpt4 key购买 nike

我是初学者,所以我的代码很乱。我还没有完整地评论这个游戏,所以如果你需要澄清一些变量,我可以给你。

(顺便说一句,这是一个要求制作井字游戏的c++项目)

我的主要问题是,我将如何重复我的棋盘(每次有人在井字游戏中移动时都会更新)?我想不出这样做的方法,所以如果有人给我想法,而不是直接的答案,我将不胜感激。

下面我的代码只是为了让您了解我在做什么,以及如果您对如何修复我的代码有任何建议(即组织或错误,发生的可能性为 100%)。

    #include <iostream>
using namespace std;
char a[3][3];//sets 3x3 matrix
a[0][0]='1';//upper row left corner is 1
a[0][1]='2';//upper row middle is 2
a[0][2]='3';//upper row right corner is 3
a[1][0]='4';//middle row left is 4
a[1][1]='5';//middle row middle is 5
a[1][2]='6';//middle row right is 6
a[2][0]='7';//bottom row left is 7
a[2][1]='8';//bottom row middle is 8
a[2][2]='9';//bottom row right is 9




cout << " | | " << endl;//all these "shapes" make the board.

cout << " " << a[0][0] << " | " << a[0][1] << " | " << a[0][2] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << a[1][0] << " | " << a[1][1] << " | " << a[1][2] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << a[2][0] << " | " << a[2][1] << " | " << a[2][2] << endl;

cout << " | | " << endl;




bool match = true;//this tells the consul the match has not ended

bool checker;//checks if you actually chose X or O
checker=true;

cout << "play!play!play! you'll need two people" << endl;
cout << "decide who takes X, then press 1 to take X" << endl;
cout << "or press 2 to take O" << endl;


cin >> player;//so, organize will be the thing (1 or 2) that the player will put in

char XO;//helps make X and O

if (player == 1)
{
cout << "you chose X" << endl;
XO = 'X';
}

else if (player == 2)
{
cout << "you chose O" << endl;
XO = 'O';
}
else
{
cout << "press 1 or 2 only please" << endl;
checker=false;
}




bool invalid;//if you "accidentally" put your move in an illegal square, this will help you redo a move.
bool gameover = true;//helps differentiate between draws and wins

int nowwestart;//starts game
cout << "player play your move" << endl;//tells you to move it
cin >> nowwestart;
invalid = true;//you always make a valid move first turn.

if (nowwestart == 1 && a[0][0] == '1')//when you place your marker on square 1, i need to tell consul that your move equals a certain square

{
a[0][0]=XO;
cout << " | | " << endl;//all these "shapes" make the board.

cout << " " << a[0][0] << " | " << a[0][1] << " | " << a[0][2] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << a[1][0] << " | " << a[1][1] << " | " << a[1][2] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << a[2][0] << " | " << a[2][1] << " | " << a[2][2] << endl;

cout << " | | " << endl;


}

else if (nowwestart == 2 && a[0][1] == '2')
{
a[0][1]=XO;
cout << " | | " << endl;//all these "shapes" make the board.

cout << " " << a[0][0] << " | " << a[0][1] << " | " << a[0][2] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << a[1][0] << " | " << a[1][1] << " | " << a[1][2] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << a[2][0] << " | " << a[2][1] << " | " << a[2][2] << endl;

cout << " | | " << endl;

}

else if (nowwestart == 3 && a[0][2] == '3')
{
a[0][2]=XO;
cout << " | | " << endl;//all these "shapes" make the board.

cout << " " << a[0][0] << " | " << a[0][1] << " | " << a[0][2] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << a[1][0] << " | " << a[1][1] << " | " << a[1][2] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << a[2][0] << " | " << a[2][1] << " | " << a[2][2] << endl;

cout << " | | " << endl;

}

else if (nowwestart == 4 && a[1][0] == '4')
{
a[1][0]=XO;
cout << " | | " << endl;//all these "shapes" make the board.

cout << " " << a[0][0] << " | " << a[0][1] << " | " << a[0][2] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << a[1][0] << " | " << a[1][1] << " | " << a[1][2] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << a[2][0] << " | " << a[2][1] << " | " << a[2][2] << endl;

cout << " | | " << endl;


}

else if (nowwestart == 5 && a[1][1] == '5')
{
a[1][1]=XO;
cout << " | | " << endl;//all these "shapes" make the board.

cout << " " << a[0][0] << " | " << a[0][1] << " | " << a[0][2] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << a[1][0] << " | " << a[1][1] << " | " << a[1][2] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << a[2][0] << " | " << a[2][1] << " | " << a[2][2] << endl;

cout << " | | " << endl;

}

else if (nowwestart == 6 && a[1][2] == '6')
{
a[1][2]=XO; cout << " | | " << endl;//all these "shapes" make the board.

cout << " " << a[0][0] << " | " << a[0][1] << " | " << a[0][2] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << a[1][0] << " | " << a[1][1] << " | " << a[1][2] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << a[2][0] << " | " << a[2][1] << " | " << a[2][2] << endl;

cout << " | | " << endl;

}

else if (nowwestart == 7 && a[2][0] == '7')
{
a[2][0]=XO;
cout << " | | " << endl;//all these "shapes" make the board.

cout << " " << a[0][0] << " | " << a[0][1] << " | " << a[0][2] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << a[1][0] << " | " << a[1][1] << " | " << a[1][2] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << a[2][0] << " | " << a[2][1] << " | " << a[2][2] << endl;

cout << " | | " << endl;

}

else if (nowwestart == 8 && a[2][1] == '8')
{
a[2][1]=XO;
cout << " | | " << endl;//all these "shapes" make the board.

cout << " " << a[0][0] << " | " << a[0][1] << " | " << a[0][2] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << a[1][0] << " | " << a[1][1] << " | " << a[1][2] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << a[2][0] << " | " << a[2][1] << " | " << a[2][2] << endl;

cout << " | | " << endl;

}

else if (nowwestart == 9 && a[2][2] == '9')
{
a[2][2]=XO;
cout << " | | " << endl;//all these "shapes" make the board.

cout << " " << a[0][0] << " | " << a[0][1] << " | " << a[0][2] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << a[1][0] << " | " << a[1][1] << " | " << a[1][2] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << a[2][0] << " | " << a[2][1] << " | " << a[2][2] << endl;

cout << " | | " << endl;

}

else
{
cout << "you made an invalid move, please do it again" << endl;
invalid=false;//you made an illegal move :(
}


while(!invalid);



match = false;//when match has ended...
if (a[0][0] != '1')//all possible wins through square 1
{
if (a[0][0] == a[1][0] && a[1][0] == a[2][0])
{
match = true;
}

else if (a[0][0] == a[0][1] && a[0][1] == a[0][2])
{
match = true;
}

else if (a[0][0] == a[1][1] && a[1][1] == a[2][2])
{
match= true;
}
}

if (a[0][1] != '2')//all possible wins through square 2
{
if (a[0][1] == a[1][1] && a[1][1] == a[2][1])
{
match = true;
}
}

if (a[0][2] != '3')//all possible wins through square 3
{
if (a[0][2] == a[1][2] && a[1][2] == a[2][2])
{
match = true;
}

else if (a[0][2] == a[1][1] && a[1][1] == a[2][0])
{
match = true;
}
}

if (a[1][0] != '4')//all possible wins through square 4
{
if (a[1][0] == a[1][1] && a[1][1] == a[1][2])
{
match = true;
}
}

if (a[2][0] != '7')//all possible wins through square 7
{
if (a[2][0] == a[2][1] && a[2][1] == a[2][2])
{
match = true;
}
}

else//anything beside win is draw
{
gameover=false;//no one won...
match=true;//but the match is done anyway
}

if (match==true)//if the match is done
{
if (gameover==true)//if someone won
{
cout << "player" << player << "won" << player << endl;
}

cout << "the game has ended. play again? 1-yes, 2-false (press 2 please)" << endl;
if (1)
{
match = false;//dang it, you are still playing. the borad is below.
char a[3][3];//sets 3x3 matrix
a[0][0]='1';//upper row left corner is 1
a[0][1]='2';//upper row middle is 2
a[0][2]='3';//upper row right corner is 3
a[1][0]='4';//middle row left is 4
a[1][1]='5';//middle row middle is 5
a[1][2]='6';//middle row right is 6
a[2][0]='7';//bottom row left is 7
a[2][1]='8';//bottom row middle is 8
a[2][2]='9';//bottom row right is 9

}
player = 1;
}

else
{
if (player == 1)
{
player = 2;
}
else
{
player = 1;
}
}




while (!match);

cout << endl;
return 0;
}




int main()
{
dot();
}

最佳答案

您可以使用用户输入循环整个程序。 while (getline(std::cin, input)。并用适当的符号更新二维数组。我建议你使用常量来表示 X 和 O。这样代码就会清晰. 而且我还建议你将重复的代码移到函数中,这同样会增加清晰度。

关于c++ - C++ 中的 Tic-Tac-Toe 帮助,如何制作循环以便 Tic Tac Toe 游戏每次都会重复棋盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58619023/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com