Assignment 4
Assignment 4
Assignment 4
Title
Creating a Simple Networked Game
Introduction
For this assignment, we will take a break from building the 201 course website, and implement
a standalone networked game. You will be implementing a simplified version of the card game
black jack. You will be using pure Java to create the game, with a simple console based UI. You
will need to use your knowledge of networking and multi-threading in order to complete the
program.
Server/Client Set Up
Your application will need two programs: the server and the client. The server should keep
track of all the current games in progress and the players associated with each game. The server
program should be started as follows:
Game Set Up
On the client side, when the user successfully connects to the Black Jack server, a menu should
print with the option to either start a game or join a game. If the user inputs a ‘1’ indicating they
wish to start a game, the program should behave as follows:
1) Ask the user to choose the number of players in the game. This will be a number between 1
and 3, inclusively. Make sure you check for appropriate values here.
2) The user should then be prompted for a unique name for their game.
a) If the user enters a name that is already in use by another ongoing game, they
should receive a message that their input was invalid, and the program should go
back to step 2).
3) The user should then be prompted for their username, which can be anything except the
empty string.
4) The user should then see a message with the number of players that must join before the
game can start.
Image 3: Starting a game
If the user inputs a ‘2’, indicating they wish to join a game, the program should behave as
follows:
1) The user should be prompted for the name of the game they wish to join.
a) If a game with this name that is waiting for players does not exist, then print a
message that the choice was invalid. Have the program go back to step 1).
2) Have the user enter their choice of username.
a) If this username has already been chosen by another player in this game, then print
a message that the choice was invalid. Have the program go to 3)
3) Print a message that the game will begin shortly after other players join.
Image 5: Player that started the game gets updated on when players join
For the users that joined the game, they should not get any updates, and are only notified when
the game starts.
Image 6: Player that joined the game is only notified when the the game has been started
If the player chooses to ‘hit’ and their new card sum exceeds 21, they automatically ‘bust’ and
lose the round, giving their bet chips to the dealer. Until the player chooses to ‘stay’, the player
can ‘hit’ as many times as they like (unless they ‘bust’ during the process). If the player has an
Ace in their hand, they can treat it in their sum as either a 1 or an 11. If their hand would ‘bust’
if the Ace was valued at 11, then the Ace is automatically treated with a value of 1.
After each player stays or has busted, the dealer reveals their face down card. The dealer is then
required to ‘hit’ until their card sum is greater than or equal to 17. If the dealer is dealt an Ace at
any point, they should ‘stay’ only if the Ace valued at 11 brings their total to 17 or more.
However, if the Ace valued at 11 causes the dealer to bust, the value of the Ace is 1.
At this point, chips are either distributed or collected from each player based on the hand of the
dealer (as described in the second section above). If any of the players now has a chip total of 0,
the game ends. The player with the most chips at the end will be declared the winner. If all
players have 0 chips left, the dealer is declared the winner.
Game Play
Each player will begin the game with 500 chips. The dealer (i.e. your program) should ‘shuffle’
the deck before each round— in other words the dealing of the cards should be pseudo-
random. Each player, in order of when they joined the game, should be given the opportunity
to bet chips for the round. Their bet must be greater than 0 and less than or equal to their chip
total (you can assume the player will always enter a valid bet amount). Each time a player
makes their bet, all other players should be notified with the player’s name and bet amount.
Then each player should see a message as to whose turn it is to bet next.
The dealer should assign two cards to each player and to themselves. The players’ and dealer’s
cards should be displayed with one of the dealer’s cards hidden. After the betting, the initial
state of the game should be printed. It should show the values and suits of the face up cards, the
chip total, the bet amount, and the status (card sum) of the cards for each player.
Image 9: The UI print out of each player’s current state, including the dealer
Then each player should have a turn to add cards to their hand (in the order they joined the
game, which in the screenshot example is ‘queen’, then ‘emperor’, then ‘king’). Each time a
player chooses to either ‘hit’ or ‘stay’, the other players should see a message with that player’s
choice and the dealt card (if the choice was to ‘hit’). When the current player either chooses to
‘stay’ or they ‘bust’, there should be a printout with the new state of that player’s hand.
Image 10: queen’s turn, from the perspective of Player 2 (‘emperor’) and Player 3 (‘king’)
Image 11: emperor’s turn, from the perspective of Player 1 (‘queen’) and Player 3 (‘king’)
Image 12: king’s turn, from the perspective of Player 3 (‘king’). Note the commands provided
to the user for choosing to either ‘stay’ or ‘hit’
After each player has had a turn to add cards to their hand, the dealer then adds cards to their
own hand if necessary (remember, the dealer must ‘hit’ until their sum is greater than or equal
to 17).
Image 13: From the perspective of all players, UI print out of the dealer’s hand
At this point, it should be determined for each player if they should be awarded chips,
deducted chips or neither.
Image 14: From the perspective of Player 3 (‘king’), determining new chip totals. Note that
player’s with ‘blackjack’ receive double their bet amounts.
The rounds should continue until one of the players runs out of chips. Each round, the ‘Chip
Total’ field in the UI should update to show the new chip total for each player based on the
results of the previous round.
When the game ends, print a message to each player stating the player that won the game and
the player that lost the game (the player that ran out of chips). At this point, the client program
can terminate.
Please make your console output as similar to the screenshots as possible. We will be looking
for you to include the same detail in the outputted messages to players as is shown in these
screenshots.
As an example of what to output for the other result states of the players, let us assume ‘queen’
tied with the dealer, ‘emperor’ had a lesser sum than the dealer, and ‘king’ had a greater sum
than the dealer. The output would look like the following (assuming the players made the same
bets as in the example game play in the previous screenshots):
Image 15: From the perspective of Player 3 (‘king’), example of alternate outcomes.
Program Execution
We will first run an instance of your server program, and then we will run multiple instances of
your client program. Make sure that your server can handle multiple ongoing games at once,
and multiple players in each game.
Note: You will be graded on code quality for this assignment. Please remember to include
which classes contain the main methods for your server and client programs in your ReadMe.
Grading Criteria (4.5%)
0.5% - your code conforms to the style rubric (see last page for details)
Other (0.5%)
0.5% - the server properly handles multiple ongoing games at once
Style Guide (with grading breakdown)
● Code commenting (Total = 0.1%)
○ Methods should have a 1-3 line comment above with a description of the inputs,
outputs, and what the function does (0.08%)
○ For every 20 lines of code, there should be at least 1 comment (0.02%)
● Naming conventions (Total = 0.1%)
○ Use camel case for naming variables, classes and functions (0.05%)
■ For functions and variables: first letter is lower case
■ For classes: first letter is upper case
○ Unless indexing loops, use intuitive naming for variables to improve readability
of your code (0.05%). Example:
OK:
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
NOT OK:
List<Integer> i = new ArrayList<>();
● README: Your project must contain a README that includes the following (Total =
0.1%)
○ Name, student ID number, email, lecture section number(0.025%)
○ Anything important that should be noted to your grader - something hardcoded,
functionality you know isn’t working, special instructions (0.025%)
○ Description of your class structure, indicating where your main class and/or
main method is (0.05%)
● You should be using multiple packages, multiple classes, and multiple functions within
classes (Total = 0.15%)
○ You should have at least 2 packages (and no default package) (0.025%)
○ You should have at least 6 classes (0.025%)
○ You should have at least 18 functions (includes getters and setters), each with no
more than 100 lines of code (0.1%)
● Use private variables as much as possible: if something is public explain in a comment
why it must be public (0.025%)
● If you use any static objects or methods (besides the main method), explain in a
comment why it must be static(0.025%)