程序代写代做代考 Haskell AI algorithm html data structure game fuzzing go Excel 19/10/2020, 7:46 PM

19/10/2020, 7:46 PM
FIT2102 Assignment 2: Gin Rummy
Due Date: November 6th, 23:55
Weighting: 30% of your final mark for the unit
Uploader: https://fit2102.monash/uploader/ (https://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 parser- combinator; and, leverage concepts from the course. You will also need to write a two- page-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. If you want to play with more players, you will need to edit staticgame/Main.hs. 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.
Changelog
12/10: Add missing aces to deck.
12/10: Pass a Maybe Draw to actionFunc.
Gin Rummy
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 “forming melds.”
https://fit2102.monash/resources/assignment.html Page 1 of 13

19/10/2020, 7:46 PM
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 (https://www.coololdgames.com/card- games/rummy/gin/), or even play the game (https://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 follows:
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.
Forming melds
https://fit2102.monash/resources/assignment.html Page 2 of 13

19/10/2020, 7:46 PM
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 melds.
Counting points
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.
Deliverable
For this assignment, you are required to implement a player exposing three functions. These functions are:
https://fit2102.monash/resources/assignment.html Page 3 of 13

19/10/2020, 7:46 PM
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 last
hand.
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.
type ActionFunc
= Card — ^ card on top of the discard pile
-> Maybe String — ^ player’s memory, on first player turn it will be Nothing -> Maybe Draw — ^ opponent’s chosen action, on first game turn it will be Noth
ing
-> [Card] — ^ the player’s hand
-> (Draw, String) — ^ which pile did the player chose to draw from
https://fit2102.monash/resources/assignment.html Page 4 of 13

19/10/2020, 7:46 PM
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.
type PlayFunc
= Card
-> String
-> [Card]
-> (Action, String) — ^ the player’s chosen card and new state
— ^ last picked card
— ^ the player’s memory
— ^ the player’s hand
Forming melds
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.
https://fit2102.monash/resources/assignment.html Page 5 of 13

19/10/2020, 7:46 PM
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 rank
s
— | Meld function type.

— Which melds to use for scoring. type MeldFunc
= 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 parser-combinator as presented in the course notes. The source code is included in src/Parser/.
Assessment
The assessment for this assignment will be in four parts:
1. Report (25%)
2. Code quality (30%)
3. Memory and parsing (25%) 4. Player (20%)
https://fit2102.monash/resources/assignment.html Page 6 of 13

19/10/2020, 7:46 PM
Report
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.
Code quality
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 String. 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 (https://tgdwyer.github.io/parsercombinators/). The source code is provided in src/Parser/. https://fit2102.monash/resources/assignment.html Page 7 of 13 19/10/2020, 7:46 PM 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 handle deserialisation. 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. Player We will run a tournament online (https://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 high- performing 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. Marking rubric Pass: The code compiles without warnings and your player has some heuristic strategy (see Game AI below), you use some form of memory with parsing. The report supports the code. Credit: You use the memory to store non-trivial information and have a clear report outlining your efforts. Distinction: The code is well structured and uses some advanced concepts from the https://fit2102.monash/resources/assignment.html Page 8 of 13 19/10/2020, 7:46 PM course -- higher order functions, function composition, monadic operations, etc. High Distinction: 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. 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. Game AI The goal of this assignment is not for you to develop an AI which can compete with OpenAI (https://openai.com/) or AlphaGo (https://deepmind.com/research/case-studies/alphago-the- 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 above is sufficient for an HD. 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. MinMax:3 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. https://fit2102.monash/resources/assignment.html Page 9 of 13 19/10/2020, 7:46 PM Extensions This assignment is fairly open-ended. You can achieve an HD with a solid player and very neat code, but getting a high HD will require you to go beyond. Here, you can find some ideas of what we consider extensions. All extensions need to be supported by at least one additional page in your report. Feel free to come up with your own. And don't forget, you will only be awarded marks for extension work that extends an already high quality submission. If the core of your submission is not already HD-worthy, the extension will not grant you many marks. 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: 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
player accordingly.
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-effect-free random number generation; you can use your hand as it comes from a shuffled deck.
hand, playing, discard, picked, dropped
C10;SQ;H10;HQ;DK;CJ;D3;D4;HJ,0,C7,S5,D9
https://fit2102.monash/resources/assignment.html Page 10 of 13

19/10/2020, 7:46 PM
Writing an extensive test suite
Testing in functional languages is often done semi-automatically. This is because the test framework can leverage the type system to generate arbitrary inputs — think fuzzing.
In the course material, we use Doctest (https://github.com/sol/doctest#readme). You may have seen lines starting with prop>. These mean “properties” and what is great, for us programmers, is that we do not need to come up with inputs, the testing framework does it itself.
The leading test framework in Haskell is QuickCheck (https://hackage.haskell.org/package/QuickCheck). It is actually what is called in the prop> example above. Identifying properties (sometimes called invariants) of your code can help you write better functions.
The only limitation here is that the test suite must be extensive. By that we mean that you need to test more than one aspect of your functions (not just “returns a Card”). Furthermore, you will need a compelling report showing how you used the test suite to design your code — think Test Driven Development, where you determine the function’s behaviour first rather than writing it and adding tests for cases you think of.
Plagiarism
https://www.monash.edu/students/admin/policies/academic-integrity (https://www.monash.edu/students/admin/policies/academic-integrity)
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 committed plagiarism.
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.
Tournament server
https://fit2102.monash/resources/assignment.html Page 11 of 13

19/10/2020, 7:46 PM
We will run a server for the course at https://fit2102.monash (https://fit2102.monash) with the following pages:
The uploader (https://fit2102.monash/uploader/): after logging in, this page will allow you to upload your code and compete in the tournament.
The handout (https://fit2102.monash/resources/assignment.html): this document.
The ladder (https://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 uploading.
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:
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 your player.
Summary of tournament submission rules
Respect the rules: your player must always play valid actions or it will be eliminated.
stack exec staticgame
https://fit2102.monash/resources/assignment.html Page 12 of 13

19/10/2020, 7:46 PM
Be timely: to give everyone a fair chance, your functions must all return in under one second.
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 (https://en.wikipedia.org/wiki/Gin_rummy).↩
3. https://en.wikipedia.org/wiki/Minimax↩
4. More info at SafeHaskell (https://ghc.haskell.org/trac/ghc/wiki/SafeHaskell), but this should not hinder your work.↩
https://fit2102.monash/resources/assignment.html Page 13 of 13