void Board::undo() { if(history.empty()) return; Move* move = history.top(); history.pop();//pop top move, call it move //if(move == 0) return; removePiece(move->x, move->y); //cout << "type: " << move->type << endl; //cout << "newtype: " << move->newtype << endl; setPiece(move->oldx, move->oldy, move->type, move->color); white = (move->color == "white"); if(!move->first) board[move->oldx][move->oldy]->moved(); if(move->capture != "") setPiece(move->x, move->y, move->capture, (move->color == "black")?"white":"black"); delete move; }
void ChessBoard::goBackAMove() { if( moves.size() == 0 ) return; Move prevMove = moves.top(); moves.pop(); int from_x = prevMove.from_x; int from_y = prevMove.from_y; int to_x = prevMove.to_x; int to_y = prevMove.to_y; Piece* moveBackPiece = removePiece( to_x, to_y ); moveBackPiece->nrOfMoves--; setPiece( from_x, from_y, moveBackPiece ); moveBackPiece->x = from_x; moveBackPiece->y = from_y; //castling if( moveBackPiece->type == KING && std::abs(to_x - from_x) == 2 ) { if( to_x != 2 ) { setPiece( to_x+1, to_y, removePiece( from_x+1, from_y)); } else { setPiece( to_x - 2, to_y, removePiece( from_x-1, from_y)); } } if( prevMove.trans ) moveBackPiece->type = PAWN; Piece* backInPlay = prevMove.pieceTaken; if( backInPlay != NULL ) { if( prevMove.amp ) { setPiece( to_x, from_y, backInPlay ); backInPlay->x = to_x; backInPlay->y = from_y; } else { setPiece( to_x, to_y, backInPlay ); backInPlay->x = to_x; backInPlay->y = to_y; } backInPlay->inPlay = true; } }
void Board::clearBoard() { for (int i = 0; i < BOARD_SIZE; i++) for (int j = 0; j < BOARD_SIZE; j++) setPiece(g_PIECES[NO_PIECE], i, j); m_MissingPieces = 0; m_Matches = 0; m_NoMatch = 0; }
void Board::movePiece(int x, int y, int newx, int newy, Chess* chtemp) { //cout << "Move: " << x << ", " << y << ", " << newx << ", " << newy << endl; string cap = ""; if(board[newx][newy] != 0) { cap = board[newx][newy]->type; removePiece(newx, newy); } string type = board[x][y]->type; string color = board[x][y]->color; string oldt = type; bool first = board[x][y]->beenmoved; removePiece(x, y); if(type == "pawn") { if(chtemp!=0 && ((newy == 0 && white) || (newy == 7 && !white))) { const char* labels[] = {"Queen", "Rook", "Knight", "Bishop"}; switch(chtemp->button_box("Pawn Promotion", "What Piece would you like?", labels, 4)) { case 1: type = "queen"; break; case 2: type = "rook"; break; case 3: type = "knight"; break; case 4: type = "bishop"; break; }//do pawn promotion } } else type = ""; if(oldt == "king") { ((white)?wking = ((newx*10)+newy):bking = ((newx*10)+newy)); cout << "King to: " << ((white)?wking:bking) << endl; } setPiece(newx, newy, (type == "")?oldt:type, color); board[newx][newy]->moved(); //cout << "type: " << type << endl; //cout << "oldt: " << oldt << endl; Move* temp = new Move(newx, newy, x, y, oldt, color, type); temp->first = first; if(cap != "") temp->cap(cap); history.push(temp);//push move }
void BoardItem::drawBoard() { Game::Board board = d->game->board(); for(Game::Square square = 0; square < 64; square++) { d->squares[square]->replaceCurrentPiece(0); // Remove all pieces Game::Piece pieceType = board.at(square); if(pieceType != 0 && pieceType != 255) { PieceItem *piece = new PieceItem(this, pieceType); piece->setCurrentSquare(square); scene()->addItem(piece); setPiece(piece, square); } } }
void Board::init() { clean(); white = true; check = 0; bking = 40; wking = 47; int x = 0; int y = 0; for(y = 0; y < 8; y++) for(x = 0; x < 8; x++) { if(y == 1) setPiece(x, y, "pawn", "black"); else if(y == 6) setPiece(x, y, "pawn", "white"); else if(y == 0 || y == 7) switch(x) { case 0: case 7: setPiece(x, y, "rook", (y==0)?"black":"white"); break; case 1: case 6: setPiece(x, y, "knight", (y==0)?"black":"white"); break; case 2: case 5: setPiece(x, y, "bishop", (y==0)?"black":"white"); break; case 3: setPiece(x, y, "queen", (y==0)?"black":"white"); break; case 4: setPiece(x, y, "king", (y==0)?"black":"white"); break; } else board[x][y] = 0; } }
void loadGame(Game* game, char* filename) { FILE *in = NULL; long fsize = 0; char piecesInfo[4096]; char piecesInfoConvert[64*6+1]; int counter; int i, j; Piece* piece; assert(game); /* filename="saveGame.log"; */ in = fopen(filename, "rb"); if(in==NULL) { printf("The file doesn't exist.\n\n"); return; } fseek(in, 0, SEEK_END); fsize = ftell(in); fseek(in, 0, SEEK_SET); fread(piecesInfo, 1, fsize, in); /*printf("%s \n", piecesInfo);*/ counter = -1; j=0; for(i=0;i<32;i++) { piecesInfoConvert[++counter] = piecesInfo[j]; piecesInfoConvert[++counter] = piecesInfo[j+1]; piecesInfoConvert[++counter] = piecesInfo[j+2]; piecesInfoConvert[++counter] = piecesInfo[j+3]; piecesInfoConvert[++counter] = piecesInfo[j+4]; piecesInfoConvert[++counter] = piecesInfo[j+5]; j=j+8; } piecesInfoConvert[++counter] = '\0'; printf(piecesInfoConvert); counter = -1; for(j=0;j<8;j++) { for(i=0;i<8;i++) { setPiece(game, NULL, i, j); } } counter = 0; for(i=0;i<32;i++) { piece = game->pieces[i]; modify(piece, piecesInfoConvert[counter], piecesInfoConvert[counter+1], piecesInfoConvert[counter+2]-'0', piecesInfoConvert[counter+3]-'0', piecesInfoConvert[counter+4]-49, piecesInfoConvert[counter+5]-49 ); counter+=6; setPiece(game, piece, piece->x, piece->y); } /* game->board[0][0] = piece; */ /* printf("\n\n %c, %c \n\n",piecesInfoConvert[0],piecesInfoConvert[1]); printf("\n\n %c, %c \n\n",piecesInfoConvert[2],piecesInfoConvert[3]); printf("\n\n %d, %d \n\n", piecesInfoConvert[4]-49,piecesInfoConvert[5]-49); */ printf("Game Loaded.\n"); }
void BoardItem::setPiece(PieceItem * piece, int row, int col ) { setPiece(piece, toSquare(row, col)); }
void BoardItem::movePiece(PieceItem* piece, const Game::Square &from, const Game::Square &to) { setPiece(piece, to ); d->squares[from]->setCurrentPiece(0); }
bool ChessBoard::makeMove(int from_x, int from_y, int to_x, int to_y ) { Move move( from_x, from_y, to_x, to_y ); /* the code that validates the move, does not check the position after the move this is done in the end of this method */ if( !validateMove( move ) ) return false; //castling if( getPiece( from_x, from_y )->type == KING && std::abs(to_x - from_x) == 2) { Piece* rook; int x, y = from_y; x = (to_x != 2) ? from_x + 1 : from_x - 1; if( to_x != 2 ) { rook = removePiece(to_x+1, to_y); } else { rook = removePiece( to_x - 2, to_y); } setPiece( x, y, rook ); rook->x = x; rook->y = y; } /* adds the piece to be taken if exists, then adds the move to the stack of moves */ Piece* pieceToMove = removePiece( from_x, from_y ); move.pieceTaken = getPiece( to_x, to_y ); //amp if( (pieceToMove->type == PAWN) && (std::abs( to_x - from_x ) == 1 ) && (getPiece( to_x, to_y ) == NULL) ) { move.pieceTaken = removePiece( to_x, from_y ); move.amp = true; } //transform to piece if( pieceToMove->type == PAWN && ( to_y == 0 || to_y == 7) ) { move.trans = true; pieceToMove->type = QUEEN; } if( move.pieceTaken != NULL ) { if( pieceToMove->color == move.pieceTaken->color) { setPiece( from_x, from_y, pieceToMove ); return false; } move.pieceTaken->inPlay = false; } moves.push( move ); /* retrives the piece that is to be moved, and then it moves it. at the end the new position is updaten in the piece-object */ setPiece( to_x, to_y, pieceToMove ); pieceToMove->x = to_x; pieceToMove->y = to_y; PieceColor colorOfMovingPlayer = pieceToMove->color; pieceToMove->nrOfMoves++; /* checks if the king of the player which just moved is in check, if so the move is invalid and it goes back a move */ if( playersKingInCheck( colorOfMovingPlayer ) ) { goBackAMove(); return false; } return true; }
/* * Executes a command given by the user * * @params: (command) - the command given by the user * @return: relevant exitcode */ int executeCommand(char* command){ char str[64]; sscanf(command, "%s", str); if (str_equals(str, "quit")){ exit(0); } if (state == SETTINGS){ if (str_equals(str, "game_mode")){ return setGameMode(command); } if (str_equals(str, "difficulty")){ return setDifficulty(command); } if (str_equals(str, "user_color")){ return setUserColor(command); } if (str_equals(str, "load")){ return loadGameByCommand(command); } if (str_equals(str, "clear")){ Board_clear(&board); PieceCounter_reset(counter); return 0; } if (str_equals(str, "next_player")){ return setFirstPlayer(command); } if (str_equals(str, "rm")){ return removePiece(command); } if (str_equals(str, "set")){ return setPiece(command); } if (str_equals(str, "print")){ display(); return 0; } if (str_equals(str, "start")){ if(PieceCounter_kingIsMissing(counter)){ return -7; } turn = first; state = GAME; return 2; //special value to break the humanTurn loop so the initial board will always be checked for immediate loss or tie conditions } } else{ if (str_equals(str, "get_moves")){ return printMovesOfPiece(command); } if (str_equals(str, "move")){ return movePiece(command); } if (str_equals(str, "castle")){ return castleRook(command); } if (str_equals(str, "get_best_moves")){ return printBestMoves(command); } if (str_equals(str, "get_score")){ return printMoveValue(command); } if (str_equals(str,"save")){ return saveGameByCommand(command); } } return -1; }