*******Convert this code into Java******* #include \"stdafx.h\" #include <iostre
ID: 3780967 • Letter: #
Question
*******Convert this code into Java******* #include "stdafx.h" #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; }
Explanation / Answer
import java.io.*;
public class GlobalMembers
{
// 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;
public static final String PLUMPY = "PLUMPY";
public static final String MR_MINT = "MR. MINT";
public static final String JOLLY = "JOLLY";
public 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};
public static int[] positions = new int[NUM_PLAYERS];
public static void initialize(int[] positions, int numPlayers)
{
for (int player = 0; player < numPlayers; player++)
{
positions[player] = INITIAL_POSITION;
}
}
public static boolean isColor(String str)
{
for (int color = 0; color < NUM_COLORS; color++)
{
if (COLORS[color].equals(str))
{
return true;
}
}
return false;
}
public 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;
}
public static int findPerson(String name)
{
if (PLUMPY.equals(name))
{
return 8;
}
if (MR_MINT.equals(name))
{
return 17;
}
if (JOLLY.equals(name))
{
return 42;
}
System.out.println( "No such person in the game ");
return FINAL_POSITION;
}
public 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);
}
}
public 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
public static void printIntro()
{
System.out.print("This is a crude version of Candyland");
System.out.print(" ");
System.out.print(" ");
}
private static int draw_next = 0;
public static String draw()
{
String[] testRolls = {PLUMPY, YELLOW, RED, YELLOW, GREEN, MR_MINT, JOLLY, RED, GREEN};
final int NUM_CARDS = 9;
if (draw_next >= NUM_CARDS)
{
return "";
}
return testRolls[draw_next++];
}
private static int drawRepeat_next = 0;
public static boolean drawRepeat()
{
boolean[] testRollsRepeat = {false, true, false, true, false, false, false, false, false};
final int NUM_CARDS = 9;
int next = 0;
if (drawRepeat_next >= NUM_CARDS)
{
return false;
}
return testRollsRepeat[drawRepeat_next++];
}
public static void printWinner(int[] positions, int numPlayers)
{
for (int player = 0; player < numPlayers; player++)
{
if (positions[player] == FINAL_POSITION)
{
System.out.print("Player ");
System.out.print(player);
System.out.print(" wins!");
System.out.print(" ");
}
}
}
// Description: Play the game
public static void playGame()
{
int nextPlayer = 0;
boolean done = false;
initialize(positions, NUM_PLAYERS);
while (nowinner(positions, NUM_PLAYERS) && !done)
{
String nextCard = draw();
boolean repeat = drawRepeat();
if (!nextCard.equals(""))
{
positions[nextPlayer] = move(nextPlayer, nextCard, repeat, positions);
System.out.print("Player ");
System.out.print(nextPlayer);
System.out.print(" is at position ");
System.out.print(positions[nextPlayer]);
System.out.print(" ");
nextPlayer = (nextPlayer + 1) % NUM_PLAYERS;
}
else
{
done = true;
}
}
if (nowinner(positions, NUM_PLAYERS))
{
System.out.print("No winner");
System.out.print(" ");
}
else
{
printWinner(positions, NUM_PLAYERS);
}
}
public static void main(String args[])
{
GlobalMembers g=new GlobalMembers();
g.playGame();
g.findPerson("JOLLY");
g.printIntro();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.