Beispiel #1
0
int computer_move(int board[9], int human) {
	int computer = human * -1;
	int diff = difficulty();

	// start at random
	if (empty_board(board)) return (rand() % 9);

	// check if there is winning move
	int i;
	for (i=0; i<9; i++) { // for all the positions
		if (board[i]==0) { // if the position is empty
			board[i] = computer; // try the move
			if (iswon(board) == computer) {
				board[i] = 0;
				return i;
			}
			board[i] = 0;
		}
	}

	// for medium difficulty, check if there is blocking move
	if (diff >= 1) {
		for (i=0; i<9; i++) {
			if (board[i] == 0) {
				board[i] = human;
				if (iswon(board) == human) {
					board[i] = 0;
					return i;
				}
				board[i] = 0;
			}
		}
	}

	// for hard difficulty, make the computer unbeatable
	int bestMove, best = -2;
	if (diff >= 2) {
		for (i=0; i<9; i++) {
			if (board[i]==0) {
				board[i] = computer;
				int score = -negamax(board, human);
				board[i] = 0;

				if (score > best) {
					best = score;
					bestMove = i;
				}
			}
		}
		return bestMove;
	}

	// or place anywhere in the blank spot
	int blank[9] = {0,0,0,0,0,0,0,0,0};
	int blank_count = 0;
	for (i=0; i<9; i++)
		if (board[i] == 0)
			blank[blank_count++] = i;
	return blank[rand() % blank_count];
}
Beispiel #2
0
Board board_from_arr(int exp[BOARDSIZE][BOARDSIZE]) {
    Board b = empty_board();
    for (int r=0; r<4; r++)
        for (int c=0; c<4; c++)
            place(b, r, c, exp[r][c]);
    return b;
}
Beispiel #3
0
void game_over_common(u8 root_id)
{
    control_t *p_mbox, *p_info, *p_ctrl;

    p_mbox = ui_game_get_ctrl(root_id, GAME_BOARD_ID);
    p_info = ui_game_get_ctrl(root_id, GAME_INFO_ID);

    empty_board(p_mbox);
    ctrl_default_proc(p_mbox, MSG_LOSTFOCUS, 0, 0);
    ctrl_paint_ctrl(p_mbox, FALSE);

    p_ctrl = ctrl_get_child_by_id(p_info, 8);
    ctrl_default_proc(p_ctrl, MSG_GETFOCUS, 0, 0);
    ctrl_paint_ctrl(p_ctrl, FALSE);
}
Beispiel #4
0
int main()
{
    initialize();
    int tests = 1000;
    int passed = 0;
    fast_srandom(time(0));
    for (int i = 0; i < tests; i++) {
        board_t *b = new board_t;
        int size = fast_random(max_size - 4) + 5;
        empty_board(b, size);
        int steps = 0;
        bool failed = false;
        while (true) {
            stone_t color = steps % 2 == 0 ? STONE_BLACK : STONE_WHITE;
            index_t pos = gen_move(b, color);
            if (pos < 0)
                break;
            if (!is_legal_move(b, pos, color)) {
                failed = true;
                break;
            }
            if (pos >= 0) {
                put_stone(b, pos, color);
                if (!check_board(b)) {
                    failed = true;
                    break;
                }
            }
            steps++;
        }
        delete b;
        if (failed) {
            printf("[F]");
        } else {
            printf("[%d]", steps);
            passed++;
        }
    }
    printf("\n");
    printf("Passed %d out of %d random tests\n", passed, tests);

    return passed == tests ? 0 : 1;
}
Beispiel #5
0
Board invert_rotate_for_move(Board og, Move m) {
    Board b = empty_board();
    for (int r=0; r<BOARDSIZE; r++) {
        for (int c=0; c<BOARDSIZE; c++) {
            switch (m) {
                case Up:
                    place(b, r, c, bget(og, c, BOARDSIZE-1-r));
                    break;
                case Down:
                    place(b, r, c, bget(og, BOARDSIZE-1-c, r));
                    break;
                case Left:
                    place(b, r, c, bget(og, r, BOARDSIZE-1-c));
                    break;
                case Right:
                    place(b, r, c, bget(og, r, c));
                    break;
                default:
                    break;
            }
        }
    }
    return b;
}
Beispiel #6
0
Board board_cpy(Board other) {
    Board b = empty_board();
    memcpy(b->data, other->data, sizeof(int)*BOARDSIZE*BOARDSIZE);
    return b;
}