Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.83 KB | None | 0 0
  1. #include <iostream>
  2. using namespace::std;
  3.  
  4. class GameBoard {
  5.  
  6. public:
  7.     char board[9] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  8.     //Display the gameboard
  9.     void displayboard(){
  10.         cout << "---------------------" << endl << endl;
  11.         cout << "     |     |     " << endl;
  12.         cout << "  " << board[0] << "  |  " << board[1] << "  |  " << board[2] << endl;
  13.         cout << "_____|_____|_____" << endl;
  14.         cout << "     |     |     " << endl;
  15.         cout << "  " << board[3] << "  |  " << board[4] << "  |  " << board[5] << endl;
  16.         cout << "_____|_____|_____" << endl;
  17.         cout << "     |     |     " << endl;
  18.         cout << "  " << board[6] << "  |  " << board[7] << "  |  " << board[8] << endl;
  19.         cout << "     |     |     " << endl;
  20.         std::cout << "   Tic Tac Toe" << std::endl;
  21.     }
  22.     // Return 1 if player has won
  23.     // Return 0 if all moves have been taken aka draw
  24.     // Return -1 if games still playing
  25.     int wincheck(){
  26.         if (board[0] == board[1] && board[1] == board[2])
  27.             return 1;
  28.         else if (board[3] == board[4] && board[4] == board[5])
  29.             return 1;
  30.         else if (board[6] == board[7] && board[7] == board[8])
  31.             return 1;
  32.         else if (board[0] == board[3] && board[3] == board[6])
  33.             return 1;
  34.         else if (board[1] == board[4] && board[4] == board[7])
  35.             return 1;
  36.         else if (board[2] == board[6] && board[6] == board[8])
  37.             return 1;
  38.         else if (board[0] == board[4] && board[4] == board[8])
  39.             return 1;
  40.         else if (board[2] == board[4] && board[4] == board[6])
  41.             return 1;
  42.         //might be better to use a counter to just end the game at 9 turns since no more valid moves are avaliable??
  43.         else if (board[0] != '1' && board[1] != '2' && board[2] != '3'
  44.             && board[3] != '4' && board[4] != '5' && board[5] != '6'
  45.             && board[6] != '7' && board[7] != '8' && board[8] != '9')
  46.             return 0;
  47.         else
  48.             return -1;
  49.     }
  50.  
  51.  
  52. }GameOn;
  53.  
  54. int main(){
  55.  
  56.     int i = -1;
  57.     int choice = '5';
  58.     int playerturn = 'X';
  59.     char playerletter = 'X';
  60.  
  61.     do{
  62.         GameOn.displayboard();
  63.         if (playerturn == 'X'){
  64.             std::cout << "Player X turn: ";
  65.             playerletter = 'X';
  66.             playerturn = 'O';
  67.         }
  68.         else if (playerturn == 'O'){
  69.             std::cout << "Player O turn: ";
  70.             playerletter = 'O';
  71.             playerturn = 'X';
  72.         }
  73.         // TODO: Include a method so that when the player enters a taken index the player is allowed another turn to correct their mistake
  74.         std::cin >> choice;
  75.         if (GameOn.board[(choice - 1)] != 'X' && GameOn.board[(choice - 1)] != 'O'){
  76.             GameOn.board[(choice - 1)] = playerletter;
  77.         }
  78.         else
  79.             std::cout << "Enter a position not taken next time..." << std::endl;
  80.  
  81.         i = GameOn.wincheck();
  82.     } while (i == -1);
  83.     GameOn.displayboard();
  84.     // Win or Draw check
  85.     if (i == 0){
  86.         std::cout << "The game is a draw!" << std::endl;
  87.     }
  88.     // ERROR: playerletter displays random numbers possibly a scope issue relating to declaration/assignment error idk
  89.     else if (i == 1){
  90.         std::cout << "Player " << playerletter << " has won." << std::endl;
  91.     }
  92.     else{
  93.         std::cout << "I AM ERROR" << std::endl;
  94.     }
  95.     //IGNORE PREVIOUS COMMENTS
  96.     //to do player turn make it so we start on X then once we get the choice we check that array index to see
  97.     //if it is empty of O or X and if empty we assign that player to there and then assign the turn to the other player
  98. }
  99.  
  100.  
  101.  
  102. /*
  103. // The 'Gameboard'
  104. void gameboard(){
  105.     cout << "---------------------" << endl << endl;
  106.     cout << "     |     |     " << endl;
  107.     cout << "  " << board[0] << "  |  " << board[1] << "  |  " << board[2] << endl;
  108.     cout << "_____|_____|_____" << endl;
  109.     cout << "     |     |     " << endl;
  110.     cout << "  " << board[3] << "  |  " << board[4] << "  |  " << board[5] << endl;
  111.     cout << "_____|_____|_____" << endl;
  112.     cout << "     |     |     " << endl;
  113.     cout << "  " << board[6] << "  |  " << board[7] << "  |  " << board[8] << endl;
  114.     cout << "     |     |     " << endl;
  115.     std::cout << "   Tic Tac Toe" << std::endl;
  116.  
  117. }
  118.  
  119. // Return 1 if player has won
  120. // Return 0 if all moves have been taken aka draw
  121. // Return -1 if games still playing
  122. int wincheck(){
  123.     if (board[0] == board[1] && board[1] == board[2])
  124.         return 1;
  125.     else if (board[3] == board[4] && board[4] == board[5])
  126.         return 1;
  127.     else if (board[6] == board[7] && board[7] == board[8])
  128.         return 1;
  129.     else if (board[0] == board[3] && board[3] == board[6])
  130.         return 1;
  131.     else if (board[1] == board[4] && board[4] == board[7])
  132.         return 1;
  133.     else if (board[2] == board[6] && board[6] == board[8])
  134.         return 1;
  135.     else if (board[0] == board[4] && board[4] == board[8])
  136.         return 1;
  137.     else if (board[2] == board[4] && board[4] == board[6])
  138.         return 1;
  139.     //might be better to use a counter to just end the game at 9 turns since no more valid moves are avaliable??
  140.     else if (board[0] != '1' && board[1] != '2' && board[2] != '3'
  141.         && board[3] != '4' && board[4] != '5' && board[5] != '6'
  142.         && board[6] != '7' && board[7] != '8' && board[8] != '9')
  143.         return 0;
  144.     else
  145.         return -1;
  146. }
  147. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement