Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I believe this is Java but I need it to be Python? Is it possibile? Game.class package hw 1 ; public class Game { /

I believe this is Java but I need it to be Python? Is it possibile? Game.class
package hw1;
public class Game {
// constants
public static final int FINAL_POSITION =43;
public static final int INITIAL_POSITION =-1;
public static final int NUM_PLAYERS =2;
public static final String BLUE = "BLUE";
public static final String GREEN = "GREEN";
public static final String ORANGE = "ORANGE";
public static final String PURPLE = "PURPLE";
public static final String RED = "RED";
public static final String YELLOW = "YELLOW";
public static final String COLORS[]={ BLUE, GREEN, ORANGE, PURPLE, RED,
YELLOW };
public static final int NUM_COLORS =6;
// names of special characters marking specific spaces on the board
public static final String PLUMPY = "PLUMPY";
public static final String MR_MINT ="MR. MINT";
public static final String JOLLY = "JOLLY";
// A partial board for Candyland
// based on the picture at: http://www.lscheffer.com/CandyLand-big.jpg
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
};
public int positions[]= new int[NUM_PLAYERS];
// set all elements of the positions array to INITIAL_POSITION
// Parameters: positions -- will store where the players are
// numPlayers -- how many there are
void initialize(int positions[], 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(String str){
for (int color =0; color < NUM_COLORS; color++){
if (str == COLORS[color])
return true;
}
return false;
}
// Description: Starting after indicated position search forward on board to
// find the color
// Parameters: startPos -- index of current space of player -- start looking
//*after* this space
// color -- find the next space with this color
// Returns: index of next space of chosen color
int findColor(int startPos, String color){
for (int pos = startPos +1; pos < FINAL_POSITION; pos++)
if (board[pos]== 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
public int findPerson(String name){
try {
if (name == PLUMPY)
return 8;
if (name == MR_MINT)
return 17;
if (name == JOLLY)
return 42;
} catch (Exception e){
System.out.println("no such person in the game");
// should not get here -- just here to get program to stop
}
return FINAL_POSITION;
}
// 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
// Returns: new position of player after move
public int move(int player, String card, boolean repeat, int positions[]){
int nextPos = positions[player];
if (isColor(card)){
nextPos = findColor(positions[player], card);
if (repeat)
nextPos = findColor(nextPos, card);
return nextPos;
} else
return findPerson(card);
}
// 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
public boolean nowinner(int positions[], int numPlayers){
for (int player =0; player < NUM_PLAYERS; player++){
if (positions[player]== FINAL_POSITION)// reached the end
return false;
}
return true;
}
// Description: Display welcome String
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[]={ PLUMPY, YELLOW, RED, YELLOW, GREEN, MR_MINT,
JOLLY, RED, GREEN };
int NUM_CARDS =9;
int next =0;
if (next >= NUM_CARDS)
return "";
return testRolls[next++];
}
// 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 };
int NUM_CARDS =9;
int next =0;
if (next >= NUM_CARDS)
return false;
return testRollsRepeat[next++];
}
// 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security

Authors: Alfred Basta, Melissa Zgola

1st Edition

1435453905, 978-1435453906

More Books

Students also viewed these Databases questions