Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void clear(char board[3][3]);
- void display(const char board[3][3]);
- void takeTurn(char board[3][3], char &nextPlayer);
- char winner(char board[3][3]);
- void clear(char board[3][3]){
- for(int i = 0; i <= 2; i++)
- for(int j = 0; j <= 2; j++)
- board[i][j] = ' ';
- }
- void display(const char board[3][3]){
- for(int i = 0; i <= 2; i++) {
- for(int j = 0; j <= 2; j++) {
- cout << board[i][j];
- if (j < 2)
- cout << "|";
- }
- if(i < 2)
- cout << endl << "-----" ;
- cout << endl;
- }
- }
- void takeTurn(char board[3][3], char &nextPlayer) {
- int row, column;
- //if the data that is entered is valid then this variable will be 1
- //otherwise if the row or column is out of range it's 0
- //otherwise if it already contain something it's 0
- int is_valid = 0;
- while (is_valid != 1) {
- cout << "It is now " << nextPlayer << "'s turn." << endl;
- cout << "Please enter your move in the form row column" << endl;
- cout << "So 0 0 would be the top left, and 0 2 would be the top right" << endl;
- cin >> row >> column;
- is_valid = 1;
- // check to see if the input is in the valid range
- if ( (row < 0) || (row > 2) || (column < 0) || (column > 2) ) {
- cout << "Invalid entry: row and column must both be between 0 and 2 (inclusive)." << endl;
- cout << "Please try again." << endl;
- is_valid = 0;
- }
- // then check to see if there is something already there
- if ( board[row][column] != ' ') {
- cout << "Invalid entry: Row " << row << "at Column " << column << " already contains: " << board[row][column] << endl;
- is_valid = 0;
- }
- }
- board[row][column] = nextPlayer;
- if (nextPlayer == 'X') { nextPlayer = 'O'; return; }
- if (nextPlayer == 'O') { nextPlayer = 'X' ; return;}
- }
- char winner(char board[3][3]) {
- //check to see if any rows are all X
- for (int i=0; i <=2; i++) {
- if (board[i][0] == 'X' && board[i][1] == 'X' && board[i][2] == 'X') {
- display(board);
- cout << "Congratulations, X YOU WON!" << endl;
- return 'X';
- }
- }
- // check to see if any columns are all X
- for (int i=0; i <=2; i++) {
- if (board[0][i] == 'X' && board[1][i] == 'X' && board[2][i] == 'X') {
- display(board);
- cout << "Congratulations, X YOU WON!" << endl;
- return 'X';
- }
- }
- //check to see if any rows are all O
- for (int i=0; i <=2; i++) {
- if (board[i][0] == 'O' && board[i][1] == 'O' && board[i][2] == 'O') {
- display(board);
- cout << "Congratulations, O YOU WON!" << endl;
- return 'O';
- }
- }
- // check to see if any columns are all X
- for (int i=0; i <=2; i++) {
- if (board[0][i] == 'O' && board[1][i] == 'O' && board[2][i] == 'O') {
- display(board);
- cout << "Congratulations, O YOU WON!" << endl;
- return 'O';
- }
- }
- //check the left to right diagonal to see if there is a winner
- if (board[0][0] == 'X' && board[1][1] == 'X' && board [2][2] == 'X') {
- display(board);
- cout << "Congratulations, X YOU WON!" << endl;
- return 'X';
- }
- if (board[0][0] == 'O' && board[1][1] == 'O' && board [2][2] == 'O') {
- display(board);
- cout << "Congratulations, O YOU WON!" << endl;
- return 'O';
- }
- //check to see if the right to left diagonal is a winner
- if (board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X') {
- display(board);
- cout << "Congratulations, X YOU WON!" << endl;
- return 'X';
- }
- if (board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O') {
- display(board);
- cout << "Congratulations, O YOU WON!" << endl;
- return 'O';
- }
- //now check to see if we have filled all the board positions
- //since we have checked for all the possible wining combinations
- //at this point, if the board is filled, it must be a tie
- int is_tie = 1;
- for (int i = 0; i <=2; i++) {
- for (int j = 0; j <=2; j++) {
- if (board[i][j] == ' ') { is_tie = 0; }
- }
- }
- if (is_tie == 1) {
- display(board);
- cout << "Nobody won. Please play again.";
- return '?';
- }
- //Finally, if there is no winner, and there is no tie, then we will return a ' ' meaning that the game is still
- // active and we will play another round
- return ' ';
- }
- int main()
- {
- char board[3][3];
- char nextPlayer;
- char winningPlayer;
- clear(board);
- //winningPlayer can be:
- //' ' no winner yet
- //? tie
- //X X wins
- //O O wins
- winningPlayer = ' ';
- nextPlayer = 'X';
- /*
- board[0][0] = 'X';
- board[2][1] = 'O';
- board[1][2] = 'X';
- board[1][1] = 'O';
- board[2][0] = 'X';
- board[0][1] = 'O';*/
- while (winningPlayer == ' '){
- display(board);
- takeTurn(board, nextPlayer);
- winningPlayer = winner(board);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement