Write a game simulation program in java. You can use the Eclipse IDE The game is
ID: 3844780 • Letter: W
Question
Write a game simulation program in java. You can use the Eclipse IDE The game is Chutes and Ladders No need to create a user interface except maybe a command line to enter some parameters, but not required. The game simulation can run on its own until a player wins. All output can be printed to the console. Please do not use Swing or any other GUI framework in this project We are not looking for fancy features in the program, you can keep it as simple as you wish as long as the work demonstrates your ability to write well organized, clean and easily understood code, good choice of classes and algorithms. Game description: The children's game Chutes and Ladders, picture attached, is a chase game in which one or more players, moving alternately, move their tokens along the numbered spaces of a board according to the roll of a die: the tokens are initially off-the-board at what is effectively space zero. Tokens that land on the bottom end of a ladder are promoted to the space at the top and of the ladder, and tokens that land on the top and of a chute are demoted to the space at the bottom and of the chute Space 100 must be reached by exact roll of the die: if the roll of the die: would take the taken past space 100, the token remains where it is and play passes to the next player. The winner of the game is the first token to reach space 100. Game board: http://jharris.toomey.org/cub%20Scout%20Files/Chutes%20&20ladders% 20Board%20for20Safety%20Game.PNGExplanation / Answer
Board.java
=============================
import java.util.Scanner;
import java.io.*;
public class Board{
// A chutes and ladders configuration array (configuration) that will serve as a
//lookup table to move the player/computer to a new location if a chute or ladder is at
//that position on the board.
// A constant integer variable that contains the size of the boardGame
private final int boardSize = 100;
private int[] configuration = new int[boardSize];
private String[][] positions;
// A way to store the current index of the player and the current index of the computer
private int playerPosition;
private int computerPosition;
Scanner fileScan;
Scanner scan2;
String fileLine;
// A constructor that takes in the name of the chutes and ladders configuration file.
//Initialize the positions array. Set the player and the computer to index 0 to get started.
public Board(String fileName) throws IOException{
//reads the file
fileScan = new Scanner(new File(fileName));
//fileScan = new Scanner(file);
while (fileScan.hasNext()){
fileLine = fileScan.nextLine();
//reads each line and saves it as a new League object
scan2 = new Scanner(fileLine);
//divides the line and saves each as a paramater to make a new player object
//we did this in class so this is right.
scan2.useDelimiter(",");
String number1 = scan2.next();
number1 = number1.trim();
int iNumber1 = Integer.parseInt(number1);
String number2 = scan2.next();
number2 = number2.trim();
int iNumber2 = Integer.parseInt(number2);
}
//holds the player/computer position on the board
positions = new String[boardSize][2];
this.positions[0][0] = "P";
this.positions[0][1] = "C";
this.playerPosition = 0;
this.computerPosition = 0;
}
public int getBoardSize(){
return this.boardSize;
}
//prints the board and the position of the
//computer and the player
public void printBoard(){
for (int x = 0; x < this.boardSize; x++){
System.out.print(x + 1);
if (this.positions[x][0] == "P"){
System.out.print("*P*");
}
if (this.positions[x][1] == "C"){
System.out.print("*C*");
}
System.out.print(" ");
if ((x + 1) %10 == 0){
System.out.println("");
}
}
}
// returns the current position of the player
public int getPlayerPosition(){
return this.playerPosition;
}
//returns the current position of the computer
public int getComputerPosition(){
return this.computerPosition;
}
public void setPlayerPosition(int dice){
//resets the current position of the board to nothing.
positions[this.playerPosition][0] = "0";
this.playerPosition += dice;
int originalPos = this.playerPosition;
//increases the size of the array if the player postion + 1 is bigger
if (this.playerPosition + 1 >= configuration.length)
increaseSize();
//exits if the player has won
if (this.playerPosition >= this.getBoardSize()){
System.out.println("You win!");
System.exit(0);
}
//exicutes of the player went through a chute/ladder
if (configuration[this.playerPosition+1] != 0){
this.playerPosition = configuration[this.playerPosition +1] - 1;
if (originalPos > this.playerPosition)
System.out.println("You went down a chute!");
else if (originalPos < this.playerPosition)
System.out.println("You went up a ladder!");
}
positions[this.playerPosition][0] = "P";
}
public void setComputerPosition(int dice){
//resets the current position to nothing.
positions[this.computerPosition][1] = "0";
this.computerPosition += dice;
//exits if the computer has won
if (this.computerPosition >= this.getBoardSize()){
System.out.println("The computer wins!");
System.exit(0);
}
int originalPos = this.computerPosition;
if (this.computerPosition + 1 >= configuration.length)
increaseSize();
if (configuration[this.computerPosition+1] != 0){
this.computerPosition = configuration[this.computerPosition + 1] - 1;
//notifies the user if the computer went up/down a chute.
if (originalPos > this.computerPosition)
System.out.println("The computer went down a chute!");
else if (originalPos < this.computerPosition)
System.out.println("The computer went up a ladder!");
}
positions[this.computerPosition][1] = "C";
}
//book example page 399
//increases the capacity of the list by creating a larger array
//copies the existing array into the new one
private void increaseSize(){
int[] temp = new int[configuration.length * 2];
for (int x = 0; x < configuration.length; x++)
temp[x] = configuration[x];
configuration = temp;
}
}
========================================================================
//Game.java
import java.util.Random;
import java.io.*;
public class Game{
public static void main(String[] args) throws IOException {
Board boardGame = new Board("configurationfile.txt");
int pPosition;
int cPosition;
Random ranNumber = new Random();
//keeps playing until someone has one chutes and ladders
do{
//rolls the dice for the player
//and then sets the position on the board
int dice = ranNumber.nextInt(6) + 1;
pPosition = boardGame.getPlayerPosition() + dice;
System.out.println("You rolled a " + dice);
boardGame.setPlayerPosition(dice);
//rolls the dice for the computer
//and then sets the position on the board
dice = ranNumber.nextInt(6) + 1;
cPosition = boardGame.getComputerPosition() + dice;
System.out.println("The computer rolled a " + dice);
boardGame.setComputerPosition(dice);
boardGame.printBoard();
System.out.println();
} while (pPosition < boardGame.getBoardSize()
|| cPosition < boardGame.getBoardSize());
}
}
===============================================================================
//configurationfile.txt
5,12,
17,10,
14,19,
18,7,
//The configuration file contains numbers for chutes and ladders. For instance, if the player has landed on the number 5, then it will go to position 12 on the board (it has used a ladder). The same is true for if the player has hit a "chute".
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.