/** * \brief Main entry of example application */ int main(void) { /* Variable to store the last winner */ uint8_t winner; system_init(); delay_init(); gfx_mono_init(); init_buttons(); init_display(); /* Start game */ while (true) { winner = 0; /* Wait for button interaction */ while (get_button() == BUTTON_NONE) { } /* Draw empty board */ setup_board(); /* Start playing */ for (int i = 0; i < 5; i++) { /* User's turn */ user_turn(); /* Check if the game is over */ winner = we_have_a_winner(); if (winner || i == 4) { break; } /* Add a delay for the opponent to "think" */ delay_ms(500); /* Opponent's turn */ opponent_turn(); /* Check if the game is over */ winner = we_have_a_winner(); if (winner) { break; } } /* Game over, print winner and get ready for restart */ if (winner == 1) { /* User won */ gfx_mono_draw_string("You won!", STRING_X, 0, &sysfont); wins++; } else if (winner == 2) { gfx_mono_draw_string("You lost!", STRING_X, 0, &sysfont); } else { gfx_mono_draw_string("No winner!", STRING_X, 0, &sysfont); } gfx_mono_draw_string("Press a button", STRING_X, SQUARE3_Y, &sysfont); games++; } }
int fehn2board (char str[]) { char *fstring; int z=0; int i=0; int j=0; int number=0; int PL=0; fstring = strtok(str, " "); fstring = strtok(NULL, " "); //read boardmap section. setup_board(0); printf("%s /%i\n",fstring, strlen(fstring)); for (z=0;z<strlen(fstring);z++) { number = fstring[z]-'0'; if (is_in(fstring[z],pieces[0],6)||is_in(fstring[z],pieces[1],6)) { board.squares[i][j] = fstring[z]; j++; } if (fstring[z]=='/') { i++; j=0; } if (0<number && number<9) j=j+number; } fstring = strtok(NULL, " "); //read active player section. //if (fstring == 'b') fstring = strtok(NULL, " "); //read castling righst section. for (i=0;i<2;i++) for (j=0;j<3;j++) { board.castle[i][j]=0; board.castle[i][1]=1; } for (z=0;z<strlen(fstring);z++) { if (fstring[z] == 'Q') board.castle[0][0]=1; if (fstring[z] == 'K') board.castle[0][1]=1; if (fstring[z] == 'q') board.castle[1][0]=1; if (fstring[z] == 'k') board.castle[1][1]=1; } return PL; }
int main(){ setup(); Board board; setup_board(&board); play(&board); return 0; }
/** * Computer vs computer * * The computer plays itself * @param string for filename * @param agent_color * @return 1 if black wins, else return 0 */ int computer_vs_computer( char *file, char agent_color ) { char board[ SIZE ][ SIZE ]; /* set up board */ setup_board(file,board); /* create a new state */ struct State * state = new_state( board, 'B' ); struct State * temp_state; /* start game */ print_state( state ); temp_state = computer_player_first( state ); Free( state, sizeof( struct State ) ); state = temp_state; /* second move */ printf( "\n" ); print_state( state ); temp_state = computer_player_second( state ); Free( state, sizeof( struct State ) ); state = temp_state; /* regular game */ for( ;; ) { printf( "\n" ); print_state( state ); //printf( "\n>> Mem usage before: %lu\n", memory_usage() ); temp_state = computer_player( state ); Free( state, sizeof( struct State ) ); state = temp_state; //printf( "\n>> Mem usage after: %lu\n", memory_usage() ); if( terminal_test( state ) == 1 ) { printf( "\n" ); print_state( state ); printf( "\nNo moves left!... \n" ); printf( "\n%c wins!!!\n", opposite_player( state->player ) ); Free( state, sizeof( struct State ) ); break; } } return opposite_player( state->player ) == 'B'; }
void main(void) { setup_board(); setup_scheduler(); // adding task to the scheduler SCH_add_task(&Read_T, 0, 100); SCH_add_task(&check_buttons, 50, 500); SCH_add_task(&LED_result, 100, 50); start_50ms_timer(); //supper loop while(1) { SCH_Dispatcher_Tasks(); } }
void build_response(battleship *game, message *msg_in, message *msg_out) { unsigned type = get_message_type(msg_in); int playerID = get_player_id(msg_in); switch(game->state) { case WAITING: vprintf("game->state: WAITING\n"); switch(type) { case JOIN: if(game->sync == 2){ vprintf("game is already full!\n"); } if((playerID = get_player_id(msg_in)) != 0){ if(game->sync){ game->sync = 0; game->p2.uid = playerID; }else{ game->sync = 1; game->p1.uid = playerID; } } vprintf("I want to join!\n"); msg_out->buf[0] = 1 - game->sync; msg_out->len = 1; break; case POLL: vprintf("got a poll!\n"); playerID = get_player_id(msg_in); if(game->p1.uid == playerID){ msg_out->buf[0] = 1 - game->sync; msg_out->len = 1; if(!game->sync){ vprintf("going to SETUP state\n"); game->state = SETUP; game->turn = rand() % 2; } } break; default: vprintf("Something else.\n"); strncpy(msg_out->buf,"you are not welcome!",22); msg_out->len = strlen(msg_out->buf); break; } break; case SETUP: vprintf("game->state: SETUP\n"); switch(type){ case INIT: { board *new_board; playerID = get_player_id(msg_in); if(playerID == 0){ vprintf("bad packet\n"); return; }else if(playerID == game->p1.uid){ if(!game->p1_board.setup){ vprintf("got init from p1\n"); new_board = &(game->p1_board); }else{ vprintf("p1: no re-initialization!\n"); return; } }else if(playerID == game->p2.uid){ if(!game->p2_board.setup){ vprintf("got init from p2\n"); new_board = &(game->p2_board); }else{ vprintf("p2: no re-initialization!\n"); return; } }else{ vprintf("unknown uid\n"); return; } int error; if(error = setup_board(msg_in,new_board)){ vprintf("error making board!\n"); }else{ vprintf("board successfully created!\n"); int is_player1 = game->p1.uid == playerID; game->sync = !game->sync; msg_out->buf[0] = 1 - game->sync; msg_out->buf[1] = (game->turn - is_player1) == 0; msg_out->len = 2; } int i,j; char *icons = "~#@"; for(i=0;i<BOARD_LEN;i++){ for(j=0;j<BOARD_LEN;j++){ vprintf("%c",icons[new_board->ships[i][j]]); } vprintf("\n"); } } break; case POLL: vprintf("got a poll!\n"); playerID = get_player_id(msg_in); if(game->p1.uid == playerID || game->p2.uid == playerID){ int is_player1 = game->p1.uid == playerID; msg_out->buf[0] = 1 - game->sync; msg_out->buf[1] = (game->turn - is_player1) == 0; msg_out->len = 2; if(!game->sync){ vprintf("going to PLAY state\n"); game->state = PLAY; } } break; default: vprintf("invalid packet type\n"); } break; case PLAY: switch(type){ case MOVE: { player p = (game->turn)?game->p1:game->p2; player other_p = (!(game->turn))?game->p1:game->p2; board *bd = (game->turn)?&(game->p1_board):&(game->p2_board); board *other_bd = (!(game->turn))?&(game->p1_board):&(game->p2_board); playerID = get_player_id(msg_in); if(playerID == p.uid){ vprintf("got a move from player %d\n",1 + game->sync); int x, y; x = msg_in->buf[6]; y = msg_in->buf[7]; if(bd->guesses[x][y] == NOTHING){ int hit = other_bd->ships[y][x] == SHIP; bd->guesses[x][y] = 1 + hit; other_bd->hits += hit; msg_out->buf[0] = other_bd->hits == 17; msg_out->buf[1] = hit; game->turn = !game->turn; game->last_guess_x = x; game->last_guess_y = y; }else{ msg_out->buf[0] = 0; msg_out->buf[1] = -1; } msg_out->len = 2; int i,j; char *icons = "~#@"; for(i=0;i<BOARD_LEN;i++){ for(j=0;j<BOARD_LEN;j++){ vprintf("%c",icons[game->p1_board.ships[i][j]]); } vprintf(" "); for(j=0;j<BOARD_LEN;j++){ vprintf("%c",icons[game->p2_board.ships[i][j]]); } vprintf("\n"); } }else if(playerID == other_p.uid){ vprintf("got an out of turn move from player %d\n",1+game->turn); msg_out->buf[0] = -1; msg_out->len = 1; }else{ vprintf("invalid uid\n"); } break; } case POLL: playerID = get_player_id(msg_in); int done; if(playerID == game->p1.uid){ vprintf("got a poll from player 1\n"); done = game->p1_board.hits == 17; if(done) game->state = WAITING; msg_out->buf[0] = done; msg_out->buf[1] = game->turn; msg_out->buf[2] = game->last_guess_x; msg_out->buf[3] = game->last_guess_y; msg_out->len = 4; }else if(playerID == game->p2.uid){ vprintf("got a poll from player 2\n"); done = game->p2_board.hits == 17; if(done) game->state = WAITING; msg_out->buf[0] = done; msg_out->buf[1] = !game->turn; msg_out->buf[2] = game->last_guess_x; msg_out->buf[3] = game->last_guess_y; msg_out->len = 4; }else{ vprintf("invalid uid\n"); } if(msg_out->buf[0]){ memset(game,'\0',sizeof(*game)); game->state = WAITING; } break; default: break; } break; case FINAL: break; default: break; } }
/** * Human vs compter * * Plays vs the computer * @param string for filename * @param agent_color the agent color * @return 1 if human player won, else return 0 */ int human_vs_computer( char *file, char agent_color ) { char board[ SIZE ][ SIZE ]; char player = toupper( agent_color ); /* set up board */ setup_board(file,board); /* create a new state */ struct State * state = new_state( board, 'B' ); print_state( state ); /* START GAME */ /* start game ( B first ) */ struct State * temp_state; if( player == state->player ) { temp_state = computer_player_first( state ); Free( state, sizeof( struct State ) ); state = temp_state; } else { temp_state = human_player_first( state ); Free( state, sizeof( struct State ) ); state = temp_state; } /* second move */ printf( "\n" ); print_state( state ); if( player == state->player ) { temp_state = computer_player_second( state ); Free( state, sizeof( struct State ) ); state = temp_state; } else { temp_state = human_player_second( state ); Free( state, sizeof( struct State ) ); state = temp_state; } /* regular game */ for( ;; ) { printf( "\n" ); print_state( state ); if( state->player == player ) { temp_state = computer_player( state ); Free( state, sizeof( struct State ) ); state = temp_state; } else { temp_state = human_player( state ); Free( state, sizeof( struct State ) ); state = temp_state; } if( terminal_test( state ) == 1 ) { printf( "\n\n%c wins!!!\n", opposite_player( state->player ) ); break; } } return player == opposite_player( state->player ); }
int human_vs_computer( char *file ) { char input[ INPUT_SIZE ]; char board[ SIZE ][ SIZE ]; char player; /* set up board */ /*int _player = 0; for( int i = 0; i < 8 ; i++ ) { for( int j = 0; j < 8; j++ ) { if( _player == 0 ) { board[i][j] = 'B'; _player = 1; } else { board[i][j] = 'W'; _player = 0; } } if( _player == 0 ) _player = 1; else _player = 0; }*/ setup_board(file,board); /* choose player */ do { printf( "Please choose a player ('B' or 'W' ): " ); fgets( input, INPUT_SIZE, stdin ); } while( input[ 0 ] != 'W' && input[ 0 ] != 'B' ); player = input[0]; /* create a new state */ struct State * state = new_state( board, 'B' ); print_state( state ); /* START GAME */ /* start game ( B first ) */ struct State * temp_state; if( player == 'B' ) { temp_state = human_player_first( state ); Free( state, sizeof( struct State ) ); state = temp_state; } else { temp_state = computer_player_first( state ); Free( state, sizeof( struct State ) ); state = temp_state; } /* second move */ printf( "\n" ); print_state( state ); if( player == 'W' ) { temp_state = human_player_second( state ); Free( state, sizeof( struct State ) ); state = temp_state; } else { temp_state = computer_player_second( state ); Free( state, sizeof( struct State ) ); state = temp_state; } /* regular game */ for( ;; ) { printf( "\n" ); print_state( state ); if( state->player == player ) { temp_state = human_player( state ); Free( state, sizeof( struct State ) ); state = temp_state; } else { temp_state = computer_player( state ); Free( state, sizeof( struct State ) ); state = temp_state; } if( terminal_test( state ) == 1 ) { printf( "%c wins!!!\n", opposite_player( state->player ) ); break; } } return 1; }
int computer_vs_computer( char *file ) { char board[ SIZE ][ SIZE ]; /* set up board */ /*int _player = 0; for( int i = 0; i < 8 ; i++ ) { for( int j = 0; j < 8; j++ ) { if( _player == 0 ) { board[i][j] = 'B'; _player = 1; } else { board[i][j] = 'W'; _player = 0; } } if( _player == 0 ) _player = 1; else _player = 0; }*/ setup_board(file,board); /* create a new state */ struct State * state = new_state( board, 'B' ); struct State * temp_state; /* start game */ print_state( state ); temp_state = computer_player_first( state ); Free( state, sizeof( struct State ) ); state = temp_state; /* second move */ printf( "\n" ); print_state( state ); temp_state = computer_player_second( state ); Free( state, sizeof( struct State ) ); state = temp_state; /* regular game */ for( ;; ) { printf( "\n" ); print_state( state ); printf( "\n>> Mem usage before: %ld\n", memory_usage() ); temp_state = computer_player( state ); Free( state, sizeof( struct State ) ); state = temp_state; printf( ">> Mem usage after: %ld\n", memory_usage() ); if( terminal_test( state ) == 1 ) { printf( "\n" ); print_state( state ); printf( "\nNo moves left!... \n" ); printf( "\n%c wins!!!\n", opposite_player( state->player ) ); Free( state, sizeof( struct State ) ); break; } } return 1; }
int main (int argc, char *argv[]) { game_t *game = game_create (); game_start (game); int **old_board = NULL; old_board = setup_board (game); game->board_cursor_x = 2; game->board_cursor_y = 4; game->board[3][4] = 999999; press_key_and_loop (game, SDLK_SPACE); fail_if (!game->cursor_locked); release_key_and_loop (game, SDLK_SPACE); press_key_and_loop (game, SDLK_RIGHT); release_key_and_loop (game, SDLK_RIGHT); wait (game, 50 * 2); fail_if (!success (old_board, game, 3, 4, 2, 4)); old_board = setup_board (game); game->board_cursor_x = 3; game->board_cursor_y = 4; game->board[3][4] = 999999; press_key_and_loop (game, SDLK_SPACE); fail_if (!game->cursor_locked); release_key_and_loop (game, SDLK_SPACE); press_key_and_loop (game, SDLK_LEFT); release_key_and_loop (game, SDLK_LEFT); wait (game, 50 * 2); fail_if (!success (old_board, game, 2, 4, 3, 4)); old_board = setup_board (game); game->board_cursor_x = 2; game->board_cursor_y = 4; game->board[2][5] = 999999; press_key_and_loop (game, SDLK_SPACE); fail_if (!game->cursor_locked); release_key_and_loop (game, SDLK_SPACE); press_key_and_loop (game, SDLK_DOWN); release_key_and_loop (game, SDLK_DOWN); wait (game, 50 * 2); fail_if (!success (old_board, game, 2, 5, 2, 4)); old_board = setup_board (game); game->board_cursor_x = 2; game->board_cursor_y = 5; game->board[2][5] = 999999; press_key_and_loop (game, SDLK_SPACE); fail_if (!game->cursor_locked); release_key_and_loop (game, SDLK_SPACE); press_key_and_loop (game, SDLK_UP); release_key_and_loop (game, SDLK_UP); wait (game, 50 * 2); fail_if (!success (old_board, game, 2, 4, 2, 5)); game_destroy (game); return 0; }
main() { register BOARD *brd; /* The main Othello Board descriptor. */ register int stalemate; /* TRUE if no legal moves for anyone. */ /* Create the main Othello Board. */ if ( (brd = brdcreat()) != NULL ) { if ( get_players ( brd ) ) /* Initialise Players */ { setup_board ( brd ); /* Set up Othello Board. */ show_board ( brd ); /* Display Othello Board. */ }; }; /* Main loop - repeats until end_othello takes a non-zero value */ while ( !end_othello ) { /* Handle a single turn for a computer or human Player. */ if ( brd->player->level >= 0 ) { /* Handle the computer-controlled Player's turn - note that * do_computer() now returns TRUE if the computer could * find no legal moves for itself _or_ for the human player. * * This facility used to be in the function game_over(), but * has been shifted into do_computer() to speed up execution. */ stalemate = do_computer ( brd ); } else { do_human ( brd ); /* Handle a human Player's turn */ stalemate = FALSE; }; show_board ( brd ); /* Redisplay Othello Board. */ if ( game_over ( brd, stalemate ) ) /* If Game Over found...*/ { setup_board ( brd ); /* ...Set up Board for new game */ show_board ( brd ); /* Redisplay Othello Board. */ }; }; /* Display the error code, waiting for a key press after it is displayed. */ pause ( errors[end_othello - 1] ); while ( brd != NULL ) /* Free Othello Board descriptor memory block. */ { if ( brd->board != NULL ) free ( brd->board ); if ( brd->player != NULL ) { if ( brd->player < brd->player->opponent ) free ( brd->player ); else free ( brd->player->opponent ); }; free ( brd ); brd = brd->nxtbrd; }; };