--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION...

80.2K

Verified Solution

Question

Programming

--- TURN this Code into Java Language ---#include #include using namespace std;// constantsconst 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 boardconst 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.jpgstring 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 arevoid 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 notbool 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 colorint 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 nameint 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 moveint 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 wonbool 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 stringvoid 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 valuesstring 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 structuresbool 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 playersvoid 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 gamevoid 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;}

Answer & Explanation Solved by verified expert
4.3 Ratings (895 Votes)
public class Game final static int FINALPOSITION 43 final static int INITIALPOSITION 1 final static int NUMPLAYERS 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 NUMCOLORS 6 names of special characters marking specific spaces on the board final static String PLUMPY PLUMPY final static String MRMINT MR MINT final static String JOLLY JOLLY static int next A partial board for Candyland based on the picture at httpwwwlscheffercomCandyLandbigjpg static String board RED PURPLE YELLOW BLUE ORANGE GREEN RED PURPLE PLUMPY YELLOW BLUE ORANGE GREEN RED PURPLE YELLOW BLUE MRMINT 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    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students