Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- using namespace std;
- char spots[]{'1','2','3','4','5','6','7','8','9'};
- bool hasWon=false, hasFound=false;
- char p1input, p2input;
- char nums[]{'1','2','3','4','5','6','7','8','9'};
- void color(int num)
- {
- HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(color, num);
- }
- void board()
- {
- system("cls");
- cout<<endl;
- cout<<"\t\t "<<spots[0]<<" | "<<spots[1]<<" | "<<spots[2]<<" "<<endl;
- cout<<"\t\t---+---+---"<<endl;
- cout<<"\t\t "<<spots[3]<<" | "<<spots[4]<<" | "<<spots[5]<<" "<<endl;
- cout<<"\t\t---+---+---"<<endl;
- cout<<"\t\t "<<spots[6]<<" | "<<spots[7]<<" | "<<spots[8]<<" "<<endl;
- cout<<endl;
- }
- void winCheck()
- {
- if(spots[0]=='X'&&spots[1]=='X'&&spots[2]=='X')hasWon=true;
- if(spots[3]=='X'&&spots[4]=='X'&&spots[5]=='X')hasWon=true;
- if(spots[6]=='X'&&spots[7]=='X'&&spots[8]=='X')hasWon=true;
- if(spots[0]=='X'&&spots[3]=='X'&&spots[6]=='X')hasWon=true;
- if(spots[1]=='X'&&spots[4]=='X'&&spots[7]=='X')hasWon=true;
- if(spots[2]=='X'&&spots[5]=='X'&&spots[8]=='X')hasWon=true;
- if(spots[2]=='X'&&spots[4]=='X'&&spots[6]=='X')hasWon=true;
- if(spots[0]=='X'&&spots[4]=='X'&&spots[8]=='X')hasWon=true;
- if(spots[2]=='X'&&spots[4]=='X'&&spots[6]=='X')hasWon=true;
- if(spots[0]=='O'&&spots[1]=='O'&&spots[2]=='O')hasWon=true;
- if(spots[3]=='O'&&spots[4]=='O'&&spots[5]=='O')hasWon=true;
- if(spots[6]=='O'&&spots[7]=='O'&&spots[8]=='O')hasWon=true;
- if(spots[0]=='O'&&spots[3]=='O'&&spots[6]=='O')hasWon=true;
- if(spots[1]=='O'&&spots[4]=='O'&&spots[7]=='O')hasWon=true;
- if(spots[2]=='O'&&spots[5]=='O'&&spots[8]=='O')hasWon=true;
- if(spots[2]=='O'&&spots[4]=='O'&&spots[6]=='O')hasWon=true;
- }
- void p1move()
- {
- for(int c=0;c<9;c++)
- {
- for(int x=0;x<9;x++)
- {
- if(p1input==nums[x])hasFound=true;
- if(x==8&&hasFound==false)
- {
- cout<<"That is not a choice! Please pick a number on the board!"<<endl;
- cin>>p1input;
- }
- }
- hasFound=false;
- while(p1input==nums[c]&&spots[c]!=nums[c])
- {
- cout<<"Someone has already taken this spot! Please pick another!"<<endl;
- cin>>p1input;
- }
- if(p1input==nums[c]&&spots[c]!='X'&&spots[c]!='O')spots[c]='X';
- }
- }
- void p2move()
- {
- for(int c=0;c<9;c++)
- {
- for(int x=0;x<9;x++)
- {
- if(p2input==nums[x])hasFound=true;
- if(x==8&&hasFound==false)
- {
- cout<<"That is not a choice! Please pick a number on the board!"<<endl;
- cin>>p2input;
- }
- }
- hasFound=false;
- while(p2input==nums[c]&&spots[c]!=nums[c])
- {
- cout<<"Someone has already taken this spot! Please pick another!"<<endl;
- cin>>p2input;
- }
- if(p2input==nums[c]&&spots[c]!='X'&&spots[c]!='O')spots[c]='O';
- }
- }
- int main()
- {
- cout<<"Welcome to Tic Tac Toe!"<<endl;
- cout<<"-----------------------"<<endl;
- cout<<"In order to play, you need two players!"<<endl;
- cout<<"Player one is 'X', and player two is 'O'"<<endl;
- cout<<"Press any key to begin!"<<endl;
- system("pause>null");
- board();
- int winner=2, round=0;
- while(hasWon==false)
- {
- cout<<"Player 1, where would you like to move?"<<endl;
- cin>>p1input;
- p1move();
- system("cls");
- board();
- winCheck();
- if(hasWon==true)
- {
- winner=1;
- break;
- }
- round++;
- if(round==9&&hasWon==false)break;
- cout<<"Player 2, where would you like to move?"<<endl;
- cin>>p2input;
- p2move();
- system("cls");
- board();
- winCheck();
- round++;
- if(round==9&&hasWon==false)break;
- }
- if(round!=9)cout<<"Player "<<winner<<" is the winner!"<<endl;
- else cout<<"It's a tie!"<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement