井字游戏覆盖阵列用户输入
Tic Tac Toe overwritten array user inputs
我遇到的问题是:我在方块1中输入一个选项X然后轮到O来测试我再次输入方块1。弹出的错误信息,关于它是一个无效的正方形,请输入另一个正方形。所以我输入了一个有效的正方形,然后黑板被函数画出来,这时我发现第一个正方形中的X被覆盖了。无论当时选择的是X还是O,都会发生这种情况。我有点走投无路了,只是想在这个项目的那部分寻求一点帮助。我想,一旦我敲定了这部分内容,计算机回合的随机数生成器将很容易实现,以及检查获胜条件。
#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>
#include <ctime>
const int yCoordMax = 6;
const int xCoordMax = 2;
int xCoord;
int yCoord;
int square = 0;
const std::string PLAYER1 = "X";
const std::string COMPUTER = "O";
int turnCount = 0;
const int MAXTURN = 9;
int whoIsPlayer = 0; //denotes human with 0 and computer with 1 alternating between
std::string playerChar = " "; //the current turn's player's symbol
const std::string WIN = "You won!n";
const std::string LOSE = "You lost.n";
const std::string DRAW = "It's a draw.n";
const std::string PLAY = "You will be the X's against the computer O'snn";
const std::string INSTRUCTIONS = "Enter the number of the square you wish to marknwith 1 being top left and 9 being bottom right.nn";
const std::string INVALIDSQUARE = "Please enter a correct square number between 1 and 9.n";
const std::string SQUAREISFULL = "That square is already marked. Choose another.n";
bool squareIsFilled[9] {0}; // any NON-Zero is true; ZERO is false
//array is zero thru eight
void drawBoard(void);
void convertSquareToCoordinates(std::string);
void validMove (int, std::string);
void drawMove(int, std::string, int, int);
void computerTurn(std::string);
std::string board[7][3] =
{ //0 //1 //2
{"+----", "+----+", "----+n"}, // 0
{"| ", "| |", " |n"}, // 1 input here only [1][0], [1][1], [1][2]
{"+----", "+----+", "----+n"}, // 2
{"| ", "| |", " |n"}, // 3 input here only [3][0], [3][1], [3][2]
{"+----", "+----+", "----+n"}, // 4
{"| ", "| |", " |n"}, // 5 input here only [5][0], [5][1], [5][2]
{"+----", "+----+", "----+n"} // 6
};
int main()
{
std::srand(time(0));
std::cout << PLAY;
std::cout << INSTRUCTIONS;
drawBoard();
while (turnCount < MAXTURN)
{
if(whoIsPlayer == 0)
{
playerChar = PLAYER1;
convertSquareToCoordinates(playerChar);
whoIsPlayer = 1;
}
else
{
playerChar = COMPUTER;
convertSquareToCoordinates(playerChar);
whoIsPlayer = 0;
}
++turnCount;
}
return 0;
}
void drawBoard()
{
for (int y = 0; y <= yCoordMax; ++y)
{
for (int x = 0; x <= xCoordMax; ++x)
{
std::cout << board[y][x];
}
}
}
void convertSquareToCoordinates(std::string playerChar)
{
int square;
int yCoord;
int xCoord;
std::cout << std::endl;
std::cin >> square;
while(square < 1 || square > 9)
{
std::cout << INVALIDSQUARE;
std::cin >> square;
}
if (square == 1)
{yCoord = 1;
xCoord = 0;} //bool needs to flag invalid if square is filled
if (square == 2) //if square is empty then flag bool as filled
{yCoord = 1; //so validMove will skip execution
xCoord = 1;}
if (square == 3)
{yCoord = 1;
xCoord = 2;}
if (square == 4)
{yCoord = 3;
xCoord = 0;}
if (square == 5)
{yCoord = 3;
xCoord = 1;}
if (square == 6)
{yCoord = 3;
xCoord = 2;}
if (square == 7)
{yCoord = 5;
xCoord = 0;}
if (square == 8)
{yCoord = 5;
xCoord = 1;}
if (square == 9)
{yCoord = 5;
xCoord = 2;}
validMove(square, playerChar);
drawMove(square, playerChar, yCoord, xCoord);
}
void validMove(int square, std::string playerChar)
{
if(square == 1)
{
if(squareIsFilled[square - 1] == true){
if(whoIsPlayer == 0){
std::cout << SQUAREISFULL;}
convertSquareToCoordinates(playerChar);}
else
{squareIsFilled[square - 1] = true;}
}
if(square == 2)
{
if(squareIsFilled[square - 1] == true)
{if(whoIsPlayer == 0){
std::cout << SQUAREISFULL;}
convertSquareToCoordinates(playerChar);}
else
{squareIsFilled[square - 1] = true;}
}
if(square == 3)
{
if(squareIsFilled[square - 1] == true)
{if(whoIsPlayer == 0){
std::cout << SQUAREISFULL;}
convertSquareToCoordinates(playerChar);}
else
{squareIsFilled[square - 1] = true;}
}
// checks middle column for mark
if(square == 4)
{
if(squareIsFilled[square - 1] == true)
{if(whoIsPlayer == 0){
std::cout << SQUAREISFULL;}
convertSquareToCoordinates(playerChar);}
else
{squareIsFilled[square - 1] = true;}
}
if(square == 5)
{
if(squareIsFilled[square - 1] == true)
{if(whoIsPlayer == 0){
std::cout << SQUAREISFULL;}
convertSquareToCoordinates(playerChar);}
else
{squareIsFilled[square - 1] = true;}
}
if(square == 6)
{
if(squareIsFilled[square - 1] == true)
{if(whoIsPlayer == 0){
std::cout << SQUAREISFULL;}
convertSquareToCoordinates(playerChar);}
else
{squareIsFilled[square - 1] = true;}
}
// checks right column for mark
if(square == 7)
{
if(squareIsFilled[square - 1] == true)
{if(whoIsPlayer == 0){
std::cout << SQUAREISFULL;}
convertSquareToCoordinates(playerChar);}
else
{squareIsFilled[square - 1] = true;}
}
if(square == 8)
{
if(squareIsFilled[square - 1] == true)
{if(whoIsPlayer == 0){
std::cout << SQUAREISFULL;}
convertSquareToCoordinates(playerChar);}
else
{squareIsFilled[square - 1] = true;}
}
if(square == 9)
{
if(squareIsFilled[square - 1] == true)
{if(whoIsPlayer == 0){
std::cout << SQUAREISFULL;}
convertSquareToCoordinates(playerChar);}
else
{squareIsFilled[square - 1] = true;}
}
}
void drawMove(int square, std::string playerChar, int yCoord, int xCoord)
{
if(square == 1) //Draws left column move
board[yCoord][xCoord] = "| " + playerChar + " ";
else if(square == 2)
board[yCoord][xCoord] = "| " + playerChar + " |";
else if(square == 3)
board[yCoord][xCoord] = " " + playerChar + " |n";
if(square == 4) //Draws middle column move
board[yCoord][xCoord] = "| " + playerChar + " ";
else if(square == 5)
board[yCoord][xCoord] = "| " + playerChar + " |";
else if(square == 6)
board[yCoord][xCoord] = " " + playerChar + " |n";
if(square == 7) //Draws right column move
board[yCoord][xCoord] = "| " + playerChar + " ";
else if(square == 8)
board[yCoord][xCoord] = "| " + playerChar + " |";
else if(square == 9)
board[yCoord][xCoord] = " " + playerChar + " |n";
drawBoard();
}
看convertSquareToCoordinates
:
...
validMove(square, playerChar);
drawMove(square, playerChar, yCoord, xCoord);
如果square
指的是一个已经被填满的方块,这个函数仍然调用drawMove
对应的(yCoord, xCoord)
。
更根本的是,convertSquareToCoordinates
和validMove
以一种奇怪的方式相互调用。你的函数名没有清楚地描述函数应该做什么。
相关文章:
- C++如何通过用户输入删除列表元素
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 如何只允许用户输入正整数
- C++如何计算用户输入的数字中的偶数位数
- 通过for循环使用用户输入填充列表
- 在C++中,我如何接受不同于同一行的用户输入
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 显示基于用户输入的整数的字符
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 打印时有二叉树问题.用户输入不打印任何内容
- C ++奇怪的数组由用户输入
- 如何允许大写和小写用户输入?
- C++读取用户输入而不按回车键(Mac OS X),与Turbo Pascal中的读取键相同
- 如何存储用户输入的所有数据,然后在他们想要查看所有数据时显示它们
- 如何使用用户输入变量制作二维数组?
- 用户输入字符串的文件附加问题..C++
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 如何使用递归循环我的代码(当用户输入无效输入时,它会再次提示他们)?