# CPSC312-2016-Project2-Poker

## Poker

Authors: Patrick Tseng Thomas Chisholm

## What is the problem?

Implement poker in Haskell, specifically a simplified version like video poker to start with, where the player and the computer are both dealt a hand and the best hand wins. Then eventually (hopefully) expand the game to implement full poker where both players can fold, raise or hold, and a pot of current money is awarded to the winner.

## What is the something extra?

We have 3 extra things -

1. We started by representing cards with suits and implemented a ranking of hands from the rules of poker. The ranking system decides who’s hand should win and breaks ties when players have the same ranked hand. Because we coded our poker game to the Games2 interface we were able to test Minimax on it. However because we are using out shuffled deck which is wrapped in an IO, you have to simulate one on a test deck of shuffled cards.

We also implemented a computer player that can make decisions based on money it has left compared to how much the player has left, how good it’s cards are and what the available moves are. It first decides if the cards it has at the beginning of its turn are good, sort-of-good, or bad. Then depending on how many cards have been dealt, it decides if it should bet and how much, fold, call raise etc.

2. Then we implemented code to draw the cards to the console to let the player see their cards and quickly choose their next move. The formatting and printing of text was covered in class, but we used instances of show, and several helper methods to draw the cards in a way that represents how poker is played, including hiding the computer’s cards during the game and showing them afterward.

3. Then we added a betting system, where the player and the computer both place bets. The player is dealt three cards and either folds or places a bet. Bets are placed by choosing Then the computer either Folds, Raises or matches the player's bet (Check). Both the player and the computer are then dealt 2 more cards, and the computer bets again (this allows the player to raise, check or fold and can happen multiple times), folds or asks that the cards be revealed. When the cards are revealed, the ranking system chooses a winner, and the winner is awarded the pot of money that has been bet so far. The money that is won carries over into the next games and changes the computer player's behavior. This goes beyond what we did in class because either player can end the game at any point with any outcome. We need to keep track of who’s turn it is and how much money each player has bet or won and who should be awarded money when the game ends.

## What did we learn from doing this?

We learned that Haskell is a very viable platform for representing the logic of a game like poker. Most of the card rankings were very easy to implement. The tie breaking code was a little more involved, but generally this part of the project lent itself well to functional programming.

The turn logic, such as what actions are allowed given the current state of the game was more difficult to sort out because poker is fairly complicated. It was also hard to keep track of who’s turn it was while working with the Game interface. This might be easier in a less strict functional language by setting a global variable. Alternatively we could have restructured the Play2 interface more to accomplish this.

Drawing the cards to the console was much easier in Haskell than in other languages because of the Data.List.transpose function. When drawing the cards it was difficult to draw them side by side. We accomplished this by drawing each card, replacing the suit placeholder character with the unicode suit, then transposing the list of lists of strings. This lets each card’s nth row be printed in the same line and the cards appear to be drawn next to each other. This seems like it would be much more code in an imperative language.

Another problem we encountered is that random numbers are difficult to work with in Haskell. Before we covered how to use random numbers in class, we implemented a shuffling algorithm by using some references on stackoverflow. But we ran into the issue that because anything that returns a random number cannot be a true function, haskell requires that the result be wrapped in the IO class. The IO class is used for all things that might not always return the same value, such as user input. The IO class is also quite clumsy to work with, and we had trouble integrating our shuffling algorithm with the rest of the code until it was covered in class. Later if we had wanted to integrate another random number in some function we would have to restructure our entire program. Other languages make dealing with randomness much easier.

Haskell is very feasible for this type of game, and only minor issues were encountered during our implementation.

## Other things we learned

1. You can use unicode symbols such as ♡, ♣, ♢, ♠ in your Haskell code (depending on your compiler). This can make your code very readable, and we used this when drawing the card type. These can sometimes cause issues because they will not print out on windows.
2. Even with lazy evaluation you need to be careful about functions taking too long. We were originally generating all permutations of 52 cards, which took way too long and wasn’t helped by lazy eval.
3. Parsing that isn’t normally parsable such as monads/custom data types in Haskell is a pain, a hacky way to do it was to exploit the show function to force it into a string type, after that it was relatively easy.
4. Error checking compared to Prolog was a lot easier given the syntax. We’re able to determine if the input was valid and prevent crashing/unexpected behaviour at any point in the program.

## Possible ideas for expansion

1. Make the computer player another human player and play online
2. Implement real poker strategy in the computer player
3. Implement Blinds and multiple players
4. Implement different types of poker such as Texas Hold Em’

## References:

An indepth and complicated implementation of Poker in Haskell : https://github.com/nbouscal/poker.hs Video poker: https://en.wikipedia.org/wiki/Video_poker Game Rules: http://www.bicyclecards.com/how-to-play/basics-of-poker/