How to make a A.I for a card game.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DeathArcanaXIII

Basically i have a working card game of Rock, Paper, Scissors, were both decks have 5 cards of each, right now my opponent pick a random card of his hand(Array) and play.
I want him to always pick a card that beats the player card. This is the code i have right now for the random card pick.

func _enemy_pick():
if(Table.played == true): #Check if the player already choose his card
	var temp = randi() % Table.enemyHand.size()
	Table.pick = Table.enemyHand[temp]
	Table.enemy_actual_hand -= 1
	Table.played = false
	if(Table.pick == #Instantiate the picked card
	if(Table.pick ==
	if(Table.pick ==
	if(Table.enemy_actual_hand == 0): #Reset Hand size because of "pop_at"
		all_cards_played = true
		Table.enemyHand = [7,7,7]
	if(Table.enemy_actual_hand > 0):
		all_cards_played = false

You don’t need to call randomize() each time you call a function with RNG; you can just call it in _ready()

func _ready():

SQBX | 2023-01-03 15:40

:bust_in_silhouette: Reply From: SQBX

You can’t use RNG if you want the enemy to beat the player every time. Instead, you should do somethin’ like this:

match player.pick:
        pass # Enemy should pick scissors
        pass # Enemy should pick paper
        pass # Enemy should pick rock

To check if the enemy has the required card, you can do:

match player.pick:
        if in enemyHand:
            pass # Enemy has a playable card to beat player
            pass # Enemy doesn't have the card required to beat the player, so you can just use the first card in the enemy's hand

Thank you! the idea was to have 2 types of I.A to change between each other based on the player/enemy score, i forgot to mention that in the question. You helped alot tho

DeathArcanaXIII | 2023-01-03 16:07

Glad I could help :slight_smile:

SQBX | 2023-01-03 16:08