G2A Many GEOs
SHARE
TWEET

9x9 tic tac toe

coatimundi0 Mar 31st, 2020 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Name: Garo Martinez
  3. Lab: #10
  4. Section: 02
  5. Date: 3-28-2019
  6. Description: A CPU that plays 3-D Tic-Tac-Toe against the user. The program should randomly decides who goes first (computer/user).
  7.              Program knows and informs the user if an illegal move was made (cell already occupied, cell doesn't exist).
  8.              The program announces winner/draw.
  9. */
  10.  
  11.  
  12. #include <iostream>
  13. #include <string>
  14. #include <time.h>
  15. #include <cstdlib>
  16.  
  17. using namespace std;
  18.  
  19. class TicTacToe
  20. {
  21.     public:
  22.     // function declarations
  23.     void TheBoard ();
  24.     bool Validity ();
  25.     int isOver();
  26.     int randomNumber();
  27.     bool Draw();
  28.    
  29. };
  30.  
  31. //array is public so that we can change it when we need to!
  32. string cell[27] = {"0","1","2","3","4","5","6","7","8",
  33.                    "9","10","11","12","13","14","15","16","17",
  34.                    "18","19","20","21","22","23","24","25","26"};
  35.  
  36. // ----------------------MAIN FUNCTION----------------------
  37. int main ( )
  38. {
  39.    TicTacToe game;
  40.    
  41.    // variable declaration
  42.    int Turn = 3;
  43.    int Move;
  44.    int win=1;
  45.    
  46.    // this makes the first player random! 0 = player, 1 = CPU
  47.    srand(time(NULL));
  48.    Turn = rand()%2;
  49.    
  50.    
  51.     while(win==1)
  52. {
  53.        
  54.    cout << "\t\t\t\t***TIC TAC TOE GAME***\n" << endl;
  55.    //it displays the board, then states whose turn it is, then re-displays the board with their choice.
  56.    
  57.    game.TheBoard ();
  58.    win=game.isOver();
  59.  
  60.    if (Turn == 0 && win!=0)
  61.    {
  62.       cout << "Player turn." << endl;
  63.       cout << "Enter Cell Number: " << endl;
  64.       cin >> Move;
  65.  
  66.       if (Move>27 || Move<0)
  67.       {
  68.         cout << "Invalid option. Try again." << endl;
  69.         cout << "Player Move: " << endl;
  70.         cin >> Move;
  71.         game.Validity();
  72.        
  73.         if (game.Validity()==false)
  74.       {
  75.         cout << "Invalid option. Try again." << endl;
  76.         cout << "Player Move: " << endl;
  77.         cin >> Move;
  78.       }
  79.       }
  80.     }
  81.    //************************************************THIS WILL BE IMPORTANT*****************************************
  82.    else if (Turn == 1 && win!=0)
  83.    {
  84.       cout << "CPU turn." << endl;
  85.       srand(time(NULL));
  86.       Move=game.randomNumber();
  87.       cout << "CPU chose: " << Move << endl;
  88.    }
  89.  
  90.  
  91.  
  92.    // changes turns
  93.    
  94.    if(win==1)
  95.    {
  96.     switch (Turn)
  97.      {
  98.    case (0):
  99.       {
  100.          cell[Move] = 'x';
  101.          Turn = 1;
  102.          break;
  103.       }
  104.    case (1):
  105.       {
  106.          cell[Move] = 'o';
  107.          Turn = 0;
  108.       }
  109.      }
  110.     }
  111.   game.TheBoard ( );
  112.  }
  113. }
  114.  
  115.  
  116.  
  117. // ----------------------DETERMINES DRAW----------------------
  118.     bool TicTacToe::Draw()
  119. {
  120. for (int i = 0; i < 27; i++) {
  121.         if (cell[i] != "x" && cell[i] != "o") {
  122.             return false;
  123.                                                 }
  124.                             }
  125.     return true;
  126. }
  127.  
  128.  
  129.  
  130. // ----------------------DRAW THE BOARD----------------------
  131. void TicTacToe::TheBoard ( )
  132. {
  133.    cout << endl;
  134.    cout << cell[0] << " | " << cell[1] << " | " << cell[2] << endl;
  135.    cout << "--+---+--" << endl;
  136.    cout << cell[3] << " | " << cell[4] << " | " << cell[5] << endl;
  137.    cout << "--+---+--" << endl;
  138.    cout << cell[6] << " | " << cell[7] << " | " << cell[8] << endl;
  139.  
  140.    cout << "\t" << cell[9] << "  | " << cell[10] << " | " << cell[11] << endl;
  141.    cout << "\t" << "---+----+---" << endl;
  142.    cout << "\t" << cell[12] << " | " << cell[13] << " | " << cell[14] << endl;
  143.    cout << "\t" << "---+----+---" << endl;
  144.    cout << "\t" << cell[15] << " | " << cell[16] << " | " << cell[17] << endl;
  145.    
  146.    cout << "\t\t" << cell[18] << " | " << cell[19] << " | " << cell[20] << endl;
  147.    cout << "\t\t" << "---+----+---" << endl;
  148.    cout << "\t\t" << cell[21] << " | " << cell[22] << " | " << cell[23] << endl;
  149.    cout << "\t\t" << "---+----+---" << endl;
  150.    cout << "\t\t" << cell[24] << " | " << cell[25] << " | " << cell[26] << endl;
  151.    cout << endl;
  152. }
  153.  
  154.  
  155. //************************************************THIS WILL BE IMPORTANT*****************************************
  156. // ----------------------MOVE VALIDITY CHECKER----------------------
  157. bool TicTacToe::Validity ( )
  158. {
  159.     int Move;
  160.     if (cell[Move]=="x")
  161.     {
  162.             return false;
  163.     }
  164.     else if  (cell[Move]=="o")
  165.     {
  166.         return false;
  167.     }
  168.     else
  169.     {
  170.             return true;
  171.     }
  172. }
  173.  
  174.  
  175. //************************************************THIS WILL BE IMPORTANT*****************************************
  176. // ----------------------RANDOM NUMBER GENERATOR----------------------
  177. int TicTacToe::randomNumber()
  178. {
  179.     string block = "no";
  180.     int num;
  181.     srand(time(NULL));
  182.  
  183. while(block=="no")
  184. {
  185.       num=rand()%26;
  186.       if(cell[num]!="x"&&cell[num]!="o")
  187.       {
  188.          block="go";
  189.       }
  190. }
  191. return num;
  192. }
  193.  
  194.  
  195. // ----------------------CHECK FOR WIN----------------------
  196. int TicTacToe::isOver()
  197. {
  198.     int end=1;
  199.     TicTacToe game;
  200.     game.Draw();
  201.    
  202. if(game.Draw()==true){
  203.     cout<<"It's a Draw!"<<endl;
  204.                      }
  205.  
  206. else if(game.Draw()==false){
  207.     int win1=0, win2=0, win3=0, lose1=0, lose2=0, lose3=0;
  208.         // *********PLAYER WINS, GRID 1*********
  209. if(cell[0]=="x" && cell[1]=="x" && cell[2]=="x") // top row
  210.  {
  211.      win1=1;
  212.      cout<<"Player Wins Grid 1!"<<endl;
  213.  }
  214. else if(cell[3]=="x"&&cell[4]=="x" && cell[5]=="x") // middle row
  215.  {
  216.      win1=1;
  217.      cout<<"Player Wins Grid 1!"<<endl;
  218.  }
  219. else if(cell[6]=="x"&&cell[7]=="x" && cell[8]=="x") // bottom row
  220.  {
  221.      win1=1;
  222.      cout<<"Player Wins Grid 1!"<<endl;
  223.  }
  224. else if(cell[0]=="x"&&cell[3]=="x" && cell[6]=="x") // first column
  225.  {
  226.      win1=1;
  227.      cout<<"Player Wins Grid 1!"<<endl;
  228.  }
  229. else if(cell[1]=="x"&&cell[4]=="x" && cell[7]=="x") // middle column
  230.  {
  231.      win1=1;
  232.      cout<<"Player Wins Grid 1!"<<endl;
  233.  }
  234. else if(cell[2]=="x"&&cell[5]=="x" && cell[8]=="x") // last column
  235.  {
  236.      win1=1;
  237.      cout<<"Player Wins Grid 1!"<<endl;
  238.  }
  239. else if(cell[0]=="x"&&cell[4]=="x" && cell[8]=="x") // l->r diagonal
  240.  {
  241.      win1=1;
  242.      cout<<"Player Wins Grid 1!"<<endl;
  243.  }
  244. else if(cell[2]=="x"&&cell[4]=="x" && cell[6]=="x") // r->l diagonal
  245.  {
  246.      win1=1;
  247.      cout<<"Player Wins Grid 1!"<<endl;
  248.  }
  249. // *********PLAYER WINS, GRID 2*********
  250.  if(cell[9]=="x" && cell[10]=="x" && cell[11]=="x") // top row
  251.  {
  252.      win2=1;
  253.      cout<<"Player Wins Grid 2!"<<endl;
  254.  }
  255. else if(cell[12]=="x"&&cell[13]=="x" && cell[14]=="x") // middle row
  256.  {
  257.      win2=1;
  258.      cout<<"Player Wins Grid 2!"<<endl;
  259.  }
  260. else if(cell[15]=="x"&&cell[16]=="x" && cell[17]=="x") // bottom row
  261.  {
  262.      win2=1;
  263.      cout<<"Player Wins Grid 2!"<<endl;
  264.  }
  265. else if(cell[9]=="x"&&cell[12]=="x" && cell[15]=="x") // first column
  266.  {
  267.      win2=1;
  268.      cout<<"Player Wins Grid 2!"<<endl;
  269.  }
  270. else if(cell[10]=="x"&&cell[13]=="x" && cell[16]=="x") // middle column
  271.  {
  272.      win2=1;
  273.      cout<<"Player Wins Grid 2!"<<endl;
  274.  }
  275. else if(cell[11]=="x"&&cell[14]=="x" && cell[17]=="x") // last column
  276.  {
  277.      win2=1;
  278.      cout<<"Player Wins Grid 2!"<<endl;
  279.  }
  280. else if(cell[9]=="x"&&cell[13]=="x" && cell[17]=="x") // l->r diagonal
  281.  {
  282.      win2=1;
  283.      cout<<"Player Wins Grid 2!"<<endl;
  284.  }
  285. else if(cell[11]=="x"&&cell[13]=="x" && cell[15]=="x") // r->l diagonal
  286.  {
  287.      win2=1;
  288.      cout<<"Player Wins Grid 2!"<<endl;
  289.  }
  290.  // *********PLAYER WINS, GRID 3*********
  291.  if(cell[18]=="x" && cell[19]=="x" && cell[20]=="x") // top row
  292.  {
  293.      win3=1;
  294.      cout<<"Player Wins Grid 3!"<<endl;
  295.  }
  296. else if(cell[21]=="x"&&cell[22]=="x" && cell[23]=="x") // middle row
  297.  {
  298.      win3=1;
  299.      cout<<"Player Wins Grid 3!"<<endl;
  300.  }
  301. else if(cell[24]=="x"&&cell[25]=="x" && cell[26]=="x") // bottom row
  302.  {
  303.      win3=1;
  304.      cout<<"Player Wins Grid 3!"<<endl;
  305.  }
  306. else if(cell[18]=="x"&&cell[21]=="x" && cell[24]=="x") // first column
  307.  {
  308.      win3=1;
  309.      cout<<"Player Wins Grid 3!"<<endl;
  310.  }
  311. else if(cell[19]=="x"&&cell[22]=="x" && cell[25]=="x") // middle column
  312.  {
  313.      win3=1;
  314.      cout<<"Player Wins Grid 3!"<<endl;
  315.  }
  316. else if(cell[20]=="x"&&cell[23]=="x" && cell[26]=="x") // last column
  317.  {
  318.      win3=1;
  319.      cout<<"Player Wins Grid 3!"<<endl;
  320.  }
  321. else if(cell[18]=="x"&&cell[22]=="x" && cell[26]=="x") // l->r diagonal
  322.  {
  323.      win3=1;
  324.      cout<<"Player Wins Grid 3!"<<endl;
  325.  }
  326. else if(cell[20]=="x"&&cell[22]=="x" && cell[24]=="x") // r->l diagonal
  327.  {
  328.      win3=1;
  329.      cout<<"Player Wins Grid 3!"<<endl;
  330.  }
  331.  
  332.  
  333. // *********PLAYER LOSES, GRID 1*********
  334. if(cell[0]=="o"&&cell[1]=="o" && cell[2]=="o")
  335.  {
  336.      lose1=1;
  337.      cout<<"CPU Wins Grid 1!"<<endl;
  338.  }
  339. else if(cell[3]=="o"&&cell[4]=="o" && cell[5]=="o")
  340.  {
  341.      lose1=1;
  342.      cout<<"CPU Wins Grid 1!"<<endl;
  343.  }
  344. else if(cell[6]=="o"&&cell[7]=="o" && cell[8]=="o")
  345.  {
  346.      lose1=1;
  347.      cout<<"CPU Wins Grid 1!"<<endl;
  348.  }
  349. else if(cell[0]=="o"&&cell[3]=="o" && cell[6]=="o")
  350.  {
  351.      lose1=1;
  352.      cout<<"CPU Wins Grid 1!"<<endl;
  353.  }
  354. else if(cell[1]=="o"&&cell[4]=="o" && cell[7]=="o")
  355.  {
  356.      lose1=1;
  357.      cout<<"CPU Wins Grid 1!"<<endl;
  358.  }
  359. else if(cell[2]=="o"&&cell[5]=="o" && cell[8]=="o")
  360.  {
  361.      lose1=1;
  362.      cout<<"CPU Wins Grid 1!"<<endl;
  363.  }
  364. else if(cell[0]=="o"&&cell[4]=="o" && cell[8]=="o")
  365.  {
  366.      lose1=1;
  367.      cout<<"CPU Wins Grid 1!"<<endl;
  368.  }
  369. else if(cell[2]=="o"&&cell[4]=="o" && cell[6]=="o")
  370.  {
  371.      lose1=1;
  372.      cout<<"CPU Wins Grid 1!"<<endl;
  373.  }
  374.  // *********PLAYER LOSES, GRID 2*********
  375. if(cell[9]=="o"&&cell[10]=="o" && cell[11]=="o")
  376.  {
  377.      lose2=1;
  378.      cout<<"CPU Wins Grid 2!"<<endl;
  379.  }
  380. else if(cell[12]=="o"&&cell[13]=="o" && cell[14]=="o")
  381.  {
  382.      lose2=1;
  383.      cout<<"CPU Wins Grid 2!"<<endl;
  384.  }
  385. else if(cell[15]=="o"&&cell[16]=="o" && cell[17]=="o")
  386.  {
  387.      lose2=1;
  388.      cout<<"CPU Wins Grid 2!"<<endl;
  389.  }
  390. else if(cell[9]=="o"&&cell[12]=="o" && cell[15]=="o")
  391.  {
  392.      lose2=1;
  393.      cout<<"CPU Wins Grid 2!"<<endl;
  394.  }
  395. else if(cell[10]=="o"&&cell[13]=="o" && cell[16]=="o")
  396.  {
  397.      lose2=1;
  398.      cout<<"CPU Wins Grid 2!"<<endl;
  399.  }
  400. else if(cell[11]=="o"&&cell[14]=="o" && cell[17]=="o")
  401.  {
  402.      lose2=1;
  403.      cout<<"CPU Wins Grid 2!"<<endl;
  404.  }
  405. else if(cell[9]=="o"&&cell[13]=="o" && cell[17]=="o")
  406.  {
  407.      lose2=1;
  408.      cout<<"CPU Wins Grid 2!"<<endl;
  409.  }
  410. else if(cell[11]=="o"&&cell[13]=="o" && cell[15]=="o")
  411.  {
  412.      lose2=1;
  413.      cout<<"CPU Wins Grid 2!"<<endl;
  414.  }
  415.  // *********PLAYER LOSES, GRID 3*********
  416. if(cell[18]=="o"&&cell[19]=="o" && cell[20]=="o")
  417.  {
  418.      lose3=1;
  419.      cout<<"CPU Wins Grid 3!"<<endl;
  420.  }
  421. else if(cell[21]=="o"&&cell[22]=="o" && cell[23]=="o")
  422.  {
  423.      lose3=1;
  424.      cout<<"CPU Wins Grid 3!"<<endl;
  425.  }
  426. else if(cell[24]=="o"&&cell[25]=="o" && cell[26]=="o")
  427.  {
  428.      lose3=1;
  429.      cout<<"CPU Wins Grid 3!"<<endl;
  430.  }
  431. else if(cell[18]=="o"&&cell[21]=="o" && cell[24]=="o")
  432.  {
  433.      lose3=1;
  434.      cout<<"CPU Wins Grid 3!"<<endl;
  435.  }
  436. else if(cell[19]=="o"&&cell[22]=="o" && cell[25]=="o")
  437.  {
  438.      lose3=1;
  439.      cout<<"CPU Wins Grid 3!"<<endl;
  440.  }
  441. else if(cell[20]=="o"&&cell[23]=="o" && cell[26]=="o")
  442.  {
  443.      lose3=1;
  444.      cout<<"CPU Wins Grid 3!"<<endl;
  445.  }
  446. else if(cell[18]=="o"&&cell[22]=="o" && cell[26]=="o")
  447.  {
  448.      lose3=1;
  449.      cout<<"CPU Wins Grid 3!"<<endl;
  450.  }
  451. else if(cell[20]=="o"&&cell[22]=="o" && cell[24]=="o")
  452.  {
  453.      lose3=1;
  454.      cout<<"CPU Wins Grid 3!"<<endl;
  455.  }
  456.  
  457. int tally1, tally2;
  458. tally1 = win1+win2+win3;
  459. tally2 = lose1+lose2+lose3;
  460.  
  461.  
  462. if ((tally1==2 && tally2==1) || tally1==3)
  463.     {
  464.         cout << "Player wins!" << endl;
  465.         end=0;
  466.     }
  467. else if ((tally2==2 && tally1==1) || tally2==3)
  468.     {
  469.         cout << "CPU wins!" << endl;
  470.         end=0;
  471.     }
  472. }
  473.    
  474. return end;
  475. }
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top