markyrocks

ttt

May 22nd, 2021 (edited)
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. #include <iostream>
  2. #include<iomanip>
  3. #include <string>
  4. using namespace std;
  5. void drawBoard();
  6. void getSlotChoice(int& slot);
  7. void playGame(bool f, bool& in);
  8.  
  9. #define PLAYER_X 'X';
  10. #define PLAYER_Y 'O';
  11. char lastPlayer = PLAYER_Y;
  12. char currentPlayer = PLAYER_X;
  13. int slot;
  14.  
  15. char board[3][3] = { { '1','2','3'},{'4','5','6'},{'7','8','9'} };
  16. int main()
  17. {
  18.     bool flip = true;
  19.     bool ingame = true;
  20.     int turnCount = 0;
  21.     do {
  22.         drawBoard();
  23.         //getSlotChoice(slot);
  24.         playGame(flip,ingame);
  25.         flip = !flip;
  26.         turnCount++;
  27.     } while (ingame && turnCount<9);
  28. }
  29.  
  30. void drawBoard()
  31. {
  32.  
  33.     for (int y = 0; y < 3; y++)
  34.  
  35.     {
  36.         for (int x = 0; x < 3; x++)
  37.         {
  38.  
  39.         }
  40.     }
  41.     cout << "current board state" << endl;
  42.     cout << "        " << endl;
  43.     cout << "    " << board[0][0] << "|" << board[0][1] << "|" << board[0][2] << "    " << endl;
  44.     cout << "   +-+-+-+    " << endl;
  45.     cout << "    " << board[1][0] << "|" << board[1][1] << "|" << board[1][2] << "    " << endl;
  46.     cout << "   +-+-+-+    " << endl;
  47.     cout << "    " << board[2][0] << "|" << board[2][1] << "|" << board[2][2] << "    " << endl;
  48.     cout << "       " << endl;
  49.  
  50.  
  51.  
  52. }
  53. void getSlotChoice(int& slot)
  54.  
  55. {
  56.     cout << " player please choose a slot from 1-9 " << endl;
  57.     cin >> slot;
  58.  
  59.     while ((slot < 1) || (slot > 9))
  60.     {
  61.         cout << "invalid choice , please choose a slot 1-9 ";
  62.         cin >> slot;
  63.     }
  64.  
  65. }
  66. void playGame(bool f,bool& in)
  67. {
  68.     getSlotChoice(slot);
  69.    
  70.         auto Player = f ? currentPlayer : lastPlayer;
  71.         board[(slot - slot % 3) / 3][slot%3-1] = Player;
  72.  
  73.         int Hcount = 0;
  74.         int Vcount = 0;
  75.         int Lcount = 0;
  76.         int Rcount = 0;
  77.  
  78.     for (auto y = 0; y < 3; y++) {              //this is all untested
  79.         Hcount=0;
  80.         Vcount=0;
  81.         for (auto x = 0; x < 3; x++) {
  82.             if (board[x][y] == Player) { Hcount++; }
  83.             if (board[y][x] == Player) { Vcount++; }
  84.         }
  85.         if(Hcount==3 || Vcount==3){break;)
  86.         if (board[y][y] == Player) { Lcount++; }
  87.         if (board[y][2 - y] == Player) { Rcount++; }
  88.     }
  89.  
  90.     if (Hcount == 3 || Vcount == 3 || Lcount==3 || Rcount==3) {
  91.         cout << "player " << Player << " Wins";
  92.         in = false;
  93.     }
  94.  
  95.    /* if ((board[0][0] == lastPlayer) && (board[0][1] == lastPlayer) && (board[0][2] == lastPlayer))
  96.     {
  97.         cout << " PLayer Y wins on the top row ! " << endl;
  98.         in = false;
  99.     }
  100.     if ((board[0][0] == currentPlayer) && (board[0][1] == currentPlayer) && (board[0][2] == currentPlayer))
  101.     {
  102.         cout << " player X wins on the top row !" << endl;
  103.         in = false;
  104.     }*/
  105.  
  106. }
  107.  
Add Comment
Please, Sign In to add comment