Question
============GBoard.hpp #ifndef GBOARD_HPP #define GBOARD_HPP #include #define SIZE 15 //enum var enum gameState { X_WON, O_WON, DRAW, UNFINISHED }; //construct a class called GBoard class
============GBoard.hpp
#ifndef GBOARD_HPP
#define GBOARD_HPP
#include
#define SIZE 15
//enum var
enum gameState { X_WON, O_WON, DRAW, UNFINISHED };
//construct a class called GBoard
class GBoard
{
private:
char gameBoard[SIZE][SIZE]; //2d array that represents a 15x15 board
gameState state;
public:
GBoard(); //default constructor
gameState getGameState() const; //method that returns the value of gamestate
bool makeMove(int, int, char); //method that takes params for row, column and char
void print(); //method that prints out the board
};
=============GBoard.cpp
#include "GBoard.hpp"
#include
#include
#include
#include
using std::cout;
using std::endl;
using std::string;
//default constructor
GBoard::GBoard()
{
char empty = '-';
for (int i = 0; i < SIZE; i++) //create nested loops to fill the rows and columns with empty squares
{
for (int k = 0; k < SIZE; k++)
gameBoard[i][k] = empty;
}
gameState state = UNFINISHED;
}
//method to return current game state
gameState GBoard::getGameState() const{
return state;
}
//method to make move
bool GBoard::makeMove(int row, int col, char player)
{
if (row >= 0 && row < SIZE && col >= 0 && col < SIZE) {
if (getGameState() != UNFINISHED || gameBoard[row][col] != '-') { //game finished or square is already filled
return false;
}
gameBoard[row][col] = player; //setting current player's char to the position
}
//checking by rows if the player has won
int count = 0;
for (int i = 0; i < SIZE; i++) {
count = 0;
for (int j = 0; j < SIZE; j++) {
if (gameBoard[i][j] == player) {
count++;
if (count == 5) { //player has 5 consecutive 'x' or 'o' in a row
state = (player == 'x') ? X_WON : O_WON;
return true;
}
}
else {
count = 0;
}
}
}
//checking by columns
for (int i = 0; i < SIZE; i++) {
count = 0;
for (int j = 0; j < SIZE; j++) {
if (gameBoard[j][i] == player) {
count++;
if (count == 5) {
state = (player == 'x') ? X_WON : O_WON;
return true;
}
}
else {
count = 0;
}
}
}
//checking diagonally (left to right)
int i = 0;
int j = 0;
for (int row = 0; row < SIZE; row++) {
i = row;
j = 0;
count = 0;
while (i < SIZE && j < SIZE) {
if (gameBoard[i][j] == player) {
count++;
if (count == 5) {
state = (player == 'x') ? X_WON : O_WON;
return true;
}
}
else {
count = 0;
}
i++;
j++;
}
}
for (int col = 0; col < SIZE; col++) {
i = 0;
j = col;
while (i < SIZE && j < SIZE) {
if (gameBoard[i][j] == player) {
count++;
if (count == 5) {
state = (player == 'x') ? X_WON : O_WON;
return true;
}
}
else {
count = 0;
}
i++;
j++;
}
}
//checking diagonally (right to left)
for (int row = 0; row < SIZE; row++) {
i = row;
j = SIZE - 1;
count = 0;
while (i < SIZE && j >= 0) {
if (gameBoard[i][j] == player) {
count++;
if (count == 5) {
state = (player == 'x') ? X_WON : O_WON;
return true;
}
}
else {
count = 0;
}
i++;
j--;
}
}
for (int col = SIZE - 1; col >= 0; col--) {
i = 0;
j = col;
count = 0;
while (i < SIZE && j >= 0) {
if (gameBoard[i][j] == player) {
count++;
if (count == 5) {
state = (player == 'x') ? X_WON : O_WON;
return true;
}
}
else {
count = 0;
}
i++;
j--;
}
return true; //move has been successful
}
return false; //move was unsuccessful
}
======================================================
Not sure why the program isn't outputting any x's or o's when I try to print the board.
The GBoard represents the board for a game that is like tic-tac-toe but on a larger scale. Instead of a 3x3 board, it is played on a 15x15 board, and instead of 3 in a row, each player is trying to get 5 in a row. The makeMove takes as params an int for the row, int for the column and a char for the player. The row and columns are in the range 0-14 and should not have any access to my array out of bounds including in the condition of an if statement or loop.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started