bool Model::checkInpassing(MoveRule moveRule, const Figure& figure, Move& move) const { bool accepted; Position passantCell; if (myLastMoveRecorded != true || !(myLastMove.type & INPASSING)) { return false; } if (myLastMove.player == WHITE) { passantCell.myX = myLastMove.pos2.myX; passantCell.myY = myLastMove.pos2.myY + 1; } else { passantCell.myX = myLastMove.pos2.myX; passantCell.myY = myLastMove.pos2.myY - 1; } if (passantCell != move.pos2) { return false; } move.type = INPASSING; move.effect = 0; move.figureCapturedId = readFigure(myBoard(move.pos2)).id; if (moveRule.moveEffect == EXPLOSION) { accepted = checkExplosionEffect(moveRule, figure, move); } else { accepted = true; } return accepted; }
int Model::getFigureIdOnBoard(int x, int y) const { BoardCell boardCell = myBoard(x, y); if (boardCell == 0) { return 0; } if (boardCell == -1) { return -1; } return readFigure(boardCell).id; }
void Model::makeMoveEffectCastle(const Move& move) { CastleRule castleRule = myRules->getCastleRule(move.pos2.myX, move.pos2.myY, move.player); BoardCell boardCell = myBoard(castleRule.rookCellStart); if (boardCell.setId <= 0 || boardCell.player != move.player) { return; //there is no rook on the place } // hack make move for castling: Move rookMove; rookMove.pos1 = castleRule.rookCellStart; rookMove.pos2 = castleRule.rookCellEnd; accessFigure(boardCell).position = move.pos2; accessFigure(boardCell).wasMoved = true; myBoard.setBoardCell(move.pos2.myX, move.pos2.myY, myBoard(move.pos1.myX, move.pos1.myY)); myBoard.setBoardCell(move.pos1.myX, move.pos1.myY, 0); }
void Model::innerMakeMove(const Move& move) { BoardCell boardCell; boardCell = myBoard(move.pos1); // assert(move.player == myCurrentPlayer); if (move.type == INPASSING) { makeMoveInpassing(move); } else if (move.type == CAPTURE) { makeMoveCapture(move); } accessFigure(boardCell).position = move.pos2; accessFigure(boardCell).wasMoved = true; myBoard.setBoardCell(move.pos2.myX, move.pos2.myY, myBoard(move.pos1.myX, move.pos1.myY)); myBoard.setBoardCell(move.pos1.myX, move.pos1.myY, 0); if (myRules->getFigureData(accessFigure(boardCell).id).promoting[move.player].figure != 0 && myRules->getFigureData(accessFigure(boardCell).id).promoting[move.player].horizontal == move.pos2.myY) { makeMovePromotion(move); } if (move.effect == LONGMOVE) { makeMoveEffectLongMove(move); } if (move.effect == CASTLE) { makeMoveEffectCastle(move); } if (move.effect == EXPLOSION) { makeMoveEffectExplosion(move); } myLastMoveRecorded = true; myLastMove = move; myCurrentPlayer = 1 - myCurrentPlayer; }
bool Model::checkPosition(MoveRule moveRule, const Figure& figure, Move& move, int movetype, bool needCheck) const { if (myBoard(move.pos2.myX, move.pos2.myY) == -1) { return false; } Position curPos = move.pos2; bool accepted = false; move.figureId = figure.id; if ((movetype & MOVE) && myBoard(move.pos2.myX, move.pos2.myY) == 0 && (moveRule.moveType & MOVE)) { accepted = checkMove(moveRule, figure, move); } else if ((movetype & CAPTURE) && myBoard(curPos.myX, curPos.myY) > 0 && (moveRule.moveType & CAPTURE)) { accepted = checkCapture(moveRule, figure, move); } else if ((movetype & CAPTURE) && myBoard(curPos.myX, curPos.myY) == 0 && (moveRule.moveType == INPASSING)) { accepted = checkInpassing(moveRule, figure, move); } if (accepted == true && needCheck == true) { accepted = !checkIfCheck(moveRule, figure, move); } return accepted; }
MOVES Model::movesFigure(int player, const Figure& figure, int movetype, bool needCheck) const { bool accepted; int curLimit; MOVES avMoves; Move move; MOVERULES::iterator itRule; Position curPos; MOVERULES curRules = myRules->getMoveRules(figure.id); for (itRule = curRules.begin(); itRule != curRules.end(); ++itRule) { if ((movetype == CAPTURE && itRule->moveType == MOVE) || (movetype == MOVE && itRule->moveType == CAPTURE)) { // пропускаем правило } else if (itRule->player == ALL || itRule->player == player) { if (itRule->ruleType == JUMP) { accepted = false; move.pos1 = figure.position; move.pos2.myX = move.pos1.myX + itRule->dx; move.pos2.myY = move.pos1.myY + itRule->dy; move.player = player; accepted = checkPosition(*itRule, figure, move, movetype, needCheck); if (accepted == true) { avMoves.push_back(move); } } else if (itRule->ruleType == SLIDE) { curLimit = 0; move.pos1 = figure.position; move.pos2 = move.pos1; bool isFree; do { isFree = checkIsFree(*itRule, move.pos2); if (isFree == true) { move.pos2.myX += itRule->dx; move.pos2.myY += itRule->dy; move.player = player; accepted = checkPosition(*itRule, figure, move, movetype, needCheck); if (accepted == true) { avMoves.push_back(move); } ++curLimit; } } while (isFree == true && myBoard(move.pos2.myX, move.pos2.myY) == 0 && (itRule->limit == 0 || curLimit < itRule->limit)); } } } return avMoves; }
MOVES Model::movesFromPosition(const Position& pos1) const { assert(initiated == true); MOVES avMoves; BoardCell boardCell; boardCell = myBoard(pos1); if (boardCell.setId <= 0) { return avMoves; } //qDebug() << ":Model:" << "movesFromPosition() before moesFigure"; avMoves = movesFigure(boardCell.player, readFigure(boardCell), CAPTURE | MOVE); return avMoves; }
bool Model::checkCapture(MoveRule moveRule, const Figure& figure, Move& move) const { bool accepted; BoardCell boardCell; boardCell = myBoard(move.pos2); if (boardCell.setId <= 0 || boardCell.player == move.player) { return false; } move.type = CAPTURE; move.effect = 0; move.figureCapturedId = readFigure(boardCell).id; if (moveRule.moveEffect == EXPLOSION) { accepted = checkExplosionEffect(moveRule, figure, move); } else { accepted = true; } return accepted; }
bool Model::checkIsFree(MoveRule moveRule, Position curPos) const { bool isFree = true; Position endPos; int dx, dy; endPos = curPos; endPos.myX += moveRule.dx; endPos.myY += moveRule.dy; dx = getDirection(moveRule.dx); dy = getDirection(moveRule.dy); do { curPos.myX += dx; curPos.myY += dy; if (curPos != endPos && myBoard(curPos.myX, curPos.myY) != 0) { isFree = false; } } while (curPos != endPos && isFree == true); return isFree; }
void Model::makeMoveEffectExplosion(const Move& move) { Position curPos; int figureId; BoardCell boardCell; for (int i = -1; i < 2; ++i) { for (int j = -1; j < 2; ++j) { curPos.myX = move.pos2.myX + i; curPos.myY = move.pos2.myY + j; if ((boardCell = myBoard(curPos)).setId != 0) { figureId = mySetFigures[boardCell.player].at(boardCell.setId - 1).id; if (curPos == move.pos2 || getFigureData(figureId).explosion == true) { mySetFigures[boardCell.player].at(boardCell.setId - 1).captured = true; myBoard.setBoardCell(curPos.myX, curPos.myY, 0); } } } } }
bool Model::checkCastleEffect(MoveRule, const Figure& figure, Move& move) const { // ISSUE: add a checking for non-checked cells return false; //temporaly; because bug with castling if (figure.wasMoved == true) { return false; } CastleRule castleRule = myRules->getCastleRule(move.pos2.myX, move.pos2.myY, move.player); BoardCell boardCell = myBoard(castleRule.rookCellStart); if (boardCell.setId <= 0 || boardCell.player != move.player) { return false; } Figure rook = readFigure(boardCell); if (rook.wasMoved == true || rook.captured == true) { return false; } move.effect = CASTLE; return true; }
BoardCell Model::getBoardCell(int x, int y) const { return myBoard(x, y); }
void Model::makeMovePromotion(const Move& move) { int promotionToFigure = myRules->getFigureData(move.figureId).promoting[move.player].figure; mySetFigures[move.player].at(myBoard(move.pos2).setId - 1).id = promotionToFigure; }
void Model::makeMoveCapture(const Move& move) { int opponent = 1 - move.player; mySetFigures[opponent].at(myBoard(move.pos2).setId - 1).captured = true; }
void Model::makeMoveInpassing(const Move& move) { int opponent = 1 - move.player; mySetFigures[opponent].at(myBoard(myLastMove.pos2).setId - 1).captured = true; myBoard.setBoardCell(myLastMove.pos2.myX, myLastMove.pos2.myY, 0); }
DECISION_INFO AI::decision(GAME_INFO gameInfo) { DECISION_INFO result={0}; CBoard myBoard(gameInfo.board); //myboard CBoard targetBoard(gameInfo.targetBoard);//enemy's board //myBoard.printBoard(); int currentBlock=gameInfo.nextBlock[0]; int maxTurn=CBoard::maxTurn[currentBlock]; CBoard tmp; int maxScore=-999999999; for(int x=-2;x<10;x++) //for possible X { for(int turn=0;turn<maxTurn;turn++) //for possible rotation { tmp=myBoard; int posY=tmp.dropBlock(x,0,currentBlock,turn); if(posY==0) continue; int filledLine=tmp.countFilledLine(); int score=calculate(tmp); score+=filledLine; //printf("[[%d::( %d, %d ) ]] ",score,x,turn); if(score>maxScore) { maxScore=score; result.posX=x; result.turn=turn; result.useHold=0; } } } printf("\n"); printf("put block on... posX:%d turn: %d\n",result.posX,result.turn); int randomMessage=rand()%4; switch(randomMessage) { case 0: makeMessage(result,L"hello~ \r\nhi~"); break; case 1: makeMessage(result,L"100ÄÞº¸!\r\n1000ÄÞº¸!"); break; case 2: //clear message makeMessage(result,L" "); break; case 3: makeMessage(result,NULL); //Reuse previous message(if all data is NULL, server uses previous message) break; } /* myBoard.printBoard(); System.out.println(); myBoard.dropBlock(result.posX,0,gameInfo.nextBlock[0],result.turn); myBoard.deleteFilledLine(); myBoard.printBoard(); */ /* result.posX=rand()%10; result.turn=rand()%4; result.useHold=0; memset(result.message,0,sizeof(result.message)); //set all data to NULL */ return result; }