📜 ⬆️ ⬇️

A game (not) for fools. Writing AI for the Fool (Part 1)

I think it is not a secret to anyone that “Fool” (hereinafter this word will be written in small letters and without quotes) is the most popular card game in Russia and the countries of the former USSR (although almost unknown outside of it). Despite its name and rather simple rules, the gain in it still depends more on the player’s skill than on the random layout of the cards (in English terminology, games of both types are called game of skill and game of chance, respectively. So, the fool in more game of skill ).


The purpose of the article is to write a simple AI for the game. The word "simple" means the following:



(Strictly speaking, the first paragraph no longer gives such an AI the right to be called artificial intelligence per se , but only a pseudo-AI. But such terminology in game development is well-established, so we will not change it.)


I think the rules of the game are known to everyone, so I’ll not remind them again. For those who want to check, I advise you to contact Wikipedia , there is a pretty good article on this topic.


So, let's begin. Obviously, the more foolish a card is, the more profitable it is to have it in your hand. Therefore, we will build an algorithm on the classical assessment of the strength of the hand and making a decision (for example, about throwing one or another card) on the basis of this assessment. We assign cards values, for example:



(We use numbers that are multiples of 100 in order to get rid of floating-point in calculations and operate only with whole numbers. For what we need negative estimates, see later in the article.)


The trump cards are more valuable than any simple ones (even a trump deuce beats an "ordinary" ace), and the hierarchy in the trump suit is the same, so for their evaluation we simply add 1300 to the "base" value - then, for example, the trump deuce will cost 600 + 1300 = 700 points (that is, just a little more than an uncensored ace).


In the code (all code examples in the article will be on Kotlin), it looks like this (the relativaCardValue() function returns that same estimate, and RANK_MULTIPLIER is just a factor of 100):


 for (c in hand) { val r = c.rank val s = c.suit res += ((relativeCardValue(r.value)) * RANK_MULTIPLIER).toInt() if (s === trumpSuit) res += 13 * RANK_MULTIPLIER // еще не все, продолжение чуть ниже } 

Alas, that's not all. It is also important to consider the following evaluation rules:




Source: https://habr.com/ru/post/437346/