Question: Please write code that will look like the following picture. Use ArrayList and the following Galvinball class that I will paste below. Players will move
Please write code that will look like the following picture. Use ArrayList and the following Galvinball class that I will paste below. Players will move with ASDF keys and should be playing on a 10x10 space stored as an ArrayList. If you need to know anything else just let me know. Any help will be much appreciated as I'm struggling to do this myself.
Here's some more details.
Movement
Players will move with the ASDF keys. A will move the player up one space, S will move the player up one space, D will move the player down one space, F will move the user to the right one space. If the player tries to move into a space that is occupied by another player or off of the game board, then their move fails and their turn ends.
Scoring
The player will get an updated score after each turn, even if their move fails. If the player lands on an open space or stays in the same location, their score will be updated by adding a random value between -1 and +2 to their current score. If the player has landed on the goal location, their score is increased by 3, and the goal location is move randomly to a new open space.
End of Game
Before each players turn, the game has a 5% chance of ending. When the game ends, the player with the highest score is declared the winner and the program exits. If both players have the same score, then a random player is selected as the winner.
Field:
The Calvinball field is a 10x10 space. At the beginning of the game, the goal marker, beginning location of player 1 and beginning location of player 2 are all assigned to different spaces. Players are not able to move beyond the edges of the game or into a spot occupied by another player. In the provided CalvinBall.java file, the 10x10 field is stored as an ArrayList

import java.util.ArrayList;
import java.util.Random;
public class CalvinballGame {
// _-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_
// CS 1323: I recommend you implement this method
/**
* This is the method you will complete to allow you to return the field and
* scores of this CalvinballGame in a nice format like below
*
* __________
* __________
* __________
* ________X_
* __________
* ________1_
* __2_______
* __________
* __________
* __________
* Player 1 Score: 2
* Player 2 Score: 0
*
* @return the formatted String that can then be output to the console.
*/
public String toString() {
StringBuffer buff = new StringBuffer();
return buff.toString();
}
// _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_
private ArrayList field;
private Random rand;
private int[] scores;
public static String PLAYER_ONE_TOKEN = "1";
public static String PLAYER_TWO_TOKEN = "2";
public static String GOAL_TOKEN = "X";
public static String OPEN_TOKEN = "_";
CalvinballGame() {
field = new ArrayList();
rand = new Random();
for (int i = 0; i
field.add(OPEN_TOKEN);
}
int goalLocation = rand.nextInt(100);
field.set(goalLocation, GOAL_TOKEN);
int playerOneLocation = rand.nextInt(100);
while (goalLocation == playerOneLocation) {
playerOneLocation = rand.nextInt(100);
}
int playerTwoLocation = rand.nextInt(100);
while (goalLocation == playerTwoLocation || playerOneLocation == playerTwoLocation) {
playerTwoLocation = rand.nextInt(100);
}
field.set(goalLocation, GOAL_TOKEN);
field.set(playerOneLocation, PLAYER_ONE_TOKEN);
field.set(playerTwoLocation, PLAYER_TWO_TOKEN);
scores = new int[2];
}
public boolean isOver() {
int value = rand.nextInt(100);
if (value
return true;
} else {
return false;
}
}
public int getNewLocation(int player, String direction) {
int currentLocation = field.indexOf("" + player);
int newLocation = currentLocation;
if (direction.equalsIgnoreCase("a")) {
if (currentLocation % 10 != 0) {
newLocation = currentLocation - 1;
}
} else if (direction.equalsIgnoreCase("s")) {
if (currentLocation >= 10) {
newLocation = currentLocation - 10;
}
} else if (direction.equalsIgnoreCase("d")) {
if (currentLocation
newLocation = currentLocation + 10;
}
} else if (direction.equalsIgnoreCase("f")) {
if (currentLocation % 10 != 9) {
newLocation = currentLocation + 1;
}
}
if (!field.get(newLocation).equals(OPEN_TOKEN) && !field.get(newLocation).equals(GOAL_TOKEN)) {
newLocation = currentLocation;
}
return newLocation;
}
public void updateScoreAndMove(int player, String direction) {
int currentLocation = field.indexOf("" + player);
int newLocation = getNewLocation(player, direction);
String token = PLAYER_ONE_TOKEN;
if (player == 2) {
token = PLAYER_TWO_TOKEN;
}
field.set(currentLocation, OPEN_TOKEN);
if (field.get(newLocation).equals(GOAL_TOKEN)) {
field.set(newLocation, token);
int newGoalLocation = rand.nextInt(100);
String existingToken = field.get(newGoalLocation);
while (!existingToken.equals(OPEN_TOKEN)) {
newGoalLocation = rand.nextInt(100);
existingToken = field.get(newGoalLocation);
}
field.set(newGoalLocation, GOAL_TOKEN);
scores[player - 1] += 3;
} else {
field.set(newLocation, token);
int scoreIncrease = rand.nextInt(4) - 1;
scores[player - 1] += scoreIncrease;
}
}
public int getWinner() {
if (scores[0] > scores[1]) {
return 1;
} else if (scores[1] > scores[0]) {
return 2;
} else if (rand.nextInt(2) == 0) {
return 1;
} else {
return 2;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
