Instructions
Requirements and Specifications
- this will contain the enumerated type for card colors
- provided file in the bottom
- public enum Color {RED, GREEN, BLUE, YELLOW}
- 2 properties - a color (RED, GREEN, BLUE, YELLOW) and a number (1 - 9)
- constructor that initiates color and number
- setter and getter methods
- toString method that returns a string with the color and number like this: R-5 or G-2, etc
- provided on the bottom
// card class
import java.util.*;
public class Card {
// Card constructor; set the color and number of the card
public Card (/* parameters go here */ ) {
}
public void setFaceValue(/* parameter */) { }
public void setFaceColor(/* parameter */ ) { }
public int getFaceValue() { }
// return String value of the card: R-7, G-3, etc
// first name of color
// google how to convert java enum value to string
// then google how to get 1st character of a string
// then google how to convert an int to a String
public String toString() {
}
}
Deck
- an ArrayList of Cards
- one card for each color and number, so for example R-1, R-2 .... R-9, G-1, G-2 .... G-9, B-1, B-2 ... B-9, .....
- constructor will create the cards and add to the ArrayList; then it will call its own Shuffle method
- method to shuffle the deck of cards - checkout the shuffle method in ArrayList
- method to draw the next card from top of the deck and them remove it; call this drawCard
- method to return a card to the bottom of the deck; call this returnCard
- provide on bottom
// deck class
import java.util.*;
public class Deck {
// constructor for Deck
// creates an ArrayList of Card objects
// fills the deck with cards of all colors and all numbers
// shuffles the deck
public Deck (int num) {
}
// Shuffle method
// check out the shuffle method for collections - see link in assignment
// you need to shuffle so that you control the random number sequence
// google to find out how to control this; it is known as the random number generator seed
public void Shuffle() {
}
// drawCard method
// return the top card from the deck and then remove it
public Card drawCard() {
}
// returnCard method
// place card at bottom of deck
public void returnCard(Card card) {
}
}
Player
- each player has a name, just a string
- each player has a hand which is an ArrayList of 5 cards
- each player has a score, just an int
- each player must have access to the deck of cards; this deck will be created by the Game class, but needs to be passed in to the Player constructor and stored in the Player class so the player can draw cards from the deck
- each player needs a toString method that returns something like this
- Player: Danny Score: 55 Hand: B-1/G-8/B-3/B-9/R-9/
- in this example Danny was the player's name, his score was 55 and the cards in his hands were are shown
- each player needs a discard method that will remove a card from their hand, return it to the bottom of the deck and update their score
- each player needs a method to update their score as follows
- (C) version score is determined by adding up the facevalue of each card in a player's hand
- version score is determined the same as C version BUT THEN you divide by the number of distinct colors in the player's hand and truncating
- (A) version score - sum up facevalues for each color, then multiply by number of times that color occurs in players hand, then sum all of these up
- Provide on bottom
R-5/G-3/B-1/R-3/G-1 = 5+3+1+3+1 = 13
R-5/G-3/B-1/R-3/G-1 = 5+3+1+3+1 = 13 / 3 = 4.3333 = 4 when you truncate; you divide from 3 because player has 3 distinct colors - RGB
// player class
public class Player {
private String name;
private ArrayList hand = new ArrayList();
private int score;
private Deck theGameDeck;
// Player constructor; pass in the player's name and reference to deck
// note; deck should be created by the Game class
public Player (String n, Deck deck) {
name = n;
theGameDeck = deck;
score = 0;
for (int i=1; i<=5; i++) {
// draw a card from the deck and add to hand
}
upDateScore();
}
public void setScore(int sc) { score = sc; }
public int getScore() { return score; }
// return a string version of the player that looks like this
// Player: player's name Score: player's score Hand: player's hand like this - R-3/B-2/G-9/..... (list all the cards in hand)
// google how to convert an int (score) to string
// google how to get object from ArrayList (hand)
public String toString() {
}
// remove top card(s) from player's hand
// return it to bottom of deck
// draw another card from top of deck
// parameter num specifies how many cards to discard & draw
// be sure to update player's score when done
public void discard(int num) {
}
// method to update the player's score
// notice this is a private method; only members of this class can call it
// call any time the player's hand changes
private void upDateScore() {
}
}
Game
- each game has 2 player objects
- each game has 1 deck object
- you will need a constructor that receives the names of each player; it will create the deck and the 2 players
- you will need a getPlayer1 and getPlayer2 method that returns the corresponding player object
// game class
public class Game {
// Game constructor
// create a deck of cards by calling the Deck constructor
// create the number of players you want (default is 2)
public Game( /* pass in the player's names */ ) {
}
public Player getPlayer1() { return p1; }
public Player getPlayer2() { return p2; }
}
PlayGame
- this will be the main program
- I will provide a simple one but will test with more complex ones, so you should test with ones you create also
- DO NOT SUBMIT THIS CLASS FILE,
- Simple code bellow
// play game - main program
// demonstrate a simple test program
// create a game for 2 people
// display each player's status
// have each player discard a few cards
// display each player's status
class PlayGame {
public static void main(String[] args) {
Game g = new Game("Danny", "Karen");
System.out.println(g.getPlayer1().toString());
System.out.println(g.getPlayer2().toString());
g.getPlayer1().discard(3);
g.getPlayer2().discard(5);
System.out.println(g.getPlayer1().toString());
System.out.println(g.getPlayer2().toString());
}
}
Source Code
CARD
public class Card {
private Color color;
private int number;
// Create a card
public Card(Color color, int number) {
this.color = color;
this.number = number;
}
// Return a string representation
@Override
public String toString() {
if (color == Color.RED) {
return "R-" + number;
}
if (color == Color.GREEN) {
return "G-" + number;
}
if (color == Color.BLUE) {
return "B-" + number;
}
return "Y-" + number;
}
// Setter and getter methods
public Color getFaceColor() {
return color;
}
public void setFaceColor(Color color) {
this.color = color;
}
public int getFaceValue() {
return number;
}
public void setFaceValue(int number) {
this.number = number;
}
}
GAME
public class Game {
private Player p1;
private Player p2;
private Deck deck;
// Create a game with 2 players
public Game(String p1Name, String p2Name) {
deck = new Deck();
p1 = new Player(p1Name, deck);
p2 = new Player(p2Name, deck);
}
// Getter methods
public Player getPlayer1() {
return p1;
}
public Player getPlayer2() {
return p2;
}
}