Beispiel #1
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("");
}
Beispiel #2
0
bool
setup_game (int width, int height, int handicap, int komi)
{
    pre_game_setup ();

    if (!set_size_board (width, height))
    {
        return false;
    }

    clear_board ();

    /* place handicap */
    if (handicap >= 2)
    {
        current_player = WHITE;
    }
    else if (handicap < 0)
    {
        return false;
    }
    else
    {
        current_player = BLACK;
    }

    header.handicap = handicap;
    setup_handicap_sgf ();

    header.komi = komi;

    post_game_setup_sgf ();

    return true;
}
Beispiel #3
0
/*
Produces the first game state, with handicap stones placed.
*/
void first_game_state(
    board * dst,
    const game_record * src
){
    clear_board(dst);
    apply_handicap_stones(dst, src);
}
Beispiel #4
0
static int load_level( int level_number )
{
    int x,y;
    clear_board();
    for(y = 0;y < HEIGHT;y++)
    {
        for(x = 0;x < WIDTH;x++)
        {
            switch(level_cache[level_number][y][x])
            {
                case '|':
                    board[x][y] = NORTH;
                    break;

                case '-':
                    board[x][y] = EAST;
                    break;

                case '+':
                    board[x][y] = HEAD;
                    break;
            }
        }
    }
    return 1;
}
Beispiel #5
0
void scrollup_handler (BOARD_WIDGET* board) {

	/* ------- <SET FLAG MEMBERS> -------- */	

	if (board -> selected_index == 0)
		board -> wndFlag = false;
	else board -> wndFlag = true;

	if (board -> firstrow_index == 0)
		board -> dataFlag = false;
	else board -> dataFlag = true;

	/* ------- </SET FLAG MEMBERS> -------- */

	/* ------- <Decrease selected_index in the view> ------ */

	int index = board -> selected_index;
	index --;
	clear_board (board);
	set_rowIndex (board, index);
	update_board (board);

	/* ------- </Decrease selected_index in the view> ------ */

	/* ------ <Increase fistrow_index in the data> ------ */

	/* ------ </Increase firstrow_index in the data> ----- */

}
Beispiel #6
0
void
game_reset (void)
{
  stop_anim ();
  gtk_action_set_sensitive (undo_action, FALSE);
  gtk_action_set_sensitive (hint_action, FALSE);

  who_starts = (who_starts == PLAYER1) ? PLAYER2 : PLAYER1;
  player = who_starts;

  gameover = TRUE;
  player_active = FALSE;
  winner = NOBODY;
  column = 3;
  column_moveto = 3;
  row = 0;
  row_dropto = 0;

  clear_board ();
  set_status_message (NULL);
  gfx_draw_all ();

  move_cursor (column);
  gameover = FALSE;
  prompt_player ();
  if (!is_player_human ()) {
    if (player == PLAYER1) {
      vstr[0] = vlevel[p.level[PLAYER1]];
    } else {
      vstr[0] = vlevel[p.level[PLAYER2]];
    }
    game_process_move (playgame (vstr, vboard) - 1);
  }
}
Beispiel #7
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));
      */
    }
  }
}
Beispiel #8
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);
  }
}
Beispiel #9
0
// settings state input loop - gets the user's command and handles it
void exc(char* str, char board[BOARD_SIZE][BOARD_SIZE]){
	char * word1;
	word1 = strtok(str, " ");
	if (strcmp(word1, "minimax_depth") == 0){
		int x = atoi(strtok(NULL, " "));
		if (x > 6 || x < 1) printf(WRONG_MINIMAX_DEPTH);
		else minimax_depth = x;
	}
	else if (strcmp(word1, "user_color") == 0){
		char * color = strtok(NULL, " ");
		if (strcmp(color, "black") == 0) user_color = BLACK;
	}
	else if (strcmp(word1, "clear") == 0) clear_board(board);
	else if (strcmp(word1, "rm") == 0){
		char * coor1 = strtok(NULL, " <,>");
		char * coor2 = strtok(NULL, " <,>");
		if (coor1[0] < 'a' || coor1[0] > 'j' || atoi(coor2) < 1 ||
			atoi(coor2) > 10 || (coor1[0] - 'a' + atoi(coor2) - 1) % 2 == 1) { printf(WRONG_POSITION); }
		else board[coor1[0] - 'a'][atoi(coor2) - 1] = EMPTY;
	}
	else if (strcmp(word1, "set") == 0){
		char * coor1 = strtok(NULL, " <,>");
		char * coor2 = strtok(NULL, " <,>");
		if (coor1[0] < 'a' || coor1[0] > 'j' || atoi(coor2) < 1 ||
			atoi(coor2) > 10 || (coor1[0] - 'a' + atoi(coor2) - 1) % 2 == 1){
			printf(WRONG_POSITION);
			return;
		}
		char * a = strtok(NULL, " ");
		if (a == NULL) return;
		char * b = strtok(NULL, " ");
		if (strcmp(a, "black") == 0){
			if (strcmp(b, "m") == 0) board[coor1[0] - 'a'][atoi(coor2)-1] = BLACK_M;
			else  board[coor1[0] - 'a'][atoi(coor2) - 1] = BLACK_K;
		}
		else {
			if (strcmp(b, "m") == 0) board[coor1[0] - 'a'][atoi(coor2)-1] = WHITE_M;
			else  board[coor1[0] - 'a'][atoi(coor2) - 1] = WHITE_K;
		}
	}
	else if (strcmp(word1, "print") == 0) print_board(board);
	else printf(ILLEGAL_COMMAND);
	return;
}
Beispiel #10
0
void pagedown_handler (BOARD_WIDGET* board) {

	/* ------- <SET FLAG MEMBERS> --------  */		
	if (board -> selected_index == board -> wndTable -> len - 1)
		board -> wndFlag = false;
	else board -> wndFlag = true;

	if (board -> lastrow_index == board -> dataTable -> len - 1)
		board -> dataFlag = false;
	else board -> dataFlag = true;

	/* -------- </SET FLAG MEMBERS> --------- */

	int index = (int) (board -> wndTable -> len) * 2 - 1;
	clear_board (board);
	set_rowIndex (board, index);
	update_board (board);

}
Beispiel #11
0
static void
game_init (void)
{
  g_random_set_seed ((guint) time (NULL));
  vboard = veleng_init ();

  anim = ANIM_NONE;
  gameover = TRUE;
  player_active = FALSE;
  player = PLAYER1;
  winner = NOBODY;
  score[PLAYER1] = 0;
  score[PLAYER2] = 0;
  score[NOBODY] = 0;

  who_starts = PLAYER2;		/* This gets reversed immediately. */

  clear_board ();
}
Beispiel #12
0
int
main (int argc, char **argv) {
  int r, c;
  begin_display(&r, &c);
  /* reserve one line for message row */
  make_board(r-1, c);
  msg_row = r-1;

  prompt("Welcome to Life, Version %s, Copyright 1995, Jim Wise", VERSION_STR);
  clear_board();
	
  while (1) {	
    if (!edit())
      break;
    run();
  }

  end_display();
  exit(0);
}
Beispiel #13
0
static void
pre_game_setup (void)
{
    rb->memset (&header, 0, sizeof (header));

    clear_caches_sgf ();
    free_tree_sgf ();

    set_size_board (MAX_BOARD_SIZE, MAX_BOARD_SIZE);

    clear_board ();

    game_dirty = true;
    move_num = 0;

    play_mode = MODE_PLAY;

    rb->strcpy (save_file, DEFAULT_SAVE);

    header_marked = false;
}
int main ()
{
        do {
            if (var_rematch_ou_quit == 0)
                spy_intro();
            else
                printa_topo();
            if (var_rematch_ou_quit == 0)
                main_menu();
            printf("\nIniciando partida");
            animate("...", 500);
            printf("\n");
            printf ("\n                                   Partida %d\n", num_partida);
            num_partida += 1;
            clear_board();
            //printa_instrucoes();
            printa_board();
            partida();
            novo_jogo_ou_quit();
        } while (var_rematch_ou_quit == 1);
    return 0;
}
Beispiel #15
0
void tick(int* current, int* next) {
  clear_board(next);
  int x,y;
  for (y = 0; y < ROWS; ++y) {
    for (x = 0; x < COLUMNS; ++x) {
      int is_live = current[CELL(x,y)];
      int n = count_neighbors(current, x, y);

      if (is_live && (n < 2  || n > 3)) {
        next[CELL(x,y)] = 0;
        continue;
      } else if (is_live) {
        next[CELL(x,y)] = 1;
        continue;
      }

      if (!is_live && n == 3) {
        next[CELL(x,y)] = 1;
        continue;
      }
    }
  }
}
Beispiel #16
0
/*
Produce the current game state to board form.
*/
void current_game_state(
    board * dst,
    const game_record * src
){
    if(!current_game_bak_set)
    {
        clear_board(&current_game_bak);
        apply_handicap_stones(&current_game_bak, src);

        bool is_black = first_player_color(src);
        for(u16 i = 0; i < src->turns; ++i)
        {
            if(is_board_move(src->moves[i]))
                just_play_slow(&current_game_bak, is_black, src->moves[i]);
            else
                pass(&current_game_bak);
            is_black = !is_black;
        }
        current_game_bak_set = true;
    }

    memcpy(dst, &current_game_bak, sizeof(board));
}
Beispiel #17
0
void scrolldown_handler (BOARD_WIDGET* board) {

	/* ------- <SET FLAG MEMBERS> --------  */		
	if (board -> selected_index == board -> wndTable -> len - 1)
		board -> wndFlag = false;
	else board -> wndFlag = true;

	if (board -> lastrow_index == board -> dataTable -> len - 1)
		board -> dataFlag = false;
	else board -> dataFlag = true;

	/* -------- </SET FLAG MEMBERS> --------- */

	/* ------ <INCREASE VIEW AND DATA> ------ */
	
	int index = board -> selected_index;
	index ++;
	clear_board (board);
	set_rowIndex (board, index);
	update_board (board);

	/* ------ </INCREASE VIEW AND DATA> ------ */

}
Beispiel #18
0
/* Search thread main function */
void *search_loop(void *arg) {
    uint8_t ply;
    move_t mv, mv_tmp;

    SET_BLANK_MOVE(mv);
    SET_BLANK_MOVE(mv_tmp);

    pthread_mutex_lock(&mutex);

    /* Initializations */
    precompute_moves();
    precompute_distances();
    init_zobrist_keys();
    init_history();
    init_transposition_table();
    config_alarm(config->max_seconds);
    max_depth = config->max_depth;

    /* Setup board */
    board = set_board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
    if(board == NULL)
    	quit("Error: Could not setup new board!\n");

    /* ECO = encyclopedia of chess openings */

    if(atoi(config->name) >= 50)
        if(!load_eco(board))
            quit("Error: Could not load Encyclopedia of Chess Openings!\n");

    pthread_mutex_unlock(&mutex);
 
    /* Keep alive until the status changes to QUIT */
    while(status != QUIT) {
    	switch(status) {
    	case NOP:
    	    /* NOP: just wait for a change in status */
    	    pthread_mutex_lock(&mutex);
    	    while(status == NOP)
    	    	pthread_cond_wait(&cond, &mutex);
    	    pthread_mutex_unlock(&mutex);
    	    break;
    	case FORCE:
    	    /* FORCE: wait for a change in status but don't start searches */
    	    pthread_mutex_lock(&mutex);
    	    while(status == FORCE)
    	    	pthread_cond_wait(&cond, &mutex);
    	    pthread_mutex_unlock(&mutex);
    	    break;
    	case SEARCH:
    	    /* Iterative Deepening Search */
    	    /* Save the on-move color */
    	    onmove = board->onmove;
    	    /* Sets as blank the move to be played*/
    	    SET_BLANK_MOVE(mv);
    	    /* Starts counting the time */
    	    start_alarm();
    	    /* For each depth, search with alpha-beta minimax */
    	    for(ply = 2; ply <= max_depth; ply += 2) {
    	    	mv_tmp = alpha_beta(board, -MAX_HEU, MAX_HEU, ply);
    	    	/* Did we run out of time? If so, stops deepening iterations */
    	    	if(get_timeout())
    	    	    break;
    	    	mv = mv_tmp;
    	    }
    	    /* Stops counting the time, if it hasn't already reached limit */
    	    stop_alarm();
    	    /* If the move is still blank, use the partial move found */
    	    if(IS_BLANK_MOVE(mv))
    	    	mv = mv_tmp;
    	    /* Perform the move found in the search */
    	    move(board, mv);
    	    /* Returns to the NOP status */
    	    set_status(NOP);
    	    break;
    	case PONDER:
    	    /* Reserved for future use */
    	    set_status(NOP);
    	    break;
    	default:
    	    quit("Error: Invalid search status!\n");
    	}
    }
    
    /* Clean up memory */
    clear_eco();
    clear_transposition_table();
    clear_history();
    clear_board(board);

    /* Exit Search Thread */
    return NULL;
}
Beispiel #19
0
void
play_replay(SGFTree *tree, int color_to_replay)
{
  char *tmpc = NULL;
  float replay_score = 0.0;
  float total_score = 0.0;

  SGFNode *node = tree->root;

  /* Board size and komi are already set up correctly since the game
   * has already been loaded before this function is called. Now we
   * only have to clear the board before starting over.
   */
  clear_board();
 
  if (!quiet) {
    printf("Board Size:   %d\n", board_size);
    if (sgfGetCharProperty(node, "HA", &tmpc))
      printf("Handicap:     %s\n", tmpc);
    printf("Komi:         %.1f\n", komi);
    if (sgfGetCharProperty(node, "RU", &tmpc))
      printf("Ruleset:      %s\n", tmpc);
    if (sgfGetCharProperty(node, "GN", &tmpc))
      printf("Game Name:    %s\n", tmpc);
    if (sgfGetCharProperty(node, "DT", &tmpc))
      printf("Game Date:    %s\n", tmpc);
    if (sgfGetCharProperty(node, "GC", &tmpc))
      printf("Game Comment: %s\n", tmpc);
    if (sgfGetCharProperty(node, "US", &tmpc))
      printf("Game User:    %s\n", tmpc);
    if (sgfGetCharProperty(node, "PB", &tmpc))
      printf("Black Player: %s\n", tmpc);
    if (sgfGetCharProperty(node, "PW", &tmpc))
      printf("White Player: %s\n", tmpc);
    if (sgfGetCharProperty(node, "RE", &tmpc))
      printf("Result:       %s\n", tmpc);
  }

  /*
   * Now actually run through the file.  This is the interesting part.
   * We need to traverse the SGF tree, and every time we encounter a node
   * we need to check what move GNU Go would make, and see if it is OK. 
   */
  while (node) {
    replay_node(node, color_to_replay, &replay_score, &total_score);
    sgffile_output(tree);
    node = node->child;
  }

  if (!quiet)
    printf("Global score: %.2f / %.2f\n", replay_score, total_score);

  if (showtime) {
    gprintf("SLOWEST MOVE: %d at %1m ", slowest_movenum, slowest_move);
    fprintf(stderr, "(%.2f seconds)\n", slowest_time);
    fprintf(stderr, "AVERAGE TIME: %.2f seconds per move\n",
	    total_time / movenum);
    fprintf(stderr, "TOTAL TIME: %.2f seconds\n",
	    total_time);
  }
}
Beispiel #20
0
/*function the present the page- SET BOARD- and handle the events releated to him*/
int setBoard(SDL_Surface* screen){
	SDL_Surface* setBoard_Screen = NULL;
	board_t tmp_board;
	board_t tmp_board_to_add;
	location loc;
	SDL_Event event;
	pixle pix;
	int x, y;
	int check_Board = TRUE;
	int cnt_op_save_illegal = 0;
	int show_illegal_sign=FALSE;
	piece player_to_add;
	int move_next_page=FALSE;
	int remove = 0;
	copyStoS(tmp_board, state->board); // do the adding to the tmp board- in the end- if changed == TRUE- copy the tmp board to the state->board. else- do nothing
	setBoard_Screen = SDL_LoadBMP("Images/setboard.bmp"); 

	if (setBoard_Screen == NULL){
		return 1;
	}
	apply_surface(0, 0, setBoard_Screen, screen);
	fromStateToGui(screen,tmp_board,SET_BOARD); //converts the board(2nd paramter) to graphical form. the 3rd argumant is for internal use 
	//Update Screen
	SDL_Flip(screen);

	//we do all the actions on a tmp board for the case we cancel. only after saving the game(and verifing the board is legal)- we saving all the changes in the real board
	player_to_add.type = EMPTY;
	while (quit == FALSE && move_next_page == FALSE){

		if (SDL_PollEvent(&event)){
			if (event.type == SDL_MOUSEBUTTONUP){
				if (cnt_op_save_illegal == 1){
					show_illegal_sign = 0;
					cnt_op_save_illegal = 0;
					check_Board = TRUE;
				}
				//Get the mouse offsets
				x = event.button.x;
				y = event.button.y;
				pix.x = x;
				pix.y = y;
				if (x <= 600 && y <= 600){// the user clicked on the game Area:
					// adding a new piece to the board seleted by the user:
					if (player_to_add.type != EMPTY){
						loc = pixel_to_loc(pix);
						copyStoS(tmp_board_to_add, tmp_board);

						tmp_board_to_add[loc.column][loc.row].color = player_to_add.color;
						tmp_board_to_add[loc.column][loc.row].type = player_to_add.type;	
						copyStoS(tmp_board, tmp_board_to_add);
						player_to_add.type = EMPTY;
					}
					if (remove){ //removing a piece selected by user;
						loc = pixel_to_loc(pix);
						tmp_board[loc.column][loc.row].color = EMPTY;
						tmp_board[loc.column][loc.row].type = EMPTY;
						remove = 0;
					}
					//update the board:
					apply_surface(0, 0, setBoard_Screen, screen);
					fromStateToGui(screen, tmp_board, SET_BOARD);
					SDL_Flip(screen);
				}
				//add button:
				if ((x > 624) && (x < 624 + 155) && (y > 76) && (y < 76 + 70)){
					remove = 0;
					player_to_add = select_piece(screen);//selecting piece to add
					apply_surface(0, 0, setBoard_Screen, screen); // return to the game Area screen
					fromStateToGui(screen, tmp_board, SET_BOARD);
					SDL_Flip(screen);
					if (player_to_add.type == EMPTY){
						continue;
					}
					
				}
				//remove button:
				if ((x > 624) && (x < 624 + 155) && (y > 76 + 121) && (y < 76 + 121 + 70)){
					remove = 1; add_tiles_to_removing_spots(screen, tmp_board);  SDL_Flip(screen);
				}
				//save:
				if ((x > 624) && (x < 624 + 155) && (y > 76 + 121 * 2) && (y < 76 + 121 * 2 + 70)){
					remove = 0;
					check_Board = checkBoard(tmp_board);// checking if the board is legal
					if (check_Board == TRUE){ // if legal - do the changing on the real board
						copyStoS(state->board, tmp_board);
						break;
					}
					else{ cnt_op_save_illegal++; show_illegal_sign = 1; } // else- tell the user that the board is illegal and continue editing until the board is legal
				}
				//cancel:
				if ((x > 624) && (x < 624 + 155) && (y > 76 + 121 * 3) && (y < 76 + 121 * 3 + 70)){ break; } //discard changes and return to previous page
				if (DEBUG){
					if ((x > 790) && (y > 590)){ // clear the board button- only for checks and debug
						remove = 0;
						clear_board(tmp_board);
						//update the board:
						apply_surface(0, 0, setBoard_Screen, screen);
						fromStateToGui(screen, tmp_board, SET_BOARD);
						SDL_Flip(screen);
					}
				}
				if (check_Board == FALSE && show_illegal_sign){ add_illegal_sign(screen); SDL_Flip(screen); }
			}//end of handle key pressed 
			if (event.type == SDL_QUIT){ SDL_FreeSurface(setBoard_Screen); return 1; }
		}
	}
	//Free the loaded image
	SDL_FreeSurface(setBoard_Screen);
	return 0;
}
Beispiel #21
0
static void select_maze(void)
{
    int button;

    clear_board();
    load_level( level_from_file );
    redraw();
    rb->lcd_update();

    while (1)
    {
#if LCD_WIDTH >= 160 && LCD_HEIGHT >= 128
        draw_frame_bitmap(1);

        rb->snprintf(strbuf, sizeof(strbuf), "%d", level);
        rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
        rb->lcd_putsxy(TOP_X3 - strwdt/2, TOP_Y2, strbuf);

        rb->snprintf(strbuf, sizeof(strbuf), "%d", level_from_file);
        rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
        rb->lcd_putsxy(TOP_X2 - strwdt/2, TOP_Y1, strbuf);

        rb->strcpy(strbuf, game_type==0? "A": "B");
        rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
        rb->lcd_putsxy(TOP_X1, TOP_Y1, strbuf);

        rb->snprintf(strbuf, sizeof(strbuf), "%d", highscores[0].score);
        rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
        rb->lcd_putsxy(TOP_X4 - strwdt/2, TOP_Y2, strbuf);

#else
        rb->snprintf(strbuf, sizeof(strbuf), "Maze:  %d", level_from_file);
        rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
        rb->lcd_putsxy((WIDTH*MULTIPLIER - strwdt)/2,
                       (HEIGHT*MULTIPLIER - strhgt)/2, strbuf);
#endif

        rb->lcd_update();

        button = rb->button_get(true);
        switch (button)
        {
            case SNAKE2_QUIT:
            case SNAKE2_PLAYPAUSE:
                return;
                break;
            case SNAKE2_UP:
            case SNAKE2_RIGHT:
                if(level_from_file < num_levels)
                    level_from_file++;
                else
                    level_from_file = 0;
                load_level( level_from_file );
                redraw();
                break;
            case SNAKE2_DOWN:
            case SNAKE2_LEFT:
                if(level_from_file > 0)
                    level_from_file--;
                else
                    level_from_file = num_levels;
                load_level( level_from_file );
                redraw();
                break;
            default:
                if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
                    quit = 2;
                    return;
                }
                break;
        }
    }

}
Beispiel #22
0
//
//  函数:  WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目的:    处理主窗口的消息。
//
//  WM_COMMAND	- 处理应用程序菜单
//  WM_PAINT	- 绘制主窗口
//  WM_DESTROY	- 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;


	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// 分析菜单选择: 
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		case ID_HELP_SPLASHSCREEN:
			ghDlg = CreateDialog(hInst, MAKEINTRESOURCE(IDD_SPLASH), 0, Splash);
			ShowWindow(ghDlg, SW_SHOW);
			break;
		case ID_CONTROL_YIELD:
			if (!eval_null()){
				MessageBoxA(0, "You could only yield before a game start.", "Yield", 0);
				break;
			}
			mainboard[7][7] = 1;
			paint_board(hWnd);
			break;
		case ID_CONTROL_RESTART:
			clear_board(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:{
		Graphics *myGraphics;
		Pen *myPen;
		hdc = BeginPaint(hWnd, &ps);
		// TODO:  在此添加任意绘图代码...
		myGraphics = new Graphics(hdc);
		myPen = new Pen(Color(255, 0, 0, 0), 1);
		myGraphics->DrawRectangle(myPen, Rect(400, 20, 20, 20));
		for (int i = 0; i < 375; i += 25)
			myGraphics->DrawLine(myPen, 20, 20 + i, 370, 20 + i);
		for (int i = 0; i < 375; i += 25)
			myGraphics->DrawLine(myPen, 20 + i, 20, 20 + i, 370);
		delete myGraphics;
		delete myPen;
		EndPaint(hWnd, &ps);
		paint_board(hWnd);
	}
		break;
	case WM_DESTROY:
		GdiplusShutdown(gdiplusToken);
		PostQuitMessage(0);
		break;
	case WM_LBUTTONDOWN:
		board_clicked(hWnd, LOWORD(lParam), HIWORD(lParam));
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}
Beispiel #23
0
static int
gtp_clear_board(char *s)
{
  clear_board();
  return gtp_success("");
}
Beispiel #24
0
void replace_current_board(struct world *mzx_world, char *name)
{
  int current_board_id = mzx_world->current_board_id;
  struct board *src_board = mzx_world->current_board;

  FILE *fp = fopen_unsafe(name, "rb");
  struct zip_archive *zp;

  char version_string[4];
  int file_version;
  int success = 0;

  if(fp)
  {
    if(!fread(version_string, 4, 1, fp))
    {
      error_message(E_IO_READ, 0, NULL);
      fclose(fp);
      return;
    }

    file_version = board_magic(version_string);

    if(file_version > 0 && file_version <= MZX_LEGACY_FORMAT_VERSION)
    {
      // Legacy board.
      clear_board(src_board);

      src_board =
       legacy_load_board_allocate_direct(mzx_world, fp, file_version);

      success = 1;
      fclose(fp);
    }
    else

    if(file_version <= MZX_VERSION)
    {
      int board_id;

      // Regular board or maybe not a board at all.
      zp = zip_open_fp_read(fp);

      // Make sure it's an actual zip.
      if(zp)
      {
        // Make sure the zip contains a board.
        board_id = find_first_board(zp);

        if(board_id >= 0)
        {
          clear_board(src_board);

          src_board =
           load_board_allocate(mzx_world, zp, 0, file_version, board_id);

          success = 1;
        }
      }

      if(!success)
        error_message(E_BOARD_FILE_INVALID, 0, NULL);

      zip_close(zp, NULL);
    }

    else
    {
      error_message(E_BOARD_FILE_FUTURE_VERSION, file_version, NULL);
      fclose(fp);
    }

    if(success)
    {
      // Set up the newly allocated board.
      optimize_null_objects(src_board);

      if(src_board->robot_list)
        src_board->robot_list[0] = &mzx_world->global_robot;

      set_update_done_current(mzx_world);

      set_current_board(mzx_world, src_board);
      mzx_world->board_list[current_board_id] = src_board;
    }
  }
}
Beispiel #25
0
void Board::new_game()
{
	game_over = false;
	falling_shape = false;
	clear_board();
}
Beispiel #26
0
EightQueensProblem::EightQueensProblem(): NUM_QUEENS(8) {
	clear_board();
}
Beispiel #27
0
void board_eventhandler (BOARD_WIDGET* board, GNode* root) {
	
	int ch;
	int selected_data_index;
	STOCKINFO* temp;
	GNode* temp_node;
	GNode* market;
	bool flag;
	int remember_index;
	int i;

	activate_board (board);

	while ((ch = getch ()) != 'q') {		
		switch (ch) {

			case KEY_UP :
				scrollup_handler (board);
				break;

			case KEY_DOWN :
				scrolldown_handler (board);
				break;

			case KEY_PPAGE :
				pageup_handler (board);
				break;

			case KEY_NPAGE :
				pagedown_handler (board);
				break;

			case KEY_RESIZE :
				/* clear_board (board); */
				resize_handler (board);
				break;
			
			case '\n' :
				if ((*(int*) (board -> userdata) & 0x000f) == 1) {
					selected_data_index = board -> firstrow_index +
						board -> selected_index;
					temp = (STOCKINFO*) g_ptr_array_index (
							board -> dataTable, selected_data_index);
					if (temp -> depth <= 2) {
						market = g_node_find (root, G_LEVEL_ORDER, 
								G_TRAVERSE_NON_LEAVES, (gpointer) temp);
						temp_node = g_node_first_child (market);
						flag = ((STOCKINFO*) (temp_node -> data)) -> IsActivated;

						open_close_branch (market, !flag);

						clear_board (board);

						g_ptr_array_unref (board -> dataTable);
						board -> dataTable = node_to_array (root, 
								board -> dataTable);

						remember_index = board -> selected_index;
						set_rowIndex (board, 0);
						/*set_rowIndex (board, -1);*/
						set_rowIndex (board, remember_index);
						board -> wndFlag = true;
						board -> dataFlag = true;
						update_board (board);
					}
					break;
				}
				break;
			case 'o' :
				option_handler (board);
				break;
		}
		usleep (1000);
	}
	
	inactivate_board (board);

}
Beispiel #28
0
int main(int argc, char *argv[])
{
    char         puzzle[82];
    sudoku_hint  hints[81];
    sudoku_hint  *hint;
    int         hint_cells[9];
    int ch;     /* getch */
    int i, t;   /* temp */
    int r, c, n;    /* more temp */
    int cr = 1; /* cursor position */
    int cc = 1; /* cursor position */
    int flags = 0;

    boardy = 1;
    boardx = 1;
    cellh = 3;
    cellw = 7;

    initscr();
    noecho();
    cbreak();
    keypad(stdscr, TRUE);

    getmaxyx(stdscr, winh, winw);
    init_msg_area();
    init_panels();

    /* set up and draw board */
    init_board(&board);
    nc_init_board(&ncboard, stdscr, &board, boardy, boardx, cellh, cellw);
    draw_board(&ncboard);
    print_title_area("%s", str_entry_mode);
    update_panels();
    doupdate();
    move_cursor(&ncboard, cr, cc);

    while ((ch = getch()) != 'q') {
        if (flags & ERROR_BIT) {
            clear_msg();
            flags ^= ERROR_BIT;
        }
        switch (ch) {
            case 0xC:  /* ^L form feed FF clear screen */
                unhighlight_all(&ncboard);
                draw_board(&ncboard);
                touchwin(curscr);
                wrefresh(curscr);
                break;
            case '?':   /* show help */
                print_msg("%s", str_help);
                break;
            case 'h':
                move_cursor_left(&ncboard, &cr, &cc);
                break;
            case 'j':
                move_cursor_down(&ncboard, &cr, &cc);
                break;
            case 'k':
                move_cursor_up(&ncboard, &cr, &cc);
                break;
            case 'l':
                move_cursor_right(&ncboard, &cr, &cc);
                break;
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                set_value(&board, cr, cc, ch);
                draw_cell(&ncboard, cr, cc);
                break;
            case ' ':
            case 'd':
            case 0x08: /* ^H */
            case KEY_BACKSPACE:
                set_value(&board, cr, cc, ' ');     /* erase */
                draw_cell(&ncboard, cr, cc);
                break;
            case 'c':
                unhighlight_all(&ncboard);
                clear_board(&board);
                draw_board(&ncboard);
                break;
            case 'f': toggle_fix_mode(&board);
                /* if entering fixed mode, validate and solve puzzle */
                if (get_givens(&board, puzzle) != NULL) {
                    /* if puzzle invalid */
                    if (!sudoku_solve_hints(puzzle, hints)) {
                        toggle_fix_mode(&board);
                        print_msg("Error: %s", str_invalid_puzzle);
                        flags |= ERROR_BIT;
                    } else { /* puzzle valid, but check uniqueness */
                        print_title_area("%s", str_solve_mode);
                        if (sudoku_nsolve(puzzle, NULL, 2) > 1) {
                            print_msg("%s", str_not_unique);
                            flags |= ERROR_BIT;
                            flags |= HINTS_DISABLED;
                        }
                    }
                } else {
                    print_title_area("%s", str_entry_mode);
                    flags &= ~ HINTS_DISABLED;
                }
                /* toggle_fix_mode (un)bolds every char so refresh needed */
                draw_board(&ncboard);
                break;
            case 'u': t = undo_board(&board);   /* only works in fixed mode */
                if (t >= 0) {
                    cr = t / 9 + 1;
                    cc = t % 9 + 1;
                    draw_cell(&ncboard, cr, cc);
                }
                break;
            case 's':   /* solve puzzle if in fixed mode */
                if (!is_fixed(&board)) {
                    print_msg("%s: %s", str_not_fixed,
                            "press 'f' to fix the givens first.");
                    flags |= ERROR_BIT;
                    break;
                } /* else */
                for (i = 0; i < 81; i++) {
                    hint2rcn(hints + i, &cr, &cc, &t);
                    set_value(&board, cr, cc, t % 10 + '0');
                }
                draw_board(&ncboard);
                break;
            case 'H':   /* give hint, if in fixed mode */
                if (!is_fixed(&board)) {
                    print_msg("%s: %s", str_not_fixed,
                            "Hints are only given in solver mode.");
                    flags |= ERROR_BIT;
                    break;
                }
                if (flags & HINTS_DISABLED)
                    break;
                unhighlight_all(&ncboard);
                get_values(&board, puzzle);
                hint = next_hint(hints, puzzle);
                if (hint - hints == 81)
                    break;
                t = hint2cells(hint, hint_cells);
                for (i = 0; i < t; i++) {
                    c = hint_cells[i];
                    r = c / 9 + 1;
                    c = c % 9 + 1;
                    highlight_cell(&ncboard, r, c);
                }
                if (t > 1) {
                    hint2rcn(hint, &r, &c, &n);
                    print_msg("Hint: try a %d in the highlighted cells", n);
                }
                draw_board(&ncboard);
                break;
        }
        update_panels();
        doupdate();
        move_cursor(&ncboard, cr, cc);
    }

    endwin();

    return 0;
}
Beispiel #29
0
bool EightQueensProblem::start_game(int row){
	clear_board();
	place_queen_on_board(row-1, 0);
	bool success = place_next_queen();
	return success;
}
Beispiel #30
0
static void
init_brown(unsigned int seed)
{
  xor_srand(seed);
  clear_board();
}