/************************************************************ The Jkb Triangle Peg Solitaire Simulation Copyright (C) 2004 Jeffrey K. Brown This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. http://www.gnu.org/copyleft/gpl.html *************************************************************/ #include #include class PegBoard { private: bool started; int movecount; char history[200]; public: bool pega; bool pegb; bool pegc; bool pegd; bool pege; bool pegf; bool pegg; bool pegh; bool pegi; bool pegj; bool pegk; bool pegl; bool pegm; bool pegn; bool pego; PegBoard(); void display(); void start(char); char* showMoves(); void movePeg(char*); void togglePeg(char); void printMoves(); int getScore(); void printScore(); void playGame(char*); void printFinal(); char* getHistory(); }; PegBoard::PegBoard() { started=false; strcpy(history,""); movecount=0; pega=pegb=pegc=pegd=pege=pegf=pegg=true; pegh=pegi=pegj=pegk=pegl=pegm=pegn=pego=true; } void PegBoard::start(char peg) { started=true; movecount++; sprintf(history,"%c", peg); if (peg=='A') pega=false; if (peg=='B') pegb=false; if (peg=='C') pegc=false; if (peg=='D') pegd=false; if (peg=='E') pege=false; if (peg=='F') pegf=false; if (peg=='G') pegg=false; if (peg=='H') pegh=false; if (peg=='I') pegi=false; if (peg=='J') pegj=false; if (peg=='K') pegk=false; if (peg=='L') pegl=false; if (peg=='M') pegm=false; if (peg=='N') pegn=false; if (peg=='O') pego=false; } void PegBoard::display() { char la, lb, lc, ld, le, lf, lg, lh, li, lj, lk, ll, lm, ln, lo; if (pega) la = 'A'; else la = '.'; if (pegb) lb = 'B'; else lb = '.'; if (pegc) lc = 'C'; else lc = '.'; if (pegd) ld = 'D'; else ld = '.'; if (pege) le = 'E'; else le = '.'; if (pegf) lf = 'F'; else lf = '.'; if (pegg) lg = 'G'; else lg = '.'; if (pegh) lh = 'H'; else lh = '.'; if (pegi) li = 'I'; else li = '.'; if (pegj) lj = 'J'; else lj = '.'; if (pegk) lk = 'K'; else lk = '.'; if (pegl) ll = 'L'; else ll = '.'; if (pegm) lm = 'M'; else lm = '.'; if (pegn) ln = 'N'; else ln = '.'; if (pego) lo = 'O'; else lo = '.'; printf("\nPeg Board: Moves: %d", movecount); printf("\n%s",history); printf("\n %c", la); printf("\n %c %c", lb, lc); printf("\n %c %c %c", ld, le, lf); printf("\n %c %c %c %c", lg, lh, li, lj); printf("\n %c %c %c %c %c", lk, ll, lm, ln, lo); printf("\n"); } char* PegBoard::getHistory() { return history; } void PegBoard::togglePeg(char peg) { if (peg=='A') { pega = !pega; } if (peg=='B') { pegb = !pegb; } if (peg=='C') { pegc = !pegc; } if (peg=='D') { pegd = !pegd; } if (peg=='E') { pege = !pege; } if (peg=='F') { pegf = !pegf; } if (peg=='G') { pegg = !pegg; } if (peg=='H') { pegh = !pegh; } if (peg=='I') { pegi = !pegi; } if (peg=='J') { pegj = !pegj; } if (peg=='K') { pegk = !pegk; } if (peg=='L') { pegl = !pegl; } if (peg=='M') { pegm = !pegm; } if (peg=='N') { pegn = !pegn; } if (peg=='O') { pego = !pego; } } void PegBoard::movePeg (char* move) { if (strcmp(move,"None")!=0) { movecount++; togglePeg(move[0]); togglePeg(move[1]); togglePeg(move[2]); sprintf (history, "%s %s", history, move); } } char* PegBoard::showMoves (void) { char moves[100]; strcpy (moves,""); // A B D if ((pega==true) && (pegb==true) && (pegd==false)) { strcat (moves, "ABD "); } // A C F if ((pega==true) && (pegc==true) && (pegf==false)) { strcat(moves, "ACF "); } // B D G if ((pegb==true) && (pegd==true) && (pegg==false)) { strcat(moves, "BDG "); } // B E I if ((pegb==true) && (pege==true) && (pegi==false)) { strcat(moves, "BEI "); } // C E H if ((pegc==true) && (pege==true) && (pegh==false)) { strcat(moves, "CEH "); } // C F J if ((pegc==true) && (pegf==true) && (pegj==false)) { strcat(moves, "CFJ "); } // D B A if ((pegd==true) && (pegb==true) && (pega==false)) { strcat(moves, "DBA "); } // D E F if ((pegd==true) && (pege==true) && (pegf==false)) { strcat(moves, "DEF "); } // D H M if ((pegd==true) && (pegh==true) && (pegm==false)) { strcat(moves, "DHM "); } // D G K if ((pegd==true) && (pegg==true) && (pegk==false)) { strcat(moves, "DGK "); } // E H L if ((pege==true) && (pegh==true) && (pegl==false)) { strcat(moves, "EHL "); } // E I N if ((pege==true) && (pegi==true) && (pegn==false)) { strcat(moves, "EIN "); } // F C A if ((pegf==true) && (pegc==true) && (pega==false)) { strcat(moves, "FCA "); } // F E D if ((pegf==true) && (pege==true) && (pegd==false)) { strcat(moves, "FED "); } // F I M if ((pegf==true) && (pegi==true) && (pegm==false)) { strcat(moves, "FIM "); } // F J O if ((pegf==true) && (pegj==true) && (pego==false)) { strcat(moves, "FJO "); } // G D B if ((pegg==true) && (pegd==true) && (pegb==false)) { strcat(moves, "GDB "); } // G H I if ((pegg==true) && (pegh==true) && (pegi==false)) { strcat(moves, "GHI "); } // H E C if ((pegh==true) && (pege==true) && (pegc==false)) { strcat(moves, "HEC "); } // H I J if ((pegh==true) && (pegi==true) && (pegj==false)) { strcat(moves, "HIJ "); } // I E B if ((pegi==true) && (pege==true) && (pegb==false)) { strcat(moves, "IEB "); } // I H G if ((pegi==true) && (pegh==true) && (pegg==false)) { strcat(moves, "IHG "); } // J F C if ((pegj==true) && (pegf==true) && (pegc==false)) { strcat(moves, "JFC "); } // J I H if ((pegj==true) && (pegi==true) && (pegh==false)) { strcat(moves, "JIH "); } // K G D if ((pegk==true) && (pegg==true) && (pegd==false)) { strcat(moves, "KGD "); } // K L M if ((pegk==true) && (pegl==true) && (pegm==false)) { strcat(moves, "KLM "); } // L H E if ((pegl==true) && (pegh==true) && (pege==false)) { strcat(moves, "LHE "); } // L M N if ((pegl==true) && (pegm==true) && (pegn==false)) { strcat(moves, "LMN "); } // M H D if ((pegm==true) && (pegh==true) && (pegd==false)) { strcat(moves, "MHD "); } // M I F if ((pegm==true) && (pegi==true) && (pegf==false)) { strcat(moves, "MIF "); } // M L K if ((pegm==true) && (pegl==true) && (pegk==false)) { strcat(moves, "MLK "); } // M N O if ((pegm==true) && (pegn==true) && (pego==false)) { strcat(moves, "MNO "); } // N I E if ((pegn==true) && (pegi==true) && (pege==false)) { strcat(moves, "NIE "); } // N M L if ((pegn==true) && (pegm==true) && (pegl==false)) { strcat(moves, "NML "); } // O J F if ((pego==true) && (pegj==true) && (pegf==false)) { strcat(moves, "OJF "); } // O N M if ((pego==true) && (pegn==true) && (pegm==false)) { strcat(moves, "ONM "); } if (strlen(moves)==0) return "None"; else return moves; } int PegBoard::getScore() { int score = 0; if (pega) score ++; if (pegb) score ++; if (pegc) score ++; if (pegd) score ++; if (pege) score ++; if (pegf) score ++; if (pegg) score ++; if (pegh) score ++; if (pegi) score ++; if (pegj) score ++; if (pegk) score ++; if (pegl) score ++; if (pegm) score ++; if (pegn) score ++; if (pego) score ++; return score; } void PegBoard::printScore() { printf("\nScore: %d",getScore()); } void PegBoard::printMoves() { printf("\nMoves: %s", showMoves()); if (strcmp(showMoves(),"None")==0) printScore(); printf("\n"); } void PegBoard::printFinal() { printf("\n%s: %d",history,getScore()); } void PegBoard::playGame(char* game) { bool started=false; char* p; char* q; char* end; char move[3]; end = game + strlen(game) + 1; for (p=game; p