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

Level 4: Level 1 Battle Game Output must state You will open up a file that has

ID: 3875522 • Letter: L

Question

Level 4: Level 1 Battle Game Output must state You will open up a file that has the folowing format 1. 2. 3. Line i: Line 2: Line 3: A title line and underline The number of battles total. The number of EACH battle The strength of each player The winner ef each battle Each battle is separated by a blank line Final line of output MUST state end of battles 5. 6. 7. Output Example for example File Line n Battle Game There will be 2 lines for each battle played There can be up to 100 battles Each player has an army that consists of one of S characters and each has the following strength Total of 3 battles Battle 1, Player :Strength of 19 Battle 1, Playe2: Strength of 6 Battle 1 Winner Player i blank line here K-Knight Fe Foot soidier A - Artiliery T " Tanker S- Saiper Strength2 Strength Strength-4 Strength 5 Strength-3 Battle 2, PlaeStrength of 17 Battle 2, Playe2:Strength of 17 Battle 2 WinnerTIE BATTLE Battle 3. Player Strength of 19 Battle 3, Playe2 Strength of 30 Battle 3 Winner : Player 2 Each member of the army is separated with a space, number of army forces-6 characters ALWAYS (to make End of Battles things easier Read in each pair of lines for a battle, and then determine who is the winner, lplayer 1 or 2) or is it a tie Example Input File OUTPUT MUST BE EXACT! Indicates there are 3 battles/3 pairs if data lines KKASST (make using basic java, nothing advanced, no methods) AAASFF FATTFF ATSFKA TIILI NOTE: There will ALWAYS be 6 "characters in an army ALWAYS (to simplify the solut

Explanation / Answer


Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please make sure you place your input file in the correct folder. If using eclipse, the file should be in the project directly and NOT INSIDE src folder.


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class BattleGame {
private static int getStrength(char a)
{
if(a == 'K')
return 2;
else if(a == 'F')
return 1;
else if(a == 'A')
return 4;
else if(a == 'T')
return 5;
else if(a == 'S')
return 3;
else
return 0;
}
private static void processBattle(int battleNum, Scanner infile)
{
int player1Strength = 0;
int player2Strength = 0;
String p1 = infile.nextLine().replaceAll(" ", "");
String p2 = infile.nextLine().replaceAll(" ", "");
for(int i = 0; i < 6; i++)
{
player1Strength += getStrength(p1.charAt(i));
player2Strength += getStrength(p2.charAt(i));
}
System.out.println("Battle " + battleNum + ", Player 1 : Strength of " + player1Strength);
System.out.println("Battle " + battleNum + ", Player 2 : Strength of " + player2Strength);
System.out.print("Battle " + battleNum + " Winner : ");
if(player1Strength > player2Strength)
System.out.println("Player 1");
else if(player1Strength < player2Strength)
System.out.println("Player 2");
else
System.out.println("TIE BATTLE");
System.out.println();

}
public static void main(String[] args) {
String filename;
Scanner keyBoard = new Scanner(System.in);
System.out.println("Enter input filename: ");
filename = keyBoard.next();
//open the file and process
try {
Scanner infile = new Scanner(new File(filename));
int numBattles;
System.out.println("Battle Game");
System.out.println("-----------");
numBattles = infile.nextInt();
System.out.println("Total of " + numBattles + " battles");
infile.nextLine(); //get rid of newline
for(int i = 1; i <= numBattles; i++)
processBattle(i, infile);
System.out.print("End of Battles");
infile.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}

input file: battle.txt
===================
3
KKASST
FFFFFF
AAASFF
FATTFF
ATSFKA
TTTTTT
output
=====
Enter input filename:
battle.txt
Battle Game
-----------
Total of 3 battles
Battle 1, Player 1 : Strength of 19
Battle 1, Player 2 : Strength of 6
Battle 1 Winner : Player 1
Battle 2, Player 1 : Strength of 17
Battle 2, Player 2 : Strength of 17
Battle 2 Winner : TIE BATTLE
Battle 3, Player 1 : Strength of 19
Battle 3, Player 2 : Strength of 30
Battle 3 Winner : Player 2
End of Battles

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