1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
enum ChipColors { // value as int EMPTY == 0 ; RED == 1 ; BLACK == 2
EMPTY, RED, BLACK
};
enum ChipRanks { // value as int NOTKING == 0 ; KING == 1
NOTKING, KING
};
enum SquareColors { // value as int WHITE_SQUARE == 0 ; BLACK_SQUARE == 1
WHITE_SQUARE, BLACK_SQUARE
};
// Square class defines a square on a checkers board
class Square {
public:
Square();
Square(ChipColors cColor, ChipRanks cRank, SquareColors sColor);
void setColor(ChipColors newColor);
void setRank(ChipRanks chipRank);
void setSquareColor(SquareColors squareColor);
ChipColors getChipColor();
ChipRanks getChipRank();
SquareColors getSquareColor();
private:
ChipColors chipColor; // would look like "int chipColor;" if we didn't use enum
ChipRanks chipRank;
SquareColors squareColor;
};
// Your square member function code goes here
Square::Square() {
chipColor = EMPTY;
chipRank = NOTKING;
squareColor = WHITE_SQUARE;
}
Square::Square(ChipColors cColor, ChipRanks cRank, SquareColors sColor) {
chipColor = cColor;
chipRank = cRank;
squareColor = sColor;
}
ChipColors getChipColor() {
int chipColor;
if(ChipColors == EMPTY) {
chipColor = 0;
}
if(ChipColors == RED) {
chipColor = 1;
}
if(ChipColors == BLACK) {
chipColor = 2;
}
return chipColor;
}
ChipRanks getChipRank() {
return ChipRanks;
}
SquareColors getSquareColor() {
return SquareColors;
}
// Board class defines an 8x8 board
class Board {
public:
void displayBoard();
void makeMove();
void changeRank();
void displayChecker(int, int);
private:
void checkIfValidMove();
void removeCapturedPiece();
};
// Your Board member function code goes here
Board::Board(){
int i;
int j;
int boardSize = 8;
int color;
for (i = 0; i < boardSize; ++i){
color = (i) % 2; // Mod 2 - needed to distinguish white and black squares
for (j = 0; j < boardSize; ++j){
if ( (j + color) %2 ){
Board[i][j].setSquareColor(WHITE_SQUARE);
}
else{
Board[i][j].setSquareColor(BLACK_SQUARE);
}
}
}
}
// This function is provided for you
// display an 8x8 board of squares like the following:
//
// +---+---+---+...
// |***| |***|
// |***| x |***|
// |***| |***|
// +---+---+---+...
//
// where the square with the x in it is a white square and contains the color (empty, R or B)
// and the square with the ***'s indicates a black square on the board
void Board::displayBoard(){
std::string dashes = "+-----"; //characters for square top and bottom bounds
std::string spaces = "| "; //fill for white squares
std::string stars = "|*****"; // fill for black squares
std::string line = "";
std::string filler;
int boardSize = 8;
int i, j;
for (i = 0; i < boardSize; i++){ //repeat 8 times, once for each row of squares
line="";
for(j = 0; j < boardSize; j++){ //build line of cells for boundary ( +---+ )
line = line + dashes;
}
std::cout << line << "+" << std::endl;
line="";
for (j = 0; j < boardSize; j++){ //build first line for row in squares
if (Board[i][j].getSquareColor() == WHITE_SQUARE ){
filler = stars; // fill stars for white squares ( |*****|)
}
else{
filler = spaces; // fill spaces for black squares ( | |)
}
line = line + filler;
}
std::cout << line << "|" << std::endl;
line="";
for (j = 0; j < boardSize; j++){ //build second line for row of square ( | x | )
if (Board[i][j].getSquareColor() == WHITE_SQUARE ){
filler = stars; // fill stars for white squares ( |*****|)
}
else{
filler = "| "; // fill spaces for blacksquares
filler += board[i][j].displayChipColor(); // display color of checker
filler += " ";
}
line = line + filler;
}
std::cout << line << "|" << std::endl;
line="";
for (j = 0; j < boardSize; j++){ //build third line for row of square
if (Board[i][j].getSquareColor() == WHITE_SQUARE ){
filler = stars; // fill stars for WHITE squares ( |*****| )
}
else{
filler = spaces; // fill spaces for BLACK squares ( | | )
}
line = line + filler;
}
std::cout << line << "|" << std::endl;
}
line="";
for (j=0; j < boardSize; j++){ //build line of cells for boundary on bottom of board ( +---+ )
line = line + dashes;
}
std::cout << line << "+" << std::endl;
std::cout << std::endl;
}
void Board::displayChecker(int, int) {
std::cout << getChipColor;
}
int main() {
// declares two square objects
Square firstSquare;
Square secondSquare(RED, NOTKING, BLACK_SQUARE);
std::cout << firstSquare.getChipColor() << std::endl;
std::cout << secondSquare.getChipColor() << std::endl;
// code from lab assignment on lab webpage adjusted
Square thirdSquare;
// test if constructor set rank to NOTKING
if(thirdSquare.getChipRank() == NOTKING) {
std::cout << "Test Passed - ";
}
else {
std::cout << "Test Failed - ";
}
std::cout << "Value of rank for thirdSquare: " << thirdSquare.getChipRank() << std::endl;
// test if constructor set rank to EMPTY
if(thirdSquare.getChipColor() == EMPTY) {
std::cout << "Test Passed - ";
}
else {
std::cout << "Test Failed - ";
}
std::cout << "Value of color for thirdSquare: " << thirdSquare.getChipColor() << std::endl;
// test if square color is BLACK_SQUARE
// WHITE_SQUARE color is illegal - checkers only played on black squares
if(thirdSquare.getSquareColor() == BLACK_SQUARE) {
std::cout << "Legal move" << std::endl;
}
else {
std::cout << "Illegal move" << std::endl;
}
// outputs square information
Square fourthSquare(BLACK, NOTKING, BLACK_SQUARE);
std::cout << fourthSquare.getChipColor() << std::endl;
std::cout << fourthSquare.getChipRank() << std::endl;
std::cout << fourthSquare.getSquareColor() << std::endl;
}
| |