Exemple #1
0
int test_move(char *fen, int side, char *move, int move_correct)
{
	IS_CHECKMATE = IS_DRAW = 0;
	B b = fen_to_board(fen);
	print_board(b);
	printf("\n");
	Move m = find_move(b, side, 180);
	if(m == atomove(move, b) && move_correct) {
		printf("CORRECT\n\n");
		goto SUCESS;
	} else if(m != atomove(move, b) && !move_correct) {
		printf("CORRECT\n\n");
		print_move(m);
		printf("\n%s\n", movetoa(m));
		goto SUCESS;
	} else goto FAILURE;
	SUCESS:
		printf("\n\n#############################\n");
		free(b);
		return 1;
	FAILURE:
		printf("FAILURE: ");
		print_move(m);
		printf("\n%s\n", movetoa(m));
		printf("\n\n#############################\n");
        free(b);
        return 0;
}
Exemple #2
0
void make_move (Move_t m)
{
	if (isValid_Move (board, m))
  	{
		int score;
		Move_t new_move;
		board = transform_Move (board, m);
		print_board (board);
		print_move (m);
		score = evaluate_Static (board);
		if (score == EVAL_POS_INFTY || score == EVAL_NEG_INFTY)
		{
			print_board (board);
			printf ("The End");
			exit (0);
		}
		display ();
		score = search (board, getSearchDepth(board), &new_move);
		print_move (new_move);
		printf ("Score = %d", score);
		score = evaluate_Static (board);
		if (score == EVAL_POS_INFTY || score == EVAL_NEG_INFTY)
		{
			print_board (board);
			printf ("\nThe End\n");
			exit (0);
		}
		new_move = randomizeMove (board, new_move);
		print_move (new_move);
		board = transform_Move (board, new_move);
		print_board (board);
	  }
}
bool NetClient::send_move(const Move &move)
{
	if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
	{
		perror("Socket");
		exit(2);
	}

	if(connect(sock, (sockaddr*)&host, sizeof(host)) == -1)
	{
		perror("Connect");
		exit(3);
	}

	int n = write(sock, &move, sizeof(Move));

	if(n != sizeof(Move))
	{
		fprintf(stderr, "Error sending move\n");
		exit(4);
	}

	print_move("sent: ", move);

	return true;
}
Exemple #4
0
void	begin_game(t_env *e)
{
	if (e->my_turn)
	{
		gnl_until("0123456");
		populate_map(e);
	}
	else
	{
		e->my_turn = 1;
		gnl_until("01234");
		populate_map(e);
	}
	malloc_piece(e);
	populate_piece(e);
	if (!make_move(e))
	{
		print_move(0, 0);
		ft_freemap(e);
		exit(0);
	}
	free_piece(e);
	e->my_turn = 0;
	gnl_until("Plateau");
	begin_game(e);
}
/**
 * Computer player moves
 *
 * @param game_state the game state
 * @return a new game state
 */
struct State * computer_player( struct State * game_state )
{
    struct State * state;
    struct Move * move;
    struct GameNode * root;
    time_t stop;

    /* create a new game node */
    root = new_game_node( game_state, NULL );

    /* computer player */
    time( &timer );
    move = alpha_beta_search( root );
    time( &stop );

    /* print time */
    printf( "Time taken: %d\n", stop - timer );
    printf( "Memory used: %lu\n", memory_usage() );

    /* print move */
    printf( "Move chosen: " );
    print_move( move );

    /* create a new state and apply move */
    state = result( game_state, move );
    Free( move, sizeof( struct Move ) );
    state->player = opposite_player( game_state->player );

    delete_game_node( &root );

    return state;
}
Exemple #6
0
static void
print_move_path(const struct game *original_game,
                const move *m,
                enum move_notation_type mn)
{
    char str[MOVE_STR_BUFFER_LENGTH];
    bool first = true;
    struct game *g = game_copy(original_game);

    if (g == NULL)
        INTERNAL_ERROR();
    for (; *m != 0; ++m) {
        if (!is_uci) {
            if (game_turn(g) == white || first)
                printf("%u. ", game_full_move_count(g));
            if (first && game_turn(g) == black)
                printf("... ");
        }
        first = false;
        (void) print_move(game_current_position(g),
                          *m, str, mn, game_turn(g));
        printf("%s ", str);
        if (game_append(g, *m) != 0)
            INTERNAL_ERROR();
    }
    game_destroy(g);
}
void test_pawn_divide(int depth)
{
  int i,move_count;
  move_t ms[MOVE_STACK];
  uint64 nodes = 0;
  uint64 total_nodes = 0;
  int legal_moves = 0;
  
  if(!depth) return;
  pawnhits = 0;
  pawncollisions = 0;
  pht_init();
  depth -= 1;
  timer_start();
  move_count = move_gen(&ms[0]);
  for(i = 0; i < move_count; i++)
  {
    if(!move_make(ms[i])) continue;
    nodes = perft(depth);
    print_move(ms[i].p);
    legal_moves++;
    printf("%I64u",nodes);
    printf("\n");
    move_undo();
    total_nodes += nodes;
  }
  printf("\nNodes: %I64u",total_nodes);
  printf("\nMoves: %d",legal_moves);
  printf("\nPawnHits: %d",pawnhits);
  printf("\nPawnCollisions: %d",pawncollisions);
  printf("\n\n");
  timer_stop();
  pht_free();
}
Exemple #8
0
int     display_solution1(t_list *path, int ants)
{
	t_list	    *active;
    t_list      *inc;
    int			act_ants;

    *active = NULL;
    while (ants > 0 || active != NULL)
    {
        inc = active;
        while (inc != NULL)
        {
            inc->room = find_next_link_room(path, inc->room);
            inc = inc->next;
        }
        if (ants > 0)
        {
            add_room_link(active, path->room);
            //need to add a variable to room, to keep track of which ant is in it.
            ants--;
            act_ants++;
        }
        print_move(active);
    }
}
Exemple #9
0
void print_piece_moves(Move* head, Pos pos){
	while (head != NULL){
		if (head->piece.col == pos.col && head->piece.row == pos.row){
		print_move(head);
		}
		head = head->next;
	}
}
Exemple #10
0
void print_best_moves(Move* head, int score){
	while (head != NULL){
		if (head->score == score){
			print_move(head);
		}
		head = head->next;
	}
}
struct GameNode * computer_player( struct GameNode * game_state )
{
    struct State * state;
    struct Move * move;
    struct GameNode * new_game_state;
    //struct timeval start, end, diff;
    struct timespec start, end , diff;
    time_t stop;
    
    /* computer player */
    //gettimeofday(&start,NULL);
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
    time( &timer );
    move = alpha_beta_search( game_state );
    time( &stop );
    //gettimeofday(&end,NULL);
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);

    /* print time */
    //printf( "Time taken: %d\n", stop - timer );
    /*if( (end.tv_usec - start.tv_usec) < 0) {
      diff.tv_sec = end.tv_sec - start.tv_sec;
      diff.tv_usec = 0.000001 * (end.tv_usec - start.tv_usec);
      printf( "Time Taken: %ld.%06ld\n", diff.tv_sec, diff.tv_usec);
      
    }
    else {
      printf( "Time Taken: %ld.%06ld\n", (end.tv_sec - start.tv_sec), (end.tv_usec - start.tv_usec));
      }*/
    //printf("%lld.%.9ld", (long long)ts.tv_sec, ts.tv_nsec);

    if((end.tv_sec - start.tv_sec) < 0){
      diff.tv_sec = end.tv_sec - start.tv_sec;
      diff.tv_nsec = 1000000000 + (end.tv_nsec - start.tv_nsec);
      printf("Time Taken: %lld.%.9ld", (long long)diff.tv_sec, diff.tv_nsec);
    }
    else{
      printf("Time Taken: %lld.%.9ld", (long long)(end.tv_sec - start.tv_sec), end.tv_nsec - start.tv_nsec);
    }


    /* print move */
    printf( "Move chosen: " );
    print_move( move );

    /* create a new state and apply move */
    state = result( game_state->state, move );
    state->player = opposite_player( game_state->state->player );

    /* create a new game node */
    new_game_state = new_game_node( state, game_state );

    /* add new node to parent node */
    add_child_game_node( game_state, new_game_state );
    
    return new_game_state;
}
Exemple #12
0
/** Funcao que imprime o jogador*/
void print_player(ESTADO e){
	print_image(e.jog.x, e.jog.y, TAM,PLAYER);
	print_move(e,+1,0);
	print_move(e,-1,0);
	print_move(e,0,+1);
	print_move(e,0,-1);
	print_move(e,+1,+1);
	print_move(e,-1,-1);
	print_move(e,+1,-1);
	print_move(e,-1,+1);
}
Exemple #13
0
void print_combo(Combo c){
  Combo tmp = c;
  while(tmp){
    print_move(tmp->m);
    if(tmp->note) printf("(%s)", tmp->note);
    tmp = tmp->next;
    if(tmp) printf(",");
  }
  printf("\n");
}
Exemple #14
0
void test_move(char *fen, int side, char *correct_move)
{
	B b = fen_translator(fen);
	print_board(b);
	printf("\n");
	Move m = find_move(b, side, 30);
	if(m == atomove(correct_move, b)) printf("CORRECT");
	else print_move(m);
	printf("\n");
	free(b);
}
void test_divide(int depth)
{
  int i,move_count;
  move_t ms[MOVE_STACK];
  uint64 nodes;
  uint64 total_nodes;
  int legal_moves;
  #ifdef EVASIONS
  char strbuff[256];
  move_t ms_test[MOVE_STACK];
  #endif
  
  if(!depth) return;
  nodes = 0;
  total_nodes = 0;
  legal_moves = 0;
  
  pht_init();
  depth -= 1;
  timer_start();
    
  #ifdef EVASIONS
  if(is_in_check(board->side))
  { move_count = move_gen_evasions(&ms[0]);
    if(move_count != move_gen_legal(&ms_test[0]))
    { board_to_fen(&strbuff[0]);
      printf("error: \n %s \n",strbuff);
    }
  }
  else
  { move_count = move_gen(&ms[0]);
  }
  #else
  move_count = move_gen(&ms[0]);
  #endif
    
  for(i = 0; i < move_count; i++)
  { if(!move_make(ms[i])) continue;
    nodes = perft(depth);
    print_move(ms[i].p);
    legal_moves++;
    printf("%I64u",nodes);
    printf("\n");
    move_undo();
    total_nodes += nodes;
  }
  printf("\nNodes: %I64u",total_nodes);
  printf("\nMoves: %d",legal_moves);
  printf("\n\n");
  timer_stop();
  pht_free();
}
struct GameNode * human_player_second( struct GameNode * game_state )
{
    char input[ INPUT_SIZE ];
    struct State * state;
    struct Move * move;
    struct GameNode * new_game_state;

    /* must remove piece orthoganally adjacent */

    printf( "Please remove a piece of your color adjacent to the piece removed\n" );

    /* get input */
    do 
    {
        printf( "Please enter a move: " );
        fgets( input, INPUT_SIZE, stdin );

        move = translate_first_in_move( input );

        /* make sure piece is valid */
        if( move != NULL )
        {
            if( validate_second_in_move( game_state->state, move ) == 0 )
            {
                /* invalid move */
                Free( move, sizeof( struct Move ) );
            }
            else
                break;
        }
    }
    while( 1 );

    printf( "Move chosen: " );
    print_move( move );

    /* create a new state */
    state = new_state( game_state->state->board, opposite_player( game_state->state->player ) );

    /* apply move */
    state->board[ move->start_row ][ move->start_col ] = 'O';

    /* create a new game node */
    new_game_state = new_game_node( state, game_state );

    /* add new node to parent node */
    add_child_game_node( game_state, new_game_state );

    return new_game_state;
    
}
Exemple #17
0
Fichier : ai.c Projet : ejrh/ejrh
void print_game(GAME *game)
{
    int i;

    printf("GAME(%x), %d players, current %d, owner %d, pile ", game, game->num_players, game->current_player, game->pile_owner);
    print_move(game->pile);
    printf("\n");

    for (i = 0; i < game->num_players; i++)
    {
        printf("    rank %d, hand ", game->players[i].rank);
        print_deck(game->players[i].hand);
        printf("\n");
    }
}
Exemple #18
0
static void
cmd_hint(void)
{
    move m;
    char str[MOVE_STR_BUFFER_LENGTH];

    mtx_lock(&game_mutex);

    if (engine_get_best_move(&m) == 0) {
        print_move(current_position(), m, str, conf->move_not, turn());
        printf("Hint: %s\n", str);
    }

    mtx_unlock(&game_mutex);
}
Exemple #19
0
// manages the computer's turn
int computer_turn(char board[BOARD_SIZE][BOARD_SIZE],COLOR color){
	curr_player = color;
	alpha_beta_minimax(board, color, 0, -100, 100);
	Move * move2do = best_move;
	int ret_val;
	if (move2do == NULL) ret_val = WIN_POS;
	else{ 
		exc_move(board, move2do);
		printf("Computer: move ");
		print_move(move2do);
		print_board(board);
		ret_val = GAME_ON;
	}
	clear_old_moves(moves_head);
	return ret_val;
}
static void uci_go(char *options) {
  char *ponder, *infinite;
  char *c;
  int depth=0, nodes=0, movetime=0;
  move_t moves[MAX_ROOT_MOVES];

  int time=0;

  moves[0] = 0;

  infinite = strstr(options, "infinite");
  ponder = strstr(options, "ponder");

  c = strstr(options, "time");
  if(c != NULL) sscanf(c+5, "%d", &time);

  /*
  c = strstr(options, "btime");
  if(c != NULL) sscanf(c+6, "%d", &btime);
  c = strstr(options, "winc");
  if(c != NULL) sscanf(c + 5, "%d", &winc);
  c = strstr(options, "binc");
  if(c != NULL) sscanf(c + 5, "%d", &binc);
  c = strstr(options, "movestogo");
  if(c != NULL) sscanf(c + 10, "%d", &movestogo);
  */

  c = strstr(options, "depth");
  if(c != NULL) sscanf(c + 6, "%d", &depth);
  c = strstr(options, "nodes");
  if(c != NULL) sscanf(c + 6, "%d", &nodes);
  /*
  c = strstr(options, "mate");
  if(c != NULL) sscanf(c + 5, "%d", &mate);
  c = strstr(options, "movetime");
  if(c != NULL) sscanf(c + 9, "%d", &movetime);
  c = strstr(options, "searchmoves");
  */
  if(c != NULL) uci_parse_searchmoves(c + 12, moves);

  think(RootPosition, infinite != NULL, time, depth, nodes, movetime, moves);
  printf("bestmove "); print_move(RSI->bestmove);

  printf("\n");
  RSI->thinking_status = IDLE;
}
struct GameNode * human_player( struct GameNode * game_state )
{
    char input[ INPUT_SIZE ];
    struct State * state;
    struct Move * move;
    struct GameNode * new_game_state;

    /* get input */
    do 
    {
        printf( "Please enter a move: " );
        fgets( input, INPUT_SIZE, stdin );

        move = translate_in_move( input );

        if( move != NULL )
        {
            if( validate_action( game_state->state, move ) == 1 )
                break;
            else
            {
                Free( move, sizeof( struct Move ) );
                move = NULL;
            }
        }
    }
    while( move == NULL );

    /* print move */
    printf( "Move chosen: " );
    print_move( move );

    /* create a new state & apply move */
    state = result( game_state->state, move );
    state->player = opposite_player( game_state->state->player );

    /* create a new game node */
    new_game_state = new_game_node( state, game_state );

    /* add new node to parent node */
    add_child_game_node( game_state, new_game_state );

    return new_game_state;

}
struct GameNode * human_player_first( struct GameNode * game_state )
{
    char input[ INPUT_SIZE ];
    struct State * state;
    struct Move * move;
    struct GameNode * new_game_state;

    printf( "To begin game, please remove one piece from the board.\n" );
    printf( "Valid moves are from the 4 center squares, or one of the corners.\n" );
    printf( "Piece removed must be a piece of your color!\n" );

    /* get input */
    do 
    {
        printf( "Please enter a move: " );
        fgets( input, INPUT_SIZE, stdin );

        move = translate_first_in_move( input );

        /* make sure piece is of players color */
        if( game_state->state->board[ move->start_row ][ move->start_col ] != 'B' )
        {
            /* invalid move */
            Free( move, sizeof( struct Move ) );
        }
    }
    while( move == NULL );

    /* create a new state */
    state = new_state( game_state->state->board, opposite_player( game_state->state->player ) );

    /* apply move */
    state->board[ move->start_row ][ move->start_col ] = 'O';
    print_move( move );

    /* create a new game node */
    new_game_state = new_game_node( state, game_state );

    /* add new node to parent node */
    add_child_game_node( game_state, new_game_state );

    return new_game_state;
}
Exemple #23
0
int main(int argc, char* argv[]) {
    setlocale(LC_ALL, "");

    board_t board;
    RESET_BOARD(board);
    char buffer[256];
    int len;

    hash_init();
    run_all_tests();
    print_board(&board);

    move_t m;
    while (1) {
    top:
        print_board(&board);
        move_t move = 0;
        while (move == 0)
        { 
            printf("enter move> ");
            fgets(buffer, 255, stdin);
            if (!strcmp(buffer, "undo\n")) {
                undo_move(&board, m);            
                goto top;
            } else if (!strcmp(buffer, "exit\n")) {
                return 0;
            }
            len = strlen(buffer);
            if (buffer[len-1] == '\n') buffer[len-1] = '\0';
            move = move_from_text(buffer, &board);
        }
        make_move(&board, move);
        print_board(&board);

        printf("Calculating...\n");
        m = think(&board);
        make_move(&board, m);
        printf("Done calculating...\n");
        print_move(m);
    }
    return 0;
}
Exemple #24
0
static void			add_new_ant(t_node **sender, t_final *optimal, t_gar gar,
		int *printed)
{
	int		*i;
	int		*id;
	int		*ants;

	i = gar.i;
	id = gar.id;
	ants = gar.ants;
	sender[*i][1].ant_no = *id;
	(*id)++;
	optimal->paths[*i].ants--;
	(*i)++;
	print_move(sender[*i - 1][1].ant_no, sender[*i - 1][1].room_name, printed);
	if (!sender[*i - 1][2].room_name)
	{
		sender[*i - 1][1].ant_no = 0;
		(*ants)--;
	}
}
Exemple #25
0
/*! \brief This is an example demonstrating the accelerometer
 *         functionalities using the accelerometer driver.
 */
int main(void)
{
  volatile unsigned long i;

  // switch to oscillator 0
  pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);

  // Initialize the debug USART module.
  init_dbg_rs232(FOSC0);

  acc_init();

  // do a loop
  for (;;)
  {
    // slow down operations
    for ( i=0 ; i < 50000 ; i++);

    // display a header to user
    print_dbg("\x1B[2J\x1B[H\r\n\r\nAccelerometer Example\r\n\r\n");

    // Get accelerometer acquisition and process data
    acc_update();

    // test for fast or slow changes
    // depending on that, play with angles or moves
    if ( is_acc_slow() )
    {
      print_mouse() ;
      print_angles() ;
    }
    else
      print_move() ;

    // MEUH
    // only text here , needs to be combined with PWM
    // meuh_stop is the "end" signal from PWM
    meuh_en = is_acc_meuh( meuh_stop ) ;
  }
}
Exemple #26
0
static void
print_computer_move(move m)
{
    char str[MOVE_STR_BUFFER_LENGTH];
    enum move_notation_type mn;
    unsigned move_counter;
    bool is_black;

    if (is_xboard || is_uci)
        mn = mn_coordinate;
    else
        mn = conf->move_not;

    mtx_lock(&game_mutex);

    (void) print_move(current_position(), m, str, mn, turn());
    move_counter = game_full_move_count(game);
    is_black = (turn() == black);

    mtx_unlock(&game_mutex);

    mtx_lock(&stdout_mutex);

    tracef("%s %s", __func__, str);

    if (is_xboard) {
        printf("move %s\n", str);
    }
    else if (is_uci) {
        printf("bestmove %s\n", str);
    }
    else {
        printf("%u. ", move_counter);
        if (is_black)
            printf("... ");
        puts(str);
    }

    mtx_unlock(&stdout_mutex);
}
Exemple #27
0
static int			move_existing_ants(t_node **sender, t_pt pt, int printed,
		int *ants)
{
	while (pt.j >= 0)
	{
		if (sender[pt.i][pt.j].ant_no == 0 && (pt.j - 1 >= 0) &&
				sender[pt.i][pt.j - 1].ant_no != 0)
		{
			sender[pt.i][pt.j].ant_no = sender[pt.i][pt.j - 1].ant_no;
			sender[pt.i][pt.j - 1].ant_no = 0;
			print_move(sender[pt.i][pt.j].ant_no, sender[pt.i][pt.j].room_name,
					&printed);
			if (!sender[pt.i][pt.j + 1].room_name)
			{
				sender[pt.i][pt.j].ant_no = 0;
				(*ants)--;
			}
		}
		pt.j--;
	}
	return (printed);
}
bool NetClient::poll_reply(Move &move)
{
	if(sock == -1)
		return false;

	fd_set read_fd;
	FD_ZERO(&read_fd);
	FD_SET(sock, &read_fd);

	if(select(sock+1, &read_fd, NULL, NULL, NULL) == -1)
	{
		perror("Select");
		return false;
	}

	if(FD_ISSET(sock, &read_fd))
	{
		printf("... ");

		int n = read(sock, &move, sizeof(Move));

		if(n != sizeof(Move))
		{
			fprintf(stderr, "Error receiving move\n");
			exit(1);
		}

		print_move("received: ", move);

		shutdown(sock, SHUT_RDWR);
		close(sock);
		sock = -1;

		return true;
	}

	return false;
}
Exemple #29
0
void			iter_path(t_list *list, int path_length)
{
	t_list		*link;
	t_salle		*s;

	link = list;
	while (((t_salle*)link->content)->status != END)
		link = link->next;
	TC->useless = 2;
	s = ((t_salle *)link->content);
	link = s->link;
	while (link != NULL && ((t_salle *)link->content)->status != START)
	{
		link = s->link;
		link = norme_iter(link, path_length, s->dist);
		if (link == NULL)
			break ;
		TC->useless = 2;
		if (TC->indice != 0)
			print_move(link);
		s = ((t_salle *)link->content);
		link = s->link;
	}
}
Exemple #30
0
static void
add_move(move m)
{
    mtx_lock(&game_mutex);


    char move_str[MOVE_STR_BUFFER_LENGTH];

    (void) print_move(current_position(), m, move_str, mn_san, turn());
    if (game_append(game, m) == 0) {
        engine_process_move(m);
        debug_engine_set_player_to_move(turn());
        tracef("repro: %s", move_str);

        if (is_end()) {
            game_started = false;

            if (is_checkmate() && turn() == white)
                puts("0-1 {Black mates}");
            else if (is_checkmate() && turn() == black)
                puts("1-0 {White mates}");
            else if (is_stalemate())
                puts("1/2-1/2 {Stalemate}");
            else if (is_draw_by_insufficient_material())
                puts("1/2-1/2 {No mating material}");
            else if (is_draw_by_repetition())
                puts("1/2-1/2 {Draw by repetition}");
            else if (is_draw_by_50_move_rule())
                puts("1/2-1/2 {Draw by 50 move rule}");
            else
                abort();
        }
    }

    mtx_unlock(&game_mutex);
}