Esempio n. 1
0
/* Put fixed placement handicap stones on the board. The placement is
 * compatible with the GTP fixed handicap placement rules.
 */
void
place_fixed_handicap(int handicap)
{
  int low = board_size >= 13 ? 3 : 2;
  int mid = board_size / 2;
  int high = board_size - 1 - low;
  
  if (handicap >= 2) {
    play_move(high, low, BLACK);   /* bottom left corner */
    play_move(low, high, BLACK);   /* top right corner */
  }
  
  if (handicap >= 3)
    play_move(low, low, BLACK);    /* top left corner */
  
  if (handicap >= 4)
    play_move(high, high, BLACK);  /* bottom right corner */
  
  if (handicap >= 5 && handicap % 2 == 1)
    play_move(mid, mid, BLACK);    /* tengen */
  
  if (handicap >= 6) {
    play_move(mid, low, BLACK);    /* left edge */
    play_move(mid, high, BLACK);   /* right edge */
  }
  
  if (handicap >= 8) {
    play_move(low, mid, BLACK);    /* top edge */
    play_move(high, mid, BLACK);   /* bottom edge */
  }
}
Esempio n. 2
0
void play_events(game newGame, int *click, bool *gameOver)
{

    MLV_Keyboard_button touche;
    MLV_Button_state state;
    MLV_Event event;
    int mouseX, mouseY;
    if (!click || !newGame) {
        fprintf(stderr, "play_events parameters error");
        exit(EXIT_FAILURE);
    }
    do {
        //
        // Récupère un évènement dans la file des évènements
        // non traités.
        //
        event = MLV_get_event(
                &touche, NULL, NULL,
                NULL, NULL,
                &mouseX, &mouseY, NULL,
                &state
                );
        //
        // Traite l'évènement s'il s'agit d'un évènement clavier
        //

        if (event == MLV_KEY || MLV_MOUSE_BUTTON) {
            if (event == MLV_MOUSE_BUTTON && state == MLV_PRESSED) {
                *click = get_car_with_mouse(mouseX, mouseY, newGame);
            }
            if (event == MLV_MOUSE_BUTTON && state == MLV_RELEASED) {
                *click = -1;
            }
            if (event == MLV_MOUSE_MOTION && *click != -1) {
                cpiece tmp = game_piece(newGame, *click);
                int x = get_x(tmp);
                int y = game_height(newGame) - get_y(tmp) - get_height(tmp);
                if (mouseY > (y + get_height(tmp)) * RATIO)
                    play_move(newGame, *click, DOWN, 1);
                else if (mouseY < y * RATIO)
                    play_move(newGame, *click, UP, 1);
                else if (mouseX > (x + get_width(tmp)) * RATIO)
                    play_move(newGame, *click, RIGHT, 1);
                else if (mouseX < x * RATIO)
                    play_move(newGame, *click, LEFT, 1);
            }
            if(event == MLV_KEY && touche == 'q') {
                *gameOver = true;
            }
        }
    } while (event != MLV_NONE);
}
Esempio n. 3
0
static void
benchmark(int num_games_per_point)
{
  int i, j;
  unsigned int random_seed = 1U;
  board_size = 9;
  komi = 0.5;

  init_brown(random_seed);

  for (i = 0; i < board_size; i++) {
    for (j = 0; j < board_size; j++) {
      int white_wins = 0;
      int black_wins = 0;
      int k;
      for (k = 0; k < num_games_per_point; k++) {
	int passes = 0;
	int num_moves = 1;
	int color = WHITE;
        clear_board();
	play_move(i, j, BLACK);
        while (passes < 3 && num_moves < 600) {
	  int m, n;
          generate_move(&m, &n, color);
          play_move(m, n, color);
          if (pass_move(m, n)) {
            passes++;
          }
          else {
            passes = 0;
          }
          num_moves++;
          color = OTHER_COLOR(color);
        }
	if (passes == 3) {
	  if (compute_score() > 0) {
	    white_wins++;
	  }
	  else {
	    black_wins++;
	  }
	}
      }
      /*
      printf("%d %d %f\n", i, j,
	     (float) black_wins / (float) (black_wins + white_wins));
      */
    }
  }
}
Esempio n. 4
0
int GameState::set_fixed_handicap_2(int handicap) {
    int board_size = board.get_boardsize();
    int low = board_size >= 13 ? 3 : 2;
    int mid = board_size / 2;
    int high = board_size - 1 - low;

    int interval = (high - mid) / 2;
    int placed = 0;

    while (interval >= 3) {
        for (int i = low; i <= high; i += interval) {
            for (int j = low; j <= high; j += interval) {
                if (placed >= handicap) return placed;
                if (board.get_square(i-1, j-1) != FastBoard::EMPTY) continue;
                if (board.get_square(i-1, j) != FastBoard::EMPTY) continue;
                if (board.get_square(i-1, j+1) != FastBoard::EMPTY) continue;
                if (board.get_square(i, j-1) != FastBoard::EMPTY) continue;
                if (board.get_square(i, j) != FastBoard::EMPTY) continue;
                if (board.get_square(i, j+1) != FastBoard::EMPTY) continue;
                if (board.get_square(i+1, j-1) != FastBoard::EMPTY) continue;
                if (board.get_square(i+1, j) != FastBoard::EMPTY) continue;
                if (board.get_square(i+1, j+1) != FastBoard::EMPTY) continue;
                play_move(FastBoard::BLACK, board.get_vertex(i, j));
                placed++;
            }
        }
        interval = interval / 2;
    }

    return placed;
}
Esempio n. 5
0
void GameState::place_free_handicap(int stones) {
    int limit = board.get_boardsize() * board.get_boardsize();
    if (stones > limit / 2) {
        stones = limit / 2;
    }

    int orgstones = stones;

    int fixplace = std::min(9, stones);

    set_fixed_handicap(fixplace);
    stones -= fixplace;

    stones -= set_fixed_handicap_2(stones);

    for (int i = 0; i < stones; i++) {
        auto search = std::make_unique<UCTSearch>(*this);

        int move = search->think(FastBoard::BLACK, UCTSearch::NOPASS);
        play_move(FastBoard::BLACK, move);
    }

    if (orgstones)  {
        board.set_to_move(FastBoard::WHITE);
    } else {
        board.set_to_move(FastBoard::BLACK);
    }

    anchor_game_history();

    set_handicap(orgstones);
}
Esempio n. 6
0
static int
gtp_set_free_handicap(char *s)
{
  int i, j;
  int n;
  int handicap = 0;
  
  if (!board_empty())
    return gtp_failure("board not empty");

  while ((n = gtp_decode_coord(s, &i, &j)) > 0) {
    s += n;
    
    if (get_board(i, j) != EMPTY) {
      clear_board();
      return gtp_failure("repeated vertex");
    }

    play_move(i, j, BLACK);
    handicap++;
  }

  if (sscanf(s, "%*s") != EOF) {
      clear_board();
      return gtp_failure("invalid coordinate");
  }

  if (handicap < 2 || handicap >= board_size * board_size) {
      clear_board();
      return gtp_failure("invalid handicap");
  }

  return gtp_success("");
}
Esempio n. 7
0
/**
 * @brief ui_ggs_update
 *
 * Update a game.
 *
 * @param ui User Interface.
 */
static void ui_ggs_update(UI *ui) {
	char buffer[256], s_move[4];
	Play *play;
	int edax_turn, turn;
	Board board[1];
	
	printf("[received GGS_BOARD_UPDATE]\n");

	// set correct played game
	if (strcmp(ui->ggs->board->player[0].name, ui->ggs->me) == 0) {
		play = ui->play;
		edax_turn = BLACK;
	} else if (strcmp(ui->ggs->board->player[1].name, ui->ggs->me) == 0) {
		play = ui->play + 1;
		edax_turn = WHITE;
	} else {
		return ;
	}

	if (!ui->ggs->board->match_type->is_synchro) play = ui->play;
		
	// set board as an edax's board
	sprintf(buffer, "%s %c", ui->ggs->board->board, ui->ggs->board->turn);
	turn = board_set(board, buffer);

	// update the board... if possible
	if (!play_move(play, ui->ggs->board->move)) {
		info("<Updating with bad move %s>\n", move_to_string(ui->ggs->board->move, play->player, s_move));
	}

	if (!board_equal(board, play->board)) { // may happens when game diverges
		info("<Resynchronize boards: diverging games>\n");
		*play->board = *board; play->player = turn;
	}

	if (turn != play->player) { // should never happen: TODO fatal error?
		printf("[WARNING: updating player's turn]\n");
		play->player = turn;
	}

	printf("[%s's turn in game %s]\n", ui->ggs->board->player[play->player].name, ui->ggs->board->id);

	// playing same game... ?
	ui->is_same_play = (!ui->ggs->board->match_type->is_synchro || board_equal(ui->play[0].board, ui->play[1].board));
	if (ui->is_same_play) printf("<Playing same game...>\n");

	// set time & start thinking
	if (play->player == edax_turn) {
		printf("<My turn>\n");
		ui_ggs_play(ui, edax_turn);
	} else {
		printf("<Opponent turn>\n");
		ui_ggs_ponder(ui, edax_turn);
	}
}
Esempio n. 8
0
/* Put free placement handicap stones on the board. We do this simply
 * by generating successive black moves.
 */
void
place_free_handicap(int handicap)
{
  int k;
  int i, j;
  
  for (k = 0; k < handicap; k++) {
    generate_move(&i, &j, BLACK);
    play_move(i, j, BLACK);
  }
}
Esempio n. 9
0
bool GameState::set_fixed_handicap(int handicap) {
    if (!valid_handicap(handicap)) {
        return false;
    }

    int board_size = board.get_boardsize();
    int high = board_size >= 13 ? 3 : 2;
    int mid = board_size / 2;

    int low = board_size - 1 - high;
    if (handicap >= 2) {
        play_move(FastBoard::BLACK, board.get_vertex(low, low));
        play_move(FastBoard::BLACK, board.get_vertex(high, high));
    }

    if (handicap >= 3) {
        play_move(FastBoard::BLACK, board.get_vertex(high, low));
    }

    if (handicap >= 4) {
        play_move(FastBoard::BLACK, board.get_vertex(low, high));
    }

    if (handicap >= 5 && handicap % 2 == 1) {
        play_move(FastBoard::BLACK, board.get_vertex(mid, mid));
    }

    if (handicap >= 6) {
        play_move(FastBoard::BLACK, board.get_vertex(low, mid));
        play_move(FastBoard::BLACK, board.get_vertex(high, mid));
    }

    if (handicap >= 8) {
        play_move(FastBoard::BLACK, board.get_vertex(mid, low));
        play_move(FastBoard::BLACK, board.get_vertex(mid, high));
    }

    board.set_to_move(FastBoard::WHITE);

    anchor_game_history();

    set_handicap(handicap);

    return true;
}
Esempio n. 10
0
map createNewState(map m, int nPiece, dir d, int dist)
{
	game g = new_game(1, 1, 0, NULL);
	copy_game(m->g, g);
	map r = NULL;
	if (play_move(g, nPiece, d, dist)){
		r = newMap(g, m);
		delete_game(g);
	}
	else
		delete_game(g);
	return r;
}
Esempio n. 11
0
void play_input(game newGame, int ch, int *choosenCar)
{
    if (ch >= '0' && ch <= '9') {
        *choosenCar = ch - '0';
    }
    if (*choosenCar != -1 && *choosenCar < game_nb_pieces(newGame)) {
        switch (ch) {
        case KEY_LEFT:
            play_move(newGame, *choosenCar, LEFT, 1);
            break;
        case KEY_RIGHT:
            play_move(newGame, *choosenCar, RIGHT, 1);
            break;
        case KEY_DOWN:
            play_move(newGame, *choosenCar, DOWN, 1);
            break;
        case KEY_UP:
            play_move(newGame, *choosenCar, UP, 1);
            break;
        }
    }
}
Esempio n. 12
0
/**
 * @brief ui_ggs_join
 *
 * Join a new game.
 * This is a new game from Edax point of view.
 * This may be a saved game from GGS side.
 *
 * @param ui User Interface.
 */
static void ui_ggs_join(UI *ui) {
	char buffer[256];
	char s_move[4];
	Play *play;
	int edax_turn, i;
	
	printf("[received GGS_BOARD_JOIN]\n");

	// set correct played game
	if (strcmp(ui->ggs->board->player[0].name, ui->ggs->me) == 0) {
		play = ui->play;
		edax_turn = BLACK;
	} else if (strcmp(ui->ggs->board->player[1].name, ui->ggs->me) == 0) {
		play = ui->play + 1;
		edax_turn = WHITE;
	} else {
		warn("Edax is not concerned by this game\n");
		return ;
	}

	// non synchro games => play a single match
	if (!ui->ggs->board->match_type->is_synchro) play = ui->play;

	// set board
	sprintf(buffer, "%s %c", ui->ggs->board->board_init, ui->ggs->board->turn_init);
	play_set_board(play, buffer);

	for (i = 0; i < ui->ggs->board->move_list_n; i++) {
		if (!play_move(play, ui->ggs->board->move_list[i])) {
			error("cannot play GGS move %s ?", move_to_string(ui->ggs->board->move_list[i], play->player, s_move));
			break;
		}
	}
	printf("[%s's turn in game %s]\n", ui->ggs->board->player[play->player].name, ui->ggs->board->id);
	board_print(play->board, play->player, stdout);

	ui->is_same_play = (ui->ggs->board->move_list_n == 0 || board_equal(ui->play[0].board, ui->play[1].board) || !ui->ggs->board->match_type->is_synchro);
	if (ui->is_same_play) printf("[Playing same game]\n");

	// set time & start thinking
	if (play->player == edax_turn) {
		printf("<My turn>\n");
		ggs_client_send(ui->ggs, "tell .%s =====================================\n", ui->ggs->me);
		ui_ggs_play(ui, edax_turn);
		ui_ggs_ponder(ui, edax_turn);
	} else {
		printf("[Waiting opponent move]\n");
//		ui_ggs_ponder(ui, edax_turn);
	}
}
Esempio n. 13
0
game play_solution(game newGame, gameStruct *result, int n)
{
    char *pch, *sch;
    char *save_move = malloc(strlen(result->move) + 1);
    strcpy(save_move, result->move);
    pch = strtok(save_move, "\n");
    for (int i = 0; i < n && pch != NULL; i++) {
        pch = strtok(NULL, "\n");
    }
    sch = strtok(pch, " ");
    int piece = atoi(sch);
    sch = strtok(NULL, " ");
    if (strcmp(sch, "UP") == 0) {
        play_move(newGame, piece, UP, 1);
    } else if (strcmp(sch, "DOWN") == 0) {
        play_move(newGame, piece, DOWN, 1);
    } else if (strcmp(sch, "LEFT") == 0) {
        play_move(newGame, piece, LEFT, 1);
    } else if (strcmp(sch, "RIGHT") == 0) {
        play_move(newGame, piece, RIGHT, 1);
    }
    free(save_move);
    return newGame;
}
Esempio n. 14
0
static int
gtp_play(char *s)
{
  int i, j;
  int color = EMPTY;

  if (!gtp_decode_move(s, &color, &i, &j))
    return gtp_failure("invalid color or coordinate");

  if (!legal_move(i, j, color))
    return gtp_failure("illegal move");

  play_move(i, j, color);
  return gtp_success("");
}
Esempio n. 15
0
static int
gtp_genmove(char *s)
{
  int i, j;
  int color = EMPTY;

  if (!gtp_decode_color(s, &color))
    return gtp_failure("invalid color");

  generate_move(&i, &j, color);
  play_move(i, j, color);

  gtp_start_response(GTP_SUCCESS);
  gtp_mprintf("%m", i, j);
  return gtp_finish_response();
}
Esempio n. 16
0
/* Preliminary function for playing through the aftermath. */
static void
do_play_aftermath(int color, struct aftermath_data *a)
{
  int move;
  int pass = 0;
  int moves = 0;
  int color_to_play = color;
  DEBUG(DEBUG_AFTERMATH, "The aftermath starts.\n");

  /* Disable computing worm and owl threats. */
  disable_threat_computation = 1;
  /* Disable matching of endgame patterns. */
  disable_endgame_patterns = 1;

  while (pass < 2 && moves < board_size * board_size) {
    int reading_nodes = get_reading_node_counter();
    int owl_nodes = get_owl_node_counter();
    int move_val = reduced_genmove(&move, color_to_play);
    if (move_val < 0) {
      int save_verbose = verbose;
      if (verbose > 0)
	verbose--;
      move_val = aftermath_genmove(&move, color_to_play,
				   (color_to_play == WHITE ?
				    a->white_control : a->black_control),
				   0);
      verbose = save_verbose;
    }
    play_move(move, color_to_play);
    if (aftermath_sgftree)
      sgftreeAddPlay(aftermath_sgftree, color_to_play, I(move), J(move));
    moves++;
    DEBUG(DEBUG_AFTERMATH, "%d %C move %1m (nodes %d, %d  total %d, %d)\n",
	  movenum, color_to_play, move, get_owl_node_counter() - owl_nodes,
	  get_reading_node_counter() - reading_nodes,
	  get_owl_node_counter(), get_reading_node_counter());
    if (move != PASS_MOVE)
      pass = 0;
    else
      pass++;
    color_to_play = OTHER_COLOR(color_to_play);
  }
  
  /* Reenable worm and dragon threats and endgame patterns. */
  disable_threat_computation = 0;
  disable_endgame_patterns   = 0;
}
Esempio n. 17
0
bool GameState::play_textmove(std::string color, std::string vertex) {
    int who;
    int column, row;
    int boardsize = board.get_boardsize();

    if (color == "w" || color == "white") {
        who = FullBoard::WHITE;
    } else if (color == "b" || color == "black") {
        who = FullBoard::BLACK;
    } else return false;

    if (vertex.size() < 2) return 0;
    if (!std::isalpha(vertex[0])) return 0;
    if (!std::isdigit(vertex[1])) return 0;
    if (vertex[0] == 'i') return 0;

    if (vertex[0] >= 'A' && vertex[0] <= 'Z') {
        if (vertex[0] < 'I') {
            column = 25 + vertex[0] - 'A';
        } else {
            column = 25 + (vertex[0] - 'A')-1;
        }
    } else {
        if (vertex[0] < 'i') {
            column = vertex[0] - 'a';
        } else {
            column = (vertex[0] - 'a')-1;
        }
    }

    std::string rowstring(vertex);
    rowstring.erase(0, 1);
    std::istringstream parsestream(rowstring);

    parsestream >> row;
    row--;

    if (row >= boardsize) return false;
    if (column >= boardsize) return false;

    int move = board.get_vertex(column, row);

    play_move(who, move);

    return true;
}
Esempio n. 18
0
void
init_brown()
{
  int k;
  int i, j;

  /* The GTP specification leaves the initial board configuration as
   * well as the board configuration after a boardsize command to the
   * discretion of the engine. We choose to start with up to 20 random
   * stones on the board.
   */
  clear_board();
  for (k = 0; k < 20; k++) {
    int color = rand() % 2 ? BLACK : WHITE;
    generate_move(&i, &j, color);
    play_move(i, j, color);
  }
}
Esempio n. 19
0
int take_number_case(game g, int piece_num, dir d, char* buf, int* distance){
  while(true){
    printf("How many case?\n");
    read(0, buf,  sizeof(char)*100);
    if(strcmp(buf, "cancel") == 10)
      break;
    if(strcmp(buf, "exit") == 10)
      return -1;
    if(buf[0]<48 || buf[0]>=48+SIZE_ARRAY || buf[1] != 10)
      printf("Write a number between 0 and %d\tor write cancel or exit.\n",SIZE_ARRAY-1);
    else{
      *(distance) = atoi(buf);
      if(!play_move(g, piece_num, d, *(distance)))
	printf("The piece %d cannot move of %d case(s).\n", piece_num, *(distance));
      else
	return 1;
    }
  }
  return 0;
}
Esempio n. 20
0
std::string Protocol::search_move(bool use_san_notation)
{
    Move m = game.root(depth + 1);
    if (m.is_null()) {
        if (game.is_check(game.current_position().side())) {
            return "LOST";
        }
        return "DRAW";
    } else {
        if (use_san_notation) {
            std::string res = game.output_move(m);
            play_move(m.to_string());
            if (game.is_check(game.current_position().side())) {
                res += "+";
            }
            undo_move();
            return res;
        }
        return m.to_string();
    }
}
Esempio n. 21
0
int solve_step(t_pos *pos) {
  int moves[MAX_MOVES * 2];
  int count = find_moves(pos, moves);
  int i, cell_id, value, ret, cur_status;
  //print_moves(count, moves);
  for (i = 0; i < count; ++i) {
    cell_id = moves[2 * i];
    value = moves[2 * i + 1];
    ret = OPEN;
    play_move(pos, cell_id, value);
    cur_status = solved(pos);
    if (cur_status != OPEN) {
      ret = cur_status;
    } else if (solve_step(pos) == SOLVED) {
      ret = SOLVED;
    }
    undo_move(pos, cell_id, value);
    if (ret == SOLVED) {
      return ret;
    }
  }
  return IMPOSSIBLE;
}
Esempio n. 22
0
char* do_play(Game *game, Color c, Point pt)
// Play the move (updating the Game struct)
{
    char *ret;
    Color to_play_before;
    int   played=0;
    Info  m = 0;
    Position *pos = game->pos;

    to_play_before = board_color_to_play(pos);
    board_set_color_to_play(pos, c);

    if (point_color(pos,pt) == EMPTY) {
        ret = play_move(pos, pt);
        if (ret[0] == 0) {
            m = pt + (board_captured_neighbors(pos) << 9) 
                   + (board_ko_old(pos) << 13) 
                   + ((to_play_before) << 22);
            played = 1;
        }
        // else illegal move: nothing played
    }
    else if(pt == PASS_MOVE) {
        ret = pass_move(pos);
        m = pt + (board_captured_neighbors(pos) << 9) 
               + (board_ko_old(pos) << 13) 
               + ((to_play_before) << 22);
        played = 1;
    }
    else ret ="Error Illegal move: point not EMPTY\n";
    if (played) {
        c2++;
        slist_push(game->moves, m);
    }
    return ret;
}
bool BoardC4::play_random_move(Token player) {
	if (played_count<size) {
		Moves possible_moves=get_possible_moves(player);

		int selected=rand()/(RAND_MAX + 1.0) * possible_moves.size();
		Moves::const_iterator selected_iter=possible_moves.begin();
		while (selected>0) {
			selected--;
			selected_iter++;
		}
		play_move(**selected_iter);

		//play_move(*selected);
		//Move *selected=possible_moves[rand()%possible_moves.size()];
		//play_move(*selected);

		for (Moves::iterator iter=possible_moves.begin(); iter!=possible_moves.end(); iter++) delete *iter;

		return true;
	} else {
		//std::cout<<"board full"<<std::endl;
		return false;
	}
}
Esempio n. 24
0
int main(int argc, char *argv[]){
  set_up_pieces();
  game g = new_game_hr(NB_PIECES, pieces);
  char buf[3][100];
  int piece_num;
  dir d;
  int distance;
  while(!game_over_hr(g)){
    bool good = false;
    while(!good){
      while(!good){
	set_up_board(g);
	print_game(g);
	printf("Move the pieces for free the piece 0 to the exit:\n");
	printf("Total move: %d\n",game_nb_moves(g));
	while(!good){
	  printf("What piece do you want to move?\n");
	  read(0, buf[0], sizeof(char)*100);
	  if(strcmp(buf[0], "cancel") == 10)
	    break;
	  if(strcmp(buf[0], "exit") == 10)
	    return EXIT_SUCCESS;
	  if(buf[0][0]<48 || buf[0][0]>=48+NB_PIECES || buf[0][1] != 10)
	    printf("Write a number between 0 and %d\tor write cancel or exit.\n",NB_PIECES-1);
	  else{
	    piece_num = atoi(buf[0]);
	    good = true;
	  }
	}
	if(!good)
	  break;
	good = false;
	while(!good){
	  printf("In what direction?\n");
	  read(0, buf[1],  sizeof(char)*100);
	  if(strcmp(buf[1], "cancel") == 10)
	    break;
	  if(strcmp(buf[1], "exit") == 10)
	    return EXIT_SUCCESS;
	  if(!is_dir_option(buf[1]))
	    printf("Write one of those direction: up, down, right, left\tor write cancel or exit.\n");
	  else{
	    for(int i=0; i<4; ++i){
	      if(strcmp(buf[1], direction[i].dir_name) == 10)
		d = direction[i].dir_option;
	    }
	    if(!good_direction(g, piece_num, d)){
	      if(is_horizontal(game_piece(g, piece_num)))
		printf("The piece %d cannot move vertycaly\n", piece_num);
	      else
		printf("The piece %d cannot move horizontaly\n", piece_num);
	    }else
	      good = true;
	  }
	}
	if(!good)
	  break;
	good = false;
	while(!good){
	  printf("How many case?\n");
	  read(0, buf[2],  sizeof(char)*100);
	  if(strcmp(buf[2], "cancel") == 10)
	    break;
	  if(strcmp(buf[2], "exit") == 10)
	    return EXIT_SUCCESS;
	  if(buf[2][0]<48 || buf[2][0]>=48+SIZE_ARRAY || buf[2][1] != 10)
	    printf("Write a number between 0 and %d\tor write cancel or exit.\n",SIZE_ARRAY-1);
	  else{
	    distance = atoi(buf[2]);
	    good = play_move(g, piece_num, d, distance);
	    if(!good)
	      printf("The piece %d cannot move to that case.\n", piece_num);
	  }
	}
      }
    }
  }
  set_up_board(g);
  print_game(g);
  printf("CONGRETULATION\nYou won in %d moves\n", game_nb_moves(g));
  return EXIT_SUCCESS;
}
Esempio n. 25
0
void GameState::play_pass() {
    play_move(get_to_move(), FastBoard::PASS);
}
Esempio n. 26
0
void GameState::play_move(int vertex) {
    play_move(board.get_to_move(), vertex);
}
void 
load_and_score_sgf_file(SGFTree *tree, Gameinfo *gameinfo, 
			const char *scoringmode)
{
  int i, j, move_val;
  float result;
  char *tempc = NULL;
  char dummy;
  char text[250];
  char winner;
  int next;
  int pass = 0;
  SGFTree score_tree;
  
  sgftree_clear(&score_tree);
  sgftreeCreateHeaderNode(&score_tree, board_size, komi);
  sgffile_printboard(&score_tree);
  
  next = gameinfo->to_move;
  doing_scoring = 1;
  reset_engine();
  
  if (!strcmp(scoringmode, "finish") || !strcmp(scoringmode, "aftermath")) {
    do {
      move_val = genmove_conservative(&i, &j, next);
      if (move_val >= 0) {
	pass = 0;
	gprintf("%d %s move %m\n", movenum,
		next == WHITE ? "white (O)" : "black (X)", i, j);
      }
      else {
	++pass;
	gprintf("%d %s move : PASS!\n", movenum, 
		next == WHITE ? "white (O)" : "black (X)");
      }
      play_move(POS(i, j), next);
      sgffile_add_debuginfo(score_tree.lastnode, move_val);
      sgftreeAddPlay(&score_tree, next, i, j);
      sgffile_output(&score_tree);
      next = OTHER_COLOR(next);
    } while (movenum <= 10000 && pass < 2);

    if (pass >= 2) {
      /* Calculate the score */
      if (!strcmp(scoringmode, "aftermath"))
	score = aftermath_compute_score(next, komi, &score_tree);
      else
	score = gnugo_estimate_score(&lower_bound, &upper_bound);

      if (score < 0.0) {
	sprintf(text, "Black wins by %1.1f points\n", -score);
	winner = 'B';
      }
      else if (score > 0.0) {
	sprintf(text, "White wins by %1.1f points\n", score);
	winner = 'W';
      }
      else {
	sprintf(text, "Jigo\n");
	winner = '0';
      }
      fputs(text, stdout);
      sgftreeAddComment(&score_tree, text);
      if (sgfGetCharProperty(tree->root, "RE", &tempc)) {
	if (sscanf(tempc, "%1c%f", &dummy, &result) == 2) {
	  fprintf(stdout, "Result from file: %1.1f\n", result);
	  fputs("GNU Go result and result from file are ", stdout);
	  if (result == fabs(score) && winner == dummy)
	    fputs("identical\n", stdout);
	  else
	    fputs("different\n", stdout);
	      
	}
	else {
	  if (tempc[2] == 'R') {
	    fprintf(stdout, "Result from file: Resign\n");
	    fputs("GNU Go result and result from file are ", stdout);
	    if (tempc[0] == winner)
	      fputs("identical\n", stdout);
	    else
	      fputs("different\n", stdout);
	  }
	}
      }
      sgfWriteResult(score_tree.root, score, 1);
      sgffile_output(&score_tree);
    }
  }
  doing_scoring = 0;

  if (strcmp(scoringmode, "aftermath")) {
    /* Before we call estimate_score() we must make sure that the dragon
     * status is computed. Therefore the call to examine_position().
     */
    examine_position(next, EXAMINE_ALL);
    score = estimate_score(NULL, NULL);

    fprintf(stdout, "\n%s seems to win by %1.1f points\n",
      score < 0 ? "B" : "W",
      score < 0 ? -score : score);
  }
}
Esempio n. 28
0
static void
replay_node(SGFNode *node, int color_to_replay, float *replay_score,
	    float *total_score)
{
  SGFProperty *sgf_prop;  /* iterate over properties of the node */
  SGFProperty *move_prop = NULL; /* remember if we see a move property */
  int color; /* color of move to be made at this node. */
  
  int old_move; /* The move played in the file. */
  int new_move; /* The move generated by GNU Go. */

  char buf[BUFSIZE];

  /* Handle any AB / AW properties, and note presence
   * of move properties.
   */

  for (sgf_prop = node->props; sgf_prop; sgf_prop = sgf_prop->next) {
    switch (sgf_prop->name) {
    case SGFAB:
      /* add black */
      add_stone(get_sgfmove(sgf_prop), BLACK);
      break;
    case SGFAW:
      /* add white */
      add_stone(get_sgfmove(sgf_prop), WHITE);
      break;
    case SGFB:
    case SGFW:
      move_prop = sgf_prop;  /* remember it for later */
      break;
    }
  }

  /* Only generate moves at move nodes. */
  if (!move_prop)
    return;

  old_move = get_sgfmove(move_prop);
  color = (move_prop->name == SGFW) ? WHITE : BLACK;

  if (color == color_to_replay || color_to_replay == GRAY) {
    float new_move_value = 0.0;
    float old_move_value = 0.0;
  
    /* Get a move from the engine for color. */
    int resign;
    new_move = genmove(color, NULL, &resign);
    
    /* Pick up the relevant values from the potential_moves[] array. */
    if (new_move != PASS_MOVE)
      new_move_value = potential_moves[new_move]; 
    if (old_move != PASS_MOVE)
      old_move_value = potential_moves[old_move];
    
    /* Now report on how well the computer generated the move. */
    if (new_move != old_move || !quiet) {
      mprintf("Move %d (%C): ", movenum + 1, color);
    
      if (resign)
	printf("GNU Go resigns ");
      else {
	mprintf("GNU Go plays %1m ", new_move);
	if (new_move != PASS_MOVE)
	  printf("(%.2f) ", new_move_value);
      }
      
      mprintf("- Game move %1m ", old_move);
      if (new_move != PASS_MOVE && old_move_value > 0.0)
	printf("(%.2f) ", old_move_value);
      printf("\n");

      *replay_score += new_move_value - old_move_value;
      *total_score += new_move_value;
    }
    
    if (new_move != old_move) {
      if (resign)
	gg_snprintf(buf, BUFSIZE, "GNU Go resigns - Game move %s (%.2f)",
		    location_to_string(old_move), old_move_value);
      else {      
	gg_snprintf(buf, BUFSIZE,
		    "GNU Go plays %s (%.2f) - Game move %s (%.2f)",
		    location_to_string(new_move), new_move_value,
		    location_to_string(old_move), old_move_value);
	if (new_move != PASS_MOVE)
	  sgfCircle(node, I(new_move), J(new_move));
      }
    }
    else
      gg_snprintf(buf, BUFSIZE, "GNU Go plays the same move %s (%.2f)",
		  location_to_string(new_move), new_move_value);
    
    sgfAddComment(node, buf);
    sgffile_add_debuginfo(node, 0.0);
  }

  /* Finally, do play the move from the file. */
  play_move(old_move, color);
}
Esempio n. 29
0
// A function that will capture the offensive (game winning)
// movemnets of the AI.
void ai_move_offensively(int moves_remaining)
{
int t = 0;
int x = start_move_x;
int y = start_move_y;
int planning = 1;


if( algorithm_progress == 0 )
{
play_move( x, y );
play_move( x, y+1 );
algorithm_progress += 1;
}
else if( algorithm_progress == 1 )
{
play_move( x, y+2 );
play_move( x, y+3 );
algorithm_progress += 1;
}
else if( algorithm_progress == 2 )
{
play_move( x+1, y+3 );
play_move( x-1, y+3 );
algorithm_progress += 1;
}
else if( algorithm_progress == 3 )
{
play_move( x+2, y );
play_move( x+1, y+1 );
algorithm_progress += 1;
}
else if( algorithm_progress == 4 )
{
play_move( x+1, y );
play_move( x+1, y+2 );
algorithm_progress += 1;
}
else if( algorithm_progress == 5 )
{
play_move( x-1, y );
play_move( x-2, y-1 );
algorithm_progress += 1;
}

print_game_board();
}
Esempio n. 30
0
//defensive moves.
void ai_move_defensively(int moves_remaining)
{
	int y = 0;
	int x = 0;
	int z = 0;


	for ( x = 0; x < (BOARD_SIZE); x++)
	{	
		for( y = 0; y < (BOARD_SIZE); y++)
		{
			int rstones = 0;
			int dstones = 0;
			int diastones = 0;
		
			if(game_board[x][y] == 'B')
			{
				//Check to the right, down and diag-right for next 5 squares including this one.
				for(z = 0; z < 6; z++)
				{
					//Check right
					if(game_board[x][y + z] == 'B')
					{
						rstones++;
					}
					
					//Check down
					if(game_board[x + z][y] == 'B')
					{
						dstones++;
					}
					
					//Check diagonal
					if(game_board[x + z][y + z] == 'B')
					{
						diastones++;
					}
					
				}
				for(z = 0; z < 6; z++)
				{
					if(moves_remaining > 0)
					{
						if(rstones >= 4)
						{
							if(game_board[x][y + z - 1] == ' ')
							{
								printf("%d", rstones);
								play_move((x + 1), (y + z));
								moves_remaining--;
							}
						}			
						else if(dstones >= 4)
						{
							if(game_board[x + z - 1][y] == ' ')
							{
								play_move((x + z), (y + 1));
								moves_remaining--;
							}
						}
				    	else if(diastones >= 4)
						{
							if(game_board[x + z - 1][y + z - 1] == ' ')
							{
								play_move((x + z), (y + z));
								moves_remaining--;
							}
						}
					}
				}
			}
		}
	}
	//If moves remaining, play offensive
	if(moves_remaining > 0)
	{
		//ai_move_offensively(moves_remaining);
	}

	print_game_board();	
}