Question
C++ Having trouble to count and print the squares completed in a Board 10x10 grid Write a class called SBoard that represents a 10x10 grid,
C++ Having trouble to count and print the squares completed in a Board 10x10 grid
Write a class called SBoard that represents a 10x10 grid, where a token may be placed at any (row, column) coordinates. It should have two data members: a 2D array of char for the grid, and an int to keep track of how many tokens have been placed. It should have a default constructor that initializes all elements of the array to being empty (you can use whatever characters you want to represent that a square is empty or has a token), and also initializes the number of tokens placed (at the beginning there are no tokens on the board). It should have a method named getNumTokens that returns the number of tokens placed so far. It should have a method named placeToken that takes two ints (the row and column, in that order). If that location already has a token, or if the row or column are out of the range 0-9, then a token should not be placed and the method should return -1. Otherwise, a token should be placed there and the method should return the number of squares completed by placing that token (where only the four corners are needed to complete a square). For example, if we have the following grid, where x marks each existing token:
. . . . . . . . . .
. . . . . . . . . .
. x . x . . . x . x
x . . x . . . . . .
. . . x . x . . . .
x . . x x . . . x .
x . x # . x . x . .
. . x x . . . . . .
. . . . . . . . . .
. . . . . . . . . .
then playing a token at the spot marked # will complete four squares - a 2x2, a 3x3, a 4x4, and a 5x5 (1x1 doesn't count). It should have a method named readPlacementsFromFile that takes a string (the name of the file to read). The file will have the following format:
2 1
2 3
3 0
7 3
Where the left number on each line is the row and the right number is the column. The method should place a token at the coordinates given in each row of the file. To do this, it will call the placeToken method (ignoring the return value). Remember to close the file. It's not required, but you'll probably find it useful for testing and debugging to have a method that prints out the board. Whether you think of the array indices as being [row][column] or [column][row] doesn't matter as long as you're consistent. The files must be named: SBoard.hpp and SBoard.cpp.
*** My code below needs some more work to print the total of the squares completed.
for example
We were allowed to have helper function, which is "squaresCompleted" in my code that working with "placeToken" function from the instruction above.
For SBoard.hpp
#ifndef SBoard_hpp
#define SBoard_hpp
#include
#include
#include //For reading file
#include //For absolute value
class SBoard {
private:
char grid[10][10];
int tokensPlaced;
// Helper function that working with placeTokens function
int squaresCompleted(int row, int column);
public:
// Default constructor
SBoard();
// Number of tokens placed
int getNumTokens();
// Place token on the grid
int placeToken(int row, int column);
void readPlacementsFromFile(std::string filename);
// Display board
void printBoard();
};
#endif /* SBoard_hpp */
For SBoard.cpp
#include "SBoard.hpp"
#include
#include
SBoard::SBoard()
{
//Initialize board to 10 rows and 10 columns
for (int row = 0; row
for (int col = 0; col
grid[row][col] = '.';
//Initialize tokens to have default as zero.
tokensPlaced = 0;
}
int SBoard::getNumTokens()
{
return tokensPlaced;
}
int SBoard::placeToken(int row, int column)
{
// If a location already has a token
// or row or column are out of range 0-9
// A token should not be placed and return -1
if ( row 9 || column 9 || grid[row][column] == 'x')
{
return -1;
}
// Otherwise place token to the board
grid[row][column] = 'x';
// Increment number of tokens have been placed
tokensPlaced++;
// Return number of squares completed by placing that token
int numSquares = squaresCompleted(row, column);
return numSquares;
}
int SBoard::squaresCompleted(int row, int column)
{
int square_count = 0;
//Check column, in row, for an 'x'
for (int i = 0; i
//When an 'x' is found
if (i != column && grid[row][i] == 'x') {
//Get column difference from placed token
int a = (int)abs(i-column);
//Check placed token row - column diff for 'x'
if (row - a >= 0 && grid[row-a][column] == 'x') {
//When found check other column
if (grid[row-a][i] == 'x') {
//There's a square
square_count++;
}
}
//Check higher value row for 'x'
if (row + a
//When found check other column
if (grid[row+a][i] == 'x')
//There's a square
square_count++;
}
}
}
return square_count;
}
void SBoard::readPlacementsFromFile(std::string filename)
{
std::ifstream infile(filename.c_str());
std::string line;
int row, col;
//For each line in file
while(getline(infile, line))
{
//Place row & column
sscanf(line.c_str(), "%d %d", &row, &col);
//Call placeToken and ignore return value
placeToken(row, col);
}
//Properly close file
infile.close();
}
void SBoard::printBoard()
{
std::cout //print blank line
//Display the current board
for (int i = 0; i
for (int j = 0; j
//print each element and a space
std::cout
std::cout //print blank line
}
}
Count 3 Count 4 x..x 5x5 4X4 3x3 2x2 Count2 Count 1 placeToken returns 4Step 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