C++ Tic Tac Toe游戏2D阵列
我的代码运行,但它不会在黑板上打印任何内容。我很确定我做错了几件事,如果有人能给我指出正确的方向,那就太好了。。谢谢你的帮助C++ Tic Tac Toe游戏2D阵列,c++,visual-c++,multidimensional-array,tic-tac-toe,C++,Visual C++,Multidimensional Array,Tic Tac Toe,我的代码运行,但它不会在黑板上打印任何内容。我很确定我做错了几件事,如果有人能给我指出正确的方向,那就太好了。。谢谢你的帮助 #include<iostream> #include<iomanip> using namespace std; void drawBoard(char board[][3]); char checkWinner3by3(char board[][3]); . // // DO NOT MODIFY THE MAIN FUNCTION //
#include<iostream>
#include<iomanip>
using namespace std;
void drawBoard(char board[][3]);
char checkWinner3by3(char board[][3]);
.
//
// DO NOT MODIFY THE MAIN FUNCTION
//
int main()
{
// This array of chars represents the game board, and it holds the content
// of each space. By default all spaces are set to a blank space.
char board[3][3] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};
// The current player. Because X plays first, initialize to X
char player = 'X';
// The winner. either 'X', 'O', or 't' if it's a tie.
// Or a blank space if the game has not finished.
char winner = ' ';
// These variables will hold the number of the row and column selected
// by the players.
int row;
int column;
// boolean variables used to verify if the move is valid.
bool is_move;
bool is_row;
bool is_column;
cout<<"************ TIC TAC TOE ************\n";
// The game loops again and again until the game is over
while(winner == ' ')
{
//set the boolean variables back to false for a new turn.
is_move = false;
is_row = false;
is_column = false;
//draw the board.
drawBoard(board);
// If the game is not yet over show who`s the next player to move
cout << "Player ";
if(player == 'X')
{
cout << 1;
}
else
{
cout << 2;
}
cout << "'s turn:" << endl;
// Loop until the player selects a valid space for their move
is_move = false;
while(!is_move)
{
// Loop until the player selects a valid row
// Assume the user inputs a valid integer
is_row = false;
while(!is_row)
{
cout << "Row: ";
cin >> row;
if(row == 1 || row == 2 || row == 3)
{
is_row = true;
}
else
{
cout << endl << "Invalid row!" << endl;
}
} // end of input row loop
// Loop until the player selects a valid column
// Assume the user inputs a valid integer
is_column = false;
while(!is_column)
{
cout << "Column: ";
cin >> column;
if(column == 1 || column == 2 || column == 3)
{
is_column = true;
}
else
{
cout << endl << "Invalid column!" << endl;
}
} // end of input column loop
// If the space is empty, mark the chosen space and swich players
if(board[row-1][column-1] == ' ')
{
// Mark the space and end the player's turn
board[row-1][column-1] = player;
is_move = true;
// Switch to the other player
if(player == 'X')
{
player = 'O';
}
else
{
player = 'X';
}
} // end of marking space and changing players
// If the space is occupied
else
{
cout<<"The selected space is occupied!" << endl;
cout << "Select a different space:" << endl << endl;
drawBoard(board);
}
} // end of player's move loop
cout << endl; // for nice output formatting
//check if the player won.
winner = checkWinner3by3(board);
//If there's a winner
if(winner == 'X' || winner == 'O')
{
drawBoard(board);
//Display congratulations message
cout<<"Congratulations! Player ";
if(winner == 'X')
{
cout << 1;
}
else
{
cout << 2;
}
cout<<" is the winner!"<<endl;
}
else if(winner == 'T')
{
drawBoard(board);
//Display a message if it`s tie
cout << "It's a tie!" << endl;
}
} // End of player's turn loop
system("pause");
return 0;
}
//
// Prints the game board
// We know the board is 3 by 3 so we don't need to have the number of rows as
// a parameter.
//
// WRITE THIS FUNCTION
//
void drawBoard(char board[][3])
{
char print[][3] = {{' ',' ',' '},
{' ',' ',' '},
{' ',' ',' '}};
cout << " 1 2 3" << endl;
cout << " +---+---+---+" << endl;
cout << " 1" << " | " << print[0][0] << " | " << print[0][1] << " | " << print[0][2] << " | " << endl;
cout << " +---+---+---+" << endl;
cout << " 2" << " | " << print[1][0] << " | " << print[1][1] << " | " << print[1][2] << " | " << endl;
cout << " +---+---+---+" << endl;
cout << " 3" << " | " << print[2][0] << " | " << print[2][1] << " | " << print[2][2] << " | " << endl;
cout << " +---+---+---+" << endl;
}
//
// Checks the whole board if there is a winner.
// We know the board is 3 by 3 so we don't need to have the number of rows as
// a parameter.
//
// WRITE THIS FUNCTION
//
char checkWinner3by3(char board[][3])
{
for(i=0; i<3; i++) /* check rows */
{
if(board[i][0]==board[i][1] && board[i][0]==board[i][2])
{
return board[i][0];
}
}
for(i=0; i<3; i++) /* check columns */
{
if(board[0][i]==board[1][i] && board[0][i]==board[2][i])
{
return board[0][i];
}
}
/* test diagonals */
if(board[0][0]==board[1][1] && board[1][1]==board[2][2])
{
return board[0][0];
}
if(board[0][2]==board[1][1] && board[1][1]==board[2][0])
{
return board[0][2];
}
return ' ';
}
#包括
#包括
使用名称空间std;
空抽板(炭板[][3]);
字符校验Winner3By3(字符板[][3]);
.
//
//不要修改主功能
//
int main()
{
//这个字符数组代表游戏板,它保存内容
//默认情况下,所有空格都设置为空白。
字符板[3][3]={{{'','','','',},{'','','',},{'','','','','',};
//当前播放机。因为X首先播放,所以初始化为X
字符播放器='X';
//胜利者。如果是平局,可以是“X”、“O”或“t”。
//如果游戏尚未结束,则为空白。
char-winner='';
//这些变量将保存所选行和列的编号
//由球员们决定。
int行;
int列;
//用于验证移动是否有效的布尔变量。
布尔正在移动;
布尔是罗;
布尔是_列;
coutDrawboard
不使用其board
参数。Drawboard
不使用其board
参数。for(i=0;ifor(i=0;i//如果空间被占用
for(i=0; i<3; i++) /* check rows */
{
if(board[i][0]==board[i][1] && board[i][0]==board[i][2])
{
return board[i][0];
}
}
其他的
{
cout//如果空间被占用
其他的
{
这就是为什么我需要有人来引导我正确的方向。指出我所犯的错误,我可以尝试去改正它们。我是C++新手。是的,这就是为什么我需要有人来引导我正确的方向。指出我所犯的错误,所以我可以尝试去改正它们。我是C++新手。好吧,也许我今晚瞎了,但是问题是什么呢?这段代码?我从来没有初始化过的整数。我可能应该在我的引号中多加一点,因为如果在循环之前声明int i
,片段将是有效的。好吧,也许今晚我是瞎子,但这段代码有什么问题?我从来没有初始化过的整数。我可能应该在我的引号b中多加一点e因为如果在循环之前声明了int i
,则片段将有效。
// If the space is occupied
else
{
cout<<"The selected space is occupied!" << endl;
cout << "Select a different space:" << endl << endl;
drawBoard(board);
}