int get_gameover() { int winner; GtkWidget *button; if (ggz_read_int(game.fd, &winner) < 0) return -1; // Check if it's ok if ((winner == BLACK && game.black <= game.white) || (winner == WHITE && game.white <= game.black) || (winner == EMPTY && game.black != game.white)) game_status ("Hey! Internal incompatibility! This game was cheated!\n"); if (winner == game.num) game_status("Congratulations! You win!"); else if (winner == -game.num) game_status("That's too bad... you lost!"); else game_status("That's a draw! Not bad!"); button = g_object_get_data(G_OBJECT(main_win), "again_button"); gtk_widget_show(button); return 1; }
int play_game( individual* p1, individual* p2 ) { board* b; play* next_play; play_list* pl; int x, player, result; cgp_net* net; b = create_board(); player = MAX; while( game_status( b ) == NOT_DONE ) { pl = create_possible_plays( b, player ); /* choose play */ if( player == MAX ) { net = p1->net; } else { net = p2->net; } /* this is combining too much into one function */ next_play = select_play( pl, player, net ); /* switch active player */ if( player == MAX ) { player = MIN; } else { player = MAX; } } result = game_status( b ); delete_board( b ); /* record wins / loss / draw */ return( result ); }
static void board_update() { // handle game end ASSERT(!XB->result); switch (game_status(Game)) { case PLAYING: break; case WHITE_MATES: xboard_send(XBoard,"1-0 {White mates}"); break; case BLACK_MATES: xboard_send(XBoard,"0-1 {Black mates}"); break; case STALEMATE: xboard_send(XBoard,"1/2-1/2 {Stalemate}"); break; case DRAW_MATERIAL: xboard_send(XBoard,"1/2-1/2 {Draw by insufficient material}"); break; case DRAW_FIFTY: xboard_send(XBoard,"1/2-1/2 {Draw by fifty-move rule}"); break; case DRAW_REPETITION: xboard_send(XBoard,"1/2-1/2 {Draw by repetition}"); break; default: ASSERT(false); break; } }
static bool ponder_move_is_ok(int move) { int status; board_t board[1]; ASSERT(move==MoveNone||move_is_ok(move)); // legal ponder move? if (move == MoveNone) return false; game_get_board(Game,board); if (!move_is_legal(move,board)) return false; // UCI-legal resulting position? game_add_move(Game,move); game_get_board(Game,board); status = game_status(Game); game_rem_move(Game); if (status != PLAYING) return false; // game ended if (option_get_bool("Book") && is_in_book(board)) { return false; } return true; }
static bool active() { // position state if (game_status(Game) != PLAYING) return false; // game ended // xboard state if (XB->analyse) return true; // analysing if (!State->computer[White] && !State->computer[Black]) return false; // force mode if (XB->new_hack || XB->result) return false; // unstarted or ended game return true; // playing }
void print_board( board* b ) { int x, y, square; for( y = 0; y < 3; y++ ) { for( x = 0; x < 3; x++ ) { square = b->state[x][y]; if ( square == BLANK ) { printf( "_" ); } else if( square == X ) { printf( "X" ); } else if( square == O ) { printf( "O" ); } else { printf( "?" ); } } printf( "\n" ); } int s = game_status( b ); switch( game_status( b ) ) { case X_WIN: printf( "X wins" ); break; case O_WIN: printf( "O wins" ); break; case DRAW: printf( "Draw" ); break; case NOT_DONE: printf( "Game not finished" ); break; default: printf( "Oops, I don't knwo what the game status is" ); } printf( "\n" ); }
static void play_again(GtkButton * button, gpointer user_data) { // Check if game is over if (game.state != RVR_STATE_DONE) { game_status("Game is not over yet"); return; } // Send to server ggz_write_int(game.fd, RVR_REQ_AGAIN); // Wait for time to start game_init(); gtk_widget_hide(GTK_WIDGET(button)); return; }
static gboolean handle_move(GtkWidget * widget, GdkEventButton * event, gpointer user_data) { int x = (int)(event->x); int y = (int)(event->y); int move, status = 0; x /= PIXSIZE; y /= PIXSIZE; x++; y++; move = CART(x, y); // Check if it's right time if (game.state != RVR_STATE_PLAYING) { game_status("Game hasn't started yet"); return FALSE; } // Check if it's his turn if (game.num != game.turn) { game_status("This is not your turn yet!"); return FALSE; } // Check if out of bounds if (x < 1 || x > 8 || y < 1 || y > 8) { game_status("Move out of bounds!"); return FALSE; } // Check if duplicated if (game.board[move] != EMPTY) { game_status("Invalid move!"); return FALSE; } /* CHECK IF THE MOVE IS VALID */ // Check if it's valid up status += game_check_move(game.turn, CART(x, y)); if (status <= 0) { game_status("Invalid move!"); return FALSE; } if (event->button == 1 && rvr_buf != NULL) { game_status("Moving on %d, %d", x, y); send_my_move(CART(x, y)); } return TRUE; }
int main(int argc, char** argv) { //connect to server, given as the first argument string usage = string(argv[0]) + " <server> <port> [game #]"; if(argc < 3) { cerr << usage << endl; return 0; } bool end_game = false; bool start_game = true; if(argc == 4) { // they specified a game number, // do something different? start_game = false; //cout << "Trying to joing game #" << argv[3] << endl; } // we got enough command-line arguments, // start tying to connect: int sock_server = open_server_connection(argv[1], argv[2]); if(sock_server == -1) { return 0; } // create an instance of the player's AI class myAI gameAI; string comm_buffer; sexp_t* sexp; // log into the server, using the AI's team name string whos_turn; my_user_id = login_to_server(sock_server, gameAI.PlayerName() ); // setup the logging filename ostringstream stream; tm* myTime; time_t theTime = time(NULL); myTime = localtime(&theTime); stream << myTime->tm_hour << "." << myTime->tm_min; string name = string(argv[0]); for(int i=name.size()-1; i >=0; i--) { if(name[i] == ' ' || name[i] == '/' || name[i] == '\\' || name[i] == '.' || name[i] == ':') { name.erase(i,1); } } string log_filename = stream.str() + "-" + name + string(".gamelog"); cout << "Filename: " << log_filename << endl; #ifdef DEBUG cout << "DEBUG: logged in as user #" << my_user_id << endl; #endif // okay, we've succesfully logged in, // now start or join a game if(start_game) { // create a new game: string game_number = create_game(sock_server); gameAI.playerNumber = 1; gameAI.myBase = coordinate(0,0); gameAI.enemyBase = coordinate(25,25); } else { gameAI.playerNumber = 2; gameAI.myBase = coordinate(0,0); gameAI.enemyBase = coordinate(25,25); // join an existing game cout << "Trying to join game " << argv[3] << "..." << endl; string join_string = "(join-game " + string(argv[3]) + ")"; send_string(sock_server, join_string); sexp = rec_sexp(sock_server); if(!check_sexp(sexp, "join-accepted")) { cout << "Unabled to join game " << argv[3] << endl; goto GAME_SHUTDOWN; } // we should only try to start the game if we're the // player that just joined; otherwise, the server // will sorta freak out... send_string(sock_server, "(game-start)"); } // the server is going to spit a ton of SEXP's at us; // wade through them until we get a "new-turn" msg. sexp = rec_sexp(sock_server); while(!check_sexp(sexp, "new-turn")) { // okay, what kind of data have we gotten from the server, // and what are we supposed to do about it? if(check_sexp(sexp, "game-start-accepted")) { // the server accepted our reqeust to begin the game; // we don't care, because earlier we just assumed // that it worked :| } if(check_sexp(sexp, "unit-types")){ // this should be sent only during this initialization: // it contains the details of each type of unit #ifdef DEBUG cout << "DEBUG: updating unit types" << endl; #endif update_unit_types((myAI*)&gameAI, sexp); } if(check_sexp(sexp, "status")) { // An actual game-status message: this might not // mean much right now, but try to parse it anyway. // Don't bail if it fails, but warn us. if(!game_status(gameAI, sexp, my_user_id)) { cout << "WARN: error parsing game status" << endl; } } // get another S-expression to test: sexp = rec_sexp(sock_server); } // okay, now sexp should contain a "new-turn" message // pull out who's turn it is, then start the main game loop whos_turn = string(sexp->list->next->val); #ifdef DEBUG cout << "DEBUG: got all our pregame stuff, starting main loop!" << endl; #endif while(!end_game) { // ask the server for an update: //send_string(sock_server, "(game-status)"); //sexp = rec_sexp(sock_server); // we're not guaranteed to get a "status" message, // so handle anything unexpected before we get to // the main game loop while(!check_sexp(sexp, "status")) { #ifdef DEBUG cout << "DEBUG: waiting for game status, got \"" << sexp->list->val << "\" message" << endl; #endif if(check_sexp(sexp, "game-over")) { cout << "Game Over: "; string winner = string( sexp->list->next->val ); if( winner == my_user_id ) cout << "You Win!"; else cout << "You Lose..."; cout << endl; end_game = true; goto GAME_SHUTDOWN; } if(check_sexp(sexp, "new-turn")) { whos_turn = string(sexp->list->next->val); #ifdef DEBUG cout << "DEBUG: It's player " << whos_turn << "'s turn" << endl; #endif } sexp = rec_sexp(sock_server); } if(!check_sexp(sexp, "status")) { cerr << "WARN: expected 'status', got '" << sexp->list->val << "'" << endl; } if(!game_status(gameAI, sexp, my_user_id)) { cerr << "WARN: couldn't parse game status!" << endl; } // update the turn number gameAI.turnNumber = atoi( sexp->list->next->list->val); if(whos_turn == my_user_id) { //cout << endl << "Starting my turn... "; // 4a. Run their custom 'Play()' function gameAI.Play(); // 4b. Send their orders to the server while( gameAI.orders.size() != 0){ string myOrder = gameAI.orders.front().s_expression; gameAI.orders.pop(); // send this command to the server send_string(sock_server, myOrder); // there's no need to listen for an incoming message; // we assume errors will be handled at the beginning // of the next loop. } // let the server know we've finished out turn send_string(sock_server, "(end-turn)"); //cout << "done with my turn" << endl; } else { // it's not your turn, so take it easy for a second //cout << "Not my turn, waiting...." << endl; #ifndef WIN32 // the usleep function insn't avaible in Windows //usleep(100000); // this helps prevent climbing to 100% CPU usage #endif } // get another sexp to jump-start the next iteration of the loop sexp = rec_sexp(sock_server); } GAME_SHUTDOWN: cout << "Shutting down..." << endl; send_string(sock_server, "(leave-game)"); sexp = rec_sexp(sock_server); send_string(sock_server, "(logout)"); sexp = rec_sexp(sock_server); //End of game #ifdef WIN32 closesocket(sock_server); #else close(sock_server); #endif log_file.open(log_filename.c_str()); if(log_file.fail()) { cout << "Error writing to log file" << endl; } else { log_file << log_stream.str(); log_file.close(); } return 0; }
void display_board(void) { int i, x, y; GtkWidget *tmp; GdkPixbuf *piece = NULL; GtkStyle *style; GtkWidget *white_label; GtkWidget *black_label; char score[29]; tmp = g_object_get_data(G_OBJECT(main_win), "drawingarea"); style = gtk_widget_get_style(main_win); draw_bg(main_win); for (i = 0; i < 64; i++) { if (game.board[i] == BLACK) { piece = blackpiece; } else if (game.board[i] == WHITE) { piece = whitepiece; } else if (game_check_move(game.turn, i)) { if (game.turn == game.num) { piece = mydot; } else if (game.turn == -game.num) { piece = enemydot; } else { continue; } } else { continue; } x = (X(i) - 1) * PIXSIZE; y = (Y(i) - 1) * PIXSIZE; if (i == game.last_move) { /* if last move, mark it (draw using a different * background). */ gdk_draw_rectangle(rvr_buf, last_gc, TRUE, x + 1, y + 1, PIXSIZE - 2, PIXSIZE - 2); } gdk_draw_pixbuf(rvr_buf, pix_gc, piece, 0, 0, x + 1, y + 1, PIXSIZE - 2, PIXSIZE - 2, GDK_RGB_DITHER_NONE, 0, 0); } if (game.state == RVR_STATE_PLAYING && game.turn == game.num) game_status("It's your turn!"); else if (game.state == RVR_STATE_PLAYING && game.turn == -game.num) game_status("Wait for your oponnents turn"); // Update the scores white_label = g_object_get_data(G_OBJECT(main_win), "white_label"); black_label = g_object_get_data(G_OBJECT(main_win), "black_label"); sprintf(score, "White(%s): %d", game.names[PLAYER2SEAT(WHITE)], game.white); gtk_label_set_text(GTK_LABEL(white_label), score); sprintf(score, "Black(%s): %d", game.names[PLAYER2SEAT(BLACK)], game.black); gtk_label_set_text(GTK_LABEL(black_label), score); gtk_widget_queue_draw(tmp); }
static void comp_move(int move) { board_t board[1]; char string[256]; ASSERT(move_is_ok(move)); ASSERT(State->state==THINK); ASSERT(!XB->analyse); if(option_get_bool("RepeatPV")==true) send_pv(); // to update time and nodes // send the move game_get_board(Game,board); if (move_is_castle(move,board) && option_get_bool("Chess960")) { if (!move_to_san(move,board,string,sizeof(string))) my_fatal("comp_move(): move_to_san() failed\n"); // O-O/O-O-O } else { if (!move_to_can(move,board,string,sizeof(string))) my_fatal("comp_move(): move_to_can() failed\n"); } move_step(move); //game ended? if(game_status(Game)!= PLAYING){ //handle ics drawing stuff if(XB->ics){ switch (game_status(Game)){ case DRAW_MATERIAL: case DRAW_FIFTY: case DRAW_REPETITION: xboard_send(XBoard,"offer draw"); break; default: break; } } xboard_send(XBoard,"move %s",string); board_update(); no_mess(move); return; } // engine sended a move while in ponder mode? if(State->state==PONDER){ if(board->turn==White) xboard_send(XBoard,"0-1 {polyglot : engine moves while pondering}\n"); else xboard_send(XBoard,"1-0 {polyglot : engine moves while pondering}\n"); } // resign? if (option_get_bool("Resign") && Uci->root_move_nb > 1) { int best = Uci->best_score; if (option_get_bool("ScoreWhite") && colour_is_black(Uci->board->turn)) best = -best; if (best <= -abs(option_get_int("ResignScore"))) { State->resign_nb++; my_log("POLYGLOT %d move%s with resign score\n",State->resign_nb,(State->resign_nb>1)?"s":""); if (State->resign_nb >= option_get_int("ResignMoves")) { my_log("POLYGLOT *** RESIGN ***\n"); //send move and resign //xboard_send(XBoard,"move %s \nresign",string); //just resign xboard_send(XBoard,"resign",string); no_mess(move); return; } } else { if (State->resign_nb > 0) my_log("POLYGLOT resign reset (State->resign_nb=%d)\n",State->resign_nb); State->resign_nb = 0; } } no_mess(move); xboard_send(XBoard,"move %s",string); }