Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java programming: battle game (movement portion) Map is 5x5 sectors. Two charact

ID: 3708508 • Letter: J

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 N for the Ninja.

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 player’s sector.   The player landing on the other’s 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<m.length; i++)
{
for(int j=0; j<m[i].length; j++)
{
System.out.print("|" +m[i][j]);
}
System.out.println("| -----------");
}
}//end print method
  
public static void loadData(String[][] plnt) throws java.io.IOException
{
String fileName= "C:\Users\apauken\Desktop\SCHOOL\galaxy.txt";
Scanner input= new Scanner(new File(fileName));
String letter= " ";
  
for(int row=0; row<plnt.length; row++)
{
for(int col=0; col<5; col++)
{
letter= input.next();
if(letter.equals("o")) //check for string ver of o
plnt[row][col]=" ";
else
plnt[row][col]= Character.toString(letter.charAt(0)); //convert C or N into char, load into array
}

  
}
input.close();
}
  
  

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!

     N C

Explanation / Answer

I have build the logic based on your comments so that you don't find it difficult to understand. Please check for comments in the code for better understanding. I have made a change to planet variable by making it a class member instead of having a local variable.

PLEASE READ THE CODE CAREFULLY

Code

package cn;

import java.util.*;
import java.io.*;

public class Main
{
   public static int counter; //class-level variable, declared outside all methods

   // positions for C and N Player.
   public static int NPlayerRow, NPlayerCol, CPlayerRow, CPlayerCol;
   public static String[][] planet;
  
   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)
       {
           // Changed planet from local variable to class member.
           // This way we don't need to pass planet every time to function.
           //Create board/map
           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. :");
               int move = input.nextInt();
              
               // First check if the move is valid
               if(isValidMove(move, player)) {
                   // Update the position of player as the move is valid.
                   updatePos(player,move);
                  
                   // Check if anybody is winning.
                   if(isPlayerWinning()){
                       // Print the winning player.
                       System.out.println(player + " Won");
                       print(planet);
                       break;
                   }
                   // Update the players turn.
                   player = player == 'N'? 'C':'N';
                   print(planet);
               }
               else {
                   System.out.println("Move is invalid!!! Try Again");
               }

           }
       }
       else
       {
           System.exit(1);
       }

   }//end main method

   // check if player is winning or not.
   private static boolean isPlayerWinning() {
       // if position of C and N becomes equal then return true.
       if(NPlayerCol == CPlayerCol && NPlayerRow == CPlayerRow)
           return true;

       return false;
   }

   // update the position of the player.
   private static void updatePos(char player, int move) {
       int r = 0, c = 0;
       // update row and column according to move.
       if(move == 1) // RIGHT
           c++; // increment column
       else if(move == 2) // LEFT
           c--; // decrement column
       else if(move == 3) // UP
           r--; // decrement row
       else if(move == 4) // DOWN
           r++; // increment row
      
       // Since the position is updated.
       if(player == 'C') {
//           Mark the previous position of player blank
           planet[CPlayerRow][CPlayerCol] = " ";
           CPlayerCol += c;
           CPlayerRow += r;
           // Update the current position of player.
           planet[CPlayerRow][CPlayerCol] = "C";
       }
       else {
//           Mark the previous position of player blank
           planet[NPlayerRow][NPlayerCol] = " ";
           NPlayerCol += c;
           NPlayerRow += r;
//           Update the current position of player.
           planet[NPlayerRow][NPlayerCol] = "N";
       }
   }

   // Check if the next move is valid or not.
   private static boolean isValidMove(int move, char player) {
       int r,c;
       if(player == 'C') {
           r = CPlayerRow;
           c = CPlayerCol;
       }
       else{
           r = NPlayerRow;
           c = NPlayerCol;
       }
       //Get the 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 the next move is within the board.
       if(0 <= r && r < 5 && 0 <= c && c < 5)
           // If within return true;
           return true;
      
       // Move not within board return false.
       return false;
   }

   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<m.length; i++)
       {
           for(int j=0; j<m[i].length; j++)
           {
               System.out.print("|" +m[i][j]);
           }
           System.out.println("| -----------");
       }
   }//end print method

   public static void loadData(String[][] plnt) throws java.io.IOException
   {
//       String fileName= "C:\Users\apauken\Desktop\SCHOOL\galaxy.txt";
       String fileName="galaxy.txt";
       Scanner input= new Scanner(new File(fileName));
       String letter= " ";

       for(int row=0; row<plnt.length; row++)
       {
           for(int col=0; col<5; col++)
           {
               letter= input.next();
               if(letter.equals("o")) //check for string ver of o
                   plnt[row][col]=" ";
               else {
                   plnt[row][col]= Character.toString(letter.charAt(0)); //convert C or N into char, load into array
                   // Storing positions of C and N
                   if(letter.charAt(0)=='C'){
                       CPlayerRow = row;
                       CPlayerCol = col;
                   }
                   else if(letter.charAt(0)=='N'){
                       NPlayerRow = row;
                       NPlayerCol = col;
                   }
               }
           }


       }
       input.close();
   }
}

Sample output:

THE BATTLE FOR THE UNIVERSE
In the year 3076, a battle between man and machine rages on.
Who will perish, and who will arise victorious?
On the remains of planet Earth, the Cyborgs (C) battle the Ninjas (N).
   
   
RULES: Each player can move one sector per turn. He may move up, down, left, or right.
To win the battle, one player must land on the other player's sector.
The player landing on the other's sector wins the battle.
  
  
Enter 1 to start game, any other number to quit:
1

-----------
| | | | | |
-----------
| |C| | | |
-----------
| | | | | |
-----------
|N| | | | |
-----------
| | | | | |
-----------
C move. Enter 1 for RIGHT, 2 for LEFT, 3 for UP, or 4 for DOWN. :
3

-----------
| |C| | | |
-----------
| | | | | |
-----------
| | | | | |
-----------
|N| | | | |
-----------
| | | | | |
-----------
N move. Enter 1 for RIGHT, 2 for LEFT, 3 for UP, or 4 for DOWN. :
2
Move is invalid!!! Try Again
N move. Enter 1 for RIGHT, 2 for LEFT, 3 for UP, or 4 for DOWN. :
3

-----------
| |C| | | |
-----------
| | | | | |
-----------
|N| | | | |
-----------
| | | | | |
-----------
| | | | | |
-----------
C move. Enter 1 for RIGHT, 2 for LEFT, 3 for UP, or 4 for DOWN. :
4

-----------
| | | | | |
-----------
| |C| | | |
-----------
|N| | | | |
-----------
| | | | | |
-----------
| | | | | |
-----------
N move. Enter 1 for RIGHT, 2 for LEFT, 3 for UP, or 4 for DOWN. :
3

-----------
| | | | | |
-----------
|N|C| | | |
-----------
| | | | | |
-----------
| | | | | |
-----------
| | | | | |
-----------
C move. Enter 1 for RIGHT, 2 for LEFT, 3 for UP, or 4 for DOWN. :
2
C Won

-----------
| | | | | |
-----------
|C| | | | |
-----------
| | | | | |
-----------
| | | | | |
-----------
| | | | | |
-----------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote