井字游戏覆盖阵列用户输入



我遇到的问题是:我在方块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)

更根本的是,convertSquareToCoordinatesvalidMove以一种奇怪的方式相互调用。你的函数名没有清楚地描述函数应该做什么。

最新更新