GameState represents the state of the game "world." The GameState object tracks all of the moving entities like tanks * and shells, and provides the controller of the program (i.e. the GameDriver) access to whatever information it needs * to run the game. Essentially, GameState is the "data context" needed for the rest of the program. */

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

PART 1* I need help implementing some functions for a tank game coded in java. They are labeled under //TODO. Below are the first 3 files. I will post another question for the remaining 5 files labeled PART 2. Thank you!

/**
* GameState represents the state of the game "world." The GameState object tracks all of the moving entities like tanks
* and shells, and provides the controller of the program (i.e. the GameDriver) access to whatever information it needs
* to run the game. Essentially, GameState is the "data context" needed for the rest of the program.
*/
public class GameState {
public static final double TANK_X_LOWER_BOUND = 30.0;
public static final double TANK_X_UPPER_BOUND = RunGameView.SCREEN_DIMENSIONS.width - 100.0;
public static final double TANK_Y_LOWER_BOUND = 30.0;
public static final double TANK_Y_UPPER_BOUND = RunGameView.SCREEN_DIMENSIONS.height - 120.0;

public static final double SHELL_X_LOWER_BOUND = -10.0;
public static final double SHELL_X_UPPER_BOUND = RunGameView.SCREEN_DIMENSIONS.width;
public static final double SHELL_Y_LOWER_BOUND = -10.0;
public static final double SHELL_Y_UPPER_BOUND = RunGameView.SCREEN_DIMENSIONS.height;

public static final String PLAYER_TANK_ID = "player-tank";
public static final String AI_TANK_ID = "ai-tank";
// TODO: Feel free to add more tank IDs if you want to support multiple AI tanks! Just make sure they're unique.

// TODO: Implement.
// There's a lot of information the GameState will need to store to provide contextual information. Add whatever
// instance variables, constructors, and methods are needed.
}
/**
* Model class representing a shell that has been fired by a tank. A shell has a position and an angle, as well as a
* speed. Shells by default should be unable to turn and only move forward.
*/
// TODO: Notice that Shell has a lot in common with Tank. For full credit, you will need to find a way to share code
// between the two classes so that the logic for e.g. moveForward, etc. are not duplicated.
public class Shell {
private static final String SHELL_ID_PREFIX = "shell-";
private static final double MOVEMENT_SPEED = 4.0;

private static long uniqueId = 0L;

private final String id;
private double x;
private double y;
private double angle;

public Shell(double x, double y, double angle) {
this.id = getUniqueId();
this.x = x;
this.y = y;
this.angle = angle;
}

private String getUniqueId() {
return SHELL_ID_PREFIX + uniqueId++;
}
}
/**
* Model class representing a tank in the game. A tank has a position and an angle. It has a movement speed and a turn
* speed, both represented below as constants.
*/
// TODO: Notice that Tank has a lot in common with Shell. For full credit, you will need to find a way to share code
// between the two classes so that the logic for e.g. moveForward, etc. are not duplicated.
public class Tank {
private static final double MOVEMENT_SPEED = 2.0;
private static final double TURN_SPEED = Math.toRadians(3.0);

private final String id;
private double x;
private double y;
private double angle;

public Tank(String id, double x, double y, double angle) {
this.id = id;
this.x = x;
this.y = y;
this.angle = angle;
}

public String getId() {
return id;
}

public double getX() {
return x;
}

public double getY() {
return y;
}

public double getAngle() {
return angle;
}

// TODO: The methods below are provided so you don't have to do the math for movement. However, note that they are
// protected. You should not be calling these methods directly from outside the Tank class hierarchy. Instead,
// consider how to design your Tank class(es) so that a Tank can represent both a player-controlled tank and an AI
// controlled tank.

protected void moveForward() {
x += MOVEMENT_SPEED * Math.cos(angle);
y += MOVEMENT_SPEED * Math.sin(angle);
}

protected void moveBackward() {
x -= MOVEMENT_SPEED * Math.cos(angle);
y -= MOVEMENT_SPEED * Math.sin(angle);
}

protected void turnLeft() {
angle -= TURN_SPEED;
}

protected void turnRight() {
angle += TURN_SPEED;
}

// The following methods will be useful for determining where a shell should be spawned when it
// is created by this tank. It needs a slight offset so it appears from the front of the tank,
// even if the tank is rotated. The shell should have the same angle as the tank.

private double getShellX() {
return getX() + 30.0 * (Math.cos(getAngle()) + 0.5);
}

private double getShellY() {
return getY() + 30.0 * (Math.sin(getAngle()) + 0.5);
}
}

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Reference Types in Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education