int main() { initscr(); cbreak(); noecho(); keypad(stdscr, TRUE); // make keys work curs_set(0); // hide cursor timeout(100); int xmax; int ymax; getmaxyx(stdscr, ymax, xmax); enum Direction dir = RIGHT; Board* board = create_board(create_snake(), NULL, xmax, ymax); int i; for (i = 0; i < 6; i++) { add_new_food(board); } while(true) { erase(); display_points(board->snake, ACS_BLOCK); display_points(board->foods, ACS_DIAMOND); dir = get_next_move(dir); enum Status status = move_snake(board, dir); if (status == FAILURE) break; } endwin(); return 0; }
/* * This is the main loop of the connect four game. It first prints a * welcome message, allocates the board, and then begins the game loop */ int main() { puts("Welcome to Connect Four\n"); int* board = new_board(); while (1) { print_board(board); int column = -1; // Get human input until they enter a valid choice... while (1) { printf("Enter your move (column number): "); scanf("%d", &column); if (column < 0 || column >= WIDTH) { printf("Input a valid choice (0 - 6).\n"); } else if (place(board, column, 1) == 0) { printf("That column is full.\n"); } else { break; } } // Did the human input make him or her win? if (check_win(board, 1) != 0) { print_board(board); printf("Player 1 wins!\n"); break; } // Do the AI's move... int ai_column = get_next_move(evaluate_board(board)); place(board,ai_column, 2); // Did the AI's move cause it to win? if (check_win(board, 2) != 0) { print_board(board); printf("Player 2 wins!\n"); break; } } free(board); exit(EXIT_SUCCESS); }
/* * Legge la mossa successiva del giocatore COMPUTER. * * Restituisce 1 se l'operazione e' andata a buon fine, 0 altrimenti. */ int read_move_computer_player(int PLAYER) { unsigned int i,j; printf("\nPlayer%d - COMPUTER (%c): ",PLAYER,PLAYER==1?'X':'O'); if(!get_next_move(&i,&j)) return 0; printf("%c%c\n",(char)i+BASE_INDEX,(char)j+BASE_INDEX); if(PLAYER==1) return set_move_player1(i,j); else return set_move_player2(i,j); }
int main(int argc, char** argv){ int tmp; printf("%s\n", PROGRAM_NAME); fflush(stdout); scanf("%d", &tmp); srand(tmp); scanf("%d", &game_length); my_history = malloc(sizeof(int)*game_length); opp_history = malloc(sizeof(int)*game_length); while(1){ tmp = get_next_move(); printf("%d\n", tmp); my_history[turns_passed] = tmp; fflush(stdout); scanf("%d", opp_history+turns_passed); turns_passed ++; } return 0; }
__kernel void findRoute(__global double *graph, __global double* next_moves, __global double *output, __global int* constK, __global double* messages, __global double* rands ) { int k = constK[0]; //next_moves has size k*2*k // graph has size edges*4 // output has size k*k int num_edges = k*(k-1)/2; double alpha = 2.0; double beta = 2.0; int idx = get_global_id(0); int idy = get_global_id(1); int current_position = idx; add_to_array(output, k, current_position, idx, messages); bool possible_to_move = true; int count = 0; while(possible_to_move){ possible_to_move = false; double sum = 0.0; for (int i = 0; i < num_edges; i++){ int edge_start = i*4; int possible_goal; bool fits = false; if (graph[edge_start]==current_position){ possible_goal = edge_start+1; fits = true; } else if(graph[edge_start+1]==current_position) { possible_goal = edge_start; fits = true; } if (fits && not_visited(output, graph, possible_goal, k, idx)){ possible_to_move = true; double cost = graph[edge_start + 2]; double pheromones = graph[edge_start + 3]; double thao = pow(pheromones, alpha); double attr = pow(cost, -beta); double nomin = thao * attr; sum = sum + nomin; add_nominative(next_moves, nomin, edge_start, k, idx, messages); } } if (possible_to_move){ for (int i = idx*k*2; i < idx*k*2+k*2; i=i+2){ next_moves[i] = next_moves[i] / sum; } double random = get_random(rands, k, idx, count); int edge_choice_index = get_next_move(next_moves, random, k, idx); // in that edge, one value is our current position and the other value // is the next node double next_node = -1.0; if (current_position == graph[edge_choice_index]){ // e.g. edge 1-2 and we are in 1. Put 2 in output and into current pos next_node = graph[edge_choice_index + 1]; } else { // e.g. edge 2-1 and we are in 1. Put 2 in output and into current pos next_node = graph[edge_choice_index]; } add_to_array(output, k, next_node, idx, messages); current_position = next_node; } else { return; } clear_next_moves(next_moves, k, idx); count = count + 1; } }