void Board::takeStep(){ checkTurn(); vec step = inputVec(); if(step == vec(-1, -1)){ fprintf(stderr, "no input vector!\n"); step = inputVec(); } while(!canFlip(step.x, step.y, turnToPieceType(turn))){ fprintf(stderr, "invalid input (%d, %d)!\n", step.x, step.y); step = inputVec(); } setPieces(step.x, step.y); if(checkEnd()){ if(countColor(black) > countColor(white)){ printf("black: %d\n", countColor(black)); printf("white: %d\n", countColor(white)); printf("black wins!\n"); } else if(countColor(black) < countColor(white)){ printf("black: %d\n", countColor(black)); printf("white: %d\n", countColor(white)); printf("white wins!\n"); } else{ printf("black: %d\n", countColor(black)); printf("white: %d\n", countColor(white)); printf("ties!\n"); } printBoard(); exit(0); } else{ printBoard(); } }
bool Board::canSetPiece(bool color){ //check if one can set piece, used for checkTurn() pieceType pc = color? white: black; for(int x = 0; x < DEFAULT_SIZE; x++){ for(int y = 0; y < DEFAULT_SIZE; y++){ if(b[x][y] == empty && canFlip(x, y, pc)){ return true; } } } return false; }
bool canFlip(string s) { for(int i=1; i<s.length(); i++) { if(s[i-1]=='+' && s[i]=='+') { s[i-1]='-'; s[i]='-'; if(!canFlip(s)) return true; s[i-1]='+'; s[i]='+'; } } return false; }
bool canWin(string s) { if(s.length()==0) return false; return canFlip(s); }