void load(struct gameinfo *gi, char *filename) { unsigned int i, l, r_size; FILE *f = fopen(filename, "rb"); //open file if (f) { fscanf(f, "%d", &r_size); //read board size newgame(gi, r_size); //start a new game gi->gs->blackcount = 0; gi->gs->whitecount = 0; for (i = 0; i<r_size; i++) for (l = 0; l<r_size; l++) switch (fgetc(f)) {//read and count pieces case 'b': gi->gs->blackcount++; gi->gs->board[l][i] = 'B'; break; case 'w': gi->gs->whitecount++; gi->gs->board[l][i] = 'W'; break; default: gi->gs->board[l][i] = ' '; } gi->gs->nowplaying = fgetc(f); //get current player fclose(f); findlegal(gi); showstate(gi); } else printf("Unable to open file %s\n", filename); }
void showstates( void ) { int i; for( i = 0; i < nstate; ++i ) { printf( "\n" ); showstate( statetab[i] ); } }
void cont(struct gameinfo *gi) { char *move; if (gi->gamestarted != 1) printf("No game in progress\n"); else if (gi->gs->nowplaying == gi->playercolor) printf("It's your turn to play\n"); else { move = find_move(gi); play(gi, move); showstate(gi); free(move); } }
void undo(struct gameinfo *gi) { unsigned int i; struct gamestate *prevstate; if (gi->gamestarted == 0) printf("No game in progress\n"); else if (gi->gs->prev == 0) printf("No previous states found\n\n"); else { do { prevstate = gi->gs->prev; //go to previous state for (i = 0; i<gi->r_size; i++) free(gi->gs->board[i]); free(gi->gs->board); free(gi->gs); //free current state memory gi->gs = prevstate; } while (gi->gs->prev != 0 && gi->gs->nowplaying != gi->playercolor); //while there is a previous state and it isn't the player's turn showstate(gi); } }
void parse_command(char *buf, struct gameinfo *gi) { char *tok, *param; int i_param; tok = strtok(buf, " "); //split input if (tok != 0) { if (strcmp(tok, "newgame") == 0) { param = strtok(0, " "); //get parameter if there is one //call appropriate functions if (param == 0) { newgame(gi, gi->default_size); findlegal(gi); showstate(gi); } else { if (isnum(param)) { i_param = atoi(param); if (i_param >= 4 && i_param <= 26 && i_param%2 == 0) { newgame(gi, i_param); findlegal(gi); showstate(gi); } else printf("Invalid size for command newgame (4 <= size <= 26, size is an even number)\n"); } else printf("Invalid parameter for command newgame (parameter is an integer)\n"); } } else if (strcmp(tok, "play") == 0) { param = strtok(0, " "); if (param == 0) printf("Missing parameter for command play\n"); else if (gi->gamestarted == 0) printf("No game in progress\n"); else if (gi->gs->nowplaying != gi->playercolor) printf("It's not your turn to play\n"); else if (play(gi, param) == 1) showstate(gi); //if it's a valid move } else if (strcmp(tok, "cont") == 0) cont(gi); else if (strcmp(tok, "undo") == 0) undo(gi); else if (strcmp(tok, "suggest") == 0) suggest(gi); else if (strcmp(tok, "showstate") == 0) showstate(gi); else if (strcmp(tok, "save") == 0) { param = strtok(0, " "); if (param == 0) printf("Missing parameter for command save\n"); else if(gi->gamestarted == 0) printf("No game in progress\n"); else save(gi, param); } else if (strcmp(tok, "load") == 0) { param = strtok(0, " "); if (param == 0) printf("Missing parameter for command load\n"); else load(gi, param); } else if (strcmp(tok, "selectcolor") == 0) { param = strtok(0, " "); if (param == 0) printf("Missing parameter for command selectcolor\n"); else { if (strcmp(param, "black") == 0) gi->playercolor = 'B'; else if (strcmp(param, "white") == 0) gi->playercolor = 'W'; else printf("Invalid parameter for command selectcolor (parameter is 'black' or 'white')\n"); } } else if (strcmp(tok, "showlegal") == 0) { param = strtok(0, " "); if (param == 0) printf("Missing parameter for command showlegal\n"); else { if (strcmp(param, "on") == 0) gi->showlegal = 1; else if (strcmp(param, "off") == 0) gi->showlegal = 0; else printf("Invalid parameter for command showlegal (parameter is 'on' or 'off')\n"); } } else if (strcmp(tok, "level") == 0) { param = strtok(0, " "); if (param == 0) printf("Current difficulty level: %d\n", gi->difficulty); else { if (isnum(param)) { i_param = atoi(param); if (i_param >= 1) gi->difficulty = i_param; else printf("Invalid parameter for command level (level >= 1)\n"); } else printf("Invalid parameter for command level (parameter is an integer)\n"); } } else if (strcmp(tok, "help") == 0) { printf("Available commands:\n" "newgame [<size>] - Start a new game, optional parameter size (default %d)\n" "play <move> - Place a marker at the specified position\n" "cont - Allow the computer to play\n" "undo - Undo your last move\n" "suggest - Ask the computer to suggest a move\n" "selectcolor <black|white> - Select your color\n" "showlegal <on|off> - Show all possible moves\n" "level [<difficulty>] - Select a difficulty (difficulty >= 1) - If no parameter, show the current difficulty\n" "save <filename> - Save the current game in a file\n" "load <filename> - Load game from a file\n" "showstate - Show the current state of the game\n" "quit - Exit the game\n" "help - Show the command list\n\n" , gi->default_size); } else printf("Unknown command %s (type help for a list of commands)\n", tok); } }
int main(int argc, char** argv) { int c; int errflg = 0, fflg = 0; char *ifile; extern char *optarg; extern int optind, optopt; ai_t ai = eval1_iter_ai; // the default ai, if none is specified int depth_cutoff = 30; // the default search depth cut-off turn_t ai_turn = white; // the default first turn is white /* parse command line args */ while ((c = getopt(argc, argv, "wbh12345d:f:")) != -1) { switch(c) { case 'w': //AI white ai_turn = white; printf("AI white - human black\n"); break; case 'b': //AI black ai_turn = black; printf("human white - AI black\n"); break; case '1': // AI 1 - minimax ai = minimax_ai; break; case '2': // AI 2 - alpha-beta ai = alphabeta_ai; break; case '3': // AI 3 - iterative deepening alpha-beta ai = ab_iter_ai; break; case '4': // AI 4 - eval function 1 ai = eval1_ai; break; case '5': // AI 5 - eval function 2 ai = eval1_iter_ai; break; case 'h': //print help errflg++; break; case 'd': //depth cutoff depth_cutoff = atoi(optarg); if(depth_cutoff == 0) { fprintf(stderr, "depth cut-off must be a non-zero integer\n"); errflg++; } break; case 'f': //input file fflg++; ifile = optarg; break; case ':': /* -f or -o without operand */ fprintf(stderr, "Option -%c requires an operand\n", optopt); errflg++; break; case '?': fprintf(stderr, "Unrecognized option: -%c\n", optopt); errflg++; } } if (errflg) { printhelp(); exit(2); } //if no input file set, set to default if (fflg == 0) { ifile = DEFAULT_START; } //load starting position if(parsefile(ifile) != 0) exit(1); // show the loaded state printf("starting position:\n"); showstate(white_bits, black_bits); int result = 0; // check who starts if(ai_turn == white) { // AI starts printf("\nAI is white, AI starts. Press any key to start AI turn."); getchar(); // wait for key to be pressed result = play_ai_turn(ai_turn, ai, &bitboard_white, bitboard_black, white_bits, black_bits, depth_cutoff); while(result == 0) { int bytes_read; int nbytes = 5; char *user_input = (char *) malloc (6); int inputerr = 1; int user_x, user_y, user_dir; printf("\nYour turn to move black:\n"); while(inputerr != 0) { inputerr = 0; bytes_read = getline(&user_input, (size_t*)&nbytes, stdin); if(bytes_read == -1) inputerr++; user_x = (int)user_input[0] - 48; if(user_x < 1 || user_x > 7) inputerr++; user_y = (int)user_input[1] - 48; if(user_y < 1 || user_y > 7) inputerr++; switch(user_input[2]) { case 'N': user_dir = N; break; case 'E': user_dir = E; break; case 'W': user_dir = W; break; case 'S': user_dir = S; break; default: inputerr++; } if(inputerr) { printf("\nerror in input, try again:\n"); } else { user_x = user_x - 1; user_y = user_y - 1; int bit_number = 8*user_y + user_x; uint64_t piece = (uint64_t)1 << bit_number; // make sure its a valid piece if(piece & bitboard_black) { int i = 0; while(piece != black_bits[i]){ i++; } inputerr = trydir(user_dir, &piece, &bitboard_black, bitboard_white); if(inputerr == 0) { black_bits[i] = piece; } } else { inputerr++; } if(inputerr) printf("invalid move, try again:\n"); } } showstate(white_bits, black_bits); printf("\nAI turn\n"); result = play_ai_turn(ai_turn, ai, &bitboard_white, bitboard_black, white_bits, black_bits, depth_cutoff); } } else { // human starts, AI is black printf("AI is black, human starts. Enter the first move to start."); while(result == 0) { int bytes_read; int nbytes = 5; char *user_input = (char *) malloc (nbytes + 1); int inputerr = 1; int user_x, user_y, user_dir; printf("\nYour turn to move white:\n"); while(inputerr != 0) { inputerr = 0; bytes_read = getline(&user_input, (size_t*)&nbytes, stdin); if(bytes_read == -1) inputerr++; user_x = (int)user_input[0] - 48; if(user_x < 1 || user_x > 7) inputerr++; user_y = (int)user_input[1] - 48; if(user_y < 1 || user_y > 7) inputerr++; switch(user_input[2]) { case 'N': user_dir = N; break; case 'E': user_dir = E; break; case 'W': user_dir = W; break; case 'S': user_dir = S; break; default: inputerr++; } if(inputerr) { printf("\nerror in input, try again:\n"); } else { user_x = user_x - 1; user_y = user_y - 1; int bit_number = 8*user_y + user_x; uint64_t piece = (uint64_t)1 << bit_number; // make sure its a valid piece if(piece & bitboard_white) { int i = 0; while(piece != white_bits[i]){ i++; } inputerr = trydir(user_dir, &piece, &bitboard_white, bitboard_black); if(inputerr == 0) { white_bits[i] = piece; } } else { inputerr++; } if(inputerr) printf("invalid move, try again:\n"); } } showstate(white_bits, black_bits); printf("\nAI turn\n"); result = play_ai_turn(ai_turn, ai, &bitboard_black, bitboard_white, black_bits, white_bits, depth_cutoff); } } if(result == 1) { printf("\nAI wins!\n"); } else { printf("\nThe winner is YOU!\n"); } getchar(); return 0; }
static void printtrace(regcontext_t *REGS, char *buf) { static int first = 1; #if BIG_DEBUG u_char *addr = (u_char *)MAKEPTR(R_CS, R_IP); #endif if (first) { fprintf(debugf, "%4s:%4s " #if BIG_DEBUG ".. .. .. .. .. .. " #endif "%-30s " "%4s %4s %4s %4s %4s %4s %4s %4s %4s %4s %4s\n", "CS", "IP", "instruction", "AX", "BX", "CX", "DX", "DI", "SI", "SP", "BP", "SS", "DS", "ES"); first = 0; } fprintf(debugf, "%04x:%04x " #if BIG_DEBUG "%02x %02x %02x %02x %02x %02x " #endif "%-30s " "%04x %04x %04x %04x %04x %04x %04x %04x %04x %04x %04x ", R_CS, R_IP, #if BIG_DEBUG addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], #endif buf, R_AX, R_BX, R_CX, R_DX, R_DI, R_SI, R_SP, R_BP, R_SS, R_DS, R_ES); #if 0 fprintf(debugf, "%04x %04x %04x %04x ", ((u_short *)VECPTR(0x0D760FCA-14))[0], ((u_short *)VECPTR(0x0D760FCA-14))[1], ((u_short *)VECPTR(0x0D760F7A+8))[0], ((u_short *)VECPTR(0x0D760F7A+8))[1]); #endif showstate(R_EFLAGS, PSL_C, 'C'); showstate(R_EFLAGS, PSL_PF, 'P'); showstate(R_EFLAGS, PSL_AF, 'c'); showstate(R_EFLAGS, PSL_Z, 'Z'); showstate(R_EFLAGS, PSL_N, 'N'); showstate(R_EFLAGS, PSL_T, 'T'); showstate(R_EFLAGS, PSL_I, 'I'); showstate(R_EFLAGS, PSL_D, 'D'); showstate(R_EFLAGS, PSL_V, 'V'); showstate(R_EFLAGS, PSL_NT, 'n'); showstate(R_EFLAGS, PSL_RF, 'r'); showstate(R_EFLAGS, PSL_VM, 'v'); showstate(R_EFLAGS, PSL_AC, 'a'); showstate(R_EFLAGS, PSL_VIF, 'i'); showstate(R_EFLAGS, PSL_VIP, 'p'); putc('\n', debugf); }