I am writing a program that pits two tanks against each other. I have written all the methods except the fire that would end the game. So far
public class Tank {
Tank() {
int xPos, yPos;
char direction;
boolean loaded = 0;
}
public void move(Tank currentPlayer) {
if (yPos<=4 && yPos>=0) {
if (xPos<=4 && xPos>=0) {
if (currentPlayer.direction == 'u') {
currentPlayer.yPos--;
}
if (currentPlayer.direction == 'd') {
currentPlayer.yPos++;
}
if (currentPlayer.direction == 'l') {
currentPlayer.xPos--;
}
if (currentPlayer.direction == 'r') {
currentPlayer.xPos++;
}
}
}
}
public void turn(boolean bool, Tank currentPlayer) {
if (currentPlayer.direction == 'u') {
currentPlayer.direction ='r';
}
if (currentPlayer.direction == 'd') {
currentPlayer.direction = 'l';
}
if (currentPlayer.direction == 'l') {
currentPlayer.direction ='u';
}
if (currentPlayer.direction == 'r') {
currentPlayer.direction ='d';
}
}
public void load(Tank currentPlayer) {
currentPlayer.loaded=true;
}
public int fire(Tank currentPlayer, Tank jim) {
// ???
}
}
How do I go about finishing my program? Any help would be appreciated.
You need a getter for the x-position, the y-position, and the current orientation of the tank. For example:
By calling these methods, you should get the x position, y position, and orientation of the tank. Then to check a hit. You know what direction you’re facing. So if you’re facing up, see if the other tank is on the same x as you, and that their y is above yours.
Then in the case of a hit, in other words if the conditions are met. The game should end with the currentPlayer victorious.