The document contains code for a checker game board implementation including classes for squares and the board. It defines enums for square colors and chip ranks. Square and Board classes are defined to represent individual squares and the full board. Functions are included to initialize and print the board, read a board state from a file, and make and validate moves. However, key functions for checking and executing moves are only outlined as needing implementation. The input file format is also not specified.
9548086042 for call girls in Indira Nagar with room service
a) In the code, board is initialized by reading an input file. But y.pdf
1. a) In the code, board is initialized by reading an input file. But you have not specified the format
of input file nor have given any sample input file. For implementing check_move() and
move_checker() functions fully, it is required to be aware of input file specification and checker
game specification.
b) Among different files, code can be organised as shown below. There were some errors also in
the code that are also removed.
a) square.cpp:
---------------------------------------
#ifndef SQUARE_H_
#define SQUARE_H_
enum color {
empty, red, blue
};
enum chip_rank {
notking, king
};
//square class
class square {
public:
square() :
chip_color(empty), chip_status(notking) {
}
bool isEmpty();
void make_empty();
bool add_chip(color c, chip_rank r);
color get_color();
chip_rank get_rank();
private:
color chip_color;
chip_rank chip_status;
};
#endif /* SQUARE_H_ */
-----------------------------------------
2. square.cpp:
----------------------------------------
3. //class Board
class Board {
public:
Board(int);
void print_board();
char print_chip(int row, int col);
bool read_board(ifstream & fin);
bool make_move(int row1, int col1, int row2, int col2);
bool check_move(int row1, int col1, int row2, int col2);
void move_checker(int row1, int col1, int row2, int col2);
private:
vector > c_board;
int size;
};
#endif /* BOARD_H_ */
-----------------------------------------------
4. board.cpp:
-------------------------------------------------
#include "board.h"
//implementation of Board class
Board::Board(int dim) {
square csquare;
size = dim;
vector row;
for (int i = 0; i < size; i++)
row.push_back(csquare);
for (int i = 0; i < size; i++)
c_board.push_back(row);
}
char Board::print_chip(int row, int col) {
char c;
if (c_board[row][col].isEmpty())
return ' ';
else if (c_board[row][col].get_color() == red) {
if (c_board[row][col].get_rank() == notking)
return 'r';
4. else
return 'R';
} else {
if (c_board[row][col].get_rank() == notking)
return 'b';
else
return 'B';
}
}
void Board::print_board() {
string dashes = "+---", spaces = "| ", stars = "|***";
string line, filler;
int i, j, pat;
for (i = 0; i < size; i++) {
//cell top boundary
line = "";
for (j = 0; j < size; j++) {
line = line + dashes;
}
cout << line + "+" << endl;
pat = (i) % 2;
//row 1 of cell
line = "";
for (j = 0; j < size; j++) {
if ((j + pat) % 2) {
filler = spaces;
} else {
filler = stars;
}
line = line + filler;
}
cout << line + "|" << endl;
//row 2 of cell
line = "";
for (j = 0; j < size; j++) {
if ((j + pat) % 2) {
5. filler = "| ";
filler += print_chip(i, j);
filler += ' ';
} else {
filler = stars;
}
line = line + filler;
}
cout << line + "|" << endl;
//row 3 of cell
line = "";
for (j = 0; j < size; j++) {
if ((j + pat) % 2) {
filler = spaces;
} else {
filler = stars;
}
line = line + filler;
}
cout << line + "|" << endl;
}
//bottom boundary
line = "";
for (j = 0; j < size; j++) {
line = line + dashes;
}
cout << line + "+" << endl << endl;
}
bool Board::read_board(ifstream & fin) {
string line;
int row, col, colorIN, rankIN, lineNo = 1;
while (getline(fin, line)) {
istringstream iss(line);
iss >> row >> col >> colorIN >> rankIN;
if (iss.fail()) {
cout << "error reading line " << lineNo << endl;
6. return false;
}
if (row >= 0 && row < size && col >= 0 && col < size
&& colorIN >= (int) empty && colorIN <= (int) blue
&& rankIN >= (int) notking && rankIN <= king
&& (row % 2 + col % 2) % 2 == 1) {
if (!c_board[row][col].add_chip((color) colorIN,
(chip_rank) rankIN)) {
cout << "duplicate entry, line " << lineNo << endl;
return false;
}
} else {
cout << "Invalid entries in line " << lineNo << endl;
return false;
}
}
return true;
}
bool Board::make_move(int row1, int col1, int row2, int col2) {
if (!(row1 >= 0 && row1 < size && col1 >= 0 && col1 < size && row2 >= 0
&& row2 < size && col2 >= 0 && col2 < size) && row1 == row2
&& col1 == col2) {
cout << "Invalid square coordinates" << endl;
return false;
}
if (check_move(row1, col1, row2, col2))
move_checker(row1, col1, row2, col2);
}
bool Board::check_move(int row1, int col1, int row2, int col2) {
cout << "To do" << endl;
}
void Board::move_checker(int row1, int col1, int row2, int col2) {
cout << "To do" << endl;
}
------------------------------------------------------------------
5. main() function:
7. -------------------------------------------------
#include
#include "board.h"
using namespace std;
int main() {
int row1, col1, row2, col2, board_size;
cout << "enter board size ";
cin >> board_size;
Board myboard(board_size);
ifstream fin;
string filename;
cout << "Enter the name of the input file ";
cin >> filename;
fin.open(filename.c_str());
myboard.read_board(fin);
myboard.print_board();
while (true) {
cout << "enter a move ";
cin >> row1;
if (row1 < 0)
break;
cin >> col1 >> row2 >> col2;
if (!myboard.make_move(row1, col1, row2, col2)) {
cout << "illegal move! ";
}
else
myboard.print_board();
}
}
---------------------------------------------------
Solution
a) In the code, board is initialized by reading an input file. But you have not specified the format
of input file nor have given any sample input file. For implementing check_move() and
move_checker() functions fully, it is required to be aware of input file specification and checker
8. game specification.
b) Among different files, code can be organised as shown below. There were some errors also in
the code that are also removed.
a) square.cpp:
---------------------------------------
#ifndef SQUARE_H_
#define SQUARE_H_
enum color {
empty, red, blue
};
enum chip_rank {
notking, king
};
//square class
class square {
public:
square() :
chip_color(empty), chip_status(notking) {
}
bool isEmpty();
void make_empty();
bool add_chip(color c, chip_rank r);
color get_color();
chip_rank get_rank();
private:
color chip_color;
chip_rank chip_status;
};
#endif /* SQUARE_H_ */
-----------------------------------------
2. square.cpp:
----------------------------------------
#include "square.h"
//implementation of square class
color square::get_color() {
return chip_color;