Assignment代寫|sql代寫|Haskell代寫-FIT2102 Assignment 2
FIT2102 Assignment 2: Gin Rummy
Due Date: November 6th, 23:55
Uploader: http://fit2102.monash/uploader/ (http://fit2102.monash/uploader/)
Overview: Your goal is to implement a player for the game of Gin Rummy. Your player
needs to be able to play a valid game; manage a "memory" string with a
parsercombinator; and, leverage concepts from the course. You will also need to write a
twopage-report describing your submission.
Building and using the code: The code bundle is packaged the same way as tutorials.
To compile the code, run: stack build. To execute the code, run:
stack exec staticgame. You will need to edit staticgame/Main.hs to play with more players You
cannot edit the stack configuration.
Submission: Your player source code and your report in PDF format go in the
submission/ folder of the code bundle. To submit you will zip up just the contents of this
submission/ folder into one file named studentNo_name.zip.
Gin Rummy is a two-player game where the objective is to score 100 points before your
opponent. Points are awarded to the player whose hand has the lowest value at the end of
a round. The value of a hand depends on how you decide to organise your cards -- called
Your task for this assignment is to implement a player able to play a non-trivial game of Gin
Rummy. We will use a classic deck of 52 cards, aces low.1
You can find a longer explanation online
(http://www.coololdgames.com/cardgames/rummy/gin/), or even play the game
(http://cardgames.io/ginrummy/). Do note, though, that the variant we use has a few key
differences, namely (compared to the linked resources):
The non-dealer does not get to take the up-card, the game starts after dealing.
There is non laying-off, we only count the melds formed in your own hand.
You cannot discard the card you just drew.
There is no Big Gin, you always have to discard at the end of your turn.
In the variant of Gin Rummy we use, a round (also called "playing a hand") proceeds as
1. A dealer is chosen at random; this player will be last to go.
2. Each player is dealt ten cards; these form a hand.
3. The first card after dealing two hands is revealed and put face-side up, this forms the
discard; the rest of the cards form the stock.
4. In turn, each player then decides whether they want to pick the (visible) card from the
discard, or the (hidden) top card from the stock.
5. To end their turn, players will have to discard a card from their hand and announce if
they want to end the game.
At the end of their turn, players thus discard a card and have three choices:
1. Call Gin, which means that they managed to fit all ten cards in their hand into meld.
Calling Gin awards bonus points.
2. Knock, which means that, although they did not manage to fit all ten cards into melds,
they believe to have a hand of lower value that their opponent's. You can only Knock if
your deadwood's total value is less than 10.
3. Discard, which means that they do not want to end the game.
The core mechanic of Gin Rummy is to fit cards into melds -- think poker combinations. In
our variant of Gin Rummy, we will use three types of melds:2
Straight: a combination of three to five cards of the same suit with consecutive
numbers. For example: A? 2? 3?.
Set: a combination of three or four cards with the same rank in different suits. For
example: 8? 8? 8?.
Deadwood: any card which does not fit into a meld.
Now, the interesting part of Gin Rummy is that melds are not cumulative. This means that
a hand of cards can form different melds. Consider the following cards: 7? 7? 7? 8? 9?,
the seven of diamonds can be included in either a straight or a set but cannot be included
in both. The strategy in this game is thus to decide which cards should belong to which
The other important part of Gin Rummy is how to count points. There are two things that
matter for scoring: the number of points (or value) of a hand and the bonus points awarded
at the end of the game.
The winner of a round is the player who finishes with the lowest value hand. Cards that fit
into melds are worth 0 points, while deadwood is counted according to rank: face cards are
worth 10 points, aces 1 points and other cards according to their numeric value.
The winner of a round will score the difference in value between theirs and their opponent's
total deadwood value. If the winner called Gin, they will be awarded 25 bonus points. In
case a player Knocked but has a higher deadwood count than their opponent, their
opponent wins and gets awarded 10 bonus points.
The game stops when a player reaches 100 points, he is declared the winner.
For this assignment, you are required to implement a player exposing three functions.
These functions are:
1. actionFunc, called at the start of your turn where you choose which pile to draw from.
2. playFunc, called after you drew a card and where you decide what to announce.
3. meldFunc, called at the end of the round where you return the melds formed with your
A skeleton for the file can be found in submission/Player.hs in the code bundle.
To keep the playing field level, and to allow us to evaluate your code we ask you use only
the libraries provided. In short, you cannot edit the stack.yaml and package.yaml or add
functionality to the source code (in src/).
You will need to submit a file called studentNo_name.zip which you will create by zipping the
contents of the submission/ directory. If you have any extension, you will need to include
them in a directory titled extensions/ in your zip file.
Choosing an action
At the beginning of its turn, your player will need to decide whether to draw a card from
the discard or the stock. This function will also receive extra information about the
previous turn, unless it's the first one. You will know: the card on top of the discard; what
action you opponent took, unless you're the first player; and, what was your last memory,
if you played before.
data Draw = Stock | Discard
-- | Action function type.
-- This function is called at the beginning of a turn before the player has to - - form melds.
= Card -- ^ card on top of the discard pile
-> Maybe String -- ^ player's memory, on first player turn it will be Nothing
-> Maybe Action -- ^ opponent's chosen action, on first game turn it will be Nothing
-> [Card] -- ^ the player's hand
-> (Draw, String) -- ^ which pile did the player chose to draw from
Managing the hand
After having chosen where to draw a card from, your player will be called again with the
drawn card. It will need to decide which card to discard and what to announce.
data Action = Action Act Card data
Act = Gin | Knock | Drop
-- | Play function type.
-- A player receives the card he decided to draw (from discard or stock), her --
hand and her memory. She then choses whether to Knock or Discard.
= Card -- ^ last picked card
-> String -- ^ the player's memory
-> [Card] -- ^ the player's hand
-> (Action, String) -- ^ the player's chosen card and new state
Finally, your player needs to be able to convert a hand of cards into melds to do the
scoring. Your melds will be checked against the rules, obviously.
data Meld =
Deadwood Card -- An unmelded card
| Set3 Card Card Card -- 3 cards of same rank different suit
| Set4 Card Card Card Card -- 4 cards of same rank different suit
| Straight3 Card Card Card -- 3 cards of same suit, sequential ranks
| Straight4 Card Card Card Card -- 4 cards of same suit, sequential ranks
| Straight5 Card Card Card Card Card -- 5 cards of same suit, sequential ranks
-- | Meld function type.
-- Which melds to use for scoring. type
= String -- ^ the player's memory
-> [Card] -- ^ cards in player's hand
-> [Meld] -- ^ elected melds
Managing the memory
The common component of all the functions above is the String which is your player's
memory. Your player needs to be able to keep track of some parameters of the game
through time. This is enabled by returning a String object after playing.
Internally, your player should use a custom datatype to store information rather than a
String. To enable conversion to and from your datatype, you will have to use a
parsercombinator as presented in the course notes. The source code is included in
The assessment for this assignment will be in four parts:
2. Code quality
3. Memory and parsing
You are required to provide a report in PDF format of at least two pages, plus one per
extension. You want to summarise the workings of the code, and highlight the interesting
parts and difficulties you encountered.
In particular, describing how your strategy, and thus your code, evolved will be beneficial.
The code quality will be the main evaluation criterion for your assignment. You can think of
this as a two-part marking scheme:
1. Apply concepts from the course. The important thing here is that you need to actually
use them somewhere. For example, defining a new type and its Monad instance, but
then never actually needing to use it will not give you marks. (Note: using bind (>>=) for
the sake of using the Monad when it is not needed will not count as "effective usage.")
2. Have readable code, commented when necessary. Readable code means that you
keep your lines at a reasonable length (< 80 characters). That you provide comments
above non-trivial functions. And, that you comment sections of your code whose
function may not be clear.
Remember, the point of comments is to give a manual rather than describe the code. In the
case of a function, you would explain how to use it rather than what are the parameters,
return types, etc.
Memory and parsing
One of the key features of your player is the ability to keep track of the game. To enable
everyone to use their own datatypes, the game code will consider your memory to be a
Handling complex data as strings is cumbersome. This means you will have to implement
serialisation and deserialisation. This will be done using a parser-combinator -- of which
you can see an explanation here (http://tgdwyer.github.io/parsercombinators/). The source
code is provided in src/Parser/.
You can use the Show instance to serialise your data structures. However, you must not use
(or derive) the Read instance. We require you to use the parser-combinator supplied to
Your memory should help you make decisions. This means your player needs to use the
memory to compute relevant parameters for choosing an action. Simply storing past
information is not sufficient. For example, your player could use the memory to compute
statistics about unseen cards, your opponent's hand, etc.
We will run a tournament online (http://fit2102.monash) based on the code provided.
Except the interface, this will be the same game.
Important: Your rank in the tournament will not have a direct impact on your mark. A
highperforming player with spaghetti code will be graded lower than an average, well?written player.
However, we will also upload a number of bots on the server. They will be identifiable by
having ids below 10. Having a higher rank than them will award you marks:
5% for having a valid player, that is one which can play a game.
5% for having a continuing player, that is one which does not error during the
tournament -- e.g., timeouts.
5% for beating at least one of the bots.
5% for beating all of the bots.
Requirements: The code does not contain any excess parts, the memory is used to
store curated data about the game, the player can defeat all training opponents, and the
documentation supports the submission and have a clear report outlining your efforts.
The code is well structured and uses some advanced concepts from the course -- higher
order functions, function composition, monadic operations, etc. The code compiles
without warnings and your player has some heuristic strategy (see Game AI below).
Do note you can expect a higher mark with an average level AI with very neat code, rather
than a high-performing AI with spaghetti code.
The goal of this assignment is not for you to develop an AI which can compete with OpenAI
(http://openai.com/) or AlphaGo (http://deepmind.com/research/case-studies/alphagothe?story-so-far). The emphasis should be on code quality and applying functional concepts.
However, a more advanced AI usually requires more interesting code.
Below, you can find a non-exhaustive list of AI algorithms, ranked by implementation
difficulty, which you can use as reference. Note that a well implemented heuristic player
that is excellent with respect to all the criteria. On the contrary, a complex Monte Carlo
player (see below) which has very bad code quality and makes no use of the memory may
very well not get a passing grade.
Na?ve AI: tries to play its best card given the current state of the game, you can start by
implementing one to make sure you respect the game's rules. However, this will not
get a passing grade.
Heuristic player: has a procedure (heuristic) to determine the strength of its hand
versus its opponent's and saves additional information about the game being played to
enhance its decision at each turn.
tries to minimise the maximum loss of a player by building a tree of possible
moves and playing against itself. This method was developed for two-player, zero-sum
game with perfect information. In this context, you will have to take into account the
uncertain nature of the game.
Probabilistic player: will make use of probabilities to determine which cards have the
highest chance of winning the game (i.e., appearing in the stock) or how good their
opponent's hand is. It will make use of the memory to keep track of played cards and
refine its calculations.
You have to use a little creativity and add some functionalities (at least 2) of your own choice
Using logs to build a player
The game server (see below) will keep logs of your games against other players. Reports
will be in the following format:
hand, playing, discard, picked, dropped
The first column is the cards in the player's hand. The second whether it is your turn to play.
The third, the card at the top of the discard. The fourth, which card was picked (if it is not
the discard, then it came from the stock). And, the fifth is which card was discarded.
Reports will be formatted as: -.csv and will not have the header.
You can write a Haskell program to data-mine these reports and tailor some parts of your
Monte Carlo Tree Search
Monte Carlo Tree Search (MCTS) is the fusion between (tree) search algorithms such as
minmax and using probabilities (Monte Carlo simulation) to determine the branching in the
search tree. It makes use of a simulation phase to explore deeper. In this context, you can
leverage the memory to save already explored branches, or weight, etc.
Hint: Building a MCTS player requires having access to a source of entropy for side?effectfree random number generation; you can use your hand as it comes from a shuffled
We will be checking your code against the rest of the class, and the internet, using a
plagiarism checker. Monash applies strict penalties to students who are found to have
Any plagiarism will lead to a 0 mark for the assignment and will be investigated by the staff.
There is a zero-tolerance policy in place at Monash. So be careful and report any
collaboration with other students.
We will run a server for the course at http://fit2102.monash (http://fit2102.monash) with
the following pages:
The uploader (http://fit2102.monash/uploader/): after logging in, this page will allow
you to upload your code and compete in the tournament.
The handout (http://fit2102.monash/resources/assignment.html): this document.
The ladder (http://fit2102.monash/ladder.php): this page will display the scores of the
last tournament run.
One thing to note is that the server only accept submissions as whole files. If your code
uses a multi-file structure, you will need to concatenate them into your Player.hs before
Once you upload your player, you will see two links on the page:
home.php: shows your current ranking, last upload, and previous games played.
status.php: shows the status of your current upload. Furthermore, you can inspect your
games by clicking on their number.
Before uploading your player, please check that the following runs:
stack exec staticgame
This will run a single game with two instances of your player. You can modify this code
(found in staticgame/Main.hs) to run different versions of your code.
You cannot import external libraries because the server cannot know about them. In a
nutshell, you cannot edit the stack.yaml or the package.yaml.
The code provided uses the Safe pragma4
to make sure the code you use is okay to run. It
is also compiled with the -Werror flag which means that all warnings will throw errors and
prevent your code from compiling. So make sure you run the test suite before you upload
Summary of tournament submission rules
Respect the rules: your player must always play valid actions or it will be eliminated.
Be timely: to give everyone a fair chance, your functions must all return in under one
Be safe: your player must compile with all flags provided, including the import safe.
Single file: your code must be submitted on the server as a single file.
1. This means a deck with four suits (clubs, diamonds, spades and hearts) with the
following ranking: King, Queen, Jack, 10, 9, 8, 7, 6, 5, 4, 3, 2, Ace.?
2. Examples taken from Wikipedia (http://en.wikipedia.org/wiki/Gin_rummy).?
4. More info at SafeHaskell (http://ghc.haskell.org/trac/ghc/wiki/SafeHaskell), but this
should not hinder your work.?