int get_shallow_move(int *board, int side) { /* Return the move that gives the best position immediately after */ int i; int move = 0; int score; int flips[20]; int old_board[100]; int best_score = MIN_SCORE; // backup the existing board copy_board(board, old_board); for (i=11; i<89; ++i) { if (legal_move(board,i,side,flips) == 1) { make_move(board,i,side,flips); score = evaluate_board(board, side, 0); if (score > best_score) { best_score = score; move = i; } // reset the board before each iteration copy_board(old_board, board); } } return move; }
main() { int i,j; /* counters */ int a[DIMENSION*DIMENSION+1]; boardtype board; /* Seduko board structure */ boardtype temp; read_board(&board); print_board(&board); copy_board(&board,&temp); for (fast=TRUE; fast>=FALSE; fast--) for (smart=TRUE; smart>=FALSE; smart--) { printf("----------------------------------\n"); steps = 0; copy_board(&temp,&board); finished = FALSE; backtrack(a,0,&board); /*print_board(&board);*/ printf("It took %d steps to find this solution ",steps); printf("for fast=%d smart=%d\n",fast,smart); } }
int main (int argc, char *argv[]) { game_t *game = game_create (); game_start (game); setup_board_every_gem_different (game); int **old_board = copy_board (game); int m = game->n_rows; int n = game->n_cols; replace_gem (game, 2, 2, 999999); replace_gem (game, 3, 2, 1999999); replace_gem (game, 4, 2, 999999); int i; for (i = 0; i < 50 * 2; ++i) { game_loop (game); } for (i = 0; i < m; ++i) { fail_if (game->board[3][i] == old_board[3][i]); } for (i = 0; i < n; ++i) { if (i == 3) continue; fail_if (game->board[i][1] != old_board[i][0]); fail_if (game->board[i][0] == old_board[i][0]); } setup_board_every_gem_different (game); old_board = copy_board (game); replace_gem (game, 2, 2, 999999); replace_gem (game, 2, 3, 1999999); replace_gem (game, 2, 4, 999999); for (i = 0; i < 50 * 2; ++i) { game_loop (game); } for (i = 0; i < m; ++i) { fail_if (game->board[2][i] == old_board[2][i]); } for (i = 0; i < n; ++i) { if (i == 2) continue; fail_if (game->board[i][1] != old_board[i][0]); fail_if (game->board[i][0] == old_board[i][0]); } game_destroy (game); return 0; }
inline int expand_node_right(search_node *source_node, search_node ** new_node) { int aux, blank_row, blank_col; node_find_blank(source_node, &blank_row, &blank_col); if (blank_col < 2) { *new_node = malloc(sizeof (search_node)); if (*new_node == NULL) return -1; copy_board(source_node, *new_node); aux = (*new_node)->board[blank_row][blank_col + 1]; (*new_node)->board[blank_row][blank_col + 1] = 0; (*new_node)->board[blank_row][blank_col] = aux; (*new_node)->move = right; gen_node_key(*new_node); (*new_node)->g = source_node->g + 1; (*new_node)->previous = source_node; return 0; } else { // is not possible to expand uppward *new_node = NULL; return -2; } }
bool restart_game(void) { // reload current game if (!load_board()) return false; // redraw board draw_grid(); draw_numbers(); copy_board(); // get window's dimensions int maxy, maxx; getmaxyx(stdscr, maxy, maxx); // move cursor to board's center g.y = g.x = 4; show_cursor(); // remove log, if any remove("log.txt"); // w00t return true; }
//begin main int main (void) { char current[BOARD_SIZE][BOARD_SIZE] ; char future[BOARD_SIZE][BOARD_SIZE] ; initialize_board(current, ' ') ; initialize_board(future, ' ') ; char choice ; int stillgoing = 1; while (stillgoing) { printf("\033[2J\033[H") ; copy_board(current, future) ; print_board(current) ; printf("Please enter one of the following:\n a: to add a new cell\n r: to remove a cell\n n: to advance the simulation\n q: to quit\n p: to play the game forever\n") ; scanf("%c", &choice) ; stillgoing = user_action(current, future, choice) ; } return 0 ; } //end main
//finds route to target location and returns a pointer to the board that found it Node* find_solution (Node *start, int height_coordinate, int width_coordinate){ hash *hash_array = create_hash_table (HASH_TABLE_SIZE); set_hash_table (hash_array); Node *current, *previous, *parent; int target_found = 0; current = start; parent = start; while (!target_found){ previous = current; current->next = allocate_new_node (parent, previous); if (find_next_move (hash_array, start, current->next, parent->board, current->next->board) == 0){ if (parent->next == NULL){ printf("Couldn't find a solution\n"); exit (1); } parent = parent->next; current->next->parent = parent; copy_board (current->next->board, parent->board); } target_found = check_for_target (current->next->board, height_coordinate, width_coordinate); current = current->next; } return current; }
int user_action(char board1[BOARD_SIZE][BOARD_SIZE], char board2[BOARD_SIZE][BOARD_SIZE], char user) { switch (user) { case 'a': add_cell(board2) ; break ; case 'r': remove_cell(board2) ; break ; case 'n': apply_rules(board1, board2) ; break ; case 'q': return 0 ; case 'p': while (1) { apply_rules(board1, board2) ; copy_board(board1, board2) ; printf("\033[2J\033[H") ; usleep(500000) ; print_board(board1) ; } } return 1 ; }
void solve_board(board_t* board) { stats.solve++; if (is_complete(board)) { printf("SOLVED!\n"); print_board(board); print_stats(stats); assert(0); } else if (is_dead_end(board)) { debugf("dead end, skip\n"); } else { size_t i_row, i_col; digits_t digits; find_next_continuation(board, &i_row, &i_col, &digits); debugf("best continuation [%lu,%lu]\n", i_row, i_col); size_t i; for (i = 1; i < NDIGITS; i++) { if (0 == digits.digits[i]) { debugf("extending [%lu,%lu] <- %lu\n", i_row, i_col, i); board_t* new_board = copy_board(board); set_digit(new_board, i_row, i_col, i); solve_board(new_board); free(new_board); } } } }
void run (int height_coordinate, int width_coordinate){ move *moves_array = create_move_array (MOVES_MAX); Node *final_board, *start; int move_count = 0, target_found = 0; int initial_board [BOARD_HEIGHT][BOARD_WIDTH]; populate_board (initial_board); start = allocate_start_node (initial_board); target_found = check_for_target (initial_board, height_coordinate, width_coordinate); if (!target_found){ final_board = find_solution (start, height_coordinate, width_coordinate); move_count = populate_moves_array (final_board, moves_array); //to copy start board into last element of moves array for printing copy_board ((moves_array+(move_count-1))->board, start->board); print_moves_SDL (moves_array, move_count); // print_moves (moves_array, move_count); } else { print_list_SDL (start); printf("Target already on board\n"); } // print_list(start); free_list (start); free (moves_array); }
/* remove_row - remove a row at a specified y */ void remove_row(int y) { int i, j; for (i = y; i > 0; i--) { for (j = 0; j < 10; j++) { copy_board(&board[10 * i + j], &board[10 * (i - 1) + j]); } } }
/** * ゲームオーバーの手か * @param int pos 手 * @param stone_t turn 現在の手番 * @param stone_t board[][] 盤面 * @return int true:1, false:0 */ int is_terminate_hand(int pos, stone_t turn, const stone_t board[HIGHT][AREA]) { stone_t copy[HIGHT][AREA]; int layer; copy_board(board, copy); layer = put_stone(pos, turn, copy); // 置いてみる return check_win(layer, pos, turn, copy) || is_full(copy); }
//fills an array of structs with solution boards. Returns the position of the final board in the array int populate_moves_array (Node *list_element, move *moves_array){ //move_count initially set to 1 to leave room for start board int move_count = 1; while (list_element->previous != NULL){ copy_board (moves_array->board, list_element->board); list_element = list_element->parent; move_count++; moves_array++; } return move_count; }
int minimize(int *board, int side, int unplayed, int ply) { /* Search to depth ply with mutual recursion * Side should be the opponent of currently playing side * Return the lowest score found */ int old_board[100]; int flips[20]; int score = MAX_SCORE; if (ply == 0 || test_end(board, unplayed) == 1) return evaluate_board(board, side, unplayed); copy_board(board, old_board); for (int i=11; i<89; ++i) { if (legal_move(board, i, side, flips) == 0) continue; make_move(board, i, side, flips); score = MIN(score, maximize(board, -side, 0, ply-1)); copy_board(old_board, board); } return score; }
int calc(STONE board[N][N], int X, int Y, int turn){ int sum; STONE bcopy[N][N]; copy_board(board, bcopy); sum = reverse_left(bcopy, X, Y ,turn) + reverse_right(bcopy, X, Y, turn) + reverse_up(bcopy, X, Y, turn) + reverse_down(bcopy, X, Y, turn) + reverse_left_down(bcopy, X, Y, turn) + reverse_left_down(bcopy, X, Y, turn) + reverse_right_up(bcopy, X, Y, turn) + reverse_right_down(bcopy, X, Y, turn); return sum; }
//Allocates space for a new node, assigns it a previous and parent pointer Node *allocate_new_node (Node *parent_node, Node *previous){ Node *p_node = (Node *) malloc (sizeof (Node)); if (p_node == NULL){ printf("Malloc in allocate_node didn't work\n"); exit (1); } p_node->parent = parent_node; p_node->next = NULL; p_node->previous = previous; copy_board (p_node->board, parent_node->board); return p_node; }
Node *allocate_start_node (int initial_board[BOARD_HEIGHT][BOARD_WIDTH]){ Node *p_node = (Node *) malloc (sizeof (Node)); if (p_node == NULL){ printf("Malloc in allocate_node didn't work\n"); exit (1); } p_node->parent = NULL; p_node->next = NULL; p_node->previous = NULL; copy_board (p_node->board, initial_board); return p_node; }
int ab_minimize(int *board, int side, int unplayed, int ply,int a, int b) { /* Search to depth ply with mutual recursion * Return minimum score possible as opponent of current player * Cut off with a and b when possible */ int old_board[100]; int flips[20]; if (ply == 0 || test_end(board, unplayed) == 1) return evaluate_board(board, side, unplayed); copy_board(board, old_board); for (int i=11; i<89; ++i) { if (legal_move(board, i, side, flips) == 0) continue; make_move(board, i, side, flips); b = MIN(b, ab_maximize(board, -side, 0, ply-1, a, b)); copy_board(old_board, board); if (b <= a) break; } return b; }
//checks a specific move from specific location on the board. If move viable and doesn't lead to duplicate board, //new board shows this move and 1 is returned. If no moves found, returns 0 int check_move (movement direction, int i, int j, hash *hash_array, Node *start, Node *latest_node, int parent_board [BOARD_HEIGHT][BOARD_WIDTH], int new_board[BOARD_HEIGHT][BOARD_WIDTH]){ if (valid_move (direction, i, j, new_board)){ move_piece (direction, i, j, new_board); latest_node->identifier = allocate_board_identifier (new_board); if (insert_hash (hash_array, latest_node, latest_node->identifier, new_board) == 1){ copy_board (new_board, parent_board); } else{ return 1; } } return 0; }
int ab_maximize(int *board, int side, int unplayed, int ply, int a,int b) { /* Search to depth ply with mutual recursion * Return maximum score possible for current side * Cuts off search using a and b parameters if possible */ int old_board[100]; int flips[20]; if (ply == 0 || test_end(board, unplayed) == 1) return evaluate_board(board, side, unplayed); copy_board(board, old_board); for (int i=11; i<89; ++i) { if (legal_move(board, i, side, flips) == 0) continue; make_move(board, i, side, flips); a = MAX(a, ab_minimize(board, -side, 0, ply-1, a, b)); copy_board(old_board, board); if (b <= a) // that's a cutoff, no point further pursuing this position break; } return a; }
// main difference between get_move and maximize is that this one // returns a move, not a score int get_minimax_move(int *board, int side, int unplayed, int ply) { /* Similar to maximize(), but return move instead of score */ int score; int best_move = 0; int flips[20]; int old_board[100]; int best_score = MIN_SCORE-2; copy_board(board, old_board); for (int i=11; i<89; ++i) { if (legal_move(board, i, side, flips) == 0) continue; make_move(board, i, side, flips); score = minimize(board, -side, unplayed, ply); copy_board(old_board, board); if (score > best_score) { best_score = score; best_move = i; } } return best_move; }
int get_alphabeta_move(int *board, int side, int unplayed, int ply) { /* Return best move using alphabeta search to ply depth * Similar to ab_maximize, but finds move instead of score */ int score; int best_move = 0; int flips[20]; int old_board[100]; int best_score = MIN_SCORE-1; copy_board(board, old_board); for (int i=11; i<89; ++i) { if (legal_move(board, i, side, flips) == 0) continue; make_move(board, i, side, flips); score = ab_minimize(board, -side, unplayed, ply, best_score,MAX_SCORE+1); copy_board(old_board, board); if (score > best_score) { best_score = score; best_move = i; } } return best_move; }
// Blasphemous! maximize instead of maximise?! int maximize(int *board, int side, int unplayed, int ply) { /* The max portion of minimax * Return maximum score available from position * Used with currently playing side's turn * Search with mutual recursion to depth ply */ int old_board[100]; int flips[20]; int score = MIN_SCORE; if (ply == 0 || test_end(board, unplayed) == 1) // either reached end of search or game is over, so return return evaluate_board(board, side, unplayed); // backup the current playing board copy_board(board, old_board); for (int i=11; i<89; ++i) { if (legal_move(board, i, side, flips) == 0) continue; make_move(board, i, side, flips); score = MAX(score, minimize(board, -side, 0, ply-1)); // reset the board after computing the score copy_board(old_board, board); } return score; }
char *search(char *board, char me) { // 0 = black, 1 = white int i, j, x, y, p, q; char *best_board = board; int best_score = -49; int this_score; // choose a piece to investigate for (x=0; x<7; x++) for (y=0; y<7; y++) { if (getboard(board,x,y) != me) continue; // not my piece for (i=-2; i<=2; i++) for (j=-2; j<=2; j++) { char *new_board; if (i==0 && j==0) continue; if (!inboard(x+i,y+j)) continue; // not on the board if (getboard(board,x+i,y+j) != '-') continue; // not empty new_board = copy_board(board); if ((abs(i) > 1) || (abs(j) > 1)) // this is an extension setboard(new_board,x,y,'-'); setboard(new_board,x+i,y+j,me); // claim the surrounding squares for (p=-1; p<=1; p++) for (q=-1; q<=1; q++) { if (inboard(x+i+p,y+j+q) && (getboard(new_board,x+i+p,y+j+q) != '-')) setboard(new_board,x+i+p,y+j+q,me); } if (me == 'b') this_score = score(search(new_board,'w'),'b'); else this_score = 50 - score(new_board,'b'); if (this_score > best_score) { best_score = this_score; best_board = new_board; } } } return best_board; }
/* This is the entry-point for the game! */ void c_start(void) { /* TODO: You will need to initialize various subsystems here. This * would include the interrupt handling mechanism, and the various * systems that use interrupts. Once this is done, you can call * enable_interrupts() to start interrupt handling, and go on to * do whatever else you decide to do! */ seed_rand_with_time(); init_interrupts(); init_keyboard(); init_timer(); enable_interrupts(); int board[BOARD_SIZE][BOARD_SIZE] = { }; animation_descriptor descriptor = { }; initialize(board); int high_score = current_score(board); init_video(high_score); draw_board(board); /* Loop forever, so that we don't fall back into the bootloader code. */ while (1) { if (!isemptyqueue()) { key k = dequeue(); if (k == enter_key) { for (int *b = *board; b < *board + BOARD_SIZE * BOARD_SIZE; b++) *b = 0; initialize(board); init_video(high_score); draw_board(board); continue; } else { shift_direction direction = key_to_direction(k); // Setup animation copy_board(board, descriptor.board); descriptor.direction = direction; // Only add a box if the pieces actually move if (shift(board, direction, descriptor.offsets)) { add_random_box(board); int score = current_score(board); if (score > high_score) high_score = score; init_video(high_score); } } int num_frames = frame_count(descriptor.direction); int incr = get_axis(descriptor.direction) == horizontal_axis ? 12 : 2; int frame = 0; while (frame <= num_frames) { draw_board_frame(descriptor, frame); sleep(30); if (frame == num_frames) break; else frame = min(frame + incr, num_frames); } draw_board(board); if (!move_available(board)) { draw_failure_message(); } } } }
board_node *init_node(int **board) { board_node *node = malloc(sizeof(board_node)); node = malloc(sizeof(board_node)); node->board = copy_board(board); return node; }
int main (int argc, char *argv[]) { game_t *game = game_create (); game_start (game); int n = game->n_rows; int m = game->n_cols; int i; int j; int k; setup_board_every_gem_different (game); int **old_board = copy_board (game); game->n_gem_types = 1000000; replace_gem (game, 0, n-1, 999999); replace_gem (game, 1, n-1, 999999); replace_gem (game, 2, n-1, 999999); replace_gem (game, m-1, n-1, 999999); replace_gem (game, m-2, n-1, 999999); replace_gem (game, m-3, n-1, 999999); bool old_ones_dropped_1 = false; bool old_ones_dropped_2 = false; game_loop_n (game, 100); old_ones_dropped_1 = true; for (k = 0; k < 3; ++k) { for (j = 1; j < n; ++j) { if (game->board[k][j] != old_board[k][j-1]) { old_ones_dropped_1 = false; } } } old_ones_dropped_2 = true; for (k = m-3; k < m; ++k) { for (j = 1; j < n; ++j) { if (game->board[k][j] != old_board[k][j-1]) { old_ones_dropped_1 = false; } } } fail_if (!old_ones_dropped_1 || !old_ones_dropped_2); bool new_gems_1 = false; bool new_gems_2 = false; if (game->board[0][0] != old_board[0][0] || game->board[1][0] != old_board[1][0] || game->board[2][0] != old_board[2][0]) { new_gems_1 = true; } if (game->board[m-3][0] != old_board[m-3][0] || game->board[m-2][0] != old_board[m-2][0] || game->board[m-1][0] != old_board[m-1][0]) { new_gems_2 = true; } fail_if (!new_gems_1 || !new_gems_2); game_destroy (game); return 0; }
/** * runs minmax algorithm, extends the tree at the same time. * decides what is the best move to perform according to the minmax algorithm (with alphabeta pruning) * * @param node - the current root of the minmax tree * @param alpha - the current maximum we got (for alphabeta pruning) * @param beta - the current minimum we got (for alphabeta pruning) * @param depth - the depth of the tree * @param max - do max or min? * @param board - the current board * @param best_move - pointer that will contain the best move to make, AFTER the function finishes. * @param is_valid_move - function that decides if a move is valid * @param make_move - function that makes the move on the board * @param get_score - the scoring function for the current game */ int minmax_with_extend(vertex *node, int depth, int alpha, int beta, int max, Board *board, Move *best_move, int (*is_valid_move)(Board *board, Move *move, int value), int (*make_move)(Board* board, Move* new_move, int value), int (*get_score)(Board* board)) { int i; int unimplemented_moves_length; int current_score; Move *next_best_move; Board *copied_board = new_board(board->n, board->m); if (copied_board == NULL) { printf("ERROR: can't initiazlie copied board.\n"); exit(1); } // we stop if we got to a winning move or if we reached the requested depth if ((depth == 0) || (node->score == EXTREME_VALUE) || (node->score == -EXTREME_VALUE)) { free_board(copied_board); return node->score; } // if we don't have a linked list, but the depth isn't 0, we'll create an empty linked list if (node->children == NULL) { if ((node->children = (linked_list*)malloc(sizeof(linked_list))) == NULL) { printf("ERROR: standard function malloc has failed\n"); exit(1); } node->children->head = NULL; node->children->tail = NULL; } if ((next_best_move = (Move*)malloc(sizeof(Move))) == NULL) { printf("ERROR: malloc has failed in minmaxtree\n"); exit(1); } element *iterator = node->children->head; Move *unimplemented_moves = get_unimplemented_moves(node->children, board, &unimplemented_moves_length, max, is_valid_move); // we'll initialize best_move to SOME move, just in case EVERY move is a losing move. if (iterator != NULL) { best_move->i = iterator->node->current_move->i; best_move->j = iterator->node->current_move->j; } else { if (unimplemented_moves_length > 0) { best_move->i = unimplemented_moves[0].i; best_move->j = unimplemented_moves[0].j; // NO MOVES AT ALL - we don't care about the best move, just return the current score. } else { free_board(copied_board); free(next_best_move); free(unimplemented_moves); return node->score; } } // runs max if (max) { while (iterator != NULL) { copy_board(board, copied_board); make_move(copied_board, iterator->node->current_move, max ? FIRST_PL_TURN:SECOND_PL_TURN); current_score = minmax_with_extend(iterator->node, depth-1, alpha, beta, !max, copied_board, next_best_move, is_valid_move, make_move, get_score); // get the maximum of alpha and current score if (current_score > alpha) { alpha = current_score; // update the best move accordingly best_move->i = iterator->node->current_move->i; best_move->j = iterator->node->current_move->j; // alpha-beta pruning if (beta <= alpha) { free_board(copied_board); free(next_best_move); free(unimplemented_moves); return alpha; } } iterator = iterator->next; } for (i=0; i<unimplemented_moves_length; i++) { copy_board(board, copied_board); add_node_to_end(node->children, unimplemented_moves[i], copied_board, max ? FIRST_PL_TURN:SECOND_PL_TURN); // still need to update the score of the node. so we'll make the move and then update the score. make_move(copied_board, node->children->tail->node->current_move, node->children->tail->node->value); node->children->tail->node->score = get_score(copied_board); // now for the minmax part current_score = minmax_with_extend(node->children->tail->node, depth-1, alpha, beta, !max, copied_board, next_best_move, is_valid_move, make_move, get_score); // get the maximum of alpha and current score if (current_score > alpha) { alpha = current_score; // update the best move accordingly best_move->i = node->children->tail->node->current_move->i; best_move->j = node->children->tail->node->current_move->j; // alpha-beta pruning if (beta <= alpha) { free_board(copied_board); free(next_best_move); free(unimplemented_moves); return alpha; } } } free_board(copied_board); free(next_best_move); free(unimplemented_moves); return alpha; // runs min } else { while (iterator != NULL) { copy_board(board, copied_board); make_move(copied_board, iterator->node->current_move, max ? FIRST_PL_TURN:SECOND_PL_TURN); current_score = minmax_with_extend(iterator->node, depth-1, alpha, beta, !max, copied_board, next_best_move, is_valid_move, make_move, get_score); // get the minimum of beta and current score if (current_score < beta) { beta = current_score; // update the best move accordingly best_move->i = iterator->node->current_move->i; best_move->j = iterator->node->current_move->j; // alpha-beta pruning if (beta <= alpha) { free_board(copied_board); free(next_best_move); free(unimplemented_moves); return beta; } } iterator = iterator->next; } for (i=0; i<unimplemented_moves_length; i++) { copy_board(board, copied_board); add_node_to_end(node->children, unimplemented_moves[i], copied_board, max ? FIRST_PL_TURN:SECOND_PL_TURN); // still need to update the score of the node. so we'll make the move and then update the score. make_move(copied_board, node->children->tail->node->current_move, node->children->tail->node->value); node->children->tail->node->score = get_score(copied_board); // now for the minmax part current_score = minmax_with_extend(node->children->tail->node, depth-1, alpha, beta, !max, copied_board, next_best_move, is_valid_move, make_move, get_score); // get the minimum of beta and current score if (current_score < beta) { beta = current_score; // update the best move accordingly best_move->i = node->children->tail->node->current_move->i; best_move->j = node->children->tail->node->current_move->j; // alpha-beta pruning if (beta <= alpha) { free_board(copied_board); free(next_best_move); free(unimplemented_moves); return beta; } } } free_board(copied_board); free(next_best_move); free(unimplemented_moves); return beta; } }
/** Program entry point */ int main() { printf("\n\n Welcome to the 7 wonders of the world of the 7 colors\n" " *****************************************************\n\n" "Current board state:\n"); srand(time(NULL)); print_board(); int i = 0; int j = 0; int victories[5][5]; // victories[i][j] contains the number of victories of i over j for(i = 0 ; i < 5 ; i++){ for(j = 0 ; j < 5 ; j++){ victories[i][j] = 0; } } long int start = clock(); void (*strats[5])(char) = {improved_random_play, spider, greedy, double_greedy, mix}; char* names[5] = {"Improved Random","Spider","Greedy","Double Greedy","Mix"}; int k = 0; float score1 = 0; float score2 = 0; for(i = 0 ; i < 4 ; i++){ for(j = i+1 ; j < 5 ; j++){ for(k = 0 ; k < NB_SIMULATIONS ; k++){ printf("%s VS %s :\n", names[i], names[j]); random_filling(); copy_board(); run_game(PLAYER1,strats[i],strats[j]); score1 = 100*score(PLAYER1)/((float) BOARD_SIZE*BOARD_SIZE); score2 = 100*score(PLAYER2)/((float) BOARD_SIZE*BOARD_SIZE); if(score1 > score2){(victories[i][j])++;} else{(victories[j][i])++;} get_saved_board(); set_cell(0,BOARD_SIZE-1,PLAYER2); set_cell(BOARD_SIZE-1,0,PLAYER1); run_game(PLAYER2,strats[i],strats[j]); score1 = 100*score(PLAYER1)/((float) BOARD_SIZE*BOARD_SIZE); score2 = 100*score(PLAYER2)/((float) BOARD_SIZE*BOARD_SIZE); if(score1 > score2){(victories[i][j])++;} else{(victories[j][i])++;} } } } printf("Total execution time : %ld", (clock()-start)/CLOCKS_PER_SEC); printf("\n\n"); for(i = 0 ; i < 4 ; i++){ for(j = i+1 ; j < 5 ; j++){ printf("%s VS %s :\n", names[i], names[j]); printf("%s : %d victories\n", names[i], victories[i][j]); printf("%s : %d victories\n", names[j], victories[j][i]); printf("\n\n"); } } return 0; // Everything went well }
int main (int argc, char* argv[]) { /* * Set verifyp to 1 if you want to turn on verification. */ const int verifyp = DO_VERIFY; const int argc_min = 3; const int argc_max = 4; int gens_max = 0; char* inboard = NULL; char* outboard = NULL; char* checkboard = NULL; char* final_board = NULL; int nrows = 0; int ncols = 0; FILE* input = NULL; FILE* output = NULL; int err = 0; /* Parse command-line arguments */ if (argc < argc_min || argc > argc_max) { fprintf (stderr, "*** Wrong number of command-line arguments; " "got %d, need at least %d and no more than %d ***\n", argc - 1, argc_min - 1, argc_max - 1); print_usage (argv[0]); exit (EXIT_FAILURE); } err = to_int (&gens_max, argv[1]); if (err != 0) { fprintf (stderr, "*** <num_generations> argument \'%s\' " "must be a nonnegative integer! ***\n", argv[1]); print_usage (argv[0]); exit (EXIT_FAILURE); } /* Open input and output files */ input = fopen (argv[2], "r"); if (input == NULL) { fprintf (stderr, "*** Failed to open input file \'%s\' for reading! ***\n", argv[2]); print_usage (argv[0]); exit (EXIT_FAILURE); } if (argc < argc_max || 0 == strcmp (argv[3], "-")) output = stdout; else { output = fopen (argv[3], "w"); if (output == NULL) { fprintf (stderr, "*** Failed to open output file \'%s\' for writing! ***\n", argv[3]); print_usage (argv[0]); exit (EXIT_FAILURE); } } /* Load the initial board state from the input file */ inboard = load_board (input, &nrows, &ncols); fclose (input); /* Create a second board for ping-ponging */ outboard = make_board (nrows, ncols); /* If we want to verify the result, then make a third board and copy the initial state into it */ if (verifyp) { checkboard = make_board (nrows, ncols); copy_board (checkboard, inboard, nrows, ncols); } /* * Evolve board gens_max ticks, and time the evolution. You will * parallelize the game_of_life() function for this assignment. */ copy_board (outboard, inboard, nrows, ncols); final_board = game_of_life (outboard, inboard, nrows, ncols, gens_max); /* Print (or save, depending on command-line argument <outfilename>) the final board */ save_board (output, final_board, nrows, ncols); if (output != stdout && output != stderr) fclose (output); if (verifyp) { /* Make sure that outboard has the final state, so we can verify it. Since we ping-pong between inboard and outboard, it could be either that inboard == final_board or that outboard == final_board */ copy_board (outboard, final_board, nrows, ncols); /* Ping-pong between checkboard (contains the initial state) and inboard */ final_board = sequential_game_of_life (inboard, checkboard, nrows, ncols, gens_max); if (boards_equalp (final_board, outboard, nrows, ncols)) printf ("Verification successful\n"); else { fprintf (stderr, "*** Verification failed! ***\n"); exit (EXIT_FAILURE); } } /* Clean up */ if (inboard != NULL) free (inboard); if (outboard != NULL) free (outboard); if (checkboard != NULL) free (checkboard); return 0; }