我对编码非常陌生并且遇到了一个令人困惑的地方......我做了一个非常基本的ticTacToe游戏,当我试图检查数组的值时,看看它是否连续3个是不抓取值(或者可能是但是我超出了范围?)。
只是想知道如何检查我的多数组的值?
这是代码。
user.cpp //检查胜利功能
findstr
User.cpp //游戏循环
void User::checkForWin()
{
board board;//construct board
if ((board.ticTacToeBoard[0][0] == getUser1Char()) && (board.ticTacToeBoard[0][1] == getUser1Char()) &&
(board.ticTacToeBoard[0][2] == getUser1Char()))//Check array values to see if they win. PROBLEM AREA
{
std::cout << getUserName(_user1Char) << "You Are The Winner!" << std::endl;
exit(0);
}
我知道我已经使用了大量神奇的数字/字符而我的代码不会是最优雅的,因为我只是在学习,请任何建议都非常感谢,也主要是我发布的原因。
为什么void User::userGo(char userGo)
{
board board; //Construct board
board.initBoard(); //Initilize board
int quitGame = 1; //sets value of quit game loop...This isnt working either but a minor issue for now as im just using exit(); until i figure it out
while (quitGame == 1) //game loop
{
while (userGo == 'G') //loop to swap user goes
{
invalid1: //goto return point if user1 enters an invalid character.
cout << getUserName('A') << " Please enter a letter between A and I or Q for quit" << endl;
char player1Input; //
cin >> player1Input;
if ((player1Input == 'Q') || (player1Input == 'A') || (player1Input == 'B') || // If player input equals
(player1Input == 'C') || (player1Input == 'D') || (player1Input == 'E') || // any of these values
(player1Input == 'F') || (player1Input == 'G') || (player1Input == 'H') || // then run for loop
(player1Input == 'I'))
{
for (int iii = 0; iii < 3; iii++) // Looping through tictactoe board values
{ // values are set to A through I
for (int jjj = 0; jjj < 3; jjj++) // as there are 9 places in the array
{
switch (player1Input) // Checking user input against and placing into array in relevant spot.
{ // *Find More elegant way to right this maybe with a loop.
case 'Q':
cout << "Thanks for Playing! " << endl;
quitGame = 2;
exit(0);
case 'A':
board.ticTacToeBoard[0][0] = 'X';
break;
case 'B':
board.ticTacToeBoard[0][1] = 'X';
break;
case 'C':
board.ticTacToeBoard[0][2] = 'X';
break;
case 'D':
board.ticTacToeBoard[1][0] = 'X';
break;
case 'E':
board.ticTacToeBoard[1][1] = 'X';
break;
case 'F':
board.ticTacToeBoard[1][2] = 'X';
break;
case 'G':
board.ticTacToeBoard[2][0] = 'X';
break;
case 'H':
board.ticTacToeBoard[2][1] = 'X';
break;
case 'I':
board.ticTacToeBoard[2][2] = 'X';
break;
default:
break;
}
}
}
}
else
{
cout << "Sorry ";
getUserName('A'); // If none of the values are correct print out wrong value statement
cout << " This is not a valid move." << endl; // and send the user back to the invalid1 Marker.
goto invalid1;
}
checkForWin(); // Check for win - PROBLEM AREA
board.printBoard(); // Print board for user2 visual
userGo = 'F'; // swap to User2's Go.
}
while (userGo == 'F') //loop to swap user goes
{
invalid2: //goto return point if user2 enters an invalid character.
cout << getUserName('B') << " Please enter a letter between A and I or press Q to quit." << endl;
char player2Input;
cin >> player2Input;
if ((player2Input == 'Q') || (player2Input == 'A') || (player2Input == 'B') || // If player input equals
(player2Input == 'C') || (player2Input == 'D') || (player2Input == 'E') || // any of these values
(player2Input == 'F') || (player2Input == 'G') || (player2Input == 'H') || // then run for loop
(player2Input == 'I'))
{
for (int iii = 0; iii < 3; iii++) // Looping through tictactoe board values
{ // values are set to A through I
for (int jjj = 0; jjj < 3; jjj++) // as there are 9 places in the array
{
switch (player2Input) // Checking user input against and placing into array in relevant spot.
{ // *Find More elegant way to right this maybe with a loop.
case 'Q':
cout << "Thanks for Playing" << endl;
quitGame = 2;
exit(0);
break;
case 'A':
board.ticTacToeBoard[0][0] = 'O';
break;
case 'B':
board.ticTacToeBoard[0][1] = 'O';
break;
case 'C':
board.ticTacToeBoard[0][2] = 'O';
break;
case 'D':
board.ticTacToeBoard[1][0] = 'O';
break;
case 'E':
board.ticTacToeBoard[1][1] = 'O';
break;
case 'F':
board.ticTacToeBoard[1][2] = 'O';
break;
case 'G':
board.ticTacToeBoard[2][0] = 'O';
break;
case 'H':
board.ticTacToeBoard[2][1] = 'O';
break;
case 'I':
board.ticTacToeBoard[2][2] = 'O';
break;
default:
break;
}
}
}
}
else
{
cout << "Sorry " << _name2;
getUserName('B'); // If none of the values are correct print out wrong value statement
cout << " This is not a valid move." << endl; // and send the user back to the invalid2 Marker.
goto invalid2;
}
}
checkForWin(); // Check for win - PROBLEM AREA
board.printBoard(); // Print board for user1 visual
userGo = 'G'; // swap to User1's Go.
}
}
不会做它告诉的事情?
答案 0 :(得分:0)
在checkForWin
函数中,变量board
是 local 变量。它仅存在于该函数中并且是默认构造的。
如果 您有User::board
成员变量或全局board
变量,您的本地定义会影响并覆盖成员/全局变量
userGo
函数中的相同内容。
答案 1 :(得分:0)
您需要将一个成员变量添加到代表董事会的User
,一个可以被所有东西看到的全局变量,或者您需要让checkForWin
将一个董事会作为参数。< / p>
checkForWin(Board& board)
{
//Board board; // << remove this line
然后,您每次都会将本地电路板传递给checkForWin:
checkForWin(board);
更好的设计是让checkForWin
成为Board
而非User
的成员函数,以便您致电:
board.checkForWin();
我可以推荐其他设计批评和代码更改,但这远远超出了回答问题的范围,更好地让您吸收所有这些并以适当的速度继续学习:)