无法使函数正常工作
Can not get functions to work correctly
我正试图让这个井字游戏程序发挥作用,但我完全不知所措。我在网上找遍了,但这只会让我更加困惑。我无法让change player函数工作(playerTurn),也无法让检查平局的函数正常工作(checkTie)。请帮忙,我们将不胜感激。这是我所拥有的:
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <math.h>
#include <cstdlib>
#include <string>
using namespace std;
char checkWinner(char[][4]);
bool checkTie(char[][4]);
int playerTurn(int);
const char PLAYER1='X';
const char PLAYER2='O';
int main()
{
//variable for player
int playerNum=1;
//Game over variable
bool bGameOver=false;
//Variable to hold winner status
char winner;
bool tie;
//Variable player
string player;
//Player mark
char mark;
//Constants for board size
const int ROWS=4;
const int COLS=4;
char board[ROWS][COLS] = {{' ', '1', '2', '3'},
{ '1', '*', '*', '*'},
{'2', '*', '*', '*'},
{'3', '*', '*', '*'}};
//counter variable
int i;
//display the board
for (int i = 0; i <ROWS; i++)
{
cout << board[i][0] << "t" << board[i][1] << "t" <<
board[i][2] << "t" << board[i][3]<< endl;
}//end for
//Variables to hold selection
int rowx, colx;
while(!bGameOver)//Main game loop
{
if(playerNum=1)
{
player="Player 1";
mark=PLAYER1;
}//end if
else if(playerNum==2)
{
player="Player 2";
mark=PLAYER2;
}//end else
//Get player selection
cout << player << " - enter row:"; cin >> rowx;
cout << player << " - enter column:"; cin >> colx;
cout << "" << endl;
if (board[rowx][colx] != '*')
{
cout << "This space is taken...try again" << endl;
cout << "row:"; cin >> rowx;
cout << "column:"; cin >> colx;
}//end if
board[rowx][colx]=mark;
for (int i = 0; i <ROWS; i++)
{
cout << board[i][0] << "t" << board[i][1] << "t" <<
board[i][2] << "t" << board[i][3]<< endl;
}//end for
//call checkwinner function
winner=checkWinner(board);
playerNum=playerTurn(playerNum);
cout << "the player number is: " << playerNum << endl;
if(winner=='X')
{
cout << "Player 1 wins!" << endl;
bGameOver=true;
}//end if
if(winner=='O')
{
cout << "Player 2 wins!" << endl;
bGameOver=true;
}//end if
//call checkTie function
tie=checkTie(board);
if(tie)
{
cout << "It's a tie!" << endl;
bGameOver=true;
}//end if
}
if (bGameOver)
{
cout << "Game over!" << endl;
}//end if
system ("PAUSE");
return 0;
} //end main
//Declare function checkWinner
char checkWinner(char arr[][4])
{
//variable to hold result
char result='B';
//variable to hold counter
int i=1;
//Check player 1 status
for(i=1; i<4; i++) /* check rows */
{
if(arr[i][1]=='X' && arr[i][2]== 'X' && arr[i][3]=='X')
{
result='X';
}
}
for(i=1; i<4; i++) /* check columns */
{
if(arr[1][i]=='X' && arr[2][i]== 'X' && arr[3][i]=='X')
{
result='X';
}
}
/* test diagonals */
if(arr[1][1]=='X' && arr[2][2]== 'X' && arr[3][3]=='X')
{
result='X';
}
if(arr[1][3]=='X' && arr[2][2]=='X' && arr[3][1]=='X')
{
result='X';
}
//Check player 2 status
for(i=1; i<4; i++) /* check rows */
{
if(arr[i][1]=='O' && arr[i][2]== 'O' && arr[i][3]=='O')
{
result='O';
}
}
for(i=1; i<4; i++) /* check columns */
{
if(arr[1][i]=='O' && arr[2][i]=='O' && arr[3][i]=='O')
{
result='O';
}
}
/* test diagonals */
if(arr[1][1]=='O' && arr[2][2]== 'O' && arr[3][3]=='O')
{
result='O';
}
if(arr[1][3]=='O' && arr[2][2]=='O' && arr[3][1]=='O')
{
result='O';
}
return result;
}//end function
//Define checkTie function
bool checkTie(char arr[][4])
{
//constant for size
const int SIZE=4;
//variable to hold result
bool result=false;
//variable loop counter
int i=0;
if (arr[1][1]!='*'&&arr[1][2]!='*'&&arr[1][3]!='*'&&
arr[2][1]!='*'&&arr[2][2]!='*'&&arr[2][3]!='*'&&
arr[3][1]!='*'&&arr[3][2]!='*'&&arr[3][3]!='*')
{
result=true;
}//end if
return result;
}//end function
int playerTurn(int num)
{
//variable for result
int result;
if(num==1)
{
result=2;
}
else
{
result=1;
}
return result;
}
playerTurn()工作正常;但是,在Main Game循环的顶部,在if语句的测试中使用=而不是==,从而在每次迭代中将playerNum重置为1。
至于checkTie,您需要详细说明它在哪些方面不起作用。或者,你可以通过记录移动次数来避免需要它;如果在第9次移动后没有人获胜,那么这场比赛就是平局。
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- sdl软件渲染器不工作,工作在硬件加速的一个
- C++程序已停止工作-求解常微分方程