Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main()
- {
- //making the opening text appear
- cout << "Welcome to Tic-Tac-Toe!" << endl;
- //Making the array
- char my_array[3][3]
- {
- { '.', '.', '.' },
- { '.', '.', '.' },
- { '.', '.', '.' },
- };
- //int check;
- bool winner = false; //Set this to true when somebody wins
- string player_one = "Player X's turn! Please press 1-9 to place a piece and press Enter.";
- string player_two = "Player O's turn! Please press 1-9 to place a piece and press Enter.";
- int turn = 1;
- char symbol = 'X';
- int points = 0;
- char play_again = 'n';
- int keyPressed; //variable used in the game function
- cout << player_one << endl; //Begins the game with player one
- cout << "The board treats number inputs like this:\n";
- //Making the demonstration array
- char demo_array[3][3]
- {
- { '1', '2', '3' },
- { '4', '5', '6' },
- { '7', '8', '9' },
- };
- //Making the demonstration array show up
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- cout << demo_array[i][j];
- }
- cout << endl;
- }
- cout << "GAMEBOARD \n";
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- cout << my_array[i][j];
- }
- cout << endl;
- }
- while(!winner)
- {
- cin >> keyPressed;
- if (turn == 1)
- {
- symbol = 'X';
- cout << player_one;
- cout << endl;
- }
- else
- {
- symbol = 'O';
- cout << player_two;
- cout << endl;
- }
- if (keyPressed <= 3 && my_array[0][keyPressed-1] == '.')
- {
- my_array[0][keyPressed-1] = symbol;
- turn = -turn;
- }
- if (keyPressed > 3 && keyPressed <= 6 && my_array[1][keyPressed-4] == '.')
- {
- my_array[1][keyPressed-4] = symbol;
- turn = -turn;
- }
- if (keyPressed > 6 && keyPressed <= 9 && my_array[2][keyPressed-7] == '.')
- {
- my_array[2][keyPressed-7] = symbol;
- turn = -turn;
- }
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- if (my_array[i][j] == symbol)
- points++;
- if (points > 2)
- {
- winner = 1;
- break;
- }
- }
- points = 0;
- }
- for (int j = 0; j < 3; j++)
- {
- for (int i = 0; i < 3; i++)
- {
- if (my_array[i][j] == symbol)
- points++;
- if (points > 2)
- {
- winner = 1;
- break;
- }
- }
- points = 0;
- }
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- cout << my_array[i][j];
- }
- cout << endl;
- }
- if (my_array[0][0] == symbol && my_array[1][1] == symbol && my_array[2][2] == symbol
- or my_array[2][0] == symbol && my_array[1][1] == symbol && my_array[0][2] == symbol)
- winner = 1;
- }
- cout << "player " << symbol << " wins\npress 'y' and enter to play again: ";
- cin >> play_again;
- if (play_again == 'y')
- main();
- else
- cout << "thanks for playing";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement