Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Tic-tac-toe AI

Name: Anonymous 2011-03-15 10:07

Here is an unbeatable AI I programmed for Tic-tac-toe.
What does /prog/ think of it?

int ai(int *board) {
    int    cur_pos;
    int    cur_value;
    int    best_value    = -10000;
    int    best_pos    = 0;
    int    buffer[9];

    copyboard(buffer,board);

    for(cur_pos = 0; cur_pos < 9; cur_pos++) {
        if(board[cur_pos] == 0) {
            move(buffer,AI,cur_pos);

            //Will this move make me win? Is this the last possible move?
            if(check_board(buffer,0) == AI || board_full(buffer))
                return cur_pos;

            cur_value = (-1)*value_move(buffer,HUMAN,8);

            if(cur_value > best_value && cur_value != -8) {//If the move is better save it, do not if the opponent can defeat the ai (cur_value == -8).
                best_pos = cur_pos;
                best_value = cur_value;
            }

            undo_move(buffer,cur_pos);
        }
    }
    return best_pos;
}

int value_move(int *board, int player, int depth) {
    int    pos;
    int    wins_possible    = 0;
    int    move_value    = 0;
    int     buffer[9];

    copyboard(buffer,board);

    for(pos = 0; pos < 9; pos++) {
        if(board[pos] == 0) {
            move(buffer,player,pos);

            //Will this move make the player win?
            if(check_board(buffer,0) == player)
                wins_possible++;

            if(wins_possible > 1)        //If the previous move by this player was impossible to beat return a very high number.
                return 100;

            else if(!board_full(buffer))
                move_value -= value_move(buffer,other_player(player),depth-1); //If the move is good for the other player it's not good for this player. See: Negamax

            undo_move(buffer,pos);
        }
    }
   

    if(wins_possible == 1)        // The sooner the player wins the better.
        return depth;
    else                //Just return the sums of the values.
        return move_value;

}


int ai(int *board)Returns the best move for the given board;
int value_move(int *board, int player, int depth) Returns the value of all the possible moves.

I think the other functions are self-explanatory but still.
void copyboard(int *out, int *in) Duplicates a board.
void move(int *board, int player, int position) Moves the given player to the given position on the given board.
int check_board(int *board,int draw_winner) Returns the winner of the given board. 0 if there is no winner (Cat's game or not completed).
int board_full(int *board)Returns 1 if every position is occupied,otherwise 0.
void undo_move(int *board, int position)Empties the given position on the given board.

I'm still a novice programmer so any criticsm is welcome.

Thank you all.

Name: Anonymous 2011-03-16 7:30

>>22
/sci/ is that way --->

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List