// Negamax cu alpha-beta // http://algopedia.ro/wiki/index.php/Note_de_curs,_clasele_9-10,_22_mai_2014#Alpha-beta int minimax(char board[TABLE_WIDTH][TABLE_WIDTH], char depth, int alpha, int beta, char player, char computerPlayer) { plyCounter++; int rc; int lin, col; int val; struct TableStatus status; getTableStatus(board, &status); if (depth == 0 || status.freeTiles == 0 || getTime() - programStart > STOP_PROGRAM) { rc = status.scores[player + 1] - status.scores[-player + 1]; //printf("%d\t", rc); return rc; } lin = 0; while (lin < TABLE_WIDTH && alpha < beta) { col = 0; while (col < TABLE_WIDTH && alpha < beta) { if (board[lin][col] == GOL) { board[lin][col] = player; val = -minimax(board, depth - 1, -beta, -alpha, -player, computerPlayer); alpha = max(alpha, val); board[lin][col] = GOL; } col++; } lin++; } rc = min(alpha, beta); return rc; }
int minimax(char board[TABLE_WIDTH][TABLE_WIDTH], char depth, char player) { struct TableStatus *status = getTableStatus(board); if( depth == 0 || status->freeTiles == 0 ) { return status->scores[player + 1] > status->scores[-player + 1] ? 1 : status->scores[-player + 1] > status->scores[player + 1] ? -1 : 0; } int scor = -INFINIT; int lin, col; for ( lin = 0; lin < TABLE_WIDTH; lin++ ) { for ( col = 0; col < TABLE_WIDTH; col++ ) { if ( board[lin][col] == GOL ) { board[lin][col] = player; int miniScor = -minimax(board, depth - 1, -player); scor = max(scor, miniScor); board[lin][col] = GOL; } } } return scor; }
int main() { FILE *fin, *fout; int lin, col; char board[TABLE_WIDTH][TABLE_WIDTH]; struct TableStatus *status; int moveLine, moveCol; int score, tempScore; if(D) { fin = freopen( "nexus.in", "r", stdin ); fout = freopen( "nexus.out", "w", stdout ); } for ( lin = 0; lin < TABLE_WIDTH; lin++ ) { for ( col = 0; col < TABLE_WIDTH; col++ ) { char c = fgetc( stdin ); char nr; switch ( c ) { case 'X': nr = X; break; case 'O': nr = O; break; case '+': nr = PLUS; break; default: nr = GOL; break; } board[lin][col] = nr; } fgetc( stdin ); } status = getTableStatus(board); thisPlayer = status->currentPlayer; moveLine = moveCol = -1; score = -INFINIT; for ( lin = 0; lin < TABLE_WIDTH; lin++ ) { for ( col = 0; col < TABLE_WIDTH; col++) { if ( board[lin][col] == GOL ) { tempScore = minimax(board, DEPTH, thisPlayer); if ( tempScore > score ) { score = tempScore; moveLine = lin; moveCol = col; } } } } board[moveLine][moveCol] = thisPlayer; for ( lin = 0; lin < TABLE_WIDTH; lin++ ) { for ( col = 0; col < TABLE_WIDTH; col++ ) { switch ( board[lin][col] ) { case X: fputc( 'X', stdout ); break; case O: fputc( 'O', stdout ); break; case PLUS: fputc( '+', stdout ); break; default: fputc( '-', stdout ); break; } } fputc( '\n', stdout ); } if(D) { fclose( fin ); fclose( fout ); } return 0; }
int main() { FILE *fin, *fout; int lin, col; char board[TABLE_WIDTH][TABLE_WIDTH]; struct TableStatus status; int moveLine, moveCol; int score, tempScore; // Pt. debug redeschidem stdin si stdout, ca sa nu complicam codul if(D) { fin = freopen( "nexus.in", "r", stdin ); fout = freopen( "nexus.out", "w", stdout ); } programStart = getTime(); // Asta este pusa dupa deschiderea fiserelor, deoarece timpul acela nu se pune // Citeste tabla dupa formatul nostru for ( lin = 0; lin < TABLE_WIDTH; lin++ ) { for ( col = 0; col < TABLE_WIDTH; col++ ) { char c = fgetc( stdin ); char nr; switch ( c ) { case 'X': nr = X; break; case 'O': nr = O; break; case '+': nr = PLUS; break; default: nr = GOL; break; } board[lin][col] = nr; } fgetc( stdin ); } // Ia informatii despre tabla memset(&status, 0, sizeof(status)); getTableStatus(board, &status); thisPlayer = status.currentPlayer; moveLine = moveCol = -1; // Nu mutam nicaieri, inca rebuildPositionalEval2Scores(board); score = -INFINIT; for ( lin = 0; lin < TABLE_WIDTH; lin++ ) { // Luam toate mutarile la rand for ( col = 0; col < TABLE_WIDTH; col++) { if ( board[lin][col] == GOL ) { board[lin][col] = thisPlayer; tempScore = (-minimax(board, DEPTH, -INFINIT, +INFINIT, -thisPlayer, thisPlayer) * 100 // Scorul material + + positionalEval2(lin, col, -thisPlayer)) * 100 // Potential de extindere al adversarului - - positionalEval2(lin, col, thisPlayer); // Potential de extindere al jucatorului board[lin][col] = GOL; if ( tempScore > score ) { score = tempScore; moveLine = lin; moveCol = col; } } } } board[moveLine][moveCol] = thisPlayer; // Muta //Afiseaza for ( lin = 0; lin < TABLE_WIDTH; lin++ ) { for ( col = 0; col < TABLE_WIDTH; col++ ) { switch ( board[lin][col] ) { case X: fputc( 'X', stdout ); break; case O: fputc( 'O', stdout ); break; case PLUS: fputc( '+', stdout ); break; default: fputc( '-', stdout ); break; } } fputc( '\n', stdout ); } if(D) { fprintf(stderr, "%lld\n%f", plyCounter, (double)(getTime() - programStart) / 1000000.0f); fclose( fin ); fclose( fout ); } return 0; }
int main(){ int bets = 0, pot = 0; Deck game_deck = get_new_Deck(); printf("\n\n"); hBorder(); printf("\n\n"); welcomeSign(); hBorder(); welcome(); printf("\n"); hBorder(); printf("\n"); printf("\n"); Player one = player_init(getName(), getRupies()); printf("Hi %s, you have %i rupies\n",one.name, one.rupies); Player two = player_init("Link", one.rupies); Player three = player_init("Mario", one.rupies); Player four = player_init("Taloon", one.rupies); getStatus(&two); getStatus(&three); getStatus(&four); printf("\n\n"); hBorder(); Player *table[NUM_PLAYERS]; table[0] = &one; table[1] = &two; table[2] = &three; table[3] = &four; int gamenumber = 1; int gamesplayed = 0; int gameStatus = 1; while(gamenumber == 1){ if(gameStatus == 1){ if(gamesplayed != 0){ printf("\n\n"); welcomeSign(); hBorder(); } gamesplayed++; printf("\n\nREADY -- Here we go...\n\n"); shuffle(&game_deck, DECK_SIZE); first_Deal(&game_deck, table, NUM_PLAYERS); gamescene(3, table); print_Hand(table, 1); isHand(one.hand); bets = betAmount(&one); printf("%s, bets %i rupies\n",one.name, bets); pot += bets; /* cpuBIGACTION(table, &bets, &game_deck); #pragma omp parallel num_threads(4) int z; tid = omp_get_thread_number(); for(z=1;z<NUM_PLAYERS;z++){ if(tid == 1){ */ pot += cpuActions(&game_deck, &two, &one, bets); /* } if(tid == 1){ */ printf("...hmm...yea...\n"); pot += cpuActions(&game_deck, &three, &one, bets); /* } if(tid == 1){ */ printf("...uhhh...ok...\n"); pot += cpuActions(&game_deck, &four, &one, bets); /* } }*/ hBorder(); banter(table); playersLeft(table, &gameStatus); } if(gameStatus ==1){ print_Hand(table, 1); print_Options(); isHand(one.hand); Deck MC_Deck = create_Monte(&one); monteSuggest(monte_Analysis(&MC_Deck, &one)); printTextReco(&one); newCards(&game_deck,&one); print_Hand(table, 1); isHand(one.hand); printf("\n\n"); bets = betAmount(&one); pot += bets; pot += cpuBets(&two, &one, bets); pot += cpuBets(&three, &one, bets); pot += cpuBets(&four, &one, bets); playersLeft(table, &gameStatus); } hBorder(); printResults(table); evaluateHands(table); tableCompareTo(table); winnerIs(table, pot); hBorder(); printf("\n"); banter(table); getTableStatus(table); gamenumber = playgame(); printf("\n\n\n\n"); bets = 0; pot = 0; tableRefresh(table); } return 0; }