简单的TicTacToe。程序在无限循环中

时间:2014-02-21 18:04:24

标签: c++ loops visual-c++

对于学校,我需要制作一个TicTacToe控制台程序,让计算机选择一个随机位置来设置其“o”。

我尝试了不同的东西(切换案例陈述,if else陈述,检查和重新检查我的功能)但是,在人类玩家进行第二次移动后,计算机将不会进行任何移动,这在我的程序中会导致“让你的行动“印刷线无限重复。

我已经尝试过再次尝试解决它,但我只是一个初学者(本月刚刚开始。)请帮忙!

这是我的代码:

 // Tic_Tac_Toe.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

// Tic Tac Toe, by Your Name

// Include the libraries
#include <iostream>
#include <string>
#include <ctime>

//Use the standard namespace
using namespace std;

// Declare global variables
char Board[9];

// Declare functions
void showBoard ( );
bool moveIsValid (int m);
int whoWon ( ); // Returns 0 if no one has won, 1 if player 1 has won, and 2 if player 2 has won

void main ( )
{
    // Seed the rand function
    srand(time(NULL));

   // Declare local variables
   string Player_1_Name;
   string Player_2_Name = "Computer";
   int Whose_Turn = 1; // 1 means it's player 1's turn, 2 means it's player 2's turn
   int Move; // Stores where the player wants to move
   int totalMoves = 0;
   int aiMove = rand () % 10 - 1;


   //Assign values to the playing board
   Board[0] = '0';
   Board[1] = '1';
   Board[2] = '2';
   Board[3] = '3';
   Board[4] = '4';
   Board[5] = '5';
   Board[6] = '6';
   Board[7] = '7';
   Board[8] = '8';

   // Get player names
   cout << "Player 1: Please enter your name." << endl;
   cin >> Player_1_Name;
   cout << "Computer: I will be playing you today." << endl;


   while (whoWon ( ) == 0 && totalMoves < 9)
   {

      // Do this until the player chooses a valid move
      do
      {

         // Show the board
         showBoard ( );

         // Tell which player to move
        switch (Whose_Turn)
        {
        case(1):
            {
            cout << Player_1_Name << " , It's your turn." << endl;
                cin >> Move;
                break;
            }
        case(2):
            {
                cout << Player_2_Name << " , It's your turn." << endl;
                if (aiMove == 0)
                {
                    Move = 0;
                }
                else if (aiMove == 1)
                    Move = aiMove;
            }
        }


      } while (moveIsValid (Move) != true);

      // Add a move to the totalMoves
      totalMoves++;

      // Change whose turn it is
      switch (Whose_Turn)
      {
      case (1):
         {
            Board[Move] = 'x';
            Whose_Turn = 2;
            break;
         }
      case (2):
         {
            Board[Move] = 'o';
            Whose_Turn = 1;
         }
      }
   }

   // Show the board
   showBoard ( );

   //Tell the user who won
   if (whoWon ( ) == 1) 
   {
       cout << Player_1_Name << " has won." << endl;

   }

   else if (whoWon ( ) == 2) 
   {
       cout << Player_2_Name << " has won." << endl;
   }

   else 
   {
       cout <<"It's a tie game!" << endl;
   }
   system("PAUSE");
}

void showBoard ( )
{
   cout << endl;
   cout << Board[0] << " | " << Board[1] << " | " << Board[2] << endl;
   cout << "--+---+--" << endl;
   cout << Board[3] << " | " << Board[4] << " | " << Board[5] << endl;
   cout << "--+---+--" << endl;
   cout << Board[6] << " | " << Board[7] << " | " << Board[8] << endl;
   cout << endl;
}

bool moveIsValid (int m)
{
   if (Board[m] != 'x' && Board[m] != 'o')
   {
      return true;
   }
   else
   {
      return false;
   }
}

int whoWon ( )
{
   if (Board[0] == Board[1] && Board[1] == Board[2])
   {
      if (Board[0] == 'x')
      {
         return 1;
      }
      else
      {
         return 2;
      }
   }
    if (Board[3] == Board[4] && Board[4] == Board[5])
   {
      if (Board[3] == 'x')
      {
         return 1;
      }
      else
      {
         return 2;
      }
   }
     if (Board[6] == Board[7] && Board[7] == Board[8])
   {
      if (Board[6] == 'x')
      {
         return 1;
      }
      else
      {
         return 2;
      }
   }
      if (Board[0] == Board[3] && Board[3] == Board[6])
   {
      if (Board[0] == 'x')
      {
         return 1;
      }
      else
      {
         return 2;
      }
   }
       if (Board[1] == Board[4] && Board[4] == Board[7])
   {
      if (Board[1] == 'x')
      {
         return 1;
      }
      else
      {
         return 2;
      }
   }
        if (Board[2] == Board[5] && Board[5] == Board[8])
   {
      if (Board[2] == 'x')
      {
         return 1;
      }
      else
      {
         return 2;
      }
   }
         if (Board[0] == Board[4] && Board[4] == Board[8])
   {
      if (Board[0] == 'x')
      {
         return 1;
      }
      else
      {
         return 2;
      }
   }
          if (Board[2] == Board[4] && Board[4] == Board[6])
   {
      if (Board[2] == 'x')
      {
         return 1;
      }
      else
      {
         return 2;
      }
   }
   return 0;
}

3 个答案:

答案 0 :(得分:2)

好吧,在你的代码中,aiMove变量值没有改变。 这就是为什么代码:

if (aiMove == 0)
{
Move = 0;
}
else if (aiMove == 1)
Move = aiMove;
}
永远不会执行

,并且移动值不会更改。 您的逻辑中存在两个问题: 首先,以下代码仅生成一次aiMove值。 你应该把它放在while循环

int aiMove = rand () % 10 - 1;

其次,其余部分仅为0或1并非总是如此,因此您的代码

 if (aiMove == 0)
{
Movee = 0;
}
else if (aiMove == 1)
Move = aiMove;
}

应修改为:

if (aiMove == 0)
{
Move = 0;
}
else if (aiMove != 0)
Move = aiMove;
}

然后,您的代码将正常工作。请尝试以下方法:

while (whoWon ( ) == 0 && totalMoves < 9)
{
   system("CLS");
   aiMove = rand () % 10 - 1;
   // Do this until the player chooses a valid move
  do
   {
      // Show the board
       showBoard ( );
          // Tell which player to move
          switch (Whose_Turn)
           {
             case(1):
              {
                 cout << Player_1_Name << " , It's your turn." << endl;
                 cin >> Move;
                 break;
               }
             case(2):
              {
                cout << Player_2_Name << " , It's your turn." << endl;
                 if (aiMove == 0)
                 {
                     Move = 0;
                  }
                 else if (aiMove != 0)
                    Move = aiMove;
               }
                  break;
            }
         } while (moveIsValid (Move) != true);

方法系统(“CLS”);将在每次移动后清除您的屏幕,您将看到只有一个板子看到多个(正如在当前情况下发生的那样)。

现在你的代码将是干净的。希望这有帮助

答案 1 :(得分:0)

你没有每回合重置你的aiMove变量,所以moveIsValid返回false,这反过来又满足了while循环条件,导致重复ai移动的无限循环。

答案 2 :(得分:0)

对于初学者来说,一切都发生在int main(),所以你可能不想让它无效。其次,您似乎将模板用于控制台应用程序int t_main(),因为您似乎不需要命令行参数,因此应将其删除。第三,允许AI移动的功能永远不会中断。此外,还有一些轻微的格式问题阻碍了你。

相关问题