C++ 程序可以工作,但当玩家输入位置时,它不会';我没有出现在黑板上。我能做些什么来解决这个问题? #包括 使用名称空间std; void CreateCharBoard(int Board[][3],char CharBoard[][3]); 无效印刷板(字符字符板[][3]); 布尔支票赢家(国际棋盘[][3],国际玩家); 无效复位板(int板[][3]); bool CheckMove(int Board[][3],int行,int列); int main(){ //在此处删除并初始化(如有必要)所有必需的变量 国际板[3][3]; 炭纤维板[3][3]; int播放器,行,列; 布尔温=假; char再次='y'; //打印实验室标题,如果需要,打印此实验室正在执行的操作的说明 //这行代码将暂停程序,直到用户按任意键继续 系统(“暂停”); while(再次=='y'){ 委员会(委员会);; 系统(“清除”); while(Win==false){ 创建CharBoard(Board,CharBoard); 印刷板(纸板); cout柱; 选中移动(板、行、列); //更新板 Win=支票赢家(棋盘,-1); 如果(选中赢家(棋盘,-1)==真){ cout
看起来您更新了C++ 程序可以工作,但当玩家输入位置时,它不会';我没有出现在黑板上。我能做些什么来解决这个问题? #包括 使用名称空间std; void CreateCharBoard(int Board[][3],char CharBoard[][3]); 无效印刷板(字符字符板[][3]); 布尔支票赢家(国际棋盘[][3],国际玩家); 无效复位板(int板[][3]); bool CheckMove(int Board[][3],int行,int列); int main(){ //在此处删除并初始化(如有必要)所有必需的变量 国际板[3][3]; 炭纤维板[3][3]; int播放器,行,列; 布尔温=假; char再次='y'; //打印实验室标题,如果需要,打印此实验室正在执行的操作的说明 //这行代码将暂停程序,直到用户按任意键继续 系统(“暂停”); while(再次=='y'){ 委员会(委员会);; 系统(“清除”); while(Win==false){ 创建CharBoard(Board,CharBoard); 印刷板(纸板); cout柱; 选中移动(板、行、列); //更新板 Win=支票赢家(棋盘,-1); 如果(选中赢家(棋盘,-1)==真){ cout,c++,function,multidimensional-array,C++,Function,Multidimensional Array,看起来您更新了板 在Board中写入任何内容的唯一一行是在ResetBoard()中,将所有值重置为0 在游戏循环中有两行//更新板-我想您忘记实现该功能了 其他缺陷: 您可以调用CheckWinner两次-一次用于设置Win,一次用于检查,如果用户已经赢了 不使用CheckMove的返回-应该有一个循环来读取输入,直到播放器输入有效数据 如果有一名玩家赢了,你就在中途结束循环。检查while(Win==false)永远不会被使用,并向所有人表明它应该是while(true) while(Wi
板
在Board
中写入任何内容的唯一一行是在ResetBoard()
中,将所有值重置为0
在游戏循环中有两行//更新板-我想您忘记实现该功能了
其他缺陷:
- 您可以调用
CheckWinner
两次-一次用于设置Win
,一次用于检查,如果用户已经赢了
- 不使用
CheckMove
的返回-应该有一个循环来读取输入,直到播放器输入有效数据
- 如果有一名玩家赢了,你就在中途结束
循环。检查while(Win==false)
永远不会被使用,并向所有人表明它应该是while(true)
while(Win==false)
等是不好的样式。请改用while(!Win)
。
#include <iostream>
using namespace std;
void CreateCharBoard(int Board[][3], char CharBoard[][3]);
void PrintBoard(char CharBoard[][3]);
bool CheckWinner(int Board[][3], int Player);
void ResetBoard(int Board[][3]);
bool CheckMove(int Board[][3], int Row, int Column);
int main(){
// delcare and initialize (if necessary) all required variables here
int Board[3][3];
char CharBoard[3][3];
int Player, Row, Column;
bool Win = false;
char Again = 'y';
// Print lab header and if desired, statement of what this lab is doing
//this line of code will pause the program until the user hits any key to continue
system("pause");
while (Again == 'y'){
ResetBoard(Board);
system("clear");
while (Win == false){
CreateCharBoard(Board, CharBoard);
PrintBoard(CharBoard);
cout << "Player O's turn!" << endl;
cout << "Enter row number: "; cin >> Row;
cout << "Enter column number: "; cin >> Column;
CheckMove(Board, Row, Column);
//update board
Win = CheckWinner(Board, -1);
if (CheckWinner (Board, -1) == true){
cout << "Player O wins!" << endl;
break;
}
//set flag to denote it's x's turn
PrintBoard(CharBoard);
cout << "Player X's turn!" << endl;
cout << "Enter row number: "; cin >> Row;
cout << "Enter column number: "; cin >> Column;
CheckMove(Board, Row, Column);
//update board
Win = CheckWinner (Board, 1);
if (CheckWinner (Board, 1) == true){
cout << "Player X wins!" << endl;
break;
}
}
cout << endl << endl;
cout << "Would you like to play again? (y/n): ";
cin >> Again;
}
cout << endl << endl;
cout << "Good bye!" << endl;
return 0;
}
// function to take game board and create a board with corresponding characters
void CreateCharBoard(int Board[][3], char CharBoard[][3])
{
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
// if game board has a -1, put an "O" in the character board
if (Board[i][j]== -1){
CharBoard[i][j] = 'O';
}
// if game board has a 1, put an "X" in the character board
else if (Board[i][j]== 1){
CharBoard[i][j] = 'X';
}
// if game board has a 0, put an " " (blank space) in the character board
else{
CharBoard[i][j] = ' ';
}
}
}
}
// print the character representation of the game board
void PrintBoard(char CharBoard[][3]){
cout << " Tic Tac Toe!" << endl << endl;
cout << " 0 1 2" << endl << endl;
cout << "0 " << CharBoard[0][0] << " | " << CharBoard[0][1] << " | " << CharBoard[0][2] << endl;
cout << " -----------------" << endl;
cout << "1 " << CharBoard[1][0] << " | " << CharBoard[1][1] << " | " << CharBoard[1][2] << endl;
cout << " -----------------" << endl;
cout << "2 " << CharBoard[2][0] << " | " << CharBoard[2][1] << " | " << CharBoard[2][2] << endl << endl;
}
// function to check if a player, either "X" or "O" has won the game
bool CheckWinner(int Board[][3], int Player){
int Sum;
// check each column
for (int i = 0; i < 3; i++){
Sum = Board[i][0] + Board[i][1] + Board[i][2];
// if O is a winner
if (Sum == 3*Player){
return true;
}
}
// check each row
for (int j = 0; j < 3; j++){
Sum = Board[0][j] + Board[1][j] + Board[2][j];
// if O is a winner
if (Sum == 3*Player){
return true;
}
}
// check diagonal
Sum = Board[0][0] + Board[1][1] + Board[2][2];
if (Sum == 3*Player){
return true;
}
Sum = Board[0][2] + Board[1][1] + Board[2][0];
if (Sum == 3*Player){
return true;
}
// if none of the rows, columns, or diagonals = 3*player, then return false
return false;
}
// function to reset all elements of the game board back to 0
void ResetBoard(int Board[][3]){
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
Board[i][j] = 0;
}
}
}
// function to see if player's move is valid, if 0 (or not played) return true, otherwise false
bool CheckMove(int Board[][3], int Row, int Column){
if ((Row < 0) || (Row > 2)){
return false;
}
else if ((Column < 0) || (Column > 2)){
return false;
}
else if (Board[Row][Column] == 0){
return true;
}
else{
return false;
}
}