Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone help me setup this in JAVA? I\'m new to java. Thanks Create a new Ja

ID: 3782355 • Letter: C

Question

Can someone help me setup this in JAVA? I'm new to java.

Thanks

Create a new Java Application project. Create a new package called "hw2" Copy the classes from hw1 into the new package hw2 Create a new Java Enum Face that includes the colors (BLUE, RED, ...) and the characters (PLUMPY MR_MINT: JOLLY) include an instance variable isCharacter, a boolean which is true for the character values, false for the colors Define a class called Card: Include a Face and a boolean to indicate if the card is a "double" - this is only for the colors o Include a constructor that takes a Face and then a boolean parameter Include getFace() and isDoubled() to return the face of the card and whether it is doubled or not Override the toString() method for this class - take the easy way out and use the default toString() method provided for all enum classes to get the value of the Face if the card is "doubled", return "double " you will get return values like "RED", "double BLUE" or "MR_MINT"

Explanation / Answer

Card.java

CandyLand.java

package hw2;

public class CandyLand {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Game game = new Game();
        game.playGame();
    }
  
}

Game.java

package hw2;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Game {

    Board board;
    static final int NUM_PLAYERS = 2;
    static final int MAX_NUM_PLAYERS = 6;
    int nextRoll = 0;
    List<Player> list = new ArrayList();

    Game() {
        board = new Board(NUM_PLAYERS);
    }

    void printIntro() {
        System.out.println("This is a crude version of Candyland ");
    }

// Generate the next value "drawn"
// Returns: the value of the next card drawn. Returning the empty string indicates
//          there are no more values
    String draw() {
        String testRolls[] = {Board.PLUMPY, Board.YELLOW,
            Board.RED, Board.YELLOW, Board.GREEN,
            Board.MR_MINT, Board.JOLLY, Board.RED,
            Board.GREEN};
        final int NUM_CARDS = 9;

        if (nextRoll >= NUM_CARDS) {
            return "";
        }
        return testRolls[nextRoll];
    }

// Indicate if this card is a "double" color
// Returns: true if the color is a double, false if not.
// NOTE: This is a very bad way to do this -- but it does help to motivate structures
    boolean drawRepeat() {
        boolean testRollsRepeat[] = {false, true, false, true, false, false, false, false, false};
        final int NUM_CARDS = 9;

        if (nextRoll >= NUM_CARDS) {
            return false;
        }
        return testRollsRepeat[nextRoll];
    }

    public void addPlayers(String name) {

        if (!name.equals("done")) {
            list.add(new Player(name));
        }
    }

    void playGame() {
        // Use nextPlayer to switch among the players
        int nextPlayer = 0;
        boolean done = false;

        printIntro();

        while (list.size() <= MAX_NUM_PLAYERS) {
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter a name player: ");
            String name = sc.nextLine();
            while (name.contains(" ")) {
                System.out.println("name must be a single word. Repeat");
                name = sc.nextLine();
            }

            if (!name.equals("done")) {
                addPlayers(name);
            } else if (name.equals("done")) {
                if (list.size() < NUM_PLAYERS) {
                    System.out.println("Add more players");
                } else {
                    break;
                }

            }

        }

        while (board.nowinner() && !done) {
            String nextCard = draw();
            boolean repeat = drawRepeat();
            nextRoll++;

            if ("" != nextCard) {
                board.move(nextPlayer, nextCard, repeat);
                nextPlayer = (nextPlayer + 1) % NUM_PLAYERS;
            } else {
                done = true;
            }
        }
        if (board.nowinner()) {
            System.out.println("No winner");
        } else {
            board.printWinner();
        }
    }

}


Board.java

package hw2;

class Board {
// constants

    static final int INITIAL_POSITION = -1;

    static final String BLUE = "BLUE";
    static final String GREEN = "GREEN";
    static final String ORANGE = "ORANGE";
    static final String PURPLE = "PURPLE";
    static final String RED = "RED";
    static final String YELLOW = "YELLOW";
    static final String[] COLORS = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW};
    static final int NUM_COLORS = 6;

// names of special characters marking specific spaces on the board
    static final String PLUMPY = "PLUMPY";
    static final String MR_MINT = "MR. MINT";
    static final String JOLLY = "JOLLY";

// positions of the special characters on the board below
    private static final int JOLLY_POS = 42;
    private static final int MR_MINT_POS = 17;
    private static final int PLUMPY_POS = 8;

    private final String[] board = {RED, PURPLE, YELLOW, BLUE, ORANGE, GREEN, RED, PURPLE, PLUMPY, YELLOW,
        BLUE, ORANGE, GREEN, RED, PURPLE, YELLOW, BLUE, MR_MINT, ORANGE, GREEN,
        RED, PURPLE, YELLOW, BLUE, ORANGE, GREEN, RED, PURPLE, YELLOW, BLUE,
        ORANGE, GREEN, RED, PURPLE, YELLOW, BLUE, ORANGE, GREEN, RED, PURPLE,
        YELLOW, BLUE, JOLLY, ORANGE

    };
    final int FINAL_POSITION = board.length;

    private int[] positions;
    private final int numPlayers;

    /**
     * set all elements of the positions array to INITIAL_POSITION
     *
     * @param _numPlayers -- how many there are
     */
    Board(int _numPlayers) {
        numPlayers = _numPlayers;
        positions = new int[numPlayers];
        for (int player = 0; player < numPlayers; player++) {
            positions[player] = INITIAL_POSITION;
        }
    }

    /**
     * Description: Test if string is a valid color in the game Parameter: str
     * -- string to test Returns: true if it is a color in the game, false if
     * not
     */
    boolean isColor(Face str) {
        for (int color = 0; color < NUM_COLORS; color++) {
            if (COLORS[color].equals(str)) {
                return true;
            }
        }
        return false;
    }

    int findFace(int startPos, Face color) {
        for (int pos = startPos + 1; pos < FINAL_POSITION; pos++) {
            if (board[pos].equals(color)) {
                return pos;
            }
        }
        return FINAL_POSITION;
    }

    /**
     * Description: find position of indicated person Parameters: name -- name
     * of person to look for Returns: index of space for this name
     */
    int findPerson(Face name) {
        if (PLUMPY.equals(name)) {
            return PLUMPY_POS;
        }
        if (MR_MINT.equals(name)) {
            return MR_MINT_POS;
        }
        if (JOLLY.equals(name)) {
            return JOLLY_POS;
        }
        throw new Error("No such person in the game");
    }

    /**
     * Description: Move a player Parameters: player -- index of player to move
     * card -- indicates where to move repeat -- true if card is a "double"
     * color, false if not positions -- where the players are
     */
    void move(int player, Face card) {
        int nextPos = positions[player];

        if (isColor(card)) {
            nextPos = findFace(positions[player], card);
            positions[player] = nextPos;
        } else {
            positions[player] = findPerson(card);
        }
        System.out.println("Player " + player + " is at position "
                + positions[player]);
    }

    public int winningPosition() {
        return FINAL_POSITION;
    }

    /**
     * Description: Check for a winner Parameters: positions -- where the
     * players are numPlayers -- how many there are Returns: true if there are
     * no winners yet false if anyone has won
     */
    boolean nowinner() {
        for (int player = 0; player < numPlayers; player++) {
            if (positions[player] == FINAL_POSITION) // reached the end
            {
                return false;
            }
        }
        return true;
    }

    /**
     * Print the identity of the winner, if any. If there are no winners, do
     * nothing. Parameters: positions -- the array indicating where the players
     * are located numPlayers -- the number of players
     */
    void printWinner() {
        for (int player = 0; player < numPlayers; player++) {
            // Would be clearer to use a different constant to
            // explicitly define the winning position
            if (positions[player] == FINAL_POSITION) {
                System.out.println("Player " + player + " wins!");
            }
        }
    }
}


Player.java

package hw2;

public class Player {

    private String name;
    private int position;
    static final int INITIAL_POSITION = -1;

    Player(String name) {
        this.name = name;
        this.position = INITIAL_POSITION;
    }

    public String getName() {
        return name;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int newPosition) {
        this.position = newPosition;
    }

}


Face.java

package hw2;

public enum Face {

    GREEN(false),
    RED(false),
    PURPLE(false),
    YELLOW(false),
    BLUE(false),
    ORANGE(false),
    PLUMPY(true),
    MR_MINT(true),
    JOLLY(true);

    private final boolean isCharacter;

    Face(boolean isCharacter) {
        this.isCharacter = isCharacter;
    }

    public boolean isCharacter() {
        return isCharacter;
    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote