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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
//
//
// Lab 05 - Checkers Program
// Compile with clang++ -Wall -std=c++11 checkersLab05.cpp
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>
enum ChipColor { // value as int EMPTY==0 RED==1 BLACK==2
EMPTY, RED, BLACK
};
enum ChipRank { // value as int NOTKING==0 KING==1
NOTKING, KING
};
enum SquareColor { // 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(ChipColor newColor, ChipRank newRank, SquareColor newSqColor);
void setColor(ChipColor newColor);
void setRank(ChipRank newRank);
void setSquareColor(SquareColor newSqColor);
ChipColor getChipColor();
char displayChipColor();
char saveChipColor();
char saveChipRank();
char saveSquareColor();
private:
ChipColor chipColor;
ChipRank chipRank;
SquareColor squareColor;
};
// Your square member function code goes here
Square::Square(){
chipColor = EMPTY;
chipRank = NOTKING;
squareColor = WHITE_SQUARE;
}
Square::Square(ChipColor newColor, ChipRank newRank, SquareColor newSqColor){
chipColor = newColor;
chipRank = newRank;
squareColor = newSqColor;
}
void Square::setColor(ChipColor newColor){
chipColor = newColor;
}
void Square::setRank(ChipRank newRank){
chipRank = newRank;
}
void Square::setSquareColor(SquareColor newSqColor){
squareColor = newSqColor;
}
ChipColor Square::getChipColor(){
return chipColor;
}
char Square::displayChipColor(){
char color;
if (chipColor == EMPTY)
color = ' ';
if (chipColor == RED)
color = 'R';
if (chipColor == BLACK)
color = 'B';
return color;
}
char Square::saveChipColor(){
char color;
if (chipColor == EMPTY)
color = '0';
if (chipColor == RED)
color = '1';
if (chipColor == BLACK)
color = '2';
return color;
}
char Square::saveChipRank() {
char rank;
if(chipRank == NOTKING)
rank = '0';
if(chipRank == KING)
rank = '1';
return rank;
}
char Square::saveSquareColor() {
char color;
if(squareColor == WHITE_SQUARE)
color = '0';
if(squareColor == BLACK_SQUARE)
color = '1';
return color;
}
// Board class defines an 8x8 board
class Board {
public:
Board();
void displayBoard();
void setupBoard(std::string fileName);
void SaveBoard(std::string fileName);
private:
int boardSize = 8;
Square board[8][8];
};
// Your Board member function code goes here
Board::Board(){
int i=0;
int j=0;
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 *| |* x *|
// |*****| |*****|
// +-----+-----+-----+...
//
// where the square with the x in it is a black square where the x indicates the color (empty, R or B)
// and the blank squares are just spaces;
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 i,j,color;
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;
color = (i) % 2; // Mod 2 - needed to distinguish white and black squares
line="";
for (j=0; j < boardSize; j++){ //build first line for row of square
if ( (j + color) %2 ){
filler = spaces; // fill spaces for white squares ( | |)
}
else{
filler = stars; // fill stars 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 ((j + color) % 2 ){
filler = spaces; // fill spaces 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 ( (j + color) %2 ){
filler = spaces; // fill spaces for white squares ( | | )
}
else{
filler = stars; // fill stars 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::setupBoard(std::string fileName) {
std::ifstream inf;
inf.open(fileName);
if(!inf.is_open()) {
std::cout << "Sorry, could not open file." << std::endl;
exit(0);
}
}
void Board::SaveBoard(std::string fileName) {
std::ofstream fout;
fout.open(fileName);
if(!fout) {
std::cout << "Sorry could not save game." << std::endl;
exit(0);
}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
fout << board[i][j].saveChipColor() << ' ';
fout << board[i][j].saveChipRank() << ' ';
fout << board[i][j].saveSquareColor() << ' ';
fout << "0";
fout << std::endl;
}
}
fout.close();
return;
}
int main(){
Board gameBoard;
gameBoard.displayBoard();
std::ifstream inputFile;
std::string fileName;
std::cout << "Please enter the file name:" << std::endl << "newGame.txt or savedGame.txt" << std::endl;
getline(std::cin, fileName);
inputFile.open(fileName);
if(!inputFile.is_open()) {
std::cout << "Sorry, could not load game." << std::endl;
exit(0);
}
gameBoard.setupBoard(fileName);
gameBoard = Board::SaveBoard(fileName);
Board::SaveBoard(fileName);
Square firstSquare;
Square secondSquare(RED, NOTKING, BLACK_SQUARE);
std::cout << firstSquare.getChipColor() << std::endl;
std::cout << secondSquare.getChipColor() << std::endl;
return 0;
}
|