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
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.