Advertisement
KeeganT

Tic Tac Toe

Dec 11th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3.  
  4. using namespace std;
  5.  
  6. char spots[]{'1','2','3','4','5','6','7','8','9'};
  7. bool hasWon=false, hasFound=false;
  8. char p1input, p2input;
  9. char nums[]{'1','2','3','4','5','6','7','8','9'};
  10.  
  11. void color(int num)
  12. {
  13.     HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);
  14.     SetConsoleTextAttribute(color, num);
  15. }
  16.  
  17. void board()
  18. {
  19.     system("cls");
  20.     cout<<endl;
  21.     cout<<"\t\t "<<spots[0]<<" | "<<spots[1]<<" | "<<spots[2]<<" "<<endl;
  22.     cout<<"\t\t---+---+---"<<endl;
  23.     cout<<"\t\t "<<spots[3]<<" | "<<spots[4]<<" | "<<spots[5]<<" "<<endl;
  24.     cout<<"\t\t---+---+---"<<endl;
  25.     cout<<"\t\t "<<spots[6]<<" | "<<spots[7]<<" | "<<spots[8]<<" "<<endl;
  26.     cout<<endl;
  27. }
  28.  
  29. void winCheck()
  30. {
  31.     if(spots[0]=='X'&&spots[1]=='X'&&spots[2]=='X')hasWon=true;
  32.     if(spots[3]=='X'&&spots[4]=='X'&&spots[5]=='X')hasWon=true;
  33.     if(spots[6]=='X'&&spots[7]=='X'&&spots[8]=='X')hasWon=true;
  34.     if(spots[0]=='X'&&spots[3]=='X'&&spots[6]=='X')hasWon=true;
  35.     if(spots[1]=='X'&&spots[4]=='X'&&spots[7]=='X')hasWon=true;
  36.     if(spots[2]=='X'&&spots[5]=='X'&&spots[8]=='X')hasWon=true;
  37.     if(spots[2]=='X'&&spots[4]=='X'&&spots[6]=='X')hasWon=true;
  38.     if(spots[0]=='X'&&spots[4]=='X'&&spots[8]=='X')hasWon=true;
  39.     if(spots[2]=='X'&&spots[4]=='X'&&spots[6]=='X')hasWon=true;
  40.     if(spots[0]=='O'&&spots[1]=='O'&&spots[2]=='O')hasWon=true;
  41.     if(spots[3]=='O'&&spots[4]=='O'&&spots[5]=='O')hasWon=true;
  42.     if(spots[6]=='O'&&spots[7]=='O'&&spots[8]=='O')hasWon=true;
  43.     if(spots[0]=='O'&&spots[3]=='O'&&spots[6]=='O')hasWon=true;
  44.     if(spots[1]=='O'&&spots[4]=='O'&&spots[7]=='O')hasWon=true;
  45.     if(spots[2]=='O'&&spots[5]=='O'&&spots[8]=='O')hasWon=true;
  46.     if(spots[2]=='O'&&spots[4]=='O'&&spots[6]=='O')hasWon=true;
  47. }
  48.  
  49. void p1move()
  50. {
  51.     for(int c=0;c<9;c++)
  52.     {
  53.         for(int x=0;x<9;x++)
  54.         {
  55.             if(p1input==nums[x])hasFound=true;
  56.             if(x==8&&hasFound==false)
  57.             {
  58.                 cout<<"That is not a choice! Please pick a number on the board!"<<endl;
  59.                 cin>>p1input;
  60.             }
  61.         }
  62.         hasFound=false;
  63.         while(p1input==nums[c]&&spots[c]!=nums[c])
  64.         {
  65.             cout<<"Someone has already taken this spot! Please pick another!"<<endl;
  66.             cin>>p1input;
  67.         }
  68.         if(p1input==nums[c]&&spots[c]!='X'&&spots[c]!='O')spots[c]='X';
  69.     }
  70. }
  71.  
  72. void p2move()
  73. {
  74.     for(int c=0;c<9;c++)
  75.     {
  76.         for(int x=0;x<9;x++)
  77.         {
  78.             if(p2input==nums[x])hasFound=true;
  79.             if(x==8&&hasFound==false)
  80.             {
  81.                 cout<<"That is not a choice! Please pick a number on the board!"<<endl;
  82.                 cin>>p2input;
  83.             }
  84.         }
  85.         hasFound=false;
  86.         while(p2input==nums[c]&&spots[c]!=nums[c])
  87.         {
  88.             cout<<"Someone has already taken this spot! Please pick another!"<<endl;
  89.             cin>>p2input;
  90.         }
  91.         if(p2input==nums[c]&&spots[c]!='X'&&spots[c]!='O')spots[c]='O';
  92.     }
  93. }
  94.  
  95. int main()
  96. {
  97.     cout<<"Welcome to Tic Tac Toe!"<<endl;
  98.     cout<<"-----------------------"<<endl;
  99.     cout<<"In order to play, you need two players!"<<endl;
  100.     cout<<"Player one is 'X', and player two is 'O'"<<endl;
  101.     cout<<"Press any key to begin!"<<endl;
  102.     system("pause>null");
  103.     board();
  104.     int winner=2, round=0;
  105.     while(hasWon==false)
  106.     {
  107.         cout<<"Player 1, where would you like to move?"<<endl;
  108.         cin>>p1input;
  109.         p1move();
  110.         system("cls");
  111.         board();
  112.         winCheck();
  113.         if(hasWon==true)
  114.         {
  115.             winner=1;
  116.             break;
  117.         }
  118.         round++;
  119.         if(round==9&&hasWon==false)break;
  120.         cout<<"Player 2, where would you like to move?"<<endl;
  121.         cin>>p2input;
  122.         p2move();
  123.         system("cls");
  124.         board();
  125.         winCheck();
  126.         round++;
  127.         if(round==9&&hasWon==false)break;
  128.     }
  129.     if(round!=9)cout<<"Player "<<winner<<" is the winner!"<<endl;
  130.     else cout<<"It's a tie!"<<endl;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement