Tic Tac Toe覆盖了数组用户输入

时间:2015-10-22 03:59:15

标签: c++ arrays tic-tac-toe

我遇到的问题是: 我在方形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's\n\n";
const std::string INSTRUCTIONS = "Enter the number of the square you wish to mark\nwith 1 being top left and 9 being bottom right.\n\n";
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();
}

1 个答案:

答案 0 :(得分:0)

查看convertSquareToCoordinates

...
validMove(square, playerChar);
drawMove(square, playerChar, yCoord, xCoord);

如果square引用已填充的方格,则此函数仍会在相应的drawMove上调用(yCoord, xCoord)

更重要的是,convertSquareToCoordinatesvalidMove以奇怪的方式互相称呼。并且您的函数名称不能清楚地描述函数应该执行的操作。