Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "iostream"
- #include "ctime"
- #include "cstdlib"
- using namespace std;
- void print_table(int t[3][3]);
- int check_winner(int t[3][3]);
- int main()
- {
- int table[3][3] = {{0,0,0},{0,0,0},{0,0,0}};
- int player = 0, check_round = 0;
- bool mode = true;
- int p;
- cout<<"Do you want to play 1P or 2P?(1 or 2): ";
- cin>>p;
- if (p == 1)
- {
- mode = false;
- }
- srand(time(NULL));
- print_table(table);
- int winner=0,L,C;
- while(winner == 0 && check_round < 9)
- {
- if(player == 0)
- {
- if(mode)
- {
- do
- {
- cout<<"Enter coordinate for 'o'(e.g., 1 2): ";
- cin>>L>>C;
- } while(L<0||L>2||C<0||C>2||table[L][C]!=0);
- table[L][C] = 1;
- }
- else
- {
- do
- {
- L = rand() % 3;
- C = rand() % 3;
- } while(table[L][C]!=0);
- table[L][C] = 1;
- }
- }
- else
- {
- do
- {
- cout<<"Enter coordinate for 'x'(e.g., 1 2): ";
- cin>>L>>C;
- } while(L<0||L>2||C<0||C>2||table[L][C]!=0);
- table[L][C] = 2;
- }
- check_round++;
- winner = check_winner(table);
- player = !player;
- cout<<endl;
- print_table(table);
- }
- if(check_round == 9 && winner == 0)
- {
- cout<<"Draw\n";
- }
- else if(winner == 1)
- {
- cout<<"Player 'o' win\n";
- }
- else if(winner == 2)
- {
- cout<<"Player 'x' win\n";
- }
- return 0;
- }
- void print_table(int t[3][3])
- {
- for (int i = 0; i < 3; ++i)
- {
- for (int j = 0; j < 3; ++j)
- {
- cout<<" ";
- if(t[i][j] == 1)
- {
- cout <<"o";
- }
- else if (t[i][j] == 2)
- {
- cout<<"x";
- }
- else
- {
- cout<<" ";
- }
- cout<<" ";
- if(j!=2)
- {
- cout<<"|";
- }
- }
- cout<<endl;
- if (i!=2)
- {
- cout<<"---+---+---\n";
- }
- }
- }
- int check_winner(int t[3][3])
- {
- if(t[0][0] != 0 && t[0][0] == t[0][1] && t[0][1] == t[0][2])
- return t[0][0];
- else if(t[1][0] != 0 && t[1][0] == t[1][1] && t[1][1] == t[1][2])
- return t[1][0];
- else if(t[2][0] != 0 && t[2][0] == t[2][1] && t[2][1] == t[2][2])
- return t[2][0];
- else if(t[0][0] != 0 && t[0][0] == t[1][0] && t[1][0] == t[2][0])
- return t[0][0];
- else if(t[0][1] != 0 && t[0][1] == t[1][1] && t[1][1] == t[2][1])
- return t[0][1];
- else if(t[0][2] != 0 && t[0][2] == t[1][2] && t[1][2] == t[2][2])
- return t[0][2];
- else if(t[0][0] != 0 && t[0][0] == t[1][1] && t[1][1] == t[2][2])
- return t[0][0];
- else if(t[2][0] != 0 && t[2][0] == t[1][1] && t[1][1] == t[0][2])
- return t[2][0];
- else
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement