Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 2. Task Specification
- You will develop, in Java, a multi-threaded card playing simulation. Within your design you will
- need to implement (at least) a thread-safe Card class and a thread-safe Player class
- (depending upon your design, you may also implement additional classes, for instance a
- CardDeck class). You will also develop an executable CardGame class.
- The game has n players, each numbered 1 to n (which for clarity in the illustration below are
- named player1, player2, … , playern), with n being a positive integer, and n decks of cards, again,
- each numbered 1 to n (which for clarity in the illustration below are named deck1, deck2, … ,
- deckn). Each player will hold a hand of 4 cards. Both these hands and the decks will be drawn
- from a pack which contains 8n cards. Each card has a face value (denomination) of a nonnegative
- integer1.
- The decks and players will form a ring topology (see illustration in the figure below for the case
- where n = 4). At the start of the game, each player will be distributed four cards in a round-robin
- 3
- If a player does not start with a winning hand, they will implement a simple game strategy, as
- specified below (note, the strategy is not optimal).
- Each player will prefer certain card denominations, which reflect their index value, e.g., player1
- will prefer 1s, player2 will prefer 2s, etc. After drawing a card from their left, a player will discard
- one of their cards to the deck on their right, (e.g. player2 will draw from deck2 and discard to
- deck3). The card they discard must display a value which is not of their preferred denomination.
- Additionally, a player must not hold onto a non-preferred denomination card indefinitely, so you
- must implement your Player class to reflect this restriction (otherwise the game may stagnate).
- Developing a solution
- You will need to implement an executable class called CardGame, whose main method requests
- via the command line (terminal window) the number of players in the game (i.e. ‘n’), and on
- receiving this, the location of a valid input pack. A valid input pack is a plain text file, where each
- row contains a single non-negative integer value, and has 8n rows. After reading in the input
- pack, the CardGame class should distribute the hands to the players, fill the decks and start the
- required threads for the players. If the pack is invalid, the program should inform the user of this,
- and request a valid pack file.
- As a player processes their hand, each of its actions should be printed to an output file which is
- named after that particular player (i.e. the output file for the first player should be named
- player1_output.txt). In game actions should be printed to the file in a similar form to the
- following example:
- player 2 draws a 4 from deck 2
- player 2 discards a 3 to deck 3
- player 2 current hand is 1 1 2 4
- Additionally, at the start of the game the first line of the file should give the hand dealt, e.g.
- player 2 initial hand 1 1 2 3
- and at the end of the game the last lines of the file should read either:
- player 2 wins
- player 2 exits
- player 2 final hand: 2 2 2 2
- if for instance player 2 wins, or
- player 3 has informed player 2 that player 3 has won
- player 2 exits
- player 2 hand: 2 2 3 5
- in the case where player 3 has won (and the values displayed match the hands and decks
- concerned). There should also be a message printed to the terminal window (as is the case when
- a player wins immediately), i.e. if the 4th player wins, then
- player 4 wins
- 4
- should be printed to the screen. There should only be one player declaring it has won for any
- single game. If the game is won immediately (a winning hand is initially dealt), the output files
- should still be written for all players (although each file will only contain four lines). In addition
- to the player output files, there should also be n deck output file written at the end of the game
- (named, e.g. deck1_output.txt), which should contain a single line of text detailing the contents of
- the deck at the end of the game, e.g.
- deck2 contents: 1 3 3 7
- The combination of a card draw and a discard should be treated as a single atomic action.
- Therefore at the end of the game every player should hold four cards, and every deck should
- contain four cards. The program developed should follow the object_oriented paradigm.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement