Tic-Tac-Toe使用C++和多维数组
Tic Tac Toe using C++ and multidimensional arrays
感谢大家的帮助,我是编程新手,技术不太熟练,所以我很感谢大家的耐心。我开始工作了!代码中有几个问题,从用于检查"游戏结束"的for循环到用于设置棋盘的计数器。但最终的代码似乎没有bug。它在控制台上打印出一个带有标记方块的网格,允许两个人一起玩。现在很开心。
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
bool over = false;
void print_board(char board[3][3])
{
cout<<"n------------------n";
cout<<"n "<<board[0][0]<<" | "<<board[0][1]<< " | "<<board[0][2];
cout<<"n------------------n";
cout<<"n "<<board[1][0]<<" | "<<board[1][1]<< " | "<<board[1][2];
cout<<"n------------------n";
cout<<"n "<<board[2][0]<<" | "<<board[2][1]<< " | "<<board[2][2];
cout<<"n------------------n";
}
bool checkForCorrectMove (char board[3][3], int choice, bool player1Turn)
{
int xCor;
int yCor;
xCor = ((choice + 2) / 3) - 1;
yCor = (choice - 1) % 3;
if (((board[xCor][yCor]) != 'X') && ((board[xCor][yCor]) != 'O'))
{
if (player1Turn)
board[xCor][yCor] = 'X';
if (!player1Turn)
board[xCor][yCor] = 'O';
return true;
}
else
return false;
}
bool checkRow(char board[3][3])
{
for (int i = 0; i<3; i++)
{
if ((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]))
{
return true;
}
}
return false;
}
bool checkColumn (char board[3][3])
{
for (int i = 0; i<3; i++)
{
if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]))
{
return true;
}
}
return false;
}
bool checkForWin (char board[3][3])
{
bool win = false;
bool line_win = false;
if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]))
{
win = true;
return win;
}
if ((board[2][0] == board[1][1]) && (board[1][1] == board[0][2]))
{
win = true;
return win;
}
if (checkColumn(board) || checkRow (board))
{
win = true;
return win;
}
return win;
}
int main()
{
cout << "Welcome to Tic Tac Toe!n";
int choice;
int boardcounter = 1;
bool isOkayToMove = true;
bool player1Turn = true;
char board[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = boardcounter + '0';
boardcounter++;
}
}
print_board(board);
do {
if (player1Turn)
{
cout <<"nnPlayer 1, please enter a number that corresponds to an open space: ";
}
else
{
cout <<"nnPlayer 2, please enter a number that corresponds to an open space: ";
}
cin>>choice;
isOkayToMove = checkForCorrectMove(board, choice, player1Turn);
if (isOkayToMove)
{
over = checkForWin(board);
print_board(board);
player1Turn = !player1Turn;
}
else
{
cout <<"nnYou have attempted to move into a space that is already occupied, please try again.";
print_board(board);
}
}
while (over == false);
cout <<"nnCongratulations!";
player1Turn = !player1Turn;
if (player1Turn)
cout<<" Player 1 has won!";
else
cout<<" Player 2 has won!";
}
我试图用C++创建一个井字游戏,但我很难。我知道以前有人问过这个问题,但通过看别人的例子,很难弄清楚我的特定代码(我正在学习C++)出了什么问题。这是代码:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
bool over = false;
void print_board(char board[3][3])
{
cout<<"n------------------n";
cout<<"n "<<board[0][0]<<" | "<<board[0][1]<< " | "<<board[0][2];
cout<<"n------------------n";
cout<<"n "<<board[1][0]<<" | "<<board[1][1]<< " | "<<board[1][2];
cout<<"n------------------n";
cout<<"n "<<board[2][0]<<" | "<<board[2][1]<< " | "<<board[2][2];
cout<<"n------------------n";
}
bool checkForCorrectMove (char board[][3], int choice, bool player1Turn)
{
int xCor;
int yCor;
switch(choice)
{
case 1: xCor = 0, yCor = 0;
case 2: xCor = 0, yCor = 1;
case 3: xCor = 0, yCor = 2;
case 4: xCor = 1, yCor = 0;
case 5: xCor = 1, yCor = 1;
case 6: xCor = 1, yCor = 2;
case 7: xCor = 2, yCor = 0;
case 8: xCor = 2, yCor = 1;
case 9: xCor = 2, yCor = 2;
}
if (((board[xCor][yCor]) != 'X') && ((board[xCor][yCor]) != 'O'))
{
if (player1Turn)
board[xCor][yCor] = 'X';
if (!player1Turn)
board[xCor][yCor] = 'O';
return true;
}
else
return false;
}
bool checkForWin (char board[][3])
{
bool win = false;
if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]))
{
win = true;
}
else if ((board[2][0] == board[1][1]) && (board[1][1] == board[0][2]))
{
win = true;
}
for (int i = 0; i<3; i++)
{
for (int j = 0; i < 3; j++)
{
if (board[i][j] != board [i][0]);
win = false;
}
}
for (int i = 0; i<3; i++)
{
for (int j = 0; i < 3; j++)
{
if (board[i][j] != board [0][j]);
win = false;
}
}
return win;
}
int main()
{
cout << "Welcome to Tic Tac Toe!n";
int choice;
int boardcounter = 1;
bool isOkayToMove = true;
bool player1Turn = true;
char board[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = (char) boardcounter;
boardcounter++;
}
}
print_board(board);
do {
if (player1Turn)
{
cout <<"nnPlayer 1, please enter a number that corresponds to an open space: ";
}
else
{
cout <<"nnPlayer 2, please enter a number that corresponds to an open space: ";
}
cin>>choice;
switch(choice);
isOkayToMove = checkForCorrectMove(board, choice, player1Turn);
if (isOkayToMove)
{
print_board(board);
over = checkForWin(board);
player1Turn = !player1Turn;
}
else
{
cout <<"nnYou have attempted to move into a space that is already occupied, please try again.";
print_board(board);
}
}
while (over == false);
cout <<"nnCongratulations!";
if (player1Turn)
cout<<" Player 1 has won!";
else
cout<<" Player 2 has won!";
}
我发现了一个问题:
int boardcounter = 1;
//...
for (int j = 0; j < 3; j++)
{
board[i][j] = (char) boardcounter;
boardcounter++;
}
问题是强制转换(char)
不会将int
变量转换为文本表示。强制转换实际上将整数转换为较小大小的整数变量。
有许多方法可以将数字转换为字符,例如snprintf
、tostring
和ostringstream
。
由于你有一个有限的范围,即数字0-9,你可以逃脱:
board[row][column] = '0' + boardcounter;
我建议您查看您的代码,找到其他将整数转换为char
的地方,并相应地进行更改。
顺便说一句,大多数Tic-Tac-Toe游戏都使用"、"X"answers"O"作为值。
相关文章:
- 如何使用Visual Studio 2017在C++中为参数化对象数组使用唯一指针
- 为动态创建的数组使用静态关键字有什么意义吗?
- C++ 2D shared_ptr数组使用抽象多态类型初始化
- 使用std ::数组使用CUDA推力来端口C 代码的简便方法
- 使用动态数组使用 c++ 的前向差异表
- C 指针数组使用STL开始和结束
- 对我的基类对象数组使用大量的静态类型转换——这可以吗
- 如何使用并行数组使用 c++ 制作包含数量和单词的列表
- 正确的语法以对 char* 数组使用 new
- 在计算使用了多少个数组索引的同时,如何填充一个来自用户输入的字符的数组?(使用cin.get)
- 数组使用索引返回最大值
- 对结构数组使用构造函数
- 数组使用的堆栈(参数传递/返回)
- 动态数组使用相同的值来实现整体
- 部分模板专用化数组使用情况
- 对指针数组使用begin()、end()
- 找出哪个对象/数组使用最多的堆栈内存
- 对C++数组使用 try/catch
- 对 STL 列表数组使用迭代器的语法
- 对程序中的临时数组使用内存映射文件