简体   繁体   中英

Different Strategies in a card game in Haskell

I am coding different AIs for this card game (called Algo or Coda) with the goal of making them playing against each other. Because of my lack of experience in Haskell I hardwired the different strategies to the code and I have currently 3 versions of the same game but with different combination of AI's. Let me give one example.

Briefly the game consists in guessing the opponent's cards, during one round a player is faced with two to three moments of choice:

  1. Choose which Card to announce as a guess;
  2. If the guess was correct choose to keep guessing or finish his/her turn;
  3. When hearing an opponent's guess choose which information stract from that.

Suppose a1, b1 and c1 are possible actions for moment 1. Similarly a2, b2 and c2 are for moment 2 and a3, b3 and c3 are for moment 3.

Within all my files I have a file for the AI with the strategy a1, b1, c1 playing against the Ai with strategy a2, b2, c2. And two other files with different strategies.

However what I would like to have is a module called 'Game' with a function that receives the kinds of players and runs the game applying the strategies that each player has for each choice moment.

One idea I have in mind to solve this problem is to define the data type:

data Player = Player {moment1::StateOfTheGame -> Guess, 
                      moment2::StateOfTheGame -> Bool, 
                      moment3::Guess -> EpistemicState}

But them I do not know how to define the functions moment1, moment2 and moment3.

Another idea that I had was to define strategies to be a class and write the instances of each strategy for the data type Player. But again I am not sure how to spell out this idea.

How can I have a single module called 'Game' with a function that receives the kinds of players and runs the game applying the strategies that each player has for each choice moment?

Make a module for each strategy. Export the functions you need to implement player from the module. In your main program create a player and pass in the functions you have exported to the player's constructor. The rest of your program works with players so it doesn't even matter what you name your functions they will be accessed through the player "interface". It ought to be a fairly straight forward application of higher order functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM