⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2_2.cpp

📁 3*3的棋局中,最先三子一线者胜出的游戏
💻 CPP
字号:

/*第2题	TICTACTOE游戏--源代码及关键源代码注解如下:*/
#include <iostream.h>
#include <stdlib.h>

void PrintBoard();              // game board
void PrintInfo();               // some info about the rules of the game is printed
void PrintStats(int, int, int);      // prints the stats of the  match
void IfError(int&, int&); // When invalid input is entered, prints the error
void ChoiceOfChar(char&);      // Asks the users if they want to play again
void PromptTurnO(int&, int&);   // Prompts the first user for data
void PromptTurnX(int&, int&);   // Second user has to input coordinates
char board[3][3];               // array definition
int main()
{
 int ROW;          // elements of 2 dimensional array!
 int COLUMN;
 int FirstPlayer;      // number of winning games for first player
 int SecondPlayer;
 int Draws;         // number of draws in a game
 FirstPlayer = 0;
 SecondPlayer = 0;
 Draws = 0;
 char choice;
 choice = 'Y';       // Initial value is Y in order to initialize the main loop
 PrintInfo();
 while(choice == 'Y') // In case the players want to play again
 {                                     		// this is the main loop
  for (ROW = 0; ROW < 3; ROW++)       		// these forloops are used to process the array
  for (COLUMN = 0; COLUMN < 3; COLUMN++)  	// by row and column
  board[ROW][COLUMN]= ' ';                   	// blank characters as initial values
  int x;            							// (x,y) coordinates entered by the users
  int y;
  int SpotsOnBoard; 							// this is the sum of spots available on the game board
  SpotsOnBoard = 0;
  while ( SpotsOnBoard <= 9) 					// because there is only 9 characters available on the board
  {
   if (SpotsOnBoard == 0)
   PrintBoard();  							// This function draws initialy the blank game board
                 							// so it is easier for the user to pick the x and y values

   PromptTurnO(x, y);

   IfError(x, y);     						// If the user's input is invalid, this function prints
                   						// the error message and prompts for correction
   board[x - 1][y - 1]= 'O';         				// actually the array starts from 0, NOT from 1
   SpotsOnBoard++;             				// (as entered by the user)
                               				// so the program have to "fix" the input
   PrintBoard();						         // If 3 coordintates are O's in one of 8 possible
              							//winning combinations, then O wins
   if (board[0][0]== 'O' && board[1][1]== 'O' && board[2][2]== 'O')
    {
     cout << " O player wins the game" << endl;
     FirstPlayer++;
     break;
    }
   else if (board[2][0]== 'O' && board[1][1]== 'O' && board[0][2]== 'O')
    {
     cout << "O player wins the game" <<endl;
     FirstPlayer++;
     break;
    }
   else if (board[0][0]== 'O' && board[1][0]== 'O' && board[2][0] == 'O')
    {
     cout << "O player wins the game" << endl;
     FirstPlayer++;
     break;
     }
   else if (board[0][1]== 'O' && board[1][1]== 'O' && board[2][1]== 'O')
    {
     cout << "O player wins the game" << endl;
     FirstPlayer++;
     break;
    }
   else if (board[0][2]== 'O' && board[1][2]== 'O' && board[2][2]== 'O')
    {
     cout << "O player wins the game" << endl;
     FirstPlayer++;
     break;
    }
   else if (board[0][0]== 'O' && board[0][1]== 'O' && board[0][2]== 'O')
    {
     cout << "O player wins the game" << endl;
     FirstPlayer++;
     break;
    }
   else if (board[1][0]== 'O' && board[1][1]== 'O' && board[1][2]== 'O')
    {
     cout << "O player wins the game" << endl;
     FirstPlayer++;
     break;
    }
   else if (board[2][0]== 'O' && board[2][1]== 'O' && board[2][2]== 'O')
    {
     cout << "O player wins the game" <<endl;
     FirstPlayer++; 						// counts the number of won games
     break;   							// break statement is used to make sure that only one possibility
    }         							// will be processed
   else if (SpotsOnBoard == 9)
    {
     cout << "Draw!" <<endl;
     Draws++;
     break;  							// Without this brake statement it was not working propertly
    }

  PromptTurnX(x, y);
  IfError(x, y);
  board[x - 1][y - 1]= 'X';
  SpotsOnBoard++;
  PrintBoard();
  if (board[0][0]== 'X' && board[1][1]== 'X' && board[2][2]== 'X')
    {
     cout << "X player is victorious" << endl;
     SecondPlayer++;
    }
  else if (board[2][0]== 'X' && board[1][1]== 'X' && board[0][2]== 'X')
    {
     cout << "X player is victorious" << endl;
      SecondPlayer++;
    }
  else if (board[0][0]== 'X' && board[1][0]== 'X' && board[2][0]== 'X')
    {
     cout << "X player is victorious" << endl;
     SecondPlayer++;
    }
  else if (board[0][1]== 'X' && board[1][1]== 'X' && board[2][1]== 'X')
    {
     cout << "X player is victorious" << endl;
     SecondPlayer++;
    }
  else if (board[0][2]== 'X' && board[1][2]== 'X' && board[2][2]== 'X')
    {
     cout << "X player is victorious" << endl;
     SecondPlayer++;
    }
  else if (board[0][0]== 'X' && board[0][1]== 'X' && board[0][2]== 'X')
    {
     cout << "X player is victorious" << endl;
     SecondPlayer++;
    }
  else if (board[1][0]== 'X' && board[1][1]== 'X' && board[1][2]== 'X')
    {
     cout << "X player is victorious" << endl;
     SecondPlayer++;
    }
  else if (board[2][0]== 'X' && board[2][1]== 'X' && board[2][2]== 'X')
    {
     cout << "X player is victorious" << endl;
     SecondPlayer++;
    }
 }
  ChoiceOfChar(choice);
}

  PrintStats(FirstPlayer, SecondPlayer, Draws);

  system("PAUSE");
  return 0;
}

void PrintBoard()
// Post : The Game board is printed to help with choosing the coordintates
//        Either X or O will be printed on the board.
//        Board is updated while the game progresses
{
 cout << endl;
 cout << "  1   2   3 " << endl;
 cout << "1 " << board[0][0] << " | " << board[0][1]<<" | "<< board[0][2]<< endl;
 cout << " ---|---|---" << endl;;
 cout << "2 " << board[1][0]<< " | " << board[1][1]<< " | " << board[1][2]<< endl;
 cout << " ---|---|---" << endl;
 cout << "3 " << board[2][0]<< " | " << board[2][1]<< " | " << board[2][2]<< endl;
 cout << endl;
 cout << endl;
}

void PrintInfo()
// Post : Some info about the rules of the game is printed
{
 cout << "First player is represented by O, second by X." << endl;
 cout << "Enter the coordinates for Rows and Columns" << endl;
 cout << "Strike enter when Done." << endl;
 cout << endl;
}

void PrintStats(int FirstPlayer, int SecondPlayer, int Draws)
// Pre : Players have to decide to exit the game.
// Post : Returns the number of won games and draws.
{
 cout << "The O player have won " << FirstPlayer << " times" << endl;
 cout << "The X player have won " << SecondPlayer << " times" << endl;
 cout << "There was " << Draws << " draws" << endl;
 cout << "Thanks for using my program" << endl;
}

void IfError(int& x, int& y)
// Pre : User entered invalid data, program doesn't kow where to put them
// Post : Error message is printed, user is prompted to enter the right input
{
 while (x > 3 || x < 1 || y > 3 || y < 1 ||('O'== board[x - 1][y - 1]
      ||'X'== board[x - 1][y - 1]))
    {
     cout << "This coordinate is not allowed, try again" << endl;
     cout << "row: ";
     cin  >> x;
     cout << "column: ";
     cin  >> y;
    }
}

void ChoiceOfChar(char& choice)
// Pre : Entire program has been executed
// Post : User is given a chance to play again
{
 cout << endl;
 cout << " Press CAPITAL Y if you want to play again." << endl;
 cout << " Otherwise, press any other letter key to exit the loop." << endl;
 cin  >> choice;
}

void PromptTurnO(int& x, int& y)
// Post : First player enters the coordinates of a spot on a game board
{
 cout << "Turn of the first player (O), enter the coordinates" << endl;
 cout << "Row: ";
 cin  >> x;
 cout << "Column: ";
 cin  >> y;
}

void PromptTurnX(int& x, int& y)
// Post : Second player enters the coordinates of a spot on a game board
{
 cout << "Turn of the second player (X), enter the coordinates"<< endl;
 cout << "Row: ";
 cin  >> x;
 cout << "Column: ";
 cin  >> y;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -