Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <cstring>
- using namespace std;
- char tabel[4][4];
- bool game, corect;
- int Player, Player1, pctO, pctX;
- void display()
- {
- system("cls");
- for(int i=1;i<=3;i++)
- {
- for(int j=1;j<=3;j++)
- {
- cout<<tabel[i][j]<<" ";
- } cout<<endl;
- }
- }
- void winner(string winner)
- { char raspuns[10] = " ";
- display();
- cout<<endl<<"Winner: "<<winner;
- game = true;
- exit(0);
- }
- void checkForWin()
- {
- for(int i=1;i<=3;i++)
- {
- for(int j=1;j<=3;j++){
- if(tabel[i][j]== 'O')
- pctO++;
- else if(tabel[i][j]== 'X')
- pctX++;
- }
- if(pctO == 3) winner("O");
- if(pctX == 3) winner("X");
- pctX = 0, pctO = 0;
- }
- for(int i=1;i<=3;i++)
- {
- for(int j=1;j<=3;j++){
- if(tabel[j][i]== 'O')
- pctO++;
- else if(tabel[j][i]== 'X')
- pctX++;
- }
- if(pctO == 3) winner("O");
- if(pctX == 3) winner("X");
- pctX = 0, pctO = 0;
- }
- for(int j=1;j<=3;j++){
- if(tabel[j][j]== 'O')
- pctO++;
- else if(tabel[j][j]== 'X')
- pctX++;
- }
- if(pctO == 3) winner("O");
- if(pctX == 3) winner("X");
- pctX = 0, pctO = 0;
- for(int j=3;j>=1;j--){
- if(tabel[3-j+1][j]== 'O')
- pctO++;
- else if(tabel[3-j+1][j]== 'X')
- pctX++;
- }
- if(pctO == 3) winner("O");
- if(pctX == 3) winner("X");
- pctX = 0, pctO = 0;
- }
- int main()
- {
- for(int i=1;i<=3;i++)
- for(int j=1;j<=3;j++)
- tabel[i][j]=(char)176;
- while(!game)
- {
- display();
- while(!corect)
- {
- cout<<"X:"<<" ";
- cin>>Player>>Player1;
- if(tabel[Player][Player1]!=(char)176 || Player<1 || Player1<1 || Player>3 || Player1>3 ) cout<<endl<<"Invalid position."<<endl;
- else
- {
- tabel[Player][Player1] = 'X';
- corect = true;
- }
- }
- corect = false;
- checkForWin();
- while(!corect)
- {
- cout<<"O:"<<" ";
- cin>>Player>>Player1;
- if(tabel[Player][Player1]!=(char)176 || Player<1 || Player1<1 || Player>3 || Player1>3 ) cout<<endl<<"Invalid position."<<endl;
- else{
- tabel[Player][Player1] = 'O';
- corect = true;
- }
- }
- corect = false;
- checkForWin();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement