Random choice for game bot

Hi guys, so my homework is to make a tic-tac-toe game, and when I was trying to make a bot to play against the player, I realized that my programming way make the bot always prioritize some move, I wonder if someone know how to make my bot randomly choose one of the options when making a move.

I think about use random number and assign it to the move but when the condition to make the move do not meet, it will take a long time to run

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  
		
int AI_move(int i, setting_game setting, int playerturn)
{
	int move; 
	
	if (player[i].difficulty == 1)
		return AI_move_random(setting, playerturn);
	
	if (player[i].difficulty == 2)	
	{
		if (playerturn == 1) move = AI_move_random(setting, playerturn);
		else if (playerturn == 2) move = AI_move_near(i, playerturn);
		else if (playerturn >= 3)
		{
			move = AI_move_win_vertical(i, playerturn);
			if (move == 0) move = AI_move_win_horizontal(i, playerturn);
			if (move == 0) move = AI_move_win_diagonal_left(i, playerturn);
			if (move == 0) move = AI_move_win_diagonal_right(i, playerturn); 
			if (move == 0) move = AI_move_near(i, playerturn);
			if (move == 0) move = AI_move_random(setting, playerturn);		
		}	
		return move;
	}	
	
	if (player[i].difficulty == 3)	
	{
		if (playerturn == 1) move = AI_move_random(setting, playerturn);
		else if (playerturn == 2) move = AI_move_near(i, playerturn);
		else if (playerturn >= 3)
		{
			if (move == 0) move = AI_move_block_horizontal(i, playerturn);
			if (move == 0) move = AI_move_block_vertical(i, playerturn); 
			if (move == 0) move = AI_move_block_diagonal_left(i, playerturn);
			if (move == 0) move = AI_move_block_diagonal_right(i, playerturn); 
						
			if (move == 0) move = AI_move_win_vertical(i, playerturn);  
			if (move == 0) move = AI_move_win_horizontal(i, playerturn);
			if (move == 0) move = AI_move_win_diagonal_left(i, playerturn);
			if (move == 0) move = AI_move_win_diagonal_right(i, playerturn);
			if (move == 0) move = AI_move_near(i, playerturn);
			if (move == 0) move = AI_move_random(setting, playerturn);		
		}	
		cout << move;
		return move;
	}	
	
vector <int> moves;
... push back valid moves
... randomly pick from 0 to size in move vector.

there are 9 moves on an empty board, and it goes down from there.
it should not take more than 9 decisions to find a move, then... it should be very, very fast.
at any given time, not counting the first move, there should only be 2-3 moves that make any sense at all. Often, you have 1 move or lose.

TTT is often used to show transposition table AI -- turns out there are very few board positions once you realize that mirrored and rotated boards are the same solution as their counterpart(s).
Last edited on
@jonnin
Unfortunately that I do not be allowed to use vector in this assignment, and my board is free-size, many loops in each move to find a correct position :v
free sized board is an issue, but vector can be replaced by whatever you like, array or linked list or even a string. its the approach that matters, not the specific tool you use to make it so.
stow the moves, and randomly pick one, however you like. Vector is just the default/typical way if you can use modern C++.
There is no need for a container to store the moves. Count the number of available moves and pick a random move number; convert that random move number to a row, col pair.

Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <random>

int random_int( int minv, int maxv ) // generate a random integer in [minv,maxv]
{
    static std::mt19937 rng( std::random_device{}() ) ;
    static std::uniform_int_distribution<int> distrib ;
    using param_type = decltype(distrib)::param_type ;

    distrib.param( param_type{ minv, maxv } ) ;
    return distrib(rng) ;
}

enum cell : char { SPACE = ' ', NOUGHT = 'O', CROSS = 'X' };
constexpr int MAX_SZ = 256 ;
using board_type = cell[MAX_SZ][MAX_SZ] ;
struct move_type { int row = 0 ; int col = 0 ; } ;
constexpr move_type INVALID_MOVE { -1, -1 } ;

int num_available_moves( const board_type& board, int nrows, int ncols )
{
    int num_available = 0 ;

    for( int row = 0 ; row < nrows ; ++row )
        for( int col = 0 ; col < ncols ; ++col )
            num_available += int( board[row][col] == SPACE ) ;

    return num_available ;
}

move_type generate_random_move( const board_type& board, int nrows, int ncols )
{
    const int num_available = num_available_moves( board, nrows, ncols ) ;

    if( num_available > 0 )
    {
        // pick a random move and return its row,col position
        int move_number = random_int( 1, num_available ) ;

        for( int row = 0 ; row < nrows ; ++row )
            for( int col = 0 ; col < ncols ; ++col )
            {
                if( board[row][col] == SPACE ) --move_number ;
                if( move_number == 0 ) return { row, col } ;
            }
    }

    return INVALID_MOVE ; // there are no valid moves left
}
It's seems like I misdiscribe it, I mean to choose randomly one of the move I have listed, instead of always proritize the vertical one
The function I made already meant to find a move
Topic archived. No new replies allowed.