CS 210 Homework #3: MLStone

Due: Sunday March 22, 11:59pm
Turnin: by bblearn

You should turnin a mlstone.sml ML source code file that is executable from the command-line using sml mlstone.sml. It should have your name in an ML comment at the top.

Use ML to implement the following program. Write a header comment for each function, and internal comments as needed. Basic points for correctness. Extra points for clarity and brevity. You should write helper functions as needed for your solution. If you find a built-in function or external source that does part or all of the solution for you, feel free to use it. For external sources, be sure to cite what you incorporated and where it came from.

For this assignment you are to write a "MLStone" game in ML.

MLStone is a ultra-simplified cheesy interactive card game for two players: in this case, a human called "Player" versus a computer called "AI".

MLStone Cards

MLStone cards cost energy to play. Each one is either a creature that will help the player (the default, called a minion), or a magic spell. Cards have the format:
( cost, spellp, name, attack, health )
with semantics as follows. All numbers are integers.
Field Meaning as Minion Meaning as Spell, if different
cost energy needed to play the card
spellp false for minions true for spell cards
name String name of the card. While it is pure decoration, it allows decks of cards to have a fun theme.
attack damage the minion can do each turn damage the spell does (once)
health cumulative damage to kill the minion the spell heals this much health

The MLStone Game State

The state of a game of MLStone is defined by the following ML symbols:
Symbol Meaning
Health Player's health. Integer, starts at 30. <=0 means enemy wins
AIHealth Computer player's health. Integer, starts at 30. <=0 means player wins
Energy Energy available for play. Integer >= 0. Set from the game turn number at the start of each player's turn, and expended by playing cards.
Hand List of lists. Each element sublist represents a card that may be played.
AIHand List of lists. Each element sublist represents a card that may be played.
Minions List of lists. Each element sublist represents a creature that is available to help the player. Format of elements is identical to the card format described above, except that the first two elements (energy and spellp) are omitted.
Monsters List of lists. Each element sublist represents a creature that is available to help the computer player. Format of elements is identical to the card format described above, except that the first two elements (energy and spellp) are omitted.
Such a game state is to be read in using ML's use function from a file named "game.sml", formatted as legal ML code assignments to these symbols. Example:
val Health=30;
val AIHealth=30;
val Hand=[	     (1, false, "C slug", 0, 1),
	     (1, false, "int", 1, 1),
	     (1, true, "++", 0, 1),
	     (1, true, "^=", 4, 0),
	     (2, false, "for (;;)", 2, 2),
	     (3, false, "switch", 3, 3),
	     (4, false, "array a[100]", 4, 4),
	     (4, true, "Bus Error Fireball", 6, 0)
            ];

val AIHand=[     (1, true, "Atom", 0, 1),
	     (1, true, "Car", 1, 0),
	     (1, true, "Cdr", 2, 0),
	     (1, false, "Parenthesis", 1, 1),
	     (2, false, "Cons Cell", 2, 1),
	     (2, false, "defun", 3, 2),
	     (3, true, "Mapcar", 4, 0),
	     (4, false, "ML Monster", 2, 6)
            ];
The Minions and Monsters lists always start empty, but of course a saved game might add assignments of those variables to the file. Similarly the energy available is derived from the current turn number, which starts at 1, but a Saved Game would add an assignment to Energy to indicate the turn number.

Game Play Loop

Your program should write to standard output and read from standard input. The game should implement a loop that follows the pseudocode:
set turn to 1
repeat forever
   set player energy to equal turn number
   print game state
   while player is not finished do  ; PLAYER TURN (read-eval-print loop)
      read player command
      evaluate player command
      if Player is dead, print message and exit
      if AI is dead, print message and exit
      print game state
   set AI energy to equal turn number
   while AI is not finished to	    ; AI TURN
      select AI command
      evaluate AI command
      if Player is dead, print message and exit
      if AI is dead, print message and exit
      print game state
   increment turn

Required Player Commands

  1. Quit. Quits game.
  2. End. Ends Turn.
  3. Play "name" [on "name"]. Remove a named card from the hand; a minion is placed on the board by adding it to the "Minions" list, while a spell is cast for a one-time effect on a named monster or "AI".
  4. Attack "name" with "name". Use a minion to attack either a named monster or "AI". Note that minions may not attack the turn that they are first played on the board.
Input will be received on your ML console's standard input. The exact format of the input, i.e. what the user types, is not specified. You should use human-readable prompts to make clear what you expect input to look like. All ML input commands are fair game. You can allow the user to type a command as a whole line that you read with read-line and then parse, or prompt them for each piece of the command separately in order to avoid having to parse their replies into multiple variables. You are allowed to use assignments to store the input into variables, either setq or let, or by passing their input as parameters into other functions.

AI Player Commands and Selection

Available AI Player commands are the same as Player Commands, except with "minion" and "monster" roles reversed. The AI command selection each turn should consist of:

Combat

An attack of minion versus monster subtracts each's attack value from the other's health. If health reaches 0 the creature is removed from play. An attack on a player subtracts the creature's attack value from the player's health. No damage is done to the creature.

Extra Credit

Extra credit will be awarded if you implement significant additions to this program such as: