Advertisement
heroofhyla

Tic Tac Toe 1.0

Oct 3rd, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. /*
  2.  * Program : Tic Tac Toe
  3.  * Author  : Michael Stroud
  4.  * Version : 1.0
  5.  * Date    : 20141002
  6.  * Purpose : A simple 2 player tic tac toe game for the command line.
  7. */
  8. #include <iostream>
  9. #include <limits>
  10.  
  11. char gameboard[3][3] = {{'7','8','9'},
  12.     {'4','5','6'},
  13.     {'1','2','3'}};
  14. char playerSymbols[] = {'X','O'};
  15. void drawboard();
  16. void processPlayerMove(int);
  17. int getCoord();
  18. int xFromCoord(int coord);
  19. int yFromCoord(int coord);
  20.  
  21. enum gamestate{
  22.     UNFINISHED,
  23.     XWINS,
  24.     OWINS,
  25.     DRAW
  26. };
  27. gamestate getGameState(int);
  28. int main(){
  29.     std::cout << "***TIC TAC TOE***" << std::endl;
  30.     drawboard();
  31.     int elapsedTurns = 0;
  32.     while (getGameState(elapsedTurns) == UNFINISHED){
  33.         processPlayerMove(elapsedTurns%2);
  34.         drawboard();
  35.         ++elapsedTurns;
  36.     }
  37.     switch (getGameState(elapsedTurns)){
  38.         case DRAW:
  39.             std::cout << "Cat's game." << std::endl;
  40.             break;
  41.         case XWINS:
  42.             std::cout << "Player 1 wins." << std::endl;
  43.             break;
  44.         case OWINS:
  45.             std::cout << "Player 2 wins." << std::endl;
  46.             break;
  47.  
  48.     }
  49. }
  50.  
  51. void drawboard(){
  52.     for (int i = 0; i < 3; ++i){
  53.         for (int k = 0; k < 3; ++k){
  54.             std::cout << " " << gameboard[i][k];
  55.             if (k < 2){
  56.                 std::cout << " |";
  57.             }
  58.         }
  59.         std::cout << std::endl;
  60.         if (i < 2){
  61.             std::cout << "---+---+---" << std::endl;
  62.         }
  63.     }
  64. }
  65.  
  66. void processPlayerMove(int playerNumber){
  67.     std::cout << "Player " << playerNumber+1 << " choose where to place \'" <<
  68.         playerSymbols[playerNumber] << "\'" << std::endl;
  69.     int targetCoord = getCoord();
  70.     while (gameboard[yFromCoord(targetCoord)][xFromCoord(targetCoord)] != (targetCoord+'0')){
  71.         std::cout << "That spot is occupied. Please choose another spot." << std::endl;
  72.         targetCoord = getCoord();
  73.     }
  74.     gameboard[yFromCoord(targetCoord)][xFromCoord(targetCoord)] =playerSymbols[playerNumber];
  75. }
  76.  
  77. int getCoord(){
  78.     char userIn;
  79.     do {
  80.         std::cin >> userIn;
  81.     }while (userIn < '0' || userIn > '9');
  82.     std::cin.clear();
  83.     std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
  84.     return userIn-'0';
  85. }
  86.  
  87. int yFromCoord(int coord){
  88.     int zeroedCoord = coord-1;
  89.     zeroedCoord = 8-zeroedCoord;
  90.     return zeroedCoord/3;
  91. }
  92.  
  93. int xFromCoord(int coord){
  94.     int zeroedCoord = coord-1;
  95.     return zeroedCoord%3;
  96. }
  97.  
  98. gamestate getGameState(int elapsedTurns){
  99.     if (elapsedTurns < 5){
  100.         return UNFINISHED;
  101.     }
  102.     //check for vertical win
  103.     for (int i = 0; i < 3; i++){
  104.         if (gameboard[0][i] == gameboard[1][i] &&
  105.                 gameboard[0][i] == gameboard[2][i]){
  106.             if (gameboard[0][i] == 'X'){
  107.                 return XWINS;
  108.             }else{
  109.                 return OWINS;
  110.             }
  111.         }
  112.     }
  113.     //check for horizontal win
  114.     for (int i = 0; i < 3; i++){
  115.         if (gameboard[i][0] == gameboard[i][1] &&
  116.                 gameboard[i][0] == gameboard[1][2]){
  117.             if (gameboard[i][0] == 'X'){
  118.                 return XWINS;
  119.             }else{
  120.                 return OWINS;
  121.             }
  122.         }
  123.     }
  124.     //check for diagonal win
  125.     if (gameboard[0][0] == gameboard[1][1] &&
  126.             gameboard[0][0] == gameboard[2][2]){
  127.         if (gameboard[0][0] == 'X'){
  128.             return XWINS;
  129.         }else{
  130.             return OWINS;
  131.         }
  132.     }
  133.     if (gameboard[2][0] == gameboard [1][1] &&
  134.             gameboard[2][0] == gameboard[0][2]){
  135.         if (gameboard[2][0] == 'X'){
  136.             return XWINS;
  137.         }else{
  138.             return OWINS;
  139.         }
  140.     }
  141.     if (elapsedTurns == 9){
  142.         return DRAW;
  143.     }
  144.     return UNFINISHED;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement