Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Anything Helps: Convert the given c++ code for candyland game into java Here are

ID: 3781099 • Letter: A

Question

Anything Helps: Convert the given c++ code for candyland game into java

Here are the steps for this assignment:

Create a new Java Application project. Java does need main() to be in a class. The wizard for creating a Java Application lets you specify the name of this class. Call it hw1.Candyland, which puts it in package hw1. Java does not force you to put classes into packages, but strongly recommends it, so I am forcing you to do so.

As you know by now, writing programs as one large main() function is just wrong, so you will create classes to make the code easier to read and maintain. Start your main() with the following lines of code:

Game game = new Game();

game.play();

NetBeans and Eclipse (and, I assume, IntelliJ) both feature “quick fix” options for a number of simple errors like these. For NetBeans, click on the icon on the left, or highlight the part of the code marked as an error and enter “Alt-Enter” to get options to fix the error. Choose the option to define a new class in the hw1 package called Game.

The constructor for Game should initialize a field by constructing a Board

The Board class should cover

the board and positions arrays

the initialize(), findColor(), findPerson(), move(), nowinner() and printWinner() functions

The Game class covers everything else.

Corrections and additional instructions to make assignment clearer:

the isColor function should also be part of the Board class

initialize() will be replaced by the Board constructor

remove the positions array and numPlayers as parameters to the functions in Board, except for passing the number of players to the Board constructor

the NUM_PLAYERS constant will be defined in the Game class

move() should be changed to be void and should update the positions array

Game has to manage the counter for the next roll as a field of the class since Java does not support static local variables. (Functions must be methods in classes, so fields already cover the idea of variables keeping their value between calls to the function. Fields are typically called "instance variables" by Java programmers)

You should fix the style problems in the code: there are unnamed constants, missing function comments

for findPerson(), if the parameter does not match any known string, throw an Error (java.lang.Error) with the error message from the C++ code

***************************************

***************************************

//#include "stdafx.h"

Explanation / Answer

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 -- 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)
               System.out.println("Player " + player + " wins!");
       }
   }

   // Description: Play the game
   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();
           boolean repeat = drawRepeat();

           if ("" != nextCard) {
               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);
   }

}

-------------------------------------------------------------------------------------------------------------

package hw1;

public class Test {

   public static void main(String[] args) {
       Game game = new Game();
       game.printIntro();

       game.playGame();

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote