TicTacToe代码不会用O和X替换数字
TicTacToe code is not replacing numbers with O and X
#include <iostream>
using namespace std;
int square[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
void board();
int check_win();
int main()
{
char mark;//player 1 x player 2 0
int player = 1, choice;
int i = check_win();
while(i == -1){
board();
player = (player % 2) ? 1 : 2;
cout << "Player " << player << " enter a number";
if(player == 1)
mark = 'X';
else
mark = 'O';
cin >> choice;
switch (choice) {
case 1:
square[1] = (char)(int)mark;
break;
case 2:
square[2] = (char)(int)mark;
break;
case 3:
square[3] = (char)(int)mark;
break;
case 4:
square[4] = (char)(int)mark;
break;
case 5:
square[5] = (char)(int)mark;
break;
case 6:
square[6] = (char)(int)mark;
break;
case 7:
square[7] = (char)(int)mark;
break;
case 8:
square[8] = (char)(int)mark;
break;
case 9:
square[9] = (char)(int)mark;
break;
default:
cout << "None of these";
break;
}
i = check_win();
player++;
}
board();
if(i == 1) cout << "player " << --player << "win";
else{
cout << "Game DRAW";
}
cin.get();
return 0;
}
int check_win()
{
if (square[1] == square[2] && square[2] == square[3])
return 1;
else if (square[4] == square[5] && square[5] == square[6])
return 1;
else if (square[7] == square[8] && square[8] == square[9])
return 1;
else if (square[1] == square[4] && square[4] == square[7])
return 1;
else if (square[2] == square[5] && square[5] == square[8])
return 1;
else if (square[3] == square[6] && square[6] == square[9])
return 1;
else if (square[1] == square[5] && square[5] == square[9])
return 1;
else if (square[3] == square[5] && square[5] == square[7])
return 1;
else
return -1;
}
void board()
{
cout << "nntTic Tac Toenn";
cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
cout << endl;
cout << " | | " << endl;
cout << " " << square[1] << " | " << square[2] << " | "
<< square[3] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << square[4] << " | " << square[5] << " | " <<
square[6] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << square[7] << " | " << square[8] << " | "
<< square[9] << endl;
cout << " | | " << endl << endl;
}
如您所见,我完成了游戏本身的代码,但有一个小问题。当我选择一个像5
这样的数字时,它不会随着X
或O
而改变5
,而是88
和79
使用,我该如何解决?Mark 是保存 X
和 O
内存的 char 变量,但我认为它被这里的函数对话了。
问题是你记住了数字
int square[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
不是char
char square[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
以及当您将X
(或O
(保存在square
square[5] = (char)(int)mark;
您首先将char
转换为int
(X
转换为88
(,然后转换为char
(88
转换为X
(,最后,您将它记住在int
中(X
再次88
(。
建议:使用square
数组chars
并简单地编写
square[5] = mark;
相关文章:
- 如何在C ++中替换整数的前两位数字?
- 我有模板功能.通过使用函数std::for_each,用这个容器中的最大数字替换每个正数
- 使用 Get 的 C++ 输入被替换为空格、不正确的数字
- 在 Python/C++ 中用更改文件中的随机数替换数字
- TicTacToe代码不会用O和X替换数字
- 这个给定的代码应该将给定的数字转换为尽可能滞后的数字,no.by 用 9.It 替换合适的数字是行不通的
- 使用 sprintf 和 sscanf 将给定数字中的所有 0 替换为 5
- 我无法用字符替换重复的数字"a"
- 用递增的数字替换字符串中的空格
- 如何用C 中的数字替换字母
- C 如果指示指向NULLPTR,我可以用数字替换它
- 如何将向量中的所有元素替换为元素数字的总和
- 将两个数字替换为给定数组中介于两个之间的单个数字
- 二叉树实现 - 分支上的数字被替换
- 使用 C++ 替换 txt 文件中的文本数字字符串
- 检测几乎为零并将其替换为 0.0 或将它们保留为小数字是否更快?
- C++11正则表达式:在替换字符串中捕获组后的数字
- 如何将 std::string 中的所有非字母字符(数字和特殊字符)替换为空格
- 我应该用什么替换数字
- c++随机数组-需要用0替换数字22和33