In: Computer Science
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names of special characters marking specific spaces on the board const string PLUMPY = "PLUMPY"; const string MR_MINT = "MR. MINT"; const 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 }; int positions[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 bool 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 int findPerson(string name) { if (name == PLUMPY) return 8; if (name == MR_MINT) return 17; if (name == JOLLY) return 42; cerr << "No such person in the game" << endl; return FINAL_POSITION; // should not get here -- just here to get program to stop } // 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 int move(int player, string card, bool 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 bool 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() { cout << "This is a crude version of Candyland" << endl << endl; } // 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}; const int NUM_CARDS = 9; static 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 bool drawRepeat() { bool testRollsRepeat[] = {false, true, false, true, false, false, false, false, false}; const int NUM_CARDS = 9; static 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 -- the number of players void printWinner(int positions[], int numPlayers) { 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) cout << "Player " << player << " wins!" << endl; } } // Description: Play the game void playGame() { // Use nextPlayer to switch among the players int nextPlayer = 0; bool done = false; initialize(positions, NUM_PLAYERS); while (nowinner(positions, NUM_PLAYERS) && !done) { string nextCard = draw(); bool repeat = drawRepeat(); if ("" != nextCard) { positions[nextPlayer] = move(nextPlayer, nextCard, repeat, positions); cout << "Player " << nextPlayer << " is at position " << positions[nextPlayer] << endl; nextPlayer = (nextPlayer + 1) % NUM_PLAYERS; } else done = true; } if (nowinner(positions, NUM_PLAYERS)) cout << "No winner" << endl; else printWinner(positions, NUM_PLAYERS); } int main() { printIntro(); playGame(); return 0; }
public class Game
{
final static int FINAL_POSITION = 43;
final static int INITIAL_POSITION = -1;
final static int NUM_PLAYERS = 2;
final static String BLUE = "BLUE";
final static String GREEN = "GREEN";
final static String ORANGE = "ORANGE";
final static String PURPLE = "PURPLE";
final static String RED = "RED";
final static String YELLOW = "YELLOW";
final static String COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED,
YELLOW};
final static int NUM_COLORS = 6;
// names of special characters marking specific spaces on the
board
final static String PLUMPY = "PLUMPY";
final static String MR_MINT = "MR. MINT";
final static String JOLLY = "JOLLY";
static int next;
// A partial board for Candyland
// based on the picture at:
http://www.lscheffer.com/CandyLand-big.jpg
static 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"
};
static 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
static 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
static boolean isColor(String str) {
for (int color = 0; color < NUM_COLORS; color++) {
if (str.equals(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
static int findColor(int startPos, String 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
static int findPerson(String name) {
if (name.equals(PLUMPY)) return 8;
if (name.equals(MR_MINT)) return 17;
if (name.equals(JOLLY)) return 42;
System.out.println("No such person in the game" );
return FINAL_POSITION; // should not get here -- just here to get
program to stop
}
// 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
static 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
static 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
static void printIntro() {
System.out.println("This is a crude version of Candyland\n");
}
// Generate the next value "drawn"
// Returns: the value of the next card drawn. Returning the empty
String indicates
// there are no more values
static String draw() {
String testRolls[] = {PLUMPY, YELLOW, RED, YELLOW, GREEN, MR_MINT,
JOLLY, RED, GREEN};
final int NUM_CARDS = 9;
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
static boolean drawRepeat() {
boolean testRollsRepeat[] = {false, true, false, true, false,
false, false, false, false};
final int NUM_CARDS = 9;
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 -- the number of players
static void printWinner(int positions[], int numPlayers) {
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!");
}
}
// Description: Play the game
static void playGame() {
// Use nextPlayer to switch among the players
int nextPlayer = 0;
boolean done = false;
initialize(positions, NUM_PLAYERS);
while (nowinner(positions, NUM_PLAYERS) && !done) {
String nextCard = draw();
//System.out.println(nextCard);
boolean repeat = drawRepeat();
if (!(nextCard.equals(" "))) {
positions[nextPlayer] = move(nextPlayer, nextCard, repeat,
positions);
System.out.println("Player " +nextPlayer + " is at position "
+ positions[nextPlayer]);
nextPlayer = (nextPlayer + 1) % NUM_PLAYERS;
}
else done = true;
}
if (nowinner(positions, NUM_PLAYERS))
System.out.println("No winner" );
else printWinner(positions, NUM_PLAYERS);
}
public static void main(String a[]) {
printIntro();
playGame();
}
}