Skip Navigation | ANU Home | Search ANU | Search FEIT | Feedback
The Australian National University
Faculty of Engineering and Information Technology (FEIT)
Department of Computer Science
Printer Friendly Version of this Document
High Performance Scientific Computing COMP2310
COMP2310 Assignment 1

COMP2310 Assignment 1

This assignment is worth 20% of the total course mark.

Deadline: 23:57 Tuesday 4th September 2007

Late Penalty:
<1 hour -0.5 marks/20
<6 hour -1.0 marks/20
<12 hour -1.5 marks/20
<24 hours -2.0 marks/20
<48 hours -5.0 marks/20
<72 hours -10.0 marks/20
>72 hours -20.0 marks/20, i.e. forget it!


Submission Details

You are required to submit a single compressed tar file from the student system (or partch) using the following command:
submit comp2310 Ass1 comp2310_ass1.tar.gz
The following illustrates how (on partch) you should produce the above file:
    partch> pwd
    /students/u9507815/comp2310_ass1
    
    partch> ls
    game_advanced       game_basic              generic_queue_pack.ads
    game_advanced.adb   game_basic.adb          generic_queue_pack.ali
    game_advanced.adb~  game_basic.ali          generic_queue_pack.o
    game_advanced.ali   game_basic.o	    write-up.txt
    game_advanced.o     generic_queue_pack.adb
    
    partch> rm *.o *.ali game_advanced game_basic
    (i.e. remove the rubbish!)
    
    partch> ls
    game_advanced.adb  generic_queue_pack.adb
    game_basic.adb     generic_queue_pack.ads
    write-up.txt
    
    partch> cd ..
    
    partch> tar -zcvf comp2310_ass1.tgz comp2310_ass1/
    
    partch> submit comp2310 Ass1 comp2310_ass1.tgz
    comp2310_ass1.tgz submitted
    
The submitted will will then be processed as follows
    partch> pwd
    /students/comp2310/submission/u9507815
    
    partch> ls
    comp2310_ass1.tgz
    
    partch> tar -zxf comp2310_ass1.tgz
    
    partch> cd comp2310_ass1
    
    partch> ls
    game_advanced.adb  generic_queue_pack.adb
    game_basic.adb     generic_queue_pack.ads
    write-up.txt
    
    partch> gnatmake game_basic
    gcc-4.1 -c game_basic.adb
    gcc-4.1 -c generic_queue_pack.adb
    gnatbind -x game_basic.ali
    gnatlink game_basic.ali
    
    partch> gnatmake game_advanced
    gcc-4.1 -c game_advanced.adb
    gnatbind -x game_advanced.ali
    gnatlink game_advanced.ali
    
    partch> game_basic
    STUPID Player 3 has won the game after picking up 13 cards
    Winning hand: Jack Spades, Jack Hearts, Jack Diamonds, Jack Clubs, 9 Spades
    
    (comment - we will run your code a few times)
    
    partch> game_advanced -n 6 -m 2
    
    accepted options:
       [-n {number of players: positive}]             -> default:    4
       [-m {number of smart players: positive}]       -> default:    1
    
    number of players                6
    number of smart players          2
    
    SMART Player 5 is winner after picking up 13 cards
    Winning hand: Jack Spades, Jack Hearts, Jack Diamonds, Jack Clubs
    
    SMART Player 6 is second after picking up 17 cards
    Winning hand: Ace Spades, Ace Hearts, Ace Diamonds, Ace Clubs
    
    STUPID Player 3 is third after picking up 16 cards
    Winning hand: King Spades, King Hearts, King Diamonds, King Clubs
    
    Game stopped as only 3 players left
    Shall we play another round?
    
    (comment: we will run the advanced code a few times with different input)
    
    partch> cat write-up.txt
    This is where I give a brief write-up of what I have done and how I have
    done it. This should be in plain text.
    
Some notes:
  • Although you are producing two executables, but they should be in the same directory, and they can share files.
  • You should remove rubbish files from your directory before creating the tar file. If you submit lots of irrelevant rubbish you will lose marks.
  • Your code will be tested on partch, so make sure it works on that machine!
  • Your code should be well commented with meaningful variable names.
  • You are required to submit a file called "write-up.txt". This should be a plain ASCII text file in which you describe what you have done. I would expect this to equate to roughly 1 A4 page of text. I am expecting you to detail things like whether you used a stack or queue for the piles of cards, how you handled termination, what you did in the advanced game after a player had 4 cards and withdraws from the game etc. We will read this file before reading your code. Approximately 10% of the marks will be associated with your description given in this file.

In this assignment you will develop an Ada program to play a simple card game.

The Basic Game

There are 4 players. From a standard deck of cards you select 24 cards consisting of all four suites for six different kinds, eg the Ace, King, Queen, Jack, 10 and 9 of Clubs, Diamonds, Hearts and Spades. The players are seated around a table, and between each player is a (possibly empty) pile of cards. The game begins with each player being given four cards and each pile containing two cards. The initial assignment of cards to players or piles is random (i.e. the cards have been shuffled).

Once the game is started, each player can pick one card at a time from their right-hand pile (assuming it is not empty) and discard one card at a time to the left-hand pile. They can not pick up and discard a card at the same time. Each player can only hold 5 cards in their hand at any time. All players can play simultaneously, so there is no specific order of play (i.e. there is no concept of a turn). The game ends when one player has four cards of the same kind (e.g. four kings). There can only be one winner.

Implementation: Basic Game (worth 80% of the assignment mark)

Implement the above game using Ada. Your program should require no input.

Your program should implement two types of players:

  • A stupid player, that randomly discards one of the five cards at each turn
  • A smart player who always keeps multiple cards of the same kind. If they ever get in the situation where they have 3 cards of one kind and 2 of another, they keep the 3 cards of the same kind and discard one of the other 2 cards.
Your program should have one smart player and three random players.

When a player has won, your code should print out a message that identifies who won, how many cards they picked up, and what their final cards were when they won. The message should have the following form:
 SMART(or)STUPID Player X has won the game after picking up N cards
 Winning hand: Ace Spades, Ace Hearts, Ace Diamonds, Ace Clubs, King Spades
The program should produce no other output. The program should ensure that only one player can ever win the game and that termination takes place as soon as practically possible after any player has been identified as the winner.

Your program should also terminate if any player has picked up more than 100 cards. In this situation your program should printout a message of the following form:

 SMART(or)STUPID Player X has picked up more than 100 cards
                      Play aborted!
Your program should never printout both the "won" and "abort" message.

The following code demonstrates how to generate random integers within some range.

with Ada.Numerics.Discrete_Random;
with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with GNAT.Command_Line;   use GNAT.Command_Line;

procedure Game_Basic is
   subtype Random_Guess is Integer range 0..4;
   package My_Random is new
     Ada.Numerics.Discrete_Random(Random_Guess);
   use My_Random;
   My_Number: Random_Guess;
   Number_Generator: My_Random.Generator;

begin

   Reset(Number_Generator);
   for I in 1..10 loop
      My_Number := Random(Number_Generator);
      New_Line; Put ("Random Number "); Put (My_Number, 5);
   end loop;

end Game_Basic;

Implementation: Advanced Game (worth 20% of the assignment mark)

Only attempt this part of the assignment if you have completed the above. This will be a separate code submission.

Modify your code from above (but keep the above version) as follows

  • Permit 4, 6 or 8 players in the game. Read this value in via a command line argument. If one of these values is not given raise an exception and terminate the program.
  • For 4, 6 and 8 players select 24, 36 or 48 cards respectively, in a similar manner to above (i.e. all four suites for a given kind). Initially still provide each player with 4 cards and put 2 cards in each pile.
  • Allow the game to continue until there are only 3 players left. You should uniquely identify 1st, 2nd, 3rd etc. When a player has four cards of the same kind they should keep those four cards, discard their remaining card (to the pile on their left), and then withdraw from active participation in the game until all winners have been identified. This means that you will have to deal with the fact that there might be two (or more) piles of cards on the table with a player in between who is no longer participating in the game.
  • Have three stupid players with the rest being smart players.
  • The program should stop if any player picks up more than 250 cards.
The following code indicates how to read and process input given on the command line:
with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with GNAT.Command_Line;   use GNAT.Command_Line;

procedure Game_Advanced is
   No_Of_Players : Positive := 4;
   No_Of_Smart   : Positive := 1;


   Options_Ok : Boolean := True;
   Option     : Character;

   procedure Print_Options is

   begin
      New_Line; Put ("accepted options:");
      New_Line; Put ("   [-n {number of players: positive}]             -> default:"); Put (No_Of_Players, 5);
      New_Line; Put ("   [-m {number of smart players: positive}]       -> default:"); Put (No_Of_Smart, 5);
      New_Line;
   end Print_Options;

begin

   Print_Options;

   Initialize_Option_Scan;
   loop
      begin
         Option := Getopt ("n: m:");
         case Option is
            when ASCII.NUL => exit;
            when 'n' => No_Of_Players    := Positive'Value (Parameter);
            when 'm' => No_Of_Smart      := Positive'Value (Parameter);
            when others => raise Program_Error;
         end case;
      exception
         when others =>
            New_Line; Put ("---> Error in option -");
            Put (Option); New_Line;
            Options_Ok := False;
      end;
   end loop;

   New_Line; Put ("number of players      "); Put (No_Of_Players);
   New_Line; Put ("number of smart players"); Put (No_Of_Smart);

end Game_Advanced;