Advertisement
Guest User

Tic-Tac-Toe

a guest
Nov 28th, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.85 KB | None | 0 0
  1. //Lets play a game of tic-tac-toe
  2. //Created by Zell Faze
  3. //Available freely under the CC-0 License
  4. #include <iostream>
  5. #include <string>
  6. #include <sstream>
  7. #include <stdlib.h>
  8. using namespace std;
  9.  
  10. void outputBoard(int board[][3]);
  11. void initBoard(int board[][3]);
  12. int  checkWinners(int board[][3]);
  13. void makeAIMove(int board[][3]);
  14.  
  15. int main() {
  16.   int       board[3][3];
  17.   string    inputStr;
  18.   bool      gameDone = false;
  19.   int       selectedMove;
  20.  
  21.   //Initialise the board to empty
  22.   initBoard(board);
  23.   srand(time(NULL));
  24.  
  25.   //Main game loop
  26.   while (gameDone == false) {
  27.     outputBoard(board);
  28.    
  29.     bool inputValid = false;
  30.     int x, y;
  31.     do {
  32.       cout << "Where would you like to move? (1-9) ";
  33.       getline(cin, inputStr);
  34.       stringstream(inputStr) >> selectedMove;
  35.      
  36.       //Lets start assuming the input is valid (kinda odd I know)
  37.       inputValid = true;
  38.      
  39.       if ((selectedMove < 0) || (selectedMove > 9)) {
  40.         cout << "That move is invalid." << endl;
  41.         inputValid = false;
  42.       }
  43.      
  44.       x = (selectedMove / 3);
  45.       y = (selectedMove % 3) - 1;
  46.      
  47.       if (board[x][y] != 0) {
  48.         cout << "That move is invalid." << endl;
  49.         inputValid = false;
  50.       }
  51.     } while (inputValid == false);
  52.    
  53.     board[x][y] = 1;
  54.    
  55.     int winner;
  56.     winner = checkWinners(board);
  57.    
  58.     if (winner == 0) {
  59.       makeAIMove(board);
  60.     }
  61.    
  62.     winner = checkWinners(board);
  63.    
  64.     if (winner != 0) {
  65.       gameDone = true;
  66.       outputBoard(board);
  67.       cout << "Player " << winner << " won the game!";
  68.     }
  69.   }
  70.  
  71.   return 0;
  72. }
  73.  
  74. void initBoard(int board[][3]) {
  75.   for (int x = 0; x < 3; x++) {
  76.     for (int y = 0; y < 3; y++) {
  77.       board[x][y] = 0;
  78.     }
  79.   }
  80.  
  81.   return;
  82. }
  83.  
  84. void outputBoard(int board[][3]) {
  85.  
  86.   for (int x = 0; x < 3; x++) {
  87.     for (int y = 0; y < 3; y++) {
  88.      
  89.       //Output the state
  90.       switch (board[x][y]) {
  91.         case 0:
  92.           cout << " ";
  93.           break;
  94.         case 1:
  95.           cout << "X";
  96.           break;
  97.         case 2:
  98.           cout << "O";
  99.           break;
  100.       }
  101.      
  102.       //Output the col seperator
  103.       if (y == 2) {
  104.         cout << endl;
  105.       } else {
  106.         cout << "|";
  107.       }
  108.     }
  109.    
  110.     //Output the row seperator
  111.     if (x != 2) {
  112.       cout << "-----" << endl;
  113.     }
  114.   }
  115.  
  116.   cout << endl;
  117.   return;
  118. }
  119.  
  120. int checkWinners(int board[][3]) {
  121.   int player = 0;
  122.  
  123.   //Check for horizontal wins
  124.   for (int x = 0; x < 3; x++) {
  125.       player = board[x][0];
  126.       if (player == 0) {
  127.         continue;
  128.       }
  129.      
  130.       bool possible = true;
  131.       for (int y = 1; y < 3; y++) {
  132.         if (board[x][y] == player) {
  133.           continue;
  134.         } else {
  135.           possible = false;
  136.           break;
  137.         }
  138.       }
  139.      
  140.       if (possible == true) {
  141.         return player;
  142.       }
  143.   }
  144.  
  145.   //Check for vertical wins
  146.   for (int y = 0; y < 3; y++) {
  147.       player = board[0][y];
  148.       if (player == 0) {
  149.         continue;
  150.       }
  151.      
  152.       bool possible = true;
  153.       for (int x = 1; x < 3; x++) {
  154.         if (board[x][y] == player) {
  155.           continue;
  156.         } else {
  157.           possible = false;
  158.           break;
  159.         }
  160.       }
  161.      
  162.       if (possible == true) {
  163.         return player;
  164.       }
  165.   }
  166.  
  167.   //Check for diagonal wins
  168.   player = board[0][0];
  169.   if ((board[1][1] == player) && (board[2][2] == player)) {
  170.     return player;
  171.   }
  172.  
  173.   player = board[0][2];
  174.   if ((board[1][1] == player) && (board[2][0] == player)) {
  175.     return player;
  176.   }
  177.  
  178.   return 0;
  179. }
  180.  
  181. void makeAIMove(int board[][3]) {
  182.   int x, y;
  183.   bool validMove = false;
  184.  
  185.   do {
  186.     x = rand() % 3;
  187.     y = rand() % 3;
  188.    
  189.     if (board[x][y] == 0) {
  190.       validMove = true;
  191.     }
  192.   } while(validMove == false);
  193.  
  194.   board[x][y] = 2;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement