void draw_numbers(void) { // iterate over board's numbers for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (has_colors() && g.board[i][j] != 0 && g.board[i][j] == g.init_board[i][j]) attron(COLOR_PAIR(PAIR_INIT)); if( has_colors() && (!check_col(j) || !check_row(i) || !check_square(i,j))) attron(COLOR_PAIR(PAIR_ERR)); if (game_won() && has_colors()) attron(COLOR_PAIR(PAIR_WON)); // determine char char c = (g.board[i][j] == 0) ? '.' : g.board[i][j] + '0'; mvaddch(g.top + i + 1 + i/3, g.left + 2 + 2*(j + j/3), c); if (has_colors()) attroff(COLOR_PAIR(PAIR_INIT)); refresh(); } } }
void play_game(void){ while(TRUE){ draw_tictactoe(); int m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; bool_t player = FALSE; bool_t won = FALSE; while ((won = game_won(&m)) == FALSE && game_finish(&m) == FALSE){ // uint8_t pos = get_field_from_uart(&m); uint8_t pos = get_from_console(player); uint8_t x = pos % 3; uint8_t y = pos / 3; if(m[pos] == 0){ draw_tictactoe_field(x, y, player); m[pos] = player + 1; player = (player + 1) % 2; } else{ printf("Field is already selected!\n"); } } if(won == TRUE){ draw_won_player((player + 1) % 2); } } }
bool update_game() { int in = get_input(get_map_window()); if(in == INPUT_ACTION && get_last_action() == ACTION_QUIT) return false; else if(in == INPUT_ACTION && (get_last_action() == ACTION_SCROLL_UP || get_last_action() == ACTION_SCROLL_DOWN)) { log_scroll(get_last_action() == ACTION_SCROLL_UP); draw_log(); } else if(!dead) { update_player(); if(get_current_floor() != current_level) { set_current_map(world[get_current_floor()], current_level < get_current_floor(), current_level > get_current_floor()); current_level = get_current_floor(); } update_map(1, world[current_level]); if(is_dead()) { add_message(COLOR_HP_CRIT, "Oh dear, you've died!"); add_message(COLOR_DEFAULT, "Press q to return to the main menu"); dead = true; } if(game_won()) dead = true; draw_log(); } return true; }
/* * in : boolean TRUE = PAUSE : FALSE = CONTINUE * */ static void pause_board (gboolean pause) { if(gcomprisBoard==NULL) return; if(gamewon == TRUE && pause == FALSE) /* the game is won */ { game_won(); } if(pause) { if (animate_id) { gtk_timeout_remove (animate_id); animate_id = 0; } } else { if(animate_item) { animate_id = gtk_timeout_add (200, (GtkFunction) animate_items, NULL); } } board_paused = pause; }
/* ===================================================================== * in : boolean TRUE = PAUSE : FALSE = CONTINUE * =====================================================================*/ static void pause_board (gboolean pause) { if(gcomprisBoard==NULL) return; if(gamewon == TRUE && pause == FALSE) /* the game is won */ game_won(); board_paused = pause; }
/* * in : boolean TRUE = PAUSE : FALSE = CONTINUE * */ static void pause_board (gboolean pause) { if(gcomprisBoard_missing==NULL) return; gc_bar_hide(FALSE); if(gamewon == TRUE && pause == FALSE) /* the game is won */ { game_won(); } board_paused = pause; }
/* ===================================================================== * * =====================================================================*/ static gboolean quit_after_delay() { if(!boardRootItem) return FALSE; if(board_paused) return TRUE; if (gamewon) game_won(); else submarine_next_level(); gc_bar_hide(FALSE); return FALSE; }
double agent_simple(Game_state *current_state, int player, int x, int y) { double score = 0; int is_game_won = game_won(current_state); if (is_game_won == player) score = 1; else if (is_game_won == other(player)) score = -1; else { score += 0.5 * agent_random(current_state, player, x, y); /* score += 0.3 * super_cool_heuristic_a(current_state, player, x, y) */ /* score += 0.8 * super_cool_heuristic_b(current_state, player, x, y) */ } return score; }
/* ===================================================================== * in : boolean TRUE = PAUSE : FALSE = CONTINUE * =====================================================================*/ static void pause_board (gboolean pause) { if(gcomprisBoard==NULL) return; if (timer_id) { gtk_timeout_remove (timer_id); timer_id = 0; } if(gamewon == TRUE && pause == FALSE) /* the game is won */ game_won(); if(gamewon == FALSE && pause == FALSE && errors < 1) { gcomprisBoard->sublevel = 1; colors_next_level(); } board_paused = pause; }
static void game_do(game_t *p_game) { item_t turn = p_game->starting_color; int eval = 0; bool_t selected = FALSE; p_game->selected_x = -1; p_game->selected_y = -1; while (1) { fe_point_t where; move_t move; if (abs(eval) > 1000) { game_won(p_game, eval); eval = 0; } if (turn == p_game->computer_color) { game_minimax(p_game, p_game->p_playfield, turn, 0, 1, &move); playfield_do_move(p_game->p_playfield, p_game, move.sx, move.sy, move.dx, move.dy); turn = (turn == PF_BLACK) ? PF_WHITE : PF_BLACK; eval = game_eval_playfield(p_game, p_game->p_playfield, turn); game_draw_status(p_game, turn); continue; } if (fe_get_stylus(&where) == TRUE) { uint8_t field_x = where.x / BRICK_WIDTH; uint8_t field_y = where.y / BRICK_HEIGHT; item_t item = playfield_get_item(p_game->p_playfield, field_x, field_y); rnd += field_x ^ rnd; /* Select something */ if (COLOR(item) == turn) { /* Within field, correct color */ game_draw_one_pos(p_game, p_game->selected_x, p_game->selected_y, 0); game_draw_one_pos(p_game, field_x, field_y, 1); selected = TRUE; p_game->selected_x = field_x; p_game->selected_y = field_y; } else if (selected && item == PF_EMPTY) { /* Destination - move to! */ if (playfield_can_move(p_game->p_playfield, p_game->selected_x, p_game->selected_y, field_x, field_y)) { playfield_do_move(p_game->p_playfield, p_game, p_game->selected_x, p_game->selected_y, field_x, field_y); turn = (turn == PF_BLACK) ? PF_WHITE : PF_BLACK; } game_draw_status(p_game, turn); selected = FALSE; eval = game_eval_playfield(p_game, p_game->p_playfield, turn); } else { /* Pressed outside the playfield */ game_draw_one_pos(p_game, p_game->selected_x, p_game->selected_y, 0); selected = FALSE; /* Handle the buttons */ if (IN_RECT(where.x, where.y, RESTART_BUTTON_X, RESTART_BUTTON_Y, 34, 13)) { p_game->restart = 1; game_won(p_game, 0); p_game->restart = 0; } if (IN_RECT(where.x, where.y, HUMAN_BUTTON_X, HUMAN_BUTTON_Y, 13, 13)) p_game->computer_color = PF_INVALID; else if (IN_RECT(where.x, where.y, BLACK_BUTTON_X, BLACK_BUTTON_Y, 13, 13)) p_game->computer_color = PF_BLACK; else if (IN_RECT(where.x, where.y, WHITE_BUTTON_X, WHITE_BUTTON_Y, 13, 13)) p_game->computer_color = PF_WHITE; game_draw_status(p_game, turn); } } if ( fe_get_buttons() == FE_EVENT_EXIT) break; } }
static int main_game_loop(struct env *board, struct creature *pac, struct creature *ghost, int cnt) { unsigned long time_to_sleep; enum dir_t direction; int i, won = 0; draw_board(board); box_print(0, 0, "Welcome. Hit any key to start :)"); update_view(); blocking_input(); do { /* set up and place the players (ghosts etc) */ if (!init_players(board, pac, ghost, cnt)) return 0; /* blank out screen, ready for action */ reset_view(); draw_board(board); time_to_sleep = INITIAL_DELAY; for (;;) { /* check for user input */ direction = get_user_input(); if (direction == QUIT) break; move_pac(board, pac, direction); draw_creature(board, pac); erase_tail(board, pac); /* Move and draw N ghosts. */ for (i=0; i<cnt; i++) { ghost_move(board, &(ghost[i]), i); draw_creature(board, &(ghost[i])); erase_tail(board, &(ghost[i])); } /* Up the score if we got any cherries, * then print the new (or old) score. */ pick_up_cherries(board, pac); print_stat(board); /* update the whole screen */ update_view(); /* Check if there's any more cherries to * pick, otherwise the game is won. */ won = game_won(board); if (won) break; if (pac_caught(pac, ghost, cnt)) { down_lives(board); break; } /* * Sleep for a short while. This function is not * ANSI/ISO-C. */ usleep(time_to_sleep); if (time_to_sleep > MIN_DELAY) time_to_sleep -= DEC_DELAY; } } while (direction != QUIT && !won && lives_left(board) > 0 && play_again()); finish(won, lives_left(board)); return 1; }
double agent_alex(Game_state *state, int player, int x, int y) { Pair** rmap = NULL; double score = 0; int is_game_won = game_won(state); int a = 0, b = 0, c = 0, d = 0; int lines, i; int t_threat; /* keep track of who went first */ if(first_player == -1) first_player = player; /* player has won with this board */ if(is_game_won == player) { score = 1000000; /* player has lost with this board */ } else if (is_game_won == other(player)) { score = -1000000; } else { lines = num_of_win_places(state->width, state->height, state->num_to_connect); rmap = r_map(state); for(i = 0; i < lines; i++) { t_threat = threat_type(state, rmap, player, i); /* a - odd threat */ if(t_threat == 1) a++; /* b - even threat */ else if(t_threat == 0) b++; /* c - 2 in a rows */ if(has_n_in_row(state, rmap, player, i, 2)) c++; /* d - 1 in a rows */ if(has_n_in_row(state, rmap, player, i, 1)) d++; } /* favour the center column (but more so earlier on) */ if(state->board[state->width/2][y] == player) { score += 50; } if(first_player == player) { score += a * 16 + b * 8 + c * 2 + d; } else { score += b * 16 + a * 8 + c * 2 + d; if(state->num_of_pieces >= 5 && arrow_attack(state, other(player), rmap, 2)) { score -= 10; } } if(three_attack(state, other(player), rmap)) { score -= 100; } if(arrow_attack(state, other(player), rmap, 1)) { score -= 100; } if(arrow_attack(state, player, rmap, 1)) { score += 30; } if(three_support(state, other(player), rmap)) { score -= 100; } for(i = 0; i < lines; i++) free(rmap[i]); free(rmap); } return score; }
int main(int argc, char *argv[]) { int option; int option_index; int passes_through_deck = 3; int use_utf8 = 0; static const struct option options[] = { {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'v'}, {"passes", required_argument, NULL, 'p'}, {"utf8", no_argument, NULL, 'u'} }; program_name = argv[0]; while ((option = getopt_long(argc, argv, "hvp:u", options, &option_index)) != -1) { switch (option) { case 'v': version(); exit(0); case 'p': passes_through_deck = atoi(optarg); break; case 'u': use_utf8 = true; break; case 'h': default: usage(program_name); exit(0); } } set_utf8_mode(use_utf8); setlocale(LC_ALL, ""); initscr(); raw(); noecho(); keypad(stdscr, TRUE); start_color(); curs_set(FALSE); set_escdelay(0); assume_default_colors(COLOR_WHITE, COLOR_GREEN); init_pair(1, COLOR_BLACK, COLOR_WHITE); init_pair(2, COLOR_RED, COLOR_WHITE); init_pair(3, COLOR_WHITE, COLOR_BLUE); init_pair(4, COLOR_WHITE, COLOR_GREEN); int key; while (!term_size_ok()) { clear(); mvprintw(1, 1, SMALL_TERM_MSG); refresh(); if ((key = getch()) == 'q' || key == 'Q') { endwin(); return(0); } } clear(); draw_greeting(); refresh(); for (;;) { if ((key = getch()) == 'q' || key == 'Q') { endwin(); return(0); } if (term_size_ok()) { clear(); draw_greeting(); refresh(); if (key == KEY_SPACEBAR) { clear(); refresh(); game_init(&game, passes_through_deck); break; } } else if (key == KEY_RESIZE) { clear(); mvprintw(1, 1, SMALL_TERM_MSG); refresh(); } } do { keyboard_event(getch()); } while (!game_won()); endwin(); game_end(); printf("You won.\n"); return(0); }
int main(int argc, char *argv[]) { // define usage const char *usage = "Usage: sudoku n00b|l33t [#]\n"; // ensure that number of arguments is as expected if (argc != 2 && argc != 3) { fprintf(stderr, usage); return 1; } // ensure that level is valid if (strcmp(argv[1], "debug") == 0) g.level = "debug"; else if (strcmp(argv[1], "n00b") == 0) g.level = "n00b"; else if (strcmp(argv[1], "l33t") == 0) g.level = "l33t"; else { fprintf(stderr, usage); return 2; } // n00b and l33t levels have 1024 boards; debug level has 9 int max = (strcmp(g.level, "debug") == 0) ? 9 : 1024; // ensure that #, if provided, is in [1, max] if (argc == 3) { // ensure n is integral char c; if (sscanf(argv[2], " %d %c", &g.number, &c) != 1) { fprintf(stderr, usage); return 3; } // ensure n is in [1, max] if (g.number < 1 || g.number > max) { fprintf(stderr, "That board # does not exist!\n"); return 4; } // seed PRNG with # so that we get same sequence of boards srand(g.number); } else { // seed PRNG with current time so that we get any sequence of boards srand(time(NULL)); // choose a random n in [1, max] g.number = rand() % max + 1; } // start up ncurses if (!startup()) { fprintf(stderr, "Error starting up ncurses!\n"); return 5; } // register handler for SIGWINCH (SIGnal WINdow CHanged) signal(SIGWINCH, (void (*)(int)) handle_signal); // start the first game if (!restart_game()) { shutdown(); fprintf(stderr, "Could not load board from disk!\n"); return 6; } redraw_all(); // let the user play! int ch; int last[3] = {10}; do { // refresh the screen refresh(); // get user's input ch = getch(); // capitalize input to simplify cases ch = toupper(ch); // process user's input switch (ch) { // start a new game case 'N': g.number = rand() % max + 1; if (!restart_game()) { shutdown(); fprintf(stderr, "Could not load board from disk!\n"); return 6; } break; // restart current game case 'R': if (!restart_game()) { shutdown(); fprintf(stderr, "Could not load board from disk!\n"); return 6; } break; // let user manually redraw screen with ctrl-L case CTRL('l'): redraw_all(); break; case 'U': case CTRL('Z'): if (last[0] != 10 && !game_won()) { g.board[last[0]][last[1]] = last[2]; draw_numbers(); if (check_cols(1) || check_rows(1) || check_squares(1)) hide_banner(); show_cursor(); warn(); } break; // move cursor case KEY_UP: if (g.y > 0) { g.y -= 1; show_cursor(); } else { g.y = 8; show_cursor(); } break; case KEY_DOWN: if (g.y < 8) { g.y += 1; show_cursor(); } else { g.y = 0; show_cursor(); } break; case KEY_LEFT: if (g.x > 0) { g.x -= 1; show_cursor(); } else { g.x = 8; show_cursor(); } break; case KEY_RIGHT: if (g.x < 8) { g.x += 1; show_cursor(); } else { g.x = 0; show_cursor(); } break; //input number and check if game is won case '1' ... '9': if (g.init_board[g.y][g.x] == 0) { last[0] = g.y; last[1] = g.x; last[2] = g.board[g.y][g.x]; g.board[g.y][g.x] = ch - 48; draw_numbers(); if (check_cols(1) || check_rows(1) || check_squares(1)) hide_banner(); show_cursor(); } game_won(); warn(); break; //return to blank space case '0': case '.': case KEY_BACKSPACE: case KEY_DC: if (g.init_board[g.y][g.x] == 0) { last[0] = g.y; last[1] = g.x; last[2] = g.board[g.y][g.x]; g.board[g.y][g.x] = 0; draw_numbers(); if (check_cols(1) || check_rows(1) || check_squares(1)) hide_banner(); show_cursor(); } warn(); break; } // log input (and board's state) if any was received this iteration if (ch != ERR) log_move(ch); } while (ch != 'Q'); // shut down ncurses shutdown(); // tidy up the screen (using ANSI escape sequences) printf("\033[2J"); printf("\033[%d;%dH", 0, 0); // that's all folks printf("\nkthxbai!\n\n"); return 0; }