Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace::std;
- class GameBoard {
- public:
- char board[9] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
- //Display the gameboard
- void displayboard(){
- cout << "---------------------" << endl << endl;
- cout << " | | " << endl;
- cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
- cout << "_____|_____|_____" << endl;
- cout << " | | " << endl;
- cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
- cout << "_____|_____|_____" << endl;
- cout << " | | " << endl;
- cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
- cout << " | | " << endl;
- std::cout << " Tic Tac Toe" << std::endl;
- }
- // Return 1 if player has won
- // Return 0 if all moves have been taken aka draw
- // Return -1 if games still playing
- int wincheck(){
- if (board[0] == board[1] && board[1] == board[2])
- return 1;
- else if (board[3] == board[4] && board[4] == board[5])
- return 1;
- else if (board[6] == board[7] && board[7] == board[8])
- return 1;
- else if (board[0] == board[3] && board[3] == board[6])
- return 1;
- else if (board[1] == board[4] && board[4] == board[7])
- return 1;
- else if (board[2] == board[6] && board[6] == board[8])
- return 1;
- else if (board[0] == board[4] && board[4] == board[8])
- return 1;
- else if (board[2] == board[4] && board[4] == board[6])
- return 1;
- //might be better to use a counter to just end the game at 9 turns since no more valid moves are avaliable??
- else if (board[0] != '1' && board[1] != '2' && board[2] != '3'
- && board[3] != '4' && board[4] != '5' && board[5] != '6'
- && board[6] != '7' && board[7] != '8' && board[8] != '9')
- return 0;
- else
- return -1;
- }
- }GameOn;
- int main(){
- int i = -1;
- int choice = '5';
- int playerturn = 'X';
- char playerletter = 'X';
- do{
- GameOn.displayboard();
- if (playerturn == 'X'){
- std::cout << "Player X turn: ";
- playerletter = 'X';
- playerturn = 'O';
- }
- else if (playerturn == 'O'){
- std::cout << "Player O turn: ";
- playerletter = 'O';
- playerturn = 'X';
- }
- // TODO: Include a method so that when the player enters a taken index the player is allowed another turn to correct their mistake
- std::cin >> choice;
- if (GameOn.board[(choice - 1)] != 'X' && GameOn.board[(choice - 1)] != 'O'){
- GameOn.board[(choice - 1)] = playerletter;
- }
- else
- std::cout << "Enter a position not taken next time..." << std::endl;
- i = GameOn.wincheck();
- } while (i == -1);
- GameOn.displayboard();
- // Win or Draw check
- if (i == 0){
- std::cout << "The game is a draw!" << std::endl;
- }
- // ERROR: playerletter displays random numbers possibly a scope issue relating to declaration/assignment error idk
- else if (i == 1){
- std::cout << "Player " << playerletter << " has won." << std::endl;
- }
- else{
- std::cout << "I AM ERROR" << std::endl;
- }
- //IGNORE PREVIOUS COMMENTS
- //to do player turn make it so we start on X then once we get the choice we check that array index to see
- //if it is empty of O or X and if empty we assign that player to there and then assign the turn to the other player
- }
- /*
- // The 'Gameboard'
- void gameboard(){
- cout << "---------------------" << endl << endl;
- cout << " | | " << endl;
- cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
- cout << "_____|_____|_____" << endl;
- cout << " | | " << endl;
- cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
- cout << "_____|_____|_____" << endl;
- cout << " | | " << endl;
- cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
- cout << " | | " << endl;
- std::cout << " Tic Tac Toe" << std::endl;
- }
- // Return 1 if player has won
- // Return 0 if all moves have been taken aka draw
- // Return -1 if games still playing
- int wincheck(){
- if (board[0] == board[1] && board[1] == board[2])
- return 1;
- else if (board[3] == board[4] && board[4] == board[5])
- return 1;
- else if (board[6] == board[7] && board[7] == board[8])
- return 1;
- else if (board[0] == board[3] && board[3] == board[6])
- return 1;
- else if (board[1] == board[4] && board[4] == board[7])
- return 1;
- else if (board[2] == board[6] && board[6] == board[8])
- return 1;
- else if (board[0] == board[4] && board[4] == board[8])
- return 1;
- else if (board[2] == board[4] && board[4] == board[6])
- return 1;
- //might be better to use a counter to just end the game at 9 turns since no more valid moves are avaliable??
- else if (board[0] != '1' && board[1] != '2' && board[2] != '3'
- && board[3] != '4' && board[4] != '5' && board[5] != '6'
- && board[6] != '7' && board[7] != '8' && board[8] != '9')
- return 0;
- else
- return -1;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement