SlideShare a Scribd company logo
1 of 14
Download to read offline
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:
----------------------------------------
#include "square.h"
//implementation of square class
color square::get_color() {
return chip_color;
}
chip_rank square::get_rank() {
return chip_status;
}
bool square::isEmpty() {
return (empty == chip_color);
}
void square::make_empty() {
chip_color = empty;
}
bool square::add_chip(color c, chip_rank r) {
if (!isEmpty())
return false;
else {
chip_color = c;
chip_status = r;
return true;
}
}
--------------------------------------
3. board.h
--------------------------------------
#ifndef BOARD_H_
#define BOARD_H_
#include
#include
#include
#include
#include
#include
#include "square.h"
using namespace std;
//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';
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) {
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;
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:
-------------------------------------------------
#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
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;
}
chip_rank square::get_rank() {
return chip_status;
}
bool square::isEmpty() {
return (empty == chip_color);
}
void square::make_empty() {
chip_color = empty;
}
bool square::add_chip(color c, chip_rank r) {
if (!isEmpty())
return false;
else {
chip_color = c;
chip_status = r;
return true;
}
}
--------------------------------------
3. board.h
--------------------------------------
#ifndef BOARD_H_
#define BOARD_H_
#include
#include
#include
#include
#include
#include
#include "square.h"
using namespace std;
//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';
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) {
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;
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:
-------------------------------------------------
#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();
}
}
---------------------------------------------------

More Related Content

Similar to a) In the code, board is initialized by reading an input file. But y.pdf

Introduction You implemented a Deck class in Activity 2. This cla.pdf
Introduction You implemented a Deck class in Activity 2. This cla.pdfIntroduction You implemented a Deck class in Activity 2. This cla.pdf
Introduction You implemented a Deck class in Activity 2. This cla.pdffeelinggifts
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptkinan keshkeh
 
Nesting of for loops using C++
Nesting of for loops using C++Nesting of for loops using C++
Nesting of for loops using C++prashant_sainii
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]Abhishek Sinha
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab ManualAkhilaaReddy
 
Bti1022 lab sheet 8
Bti1022 lab sheet 8Bti1022 lab sheet 8
Bti1022 lab sheet 8alish sha
 
Bti1022 lab sheet 8
Bti1022 lab sheet 8Bti1022 lab sheet 8
Bti1022 lab sheet 8alish sha
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptkinan keshkeh
 
sodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdfsodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdfMuhammadMaazShaik
 
Arduino coding class
Arduino coding classArduino coding class
Arduino coding classJonah Marrs
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statementsMomenMostafa
 
Network lap pgms 7th semester
Network lap pgms 7th semesterNetwork lap pgms 7th semester
Network lap pgms 7th semesterDOSONKA Group
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxssuser3cbb4c
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaonyash production
 

Similar to a) In the code, board is initialized by reading an input file. But y.pdf (20)

Railwaynew
RailwaynewRailwaynew
Railwaynew
 
2 d matrices
2 d matrices2 d matrices
2 d matrices
 
Introduction You implemented a Deck class in Activity 2. This cla.pdf
Introduction You implemented a Deck class in Activity 2. This cla.pdfIntroduction You implemented a Deck class in Activity 2. This cla.pdf
Introduction You implemented a Deck class in Activity 2. This cla.pdf
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop concept
 
Nesting of for loops using C++
Nesting of for loops using C++Nesting of for loops using C++
Nesting of for loops using C++
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
Bti1022 lab sheet 8
Bti1022 lab sheet 8Bti1022 lab sheet 8
Bti1022 lab sheet 8
 
Bti1022 lab sheet 8
Bti1022 lab sheet 8Bti1022 lab sheet 8
Bti1022 lab sheet 8
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop concept
 
Include
IncludeInclude
Include
 
sodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdfsodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdf
 
Arduino coding class
Arduino coding classArduino coding class
Arduino coding class
 
Quiz using C++
Quiz using C++Quiz using C++
Quiz using C++
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 
Network lap pgms 7th semester
Network lap pgms 7th semesterNetwork lap pgms 7th semester
Network lap pgms 7th semester
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
 
C++ programming
C++ programmingC++ programming
C++ programming
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
 

More from anuradhasilks

The Arrhenius Theory of acids and bases The theo.pdf
                     The Arrhenius Theory of acids and bases  The theo.pdf                     The Arrhenius Theory of acids and bases  The theo.pdf
The Arrhenius Theory of acids and bases The theo.pdfanuradhasilks
 
oxygen in air (O2) reacts with H2 when it burns .pdf
                     oxygen in air (O2) reacts with H2 when it burns  .pdf                     oxygen in air (O2) reacts with H2 when it burns  .pdf
oxygen in air (O2) reacts with H2 when it burns .pdfanuradhasilks
 
most of the hydrogens on the benzene were replace.pdf
                     most of the hydrogens on the benzene were replace.pdf                     most of the hydrogens on the benzene were replace.pdf
most of the hydrogens on the benzene were replace.pdfanuradhasilks
 
Li2S Solution Li2S .pdf
                     Li2S  Solution                     Li2S  .pdf                     Li2S  Solution                     Li2S  .pdf
Li2S Solution Li2S .pdfanuradhasilks
 
identical is the exact same, this is the top r.pdf
                     identical is the exact same, this is the top r.pdf                     identical is the exact same, this is the top r.pdf
identical is the exact same, this is the top r.pdfanuradhasilks
 
This is a nucleophilic substitution reaction that.pdf
                     This is a nucleophilic substitution reaction that.pdf                     This is a nucleophilic substitution reaction that.pdf
This is a nucleophilic substitution reaction that.pdfanuradhasilks
 
Sulfur is oxidized and nitrogen is reduced. .pdf
                     Sulfur is oxidized and nitrogen is reduced.      .pdf                     Sulfur is oxidized and nitrogen is reduced.      .pdf
Sulfur is oxidized and nitrogen is reduced. .pdfanuradhasilks
 
TriangleU210.javapublic class TriangleU210 {    Declaring inst.pdf
TriangleU210.javapublic class TriangleU210 {    Declaring inst.pdfTriangleU210.javapublic class TriangleU210 {    Declaring inst.pdf
TriangleU210.javapublic class TriangleU210 {    Declaring inst.pdfanuradhasilks
 
There is some data missing in the problem.The composition of the m.pdf
There is some data missing in the problem.The composition of the m.pdfThere is some data missing in the problem.The composition of the m.pdf
There is some data missing in the problem.The composition of the m.pdfanuradhasilks
 
at beginning flask has only NaOH pH = 14 + log [N.pdf
                     at beginning flask has only NaOH pH = 14 + log [N.pdf                     at beginning flask has only NaOH pH = 14 + log [N.pdf
at beginning flask has only NaOH pH = 14 + log [N.pdfanuradhasilks
 
take log on both sides1n lna = ln las a--0.pdf
take log on both sides1n lna = ln las a--0.pdftake log on both sides1n lna = ln las a--0.pdf
take log on both sides1n lna = ln las a--0.pdfanuradhasilks
 
Tested on Eclipse and both class should be in same packageimport.pdf
Tested on Eclipse and both class should be in same packageimport.pdfTested on Eclipse and both class should be in same packageimport.pdf
Tested on Eclipse and both class should be in same packageimport.pdfanuradhasilks
 
Solution A person in perfect health has a utility score of 1.0U.pdf
Solution A person in perfect health has a utility score of 1.0U.pdfSolution A person in perfect health has a utility score of 1.0U.pdf
Solution A person in perfect health has a utility score of 1.0U.pdfanuradhasilks
 
Resurgent infection is suggesting that it is viral infection by meas.pdf
Resurgent infection is suggesting that it is viral infection by meas.pdfResurgent infection is suggesting that it is viral infection by meas.pdf
Resurgent infection is suggesting that it is viral infection by meas.pdfanuradhasilks
 
There are only two functional groups in the molec.pdf
                     There are only two functional groups in the molec.pdf                     There are only two functional groups in the molec.pdf
There are only two functional groups in the molec.pdfanuradhasilks
 
PsychoanalysisPsychoanalysis was founded by Sigmund Freud (1856-19.pdf
PsychoanalysisPsychoanalysis was founded by Sigmund Freud (1856-19.pdfPsychoanalysisPsychoanalysis was founded by Sigmund Freud (1856-19.pdf
PsychoanalysisPsychoanalysis was founded by Sigmund Freud (1856-19.pdfanuradhasilks
 
Product of Cyclopentane + H2O(H+) gives No Reaction.It will simply F.pdf
Product of Cyclopentane + H2O(H+) gives No Reaction.It will simply F.pdfProduct of Cyclopentane + H2O(H+) gives No Reaction.It will simply F.pdf
Product of Cyclopentane + H2O(H+) gives No Reaction.It will simply F.pdfanuradhasilks
 
Of the variedtypes of IPC, sockets arout and awaythe foremostcommon..pdf
Of the variedtypes of IPC, sockets arout and awaythe foremostcommon..pdfOf the variedtypes of IPC, sockets arout and awaythe foremostcommon..pdf
Of the variedtypes of IPC, sockets arout and awaythe foremostcommon..pdfanuradhasilks
 
C2O2 is a bidendate ligand &Cl - is a mono dendat.pdf
                     C2O2 is a bidendate ligand &Cl - is a mono dendat.pdf                     C2O2 is a bidendate ligand &Cl - is a mono dendat.pdf
C2O2 is a bidendate ligand &Cl - is a mono dendat.pdfanuradhasilks
 

More from anuradhasilks (20)

The Arrhenius Theory of acids and bases The theo.pdf
                     The Arrhenius Theory of acids and bases  The theo.pdf                     The Arrhenius Theory of acids and bases  The theo.pdf
The Arrhenius Theory of acids and bases The theo.pdf
 
oxygen in air (O2) reacts with H2 when it burns .pdf
                     oxygen in air (O2) reacts with H2 when it burns  .pdf                     oxygen in air (O2) reacts with H2 when it burns  .pdf
oxygen in air (O2) reacts with H2 when it burns .pdf
 
x+12=1 x=12 .pdf
                     x+12=1 x=12                                    .pdf                     x+12=1 x=12                                    .pdf
x+12=1 x=12 .pdf
 
most of the hydrogens on the benzene were replace.pdf
                     most of the hydrogens on the benzene were replace.pdf                     most of the hydrogens on the benzene were replace.pdf
most of the hydrogens on the benzene were replace.pdf
 
Li2S Solution Li2S .pdf
                     Li2S  Solution                     Li2S  .pdf                     Li2S  Solution                     Li2S  .pdf
Li2S Solution Li2S .pdf
 
identical is the exact same, this is the top r.pdf
                     identical is the exact same, this is the top r.pdf                     identical is the exact same, this is the top r.pdf
identical is the exact same, this is the top r.pdf
 
This is a nucleophilic substitution reaction that.pdf
                     This is a nucleophilic substitution reaction that.pdf                     This is a nucleophilic substitution reaction that.pdf
This is a nucleophilic substitution reaction that.pdf
 
Sulfur is oxidized and nitrogen is reduced. .pdf
                     Sulfur is oxidized and nitrogen is reduced.      .pdf                     Sulfur is oxidized and nitrogen is reduced.      .pdf
Sulfur is oxidized and nitrogen is reduced. .pdf
 
TriangleU210.javapublic class TriangleU210 {    Declaring inst.pdf
TriangleU210.javapublic class TriangleU210 {    Declaring inst.pdfTriangleU210.javapublic class TriangleU210 {    Declaring inst.pdf
TriangleU210.javapublic class TriangleU210 {    Declaring inst.pdf
 
There is some data missing in the problem.The composition of the m.pdf
There is some data missing in the problem.The composition of the m.pdfThere is some data missing in the problem.The composition of the m.pdf
There is some data missing in the problem.The composition of the m.pdf
 
at beginning flask has only NaOH pH = 14 + log [N.pdf
                     at beginning flask has only NaOH pH = 14 + log [N.pdf                     at beginning flask has only NaOH pH = 14 + log [N.pdf
at beginning flask has only NaOH pH = 14 + log [N.pdf
 
take log on both sides1n lna = ln las a--0.pdf
take log on both sides1n lna = ln las a--0.pdftake log on both sides1n lna = ln las a--0.pdf
take log on both sides1n lna = ln las a--0.pdf
 
Tested on Eclipse and both class should be in same packageimport.pdf
Tested on Eclipse and both class should be in same packageimport.pdfTested on Eclipse and both class should be in same packageimport.pdf
Tested on Eclipse and both class should be in same packageimport.pdf
 
Solution A person in perfect health has a utility score of 1.0U.pdf
Solution A person in perfect health has a utility score of 1.0U.pdfSolution A person in perfect health has a utility score of 1.0U.pdf
Solution A person in perfect health has a utility score of 1.0U.pdf
 
Resurgent infection is suggesting that it is viral infection by meas.pdf
Resurgent infection is suggesting that it is viral infection by meas.pdfResurgent infection is suggesting that it is viral infection by meas.pdf
Resurgent infection is suggesting that it is viral infection by meas.pdf
 
There are only two functional groups in the molec.pdf
                     There are only two functional groups in the molec.pdf                     There are only two functional groups in the molec.pdf
There are only two functional groups in the molec.pdf
 
PsychoanalysisPsychoanalysis was founded by Sigmund Freud (1856-19.pdf
PsychoanalysisPsychoanalysis was founded by Sigmund Freud (1856-19.pdfPsychoanalysisPsychoanalysis was founded by Sigmund Freud (1856-19.pdf
PsychoanalysisPsychoanalysis was founded by Sigmund Freud (1856-19.pdf
 
Product of Cyclopentane + H2O(H+) gives No Reaction.It will simply F.pdf
Product of Cyclopentane + H2O(H+) gives No Reaction.It will simply F.pdfProduct of Cyclopentane + H2O(H+) gives No Reaction.It will simply F.pdf
Product of Cyclopentane + H2O(H+) gives No Reaction.It will simply F.pdf
 
Of the variedtypes of IPC, sockets arout and awaythe foremostcommon..pdf
Of the variedtypes of IPC, sockets arout and awaythe foremostcommon..pdfOf the variedtypes of IPC, sockets arout and awaythe foremostcommon..pdf
Of the variedtypes of IPC, sockets arout and awaythe foremostcommon..pdf
 
C2O2 is a bidendate ligand &Cl - is a mono dendat.pdf
                     C2O2 is a bidendate ligand &Cl - is a mono dendat.pdf                     C2O2 is a bidendate ligand &Cl - is a mono dendat.pdf
C2O2 is a bidendate ligand &Cl - is a mono dendat.pdf
 

Recently uploaded

Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 

Recently uploaded (20)

Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
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: ----------------------------------------
  • 2. #include "square.h" //implementation of square class color square::get_color() { return chip_color; } chip_rank square::get_rank() { return chip_status; } bool square::isEmpty() { return (empty == chip_color); } void square::make_empty() { chip_color = empty; } bool square::add_chip(color c, chip_rank r) { if (!isEmpty()) return false; else { chip_color = c; chip_status = r; return true; } } -------------------------------------- 3. board.h -------------------------------------- #ifndef BOARD_H_ #define BOARD_H_ #include #include #include #include #include #include #include "square.h" using namespace std;
  • 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;
  • 9. } chip_rank square::get_rank() { return chip_status; } bool square::isEmpty() { return (empty == chip_color); } void square::make_empty() { chip_color = empty; } bool square::add_chip(color c, chip_rank r) { if (!isEmpty()) return false; else { chip_color = c; chip_status = r; return true; } } -------------------------------------- 3. board.h -------------------------------------- #ifndef BOARD_H_ #define BOARD_H_ #include #include #include #include #include #include #include "square.h" using namespace std; //class Board class Board { public: Board(int);
  • 10. 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'; else return 'R'; } else { if (c_board[row][col].get_rank() == notking)
  • 11. 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) { filler = "| "; filler += print_chip(i, j); filler += ' '; } else {
  • 12. 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; return false; } if (row >= 0 && row < size && col >= 0 && col < size && colorIN >= (int) empty && colorIN <= (int) blue
  • 13. && 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: ------------------------------------------------- #include #include "board.h" using namespace std;
  • 14. 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(); } } ---------------------------------------------------