Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int SIZE=3;
- const int NO_PLAYER = 0;
- const char PLAYER_1_SIGN = 'X';
- const char PLAYER_2_SIGN = 'O';
- const int GAME_STATUS_WINNER_PLAYER_1 = 1;
- const int GAME_STATUS_WINNER_PLAYER_2 = 2;
- const int GAME_STATUS_TIE = 3;
- const int GAME_STATUS_NO_RESULT_YET = 0;
- int playGame();
- // NOTE: all the functions that gets the matrix don't need to get the rows, since it is the same as the SIZE
- void drawBoard(char board[][SIZE]);
- int checkGameStatus(char board[][SIZE]);
- int checkIfWinnerInRow(char board[][SIZE]);
- int checkIfWinnerInCol(char board[][SIZE]);
- //int checkIfWinnerInMainDiagonal(char board[][SIZE]);
- //int checkIfWinnerInSecondaryDiagonal(char board[][SIZE]);
- int main()
- {
- int res;
- bool fPlayAgain=true;
- char answer;
- do
- {
- res = playGame();
- // NOTE: look how nice the main is when we use the const variables instead of just numbers
- if (res == GAME_STATUS_WINNER_PLAYER_1)
- cout << "Winner: player 1\n";
- else if (res == GAME_STATUS_WINNER_PLAYER_2)
- cout << "Winner: player 2\n";
- else
- cout << "TIE, no winner...\n";
- cout << "Would you like to play again (Y/N)? ";
- cin >> answer;
- while (answer != 'n' && answer != 'N' && answer != 'y' && answer != 'Y')
- {
- cout << "Invalid answer. Try again: ";
- cin >> answer;
- }
- if (answer == 'n' || answer == 'N')
- fPlayAgain = false;
- } while (fPlayAgain);
- }
- int playGame()
- {
- char board[SIZE][SIZE] = {NO_PLAYER};
- int currentPlayer = 1;
- bool fIsCellFree, fHasWinner=false;
- int row, col, res;
- do
- {
- system("cls");
- cout << "The current board:\n\n";
- drawBoard(board);
- cout << "\n";
- fIsCellFree = false;
- while (!fIsCellFree)
- {
- cout << "Please enter cordination for player #" << currentPlayer << ": ";
- cin >> row >> col;
- // TODO: as long as the row is not between 1-SIZE, ask the user to to enter data again
- // DONE
- while (row<1 ||row>SIZE)
- {
- cout<<"please enter row between 1-" << SIZE << ":\n";
- cin>>row ;
- }
- // TODO: as long as the col is not between 1-SIZE, ask the user to to enter data again
- // DONE
- while (col<1 ||col>SIZE)
- {
- cout<<"please enter col between 1-" << SIZE << ":\n";
- cin>>col ;
- }
- // TODO: complete the if to check if cell is free
- // DONE
- if (board[row-1][col-1]!=NO_PLAYER)
- {
- cout << "The cell is already taken!\n";
- system("pause");
- continue;
- }
- // if here, cell is valid...
- //-----------------------------
- // mark cell and switch player
- fIsCellFree = true;
- if (currentPlayer == 1)
- {
- board[row-1][col-1] = PLAYER_1_SIGN;
- currentPlayer = 2;
- }
- else
- {
- board[row-1][col-1] = PLAYER_2_SIGN;
- currentPlayer = 1;
- }
- }
- system("pause");
- } while (checkGameStatus(board) == GAME_STATUS_NO_RESULT_YET);
- cout << "Final Board:\n";
- drawBoard(board);
- res = checkGameStatus(board);
- return res;
- }
- int checkGameStatus(char board[][SIZE])
- {
- bool fHasWinner = false;
- int res;
- res = checkIfWinnerInRow(board);
- if (res == GAME_STATUS_WINNER_PLAYER_1 || res == GAME_STATUS_WINNER_PLAYER_2)
- return res;
- // TODO: you need to declare the following functions..
- res = checkIfWinnerInCol(board);
- if (res == GAME_STATUS_WINNER_PLAYER_1 || res == GAME_STATUS_WINNER_PLAYER_2)
- return res;
- res = checkIfWinnerInMainDiagonal(board);
- if (res == GAME_STATUS_WINNER_PLAYER_1 || res == GAME_STATUS_WINNER_PLAYER_2)
- return res;
- res = checkIfWinnerInSecondaryDiagonal(board);
- if (res == GAME_STATUS_WINNER_PLAYER_1 || res == GAME_STATUS_WINNER_PLAYER_2)
- return res;
- // TODO: check if there is a cell that is not taken yet, and return GAME_STATUS_NO_RESULT_YET
- for ( int i=0 ; i<SIZE ; i++ )
- for (int j=0 ; j<SIZE ; j++ )
- if ( board[i][j] == NO_PLAYER )
- return GAME_STATUS_NO_RESULT_YET ;
- // if we haven't returned yet, then there is no winner and the board is full, therefore TIE
- return GAME_STATUS_TIE;
- }
- void drawBoard(char board[][SIZE])
- {
- for (int i=0 ; i < SIZE ; i++)
- {
- for (int j=0 ; j < SIZE ; j++)
- {
- cout << " ";
- if (board[i][j] != NO_PLAYER)
- cout << board[i][j] ;
- else
- cout << " ";
- cout << " |";
- }
- cout << "\n";
- for (int j=0 ; j < SIZE ; j++)
- cout << "---|";
- cout << "\n";
- }
- }
- int checkIfWinnerInRow(char board[][SIZE])
- {
- bool fHasWinnerInCurrentCheck;
- for (int i=0 ; i < SIZE ; i++)
- {
- fHasWinnerInCurrentCheck = true;
- for (int j=1 ; j < SIZE && fHasWinnerInCurrentCheck ; j++)
- {
- if (board[i][j] != board[i][0] || board[i][j] == NO_PLAYER)
- fHasWinnerInCurrentCheck = false;
- }
- if (fHasWinnerInCurrentCheck)
- {
- if (board[i][0] == PLAYER_1_SIGN)
- return GAME_STATUS_WINNER_PLAYER_1;
- else
- return GAME_STATUS_WINNER_PLAYER_2;
- }
- }
- return GAME_STATUS_NO_RESULT_YET;
- }
- int checkIfWinnerInCol(char board[][SIZE])
- {
- bool fHasWinnerInCurrentCheck;
- for (int j=0 ; j < SIZE ; j++)
- {
- fHasWinnerInCurrentCheck = true;
- for (int i=1 ; i < SIZE && fHasWinnerInCurrentCheck ; i++)
- {
- if (board[i][j] != board[0][j] || board[i][j] == NO_PLAYER)
- fHasWinnerInCurrentCheck = false;
- }
- if (fHasWinnerInCurrentCheck)
- {
- if (board[0][j] == PLAYER_1_SIGN)
- return GAME_STATUS_WINNER_PLAYER_1;
- else
- return GAME_STATUS_WINNER_PLAYER_2;
- }
- }
- return GAME_STATUS_NO_RESULT_YET;
- }
Add Comment
Please, Sign In to add comment