void nextMove(char player, char board[3][3]){ int iboard[25]; int i,j,move; pair best; initializeBoard(iboard, board); US = player; THEM = otherPlayer(US); timesCalled=0; //printboard(iboard); best = chooseMoveAB(iboard,player,INT_MIN,INT_MAX); convert1dTo2d(best.move); printf("chooseMoveAB timesCalled: %d bestMove:%d\n", timesCalled,best.move); timesCalled=0; //printboard(iboard); best = chooseMove(iboard,player); convert1dTo2d(best.move); printf("chooseMove timesCalled: %d bestMove:%d\n", timesCalled,best.move); timesCalled=0; //printboard(iboard); best = chooseMoveEvaluate(iboard,player); convert1dTo2d(best.move); printf("chooseMoveEvaluate timesCalled: %d bestMove:%d\n", timesCalled,best.move); timesCalled=0; //printboard(iboard); best = chooseMoveABDepth(iboard,player,INT_MIN,INT_MAX); convert1dTo2d(best.move); printf("chooseMoveAB timesCalled: %d bestMove:%d\n", timesCalled,best.move); }
// Décision du mouvement à effectuer void decideMove(const SGameState * const gameState) { SMove choosedMove; int choice; // Décision du mouvemennt /*j'ai besoin du coup precedent(m_decidedMove) pour determiner le suivant*/ GroupMoves priorityMoves; // liste qui contient les mouvements non dupliqués et risqués évalués globalement( avec toutes les pieces énnemies voisines) GroupMoves normalMoves, riskedMoves; evaluateMoves(gameState, &normalMoves,&riskedMoves); globalEvaluation(&priorityMoves,riskedMoves, gameState); normalClassication(gameState, &normalMoves); /* Choix du type de mouvement à faire en fonction du gameState et des mouvements trouvés */ choice = chooseTypeOfMove(gameState, normalMoves.length_list, riskedMoves.length_list); if(choice == 0) choosedMove = chooseMove(gameState, priorityMoves); else choosedMove = chooseMove(gameState, normalMoves); /* Mise à jour de la variable de nombre d'allers-retours */ // Si on a fait le mouvement inverse du précédent, on incrémente le nombre if ((choosedMove.start.line == m_decidedMove.end.line) &&(choosedMove.start.col == m_decidedMove.end.col) &&(choosedMove.end.line == m_decidedMove.start.line) &&(choosedMove.end.col == m_decidedMove.start.col)) m_nbRoundTrips++; else // Sinon remise à zéro de la variable m_nbRoundTrips = 1; m_decidedMove = choosedMove; /* Vérification si le mouvement est une attaque pour la valeur de m_myMove */ if (gameState->board[m_decidedMove.end.line][m_decidedMove.end.col].content == m_enemyColor) m_myMove = true; }
void Spacebot::writeNextMove() { Move move = chooseMove(); writeMove(move); }
/*Main program*/ int main(void) { /*Variable Declarations*/ /*Used to store lines from text, copies of lines, file name of text, and tokens, respectively*/ char line[100], lineCopy[100], fileName[100], *token; /*Array of keywords that are valid commands*/ char keywords[8][14] = {"forward", "reverse", "turnright", "turnleft", "pause", "light", "reset", "loop"}; /*Variable used as a counter*/ int counter; /*Variable used to keep track of line number in text file*/ int lineCount = 0; /*Initializr status of the light*/ LIGHT_ON = false; /*Variable for getting the file from the user*/ FILE *inputFile; /*Makes sure the motors are stationary and the LED is off*/ parallelput(0); /*Prompts user to enter a file name*/ printf("Enter a file name: "); gets(fileName); /* Main loop in the program */ /*If the file is valid and can be read*/ if ( (inputFile = fopen( fileName, "r" )) != NULL ) { /*While there is another line in the text file*/ while ( !feof(inputFile) ) { /*Get the line*/ fgets( line, 99, inputFile ); /*Convert line to lowecase*/ toLowercase(line); /*Creates a copy of the line for tokenization (which changes original line)*/ strcpy(lineCopy, line); /*Increase line count by 1*/ lineCount++; /*Prevents iteration of previous line if very last line in text file is an empty line*/ if( feof(inputFile) ) break; /*If the command in the text file is valid*/ if (isValidMove(lineCopy, inputFile) == true) { /*Display message*/ printf("Line #%d: ", lineCount); /*creates a copy of the string*/ strcpy(lineCopy, line); /*create a token using line copy*/ token = removeWhiteSpace(strtok( lineCopy, " \t\n" )); /*Checks to see if line is not blank or a comment and displays a message indicating what was on the line*/ if (token == NULL) { printf("empty line\n"); continue; } if(strncmp(token, "#", 1) == 0) { printf("comment\n"); continue; } /*Loop used to check if move is one of the following:*/ /*Forward, backward, turnleft, turnright, pause*/ for (counter = 0; counter < 5; counter++) { /*If the command is one of the following*/ if (strcmp(token, keywords[counter]) == 0) { /*Select function to execture based on counter and break out of loop*/ chooseMove(counter, atoi(removeWhiteSpace(strtok(NULL, " \t\n")))); break; } } /*Check to see if the command was to configure the light*/ if (strcmp(token, keywords[5]) == 0) /*Execute light function*/ light(removeWhiteSpace(strtok( NULL, " \t\n" ))) ; /*Check to see if the command was reset*/ if (strcmp(token, keywords[6]) == 0) /*Execute reset function*/ reset(); /*Check to see if the command was a loop*/ if (strcmp(token, keywords[7]) == 0) /*Adds appropriate number of lines to get current line number and execute loop function*/ lineCount += loop(line, atoi(removeWhiteSpace(strtok(NULL, " \t\n"))), inputFile); } /*If the move is not valid*/ else { /*isplay error message indicating the line number in the text file where the error was found*/ errorMessage(lineCount); /*Reset motors and LED and waits for user to press a key then exits*/ parallelput(0); getch(); return 0; } } /* Display the results */ /*If there are no more line in the file*/ /*Notify user that commands are finished execuing*/ printf("\nReached the end of the file.\nThe program will now exit.\n"); /*Reset motors and LED and waits for user to press a key then exits*/ parallelput(0); getch(); } /*If file does not exist*/ else { /*Notify user that file does not exist*/ printf("\nThe file does not exist.\nThe program will now exit.\n"); /*Wait for user to press a key then exits*/ getch(); } }
/*This method is used to perform the loop command*/ int loop(char line[], int iterations, FILE* inputFile) { /*This method is very similar to the main() function*/ /*The only difference is it exectues the instructions iterations number of times*/ /*In addition, this function returns the number of lines within the loop as the last iteration*/ /*does not alter current position within text file*/ /*Same as main() function*/ char lineCopy[100], *token; char keywords[7][14] = {"forward", "reverse", "turnright", "turnleft", "pause", "light", "reset"}; int counter, counter2; int lineCount = 0; /*Used to keep track of current position within text file*/ fpos_t currentPosition; /*Stores current position within text file so the location can be read from again*/ fgetpos (inputFile,¤tPosition); strcpy(lineCopy, line); /*Display message*/ printf("loop %d times\n", iterations); for (counter = 0; counter < iterations; counter++) { /*Display message*/ printf("\niteration #%d:\n", counter+1); /*Sets location within text file to line right after loop comand*/ fsetpos (inputFile,¤tPosition); /*random string used so token does not become null after each iteration*/ token = "reset token"; /*Executes commands like in main() function as long as command is not endloop*/ while (strcmp(token, "endloop") != 0) { fgets( line, 99, inputFile ); lineCount++; strcpy(lineCopy, line); toLowercase(lineCopy); token = removeWhiteSpace(strtok( lineCopy, " \t\n" )); if (token == NULL || strncmp(token, "#", 1) == 0) continue; for (counter2 = 0; counter2 < 5; counter2++) { if (strcmp(token, keywords[counter2]) == 0 && counter2 < 5) { chooseMove(counter2, atoi(removeWhiteSpace(strtok(NULL, " \t\n")))); break; } } if (strcmp(token, keywords[5]) == 0) light(removeWhiteSpace(strtok( NULL, " \t\n" ))) ; if (strcmp(token, keywords[6]) == 0) reset(); if (strcmp(token, keywords[7]) == 0) loop(line, atoi(removeWhiteSpace(strtok(NULL, " \t\n"))), inputFile); } } /*Display message*/ printf("\nend loop\n\n"); /*Returns number of line within the loop (including endloop statement)*/ return (lineCount / iterations); }
pair chooseMoveEvaluate (int* iboard, int player) { ASSERT(player == XPL || player == OPL); pair myBest = {0,-1}; pair reply; int i, move; int moveCount=0, moveList[9]; //default score if(player == US){ myBest.score = INT_MIN; } else { myBest.score = INT_MAX; } timesCalled++; pair temp; temp.score = 0; temp.move = -1; if(didHeWin(iboard,US)) { //printf("%c WON\n",US); //printf("=====returning{1}====================\n"); //printboard(iboard); //temp.score = 10+timesCalled; temp.score = evaluateBoard(iboard); return temp; } else if(didHeWin(iboard,THEM)) { //printf("%c WON\n",THEM); //printf("=====returning{-1}====================\n"); //printboard(iboard); //temp.score = -10-timesCalled; temp.score = evaluateBoard(iboard); return temp; } else if(isBoardFull(iboard)) { //printf("FULL\n"); //printf("=====returning{0}====================\n"); //temp.score = 0; return temp; } // get legal moves for(i = 0; i < 9; i++) { if( iboard[convert9To25[cellScoreOrdered[i]]] == EMPTY) { moveList[moveCount++] = convert9To25[cellScoreOrdered[i]]; } } // for(i = 0; i < 9; i++) { // if( iboard[convert9To25[i]] == EMPTY) { // moveList[moveCount++] = convert9To25[i]; // } // } // any legal move //myBest.move = pickValuedMove(moveList,moveCount); //printf("Move by %c;*********************************************\n",player); //for(i=0; i < moveCount; i++) { //printf("%d ", moveList[i]); //} //printf("\n"); for(i=0; i < moveCount; i++) { move = moveList[i]; iboard[move] = player; //printf("%c(to move(%d/%d) %d);best:%d ;reply:%d \n", // player,i+1,moveCount,move,myBest.score,reply.score); //printboard(iboard); reply = chooseMove(iboard, otherPlayer(player)); iboard[move] = EMPTY; //printf("RESET %c(to move(%d/%d) %d);best:%d ;reply:%d \n", // player,i+1,moveCount,move,myBest.score,reply.score); if( // maximizinf by selecting opponents less score (player == US && myBest.score < reply.score) || // minimizing by letting them select high score (player == THEM && myBest.score > reply.score)){ // printf("%c SCORE CHANGED (score:%d, best move:%d)!!\n", // player,reply.score,move); myBest.score = reply.score; myBest.move = move; } } return myBest; }