Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main()
- {
- // First, we declare the matrix or our grid.
- char GRID[3][3];
- // We initialize all of the elements from the matrix as '*' too see the grid.
- for(int row = 0; row <= 2; ++row)
- {
- for(int col = 0; col <= 2; ++col)
- {
- GRID[row][col]=' ';
- }
- cout << '\n';
- }
- int player_move, player_won = 0;
- for(int moves = 1; moves <= 9; ++moves)
- {
- // We ask for next players move.
- cout << "Your move please: \n";
- cin >> player_move;
- // If the move is an odd number then it's Player1 turn, if not it's Player2 turn.
- if(moves % 2 == 1)
- {
- // We will use these ifs to see where our X/0 will go.
- if(player_move > 0 && player_move <= 3)
- {
- GRID[0][player_move-1] = 'X';
- }
- else if(player_move > 3 && player_move <= 6)
- {
- GRID[1][player_move-4] = 'X';
- }
- else if(player_move > 6 && player_move <=9)
- {
- GRID[2][player_move-7] = 'X';
- }
- // We show the grid after every move.
- for(int row = 0; row <= 2; ++row)
- {
- for(int col = 0; col <= 2; ++col)
- {
- cout << GRID[row][col];
- }
- cout << '\n';
- }
- // The program compares the grid we have to all known cases.
- if(GRID[0][0] == 'X' && GRID[0][1] == 'X' && GRID[0][2] == 'X') player_won = 1;
- else if(GRID[1][0] == 'X' && GRID[1][1] == 'X' && GRID[1][2] == 'X') player_won = 1;
- else if(GRID[2][0] == 'X' && GRID[2][1] == 'X' && GRID[2][2] == 'X') player_won = 1;
- else if(GRID[0][0] == 'X' && GRID[1][0] == 'X' && GRID[2][0] == 'X') player_won = 1;
- else if(GRID[0][1] == 'X' && GRID[1][1] == 'X' && GRID[2][1] == 'X') player_won = 1;
- else if(GRID[0][2] == 'X' && GRID[1][2] == 'X' && GRID[2][2] == 'X') player_won = 1;
- else if(GRID[0][0] == 'X' && GRID[1][1] == 'X' && GRID[2][2] == 'X') player_won = 1;
- else if(GRID[0][2] == 'X' && GRID[1][1] == 'X' && GRID[2][0] == 'X') player_won = 1;
- // Here the program checks if the game has been won by any of the players. If someone won then it will show who and return 0.
- if(player_won == 1)
- {
- cout << "Player1 won!";
- return 0;
- }
- }
- // This else is the exact replica of the if above.
- else
- {
- if(player_move > 0 && player_move <= 3)
- {
- GRID[0][player_move-1] = '0';
- }
- else if(player_move > 3 && player_move <= 6)
- {
- GRID[1][player_move-4] = '0';
- }
- else if(player_move > 6 && player_move <=9)
- {
- GRID[2][player_move-7] = '0';
- }
- for(int row = 0; row <= 2; ++row)
- {
- for(int col = 0; col <= 2; ++col)
- {
- cout << GRID[row][col];
- }
- cout << '\n';
- }
- if(GRID[0][0] == '0' && GRID[0][1] == '0' && GRID[0][2] == '0') player_won = 1;
- else if(GRID[1][0] == '0' && GRID[1][1] == '0' && GRID[1][2] == '0') player_won = 1;
- else if(GRID[2][0] == '0' && GRID[2][1] == '0' && GRID[2][2] == '0') player_won = 1;
- else if(GRID[0][0] == '0' && GRID[1][0] == '0' && GRID[2][0] == '0') player_won = 1;
- else if(GRID[0][1] == '0' && GRID[1][1] == '0' && GRID[2][1] == '0') player_won = 1;
- else if(GRID[0][2] == '0' && GRID[1][2] == '0' && GRID[2][2] == '0') player_won = 1;
- else if(GRID[0][0] == '0' && GRID[1][1] == '0' && GRID[2][2] == '0') player_won = 1;
- else if(GRID[0][2] == '0' && GRID[1][1] == '0' && GRID[2][0] == '0') player_won = 1;
- if(player_won == 1)
- {
- cout << "Player1 won!";
- return 0;
- }
- }
- // Test if the moves are equal to 9 then the game is over.
- if(moves == 9)
- {
- cout << "Draw!";
- return 0;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement