Question: Java programming: battle game (movement portion) Map is 5x5 sectors. Two characters, Cyborg and Ninja, have places on the map. C for the Cyborg and
Java programming: battle game (movement portion)
Map is 5x5 sectors. Two characters, Cyborg and Ninja, have places on the map. C for the Cyborg and N for the Ninja.
| N | ||||
| C | ||||
Rules: Each player may move only one sector per turn. He may move up, down, left, or right. Can NOT move diagonally. To win the battle, one player must land on the other players sector. The player landing on the others sector wins the battle.
Prompt the user to enter the row and column of the move to which the Cyborg/Ninja will move. If that sector is occupied by the other player, display a message that the first player wins. At this point, the battle ends.
Your program must be able to load positions on map from a .txt file. If a user wishes to quit before the battle ends, your program must also be able to store the planet in an external text file.
The example for how the external text file should be set up is as follows:
o o o o o o C o o o o o o o o N o o o o o o o o o
With the help of a Chegg expert, I was able to work out loading from the .txt file and displaying the board properly. In addition to this, I have initiated the game with a story and rules. Here is what I have so far (apologies in advance for any formatting issues):
import java.util.*; import java.io.*;
public class Main { public static int counter; //class-level variable, declared outside all methods public static void main(String[] args) throws java.io.IOException { Scanner input= new Scanner(System.in); int PlayGame= 0; //Introduction to game. Game title, story, rules displayed to user, ask for input to start game System.out.println("THE BATTLE FOR THE UNIVERSE"); System.out.println("In the year 3076, a battle between man and machine rages on."); System.out.println("Who will perish, and who will arise victorious?"); System.out.println("On the remains of planet Earth, the Cyborgs (C) battle the Ninjas (N)."); System.out.println(" "); System.out.println(" "); System.out.println("RULES: Each player can move one sector per turn. He may move up, down, left, or right."); System.out.println("To win the battle, one player must land on the other player's sector."); System.out.println("The player landing on the other's sector wins the battle."); System.out.println(" "); System.out.println(" "); System.out.println("Enter 1 to start game, any other number to quit: "); PlayGame= input.nextInt(); if(PlayGame==1) { //Create board/map String[][] planet= getBoard(); //load data into board counter= 0; loadData(planet); print(planet); //initialize players String[] players= {"E", "W"}; //form-level char variable, keeps track of who is current player, C or N char player= 'C'; while(true) { System.out.println(player + " move. Enter 1 for RIGHT, 2 for LEFT, 3 for UP, or 4 for DOWN. :"); } } else { System.exit(1); } }//end main method public static String[][] getBoard() //returns 5x5 array with blank spaces { String[][]m= new String[5][5]; for(int i=0; i<5; i++) { for(int j=0; j<5; j++) { m[i][j]=" "; } } return m; }//end getBoard method public static void print(String[][]m) //displays the board to player { System.out.println(" -----------"); for(int i=0; i So now that the basis of the game is laid out, I am having an extremely hard time figuring out the movement portion. I believe it makes sense to use some sort of while(true) loop, and then prompt the player for what move they want to make, and then once that is complete, initiate the movement, then flip to the other player. For the life of me, though, I am not sure how to translate the movement choice by the user into the 'tokens' (C and N) moving on the board/array. I was thinking you'd have 4 int variables: NplayerCol, NplayerRow, CplayerCol, CplayerRow, but even then, I'm not sure where to go from there. I'm almost certain if I see the code I will understand, and will feel stupid for ever being confused about this, but right now I'm just stumped. Any assistance would be greatly appreciated!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
