void TttBoard::setPeiceAt(int row, int column, char peice) { boundsCheck(row, column); if (!isMoveLegal(row, column)) { throw invalid_argument("There is already a peice in the specified cell"); } mCells[row][column].setPeice(peice); }
/* * get a square on the board when mainGame->pieceWasClicked is on * check if this square suppose to be marked- because it represent legal move * PRECONDITION: mainGame->pieceWasClicked * throws calloc error */ int isMarked(int row, int col){ move m={0}; /*check if it needs to be marked because it's possible move*/ m.row=mainGame->clickedPieceRow; m.col=mainGame->clickedPieceCol; m.newCol=col; m.newRow=row; graphicMoveToCastle(&m); return isMoveLegal(mainGame->gameInfo,&m); }
void Ghost::escape() { int nextDirection = m_previousDirection; while((nextDirection == m_previousDirection)||(!isMoveLegal(nextDirection))) { nextDirection = qrand()%4; } if(nextDirection != m_direction) { m_direction = nextDirection; m_previousDirection = (m_direction/2)*2 + (m_direction+1)%2; } moveTo(nextDirection); }
/* * handle clicking on the board * throws calloc errors, malloc__error(), calloc_error(),SDL_LoadBMPError(), SDL_DisplayFormatError(), * SDL_SetColorKeyError(), SDL_BlitSurfaceError(),SDL_FlipError() */ void handleBoardSelection(Widget *widget, int xClicked, int yClicked) { int row = 0, col = 0; move moveToBeApplied={0}; if(mainGame->isCheckMate==2) return; yClicked-=20; xClicked-=20; row = BOARD_SIZE - ( yClicked / PIECE_SIZE ) - 1; col = ( xClicked / PIECE_SIZE ); if(mainGame->pieceWasClicked){ moveToBeApplied.row = mainGame->clickedPieceRow; moveToBeApplied.col = mainGame->clickedPieceCol; moveToBeApplied.newRow = row; moveToBeApplied.newCol = col; moveToBeApplied.promoteTo = mainGame->promoteTo; graphicMoveToCastle(&moveToBeApplied); if(isMoveLegal(mainGame->gameInfo,&moveToBeApplied)){ if(standart_fail) return; mainGame->isCheckMate=applyMove(mainGame->gameInfo, &moveToBeApplied); if(standart_fail) return; } mainGame->pieceWasClicked = 0; /* need to reset the flag saying if we clicked */ mainGame->promoteTo=0; /* reseting the option to promote */ DeleteWidgets(&(screen->widgetChildren)); /*PvsAI and AI Turn*/ if( (mainGame->isCheckMate!=2) && (isAITurn(mainGame->gameInfo)) ) GUImakeAITurn(); else showGame(); } else { /*we might be clicking on a piece for movement and its with our color (player color) */ if( occupier_color(row, col , mainGame->gameInfo->boardPointer) == mainGame->gameInfo->nextTurnPlayerColor ) { mainGame->pieceWasClicked = 1;/* set the flag saying "we clicked!" */ mainGame->clickedPieceCol = col;/* save coordinates of the click */ mainGame->clickedPieceRow = row; if(isPawnBeforePromotionMarked()){ if(standart_fail) return; mainGame->promoteTo=getQueenChar(mainGame->gameInfo->nextTurnPlayerColor); } /*color the appropriate square - do a label on the label place a button - we put the piece */ DeleteWidgets(&(screen->widgetChildren)); showGame(); } } }
static char* isLegal_test() { assert(gs.initialized); printf("Running isLegal_test\n"); char fen[] = "rn1qkbnr/ppp2pp1/3p4/4p2p/4P2P/3P3b/PPP2PP1/RNBQKBNR w KQkq - 0 1"; parseFen(fen); int numMoves; MOVE moves[MAX_MOVES]; generateNonCaptures(gs.color, moves, &numMoves, gs.bitboard); for (int i=0; i < numMoves; i++) { MOVE move = moves[i]; //printf("Testing move "); //printMove(move); if (!isMoveLegal(move)) { printf("Not legal "); printMove(move); isMoveLegal(move); } assert( isMoveLegal(move)); } return 0; }
int main(int argc, const char * argv[]) { int origIndex, destIndex, origX, origY, destX, destY; bool legalMove = false; short unsigned int victory = VC_NO_VICTORY; initialize(); printBoard(); while(!victory){ while (!legalMove){ origIndex = readIndex(ORIGINE); destIndex = readIndex(DESTINATION); transf_index2rel(origIndex, &origX, &origY); transf_index2rel(destIndex, &destX, &destY); if (isMoveLegal (origX, origY, destX, destY)){ executeMove(origX, origY, destX, destY); printBoard(); victory = checkForVictory(); victoryMessage(victory); setColor(); } else printf("Illegal move, let's try again!\n"); } } printf("END\n"); return 0; }
/* * Assumes that simulator sets the info to what it currently is */ Direction Waltz::step(){ SensorInformation info = sensor->sense(); Direction d = Direction::Stay; if ((size_t)timeLeft <= stepHistory.size() || (size_t)batteryRemianing <= ((stepHistory.size() + 1) * config.BatteryConsumptionRate)){ if (!stepHistory.empty()){ d = opposite(stepHistory.top()); stepHistory.pop(); currentLocation = leadsTo(d); } if (currentLocation == make_pair(0,0)){ batteryRemianing = min(batteryRemianing + config.BatteryRechargeRate, config.BatteryCapacity); memory.clear(); // if going back to charge erase memory } else batteryRemianing -= config.BatteryConsumptionRate; return (d); //backtrack } // place current location in memory map, we were here! const point p = currentLocation; // copy constructor for point const pair<point, int> pp = {p, info.dirtLevel}; memory.insert(pp); if (info.dirtLevel > 0){ batteryRemianing -= config.BatteryConsumptionRate; return d; // stay in spot till it is clean } // if you can charge, charge if (currentLocation == make_pair(0,0) && batteryRemianing < config.BatteryCapacity){ //cout << "Charging" << endl; batteryRemianing = min(batteryRemianing + config.BatteryRechargeRate, config.BatteryCapacity); return d; } // else choose first possible direction in order of priority. backtracking lowers priority to last for (Direction d : directionPriority){ if (isMoveLegal(d, info.isWall)){ //cout << leadsTo(d).first << " , " << leadsTo(d).second ; if (memory.find(leadsTo(d)) == memory.end()){ stepHistory.push(d); currentLocation = leadsTo(d); if (currentLocation == make_pair(0,0)) batteryRemianing = min(batteryRemianing + config.BatteryRechargeRate, config.BatteryCapacity); else batteryRemianing -= config.BatteryConsumptionRate; //debugPrint(d); return d; } continue; // take first legal move } } // nothing was chosen, we are either stuck of have been to all spots around us, backtrack if possible, else stay if (!stepHistory.empty()){ d = opposite(stepHistory.top()); stepHistory.pop(); currentLocation = leadsTo(d); if (currentLocation == make_pair(0,0)) batteryRemianing = min(batteryRemianing + config.BatteryRechargeRate, config.BatteryCapacity); else batteryRemianing -= config.BatteryConsumptionRate; return (d); //backtrack } // at this point I have nothing smart to do so just take first possible direction in order of priority for (Direction dd : directionPriority){ if (isMoveLegal(dd, info.isWall)){ d = dd; stepHistory.push(d); currentLocation = leadsTo(d); break; } } if (currentLocation == make_pair(0,0)) batteryRemianing = min(batteryRemianing + config.BatteryRechargeRate, config.BatteryCapacity); else batteryRemianing -= config.BatteryConsumptionRate; return d; // nothing is possible, just stay where you are }