Write a class called Board that represents a tic-tac-toe board. It should have a
ID: 3868236 • Letter: W
Question
Write a class called Board that represents a tic-tac-toe board. It should have a 3x3 array as a data member, which will store the locations of the players' moves. It should have a default constructor that initializes the 3x3 array to being empty. It should have a method called makeMove that takes the x and y coordinates of the move (see the example below) and which player's turn it is as parameters. If that location is unoccupied, makeMove should record the move and return true. If that location is already occupied, makeMove should just return false. There should be a method called gameState that takes no parameters and returns one of the four following values: X_WON, O_WON, DRAW, or UNFINISHED - use an enum for this, not strings (the enum definition should go in Board.hpp, before the Board class definition). There should also be a method called print, which just prints out the current board to the screen. Write a class called TicTacToe that allows two people to play a game. This class will have a field for a Board object and a field to keep track of which player's turn it is. It should have a constructor that takes a char parameter that specifies whether 'x' or 'o' should have the first move. It should have a method called play that starts the game. The play method should keep looping, asking the correct player for their move and sending it to the board (with makeMove) until someone has won or it's a draw (as indicated by gameState), and then declare what the outcome was. Write a main method (in TicTacToe.cpp) that asks the user which player should go first, creates a new TicTacToe object and starts the game. For this assignment only, you will not comment out your main method. Input validation: If someone tries to take an occupied square, tell them that square is already occupied and ask for a different move. Here's an example portion of a game (already in progress): 0 1 2 0 x . . 1 . . . 2 . . . Player O: please enter your move. 1 2 0 1 2 0 x . . 1 . . o 2 . . . Player X: please enter your move. 1 2 That square is already taken. 0 1 2 0 x . . 1 . . o 2 . . . Player X: please enter your move. The files must be named: Board.hpp, Board.cpp, TicTacToe.hpp and TicTacToe.cpp
Explanation / Answer
Code:
Player Class:
#pragma once
#include <iostream>
#include <limits>
class board;
class Player
{
char playermark;
public:
char mark() const;
void setmark(bool &, Player &);
void turn(board &);
};
Player cpp:
#include "Player.h"
#include "board.h"
//returns the mark for current player i.e X or O
char Player::mark() const{
return playermark;
}
//sets the mark of each player i.e x or o
//uses markpass to tell game loop that correct inputs have been chosen
void Player::setmark(bool &markpass, Player &p2) {
char tempmark; //hold input to select piece
while (!markpass) {
std::cin >> tempmark;
if (tempmark == 'x' || tempmark == 'X' || tempmark == 'o' || tempmark == 'O') {
playermark = tempmark;
markpass = true;
}
else {
std::cout << "Incorrect input Try again: ";
}
}
//sets player 2's piece, I wanted a lowercase for a lower case and an upper for an upper
switch (playermark) {
case 'X': p2.playermark = 'O';
break;
case 'x': p2.playermark = 'o';
break;
case 'O': p2.playermark = 'X';
break;
case 'o': p2.playermark = 'x';
break;
}
}
//player turns
void Player::turn(board &game1){
size_t tempposition = 0; //holds input for space player wishes to mark
bool inputpass = false; //used to check if the input is passed, failed, or not marked
while (!inputpass) {
//tests input type
if (std::cin >> tempposition) {
game1.markboard(tempposition, playermark, inputpass); // goes to markboard function
}
//if the input type fails
else {
std::cout << "INCORRECT INPUT TYPE Try agian:";
std::cin.clear(); //clears input fail state
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), ' ');
}
}
}
board class:
#pragma once
#include <iostream>
#include <string>
class board
{
std::string position = "123456789";
public:
void drawboard();
void clearboard();
void markboard(const size_t &, const char &, bool &);
char checkwin(bool&,int&);
};
board cpp:
#include "board.h"
//outputs the board onto the screen
void board::drawboard() {
std::cout << " " << position[0] << " |" << " " << position[1] << " |" << " " << position[2] << " ";
std::cout << "___|___|___ ";
std::cout << " " << position[3] << " |" << " " << position[4] << " |" << " " << position[5] << " ";
std::cout << "___|___|___ ";
std::cout << " " << position[6] << " |" << " " << position[7] << " |" << " " << position[8] << " ";
std::cout << " | | ";
}
//clears all user input from the board
void board::clearboard() {
position = "123456789";
}
//marks board with x or o in proper space
//boardIndex is position given by user
void board::markboard(const size_t &boardIndex, const char &playermark, bool &inputpass) {
char check = position[boardIndex - 1];
//if check is used to determine if currently selected position is marked or not
if (check != 'X' && check != 'x' && check != 'O' && check != 'o') {
//tests if space choice is within domain of the board
if (boardIndex > 0 && boardIndex <= 9) {
position[boardIndex - 1] = playermark;
inputpass = true; //'Passes' input so turn is over, otherwise loops through turn again
}
else {
//entered the incorrect position
std::cout << "INCORRECT SPACE CHOICE Try again: ";
}
}
else {
//entered position that has previously been marked
std::cout << "SPACE HAS ALREADY BEEN MARKED Try again: ";
}
}
//wrote down every scenario, found two patterns (it was this or 8 if else statements *I thought this was more creative*)
char board::checkwin(bool& gamewin, int &turncnt) {
for (size_t i = 0, k = 8; i <= 3 && k >= 5; i++, k--) {
if (position[4] == position[i] && position[i] == position[k]) {
gamewin = true; //sets game condition to true, game has been won
return position[4]; //returns winning 'piece' (X or O) to test who won
}
}
int cnt = 0;
size_t initial = 2;
//second pattern
for (size_t i = 1, k = 0; i <= 7 && k <= 9; i += 4, k += 8) {
if (cnt == 1) {
i -= 2; k -= 8;
initial += 4;
}
if (position[initial] == position[i] && position[i] == position[k]) {
gamewin = true;
return position[initial];
}
cnt++;
}
//checks for cats game, must be greater than or equal to eight to return C after game loop has ended(with no winner)
if (turncnt >= 8) {
gamewin = true;
return 'C'; //retruns C for cats game
}
}
Main cpp:
#include <iostream>
#include <limits>
#include "board.h"
#include "Player.h"
int gamePlay();
void pause();
int main() {
std::cout << "Welcome to Tic Tac Toe! ";
gamePlay();
std::cout << " ";
pause();
}
int gamePlay() {
board game1;
Player p1, p2;
bool markpass = false; //checks wether players have selected their pieces
bool gamewin = false; //sets condition for game
int turncnt = 0; //counts number of turns
game1.drawboard(); //draws initial board with numbered spaces
while (!gamewin) {
if (!markpass) {
std::cout << " What team do you want: X or O? PLayer 1: ";
p1.setmark(markpass, p2); //sets piece for player 1 and 2
std::cout << "Player 2: " << p2.mark() << " ";
}
//turn for player 1
if (!(turncnt % 2)) {
std::cout << "Player 1 trun: SQUARE: ";
p1.turn(game1);
game1.checkwin(gamewin, turncnt);
}
//turn for player 2
else {
std::cout << "Player 2 trun: SQUARE: ";
p2.turn(game1);
game1.checkwin(gamewin, turncnt);
}
game1.drawboard(); // update gameboard
std::cout << " ";
turncnt++; // increment turn count
}
std::cout << "GAME OVER!!!!!!! ";
//check cats game if none, conditional is used to determine who won
if (!(game1.checkwin(gamewin, turncnt) == 'C')) {
std::cout << "WINNER: Player "
<< ((game1.checkwin(gamewin, turncnt) == p1.mark()) ? "1" : "2");
}
else {
std::cout << "Cats game!";
}
game1.clearboard();//resest board
return 0;
}
//used to pause and see results
void pause() {
char quit;
std::cout << "Press any key followed by enter to quit";
std::cin >> quit;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.