Question
Java Programming: Grid Battle Game (refine existing code) Map is 5x5 sectors. Two characters, Cyborg and Ninja, have places on the map. C for the
Java Programming: Grid Battle Game (refine existing code)
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.
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
I have been tormented by this project for the better part of 3 weeks now. With the help of a couple Chegg experts, I was able to finally get an understanding of all the moving parts here, and came up with some code that worked, and did everything asked.
However, upon submitting the code, my professor had some changes requested. This leads me to my current code below:
import java.util.*; import java.io.*; import java.security.*;
public class BattlefortheUniverse { public static int counter; //class-level variable, declared outside all methods //positions for C and N players public static int NPlayerRow; public static int NPlayerCol; public static int CPlayerRow; public static int CPlayerCol; public static String[][] planet; //form-level char variable, keeps track of who is current player, C or N public static char player; public static void main(String[] args) throws java.io.IOException { Scanner input= new Scanner(System.in); int PlayGame= 0; int WhoShotFirst=0;; //variable for determining who gets first move //Generates random number between 0-1 for determining who makes first move. Essentially heads or tails behind the scenes try { SecureRandom num=SecureRandom.getInstance("SHA1PRNG"); WhoShotFirst=num.nextInt(2); }//end Generate number catch (NoSuchAlgorithmException nsae) { System.out.println("Is SHA1PRNG spelled correctly?"); }//end catch if(WhoShotFirst==0){ player='C'; } else{ player='N'; } String[][]planet= new String[5][5]; //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 getBoard(planet); //load data into board counter= 0; loadData(planet); print(planet); //initialize players String[] players= {"C", "N"}; while(true) { System.out.println(player + " move. Enter 1 for RIGHT, 2 for LEFT, 3 for UP, or 4 for DOWN. "); int move= input.nextInt(); //check for valid move if(isValidMove(move, player)){ //update position of player, as selection is a valid move updatePos(player,move); //Check for winner if(isPlayerWinning()){ //print winning player System.out.println(player + " is the winner!!"); print(planet); break; } //update player's turn player= player=='N'? 'C':'N'; print(planet); } else{ System.out.println("Move is invalid! Try again."); }//end else }//end while statement }//end if PlayGame else { System.out.println("The Cyborgs and Ninjas will await your return. Goodbye!"); } }//end main method private static boolean isPlayerWinning() { //check whether or not player is winning //if position of C and N is equal, return true if(NPlayerCol==CPlayerCol && NPlayerRow==CPlayerRow){ return true; } else{ return false; } }//end isPlayerWinning method private static void updatePos(char player, int move) { //update position of player on map int r= 0; int c= 0; //update row and column depending on move made if(move==1) //RIGHT c++; //increment COLUMN else if(move==2) //LEFT c--; //decrease COLUMN else if(move==3)// UP r--; //decrement ROW else if(move==4)//DOWN r++; //increment ROW if(player=='C'){ planet[CPlayerRow][CPlayerCol]= " "; CPlayerCol += c; CPlayerRow += r; //update current position of C player planet[CPlayerRow][CPlayerCol]="C"; } else{ planet[NPlayerRow][NPlayerCol]= " "; NPlayerCol += c; NPlayerRow += r; //update current position of N player planet[NPlayerRow][NPlayerCol]="N"; } }//end updatePos method private static boolean isValidMove(int move, char player) { int r; int c; if(player=='C'){ r= CPlayerRow; c= CPlayerCol; } else{ r= NPlayerRow; c= NPlayerCol; } //Get next move if(move==1) //RIGHT c++; else if(move==2) //LEFT c--; else if(move==3) //UP r--; else if(move==4) //DOWN r++; //Check if next move is within dimensions of the board if(0<=r && r<5 && 0<= c && c<5){ return true; } else{ return false; } }//end isValidMove method public static void getBoard(String[][]planet) //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++) { planet[i][j]=" "; } } }//end getBoard method public static void print(String[][]m) //displays the board to player { System.out.println(" -----------"); for(int i=0; i Before my changes, the code worked. Now, however, the program crashes whenever the first player makes their first move. Here is the error I receive- java.lang.NullPointerException at BattlefortheUniverse.updatePos(BattlefortheUniverse.java:151) at BattlefortheUniverse.main(BattlefortheUniverse.java:83) My mind is simply broken from this. I am not sure why Java does not like the current code, and what needs to be done to get everything working properly. Any assistance would be so so SO appreciated!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started