In JAVA Your goal is to build a multi-player game where the goal is to be the la
ID: 3821535 • Letter: I
Question
In JAVA
Your goal is to build a multi-player game where the goal is to be the last player to survive. Each player starts with 100 health points, which are reduced every time a successful attack takes place. A player can report his condition, determined by the number of health points. The table below shows the condition as determined by health points.
100 excellent 90-99 very good
80-89 good 70-79 average
50-69 poor 30-49 terrible
1-29 critical 0 dead
When the program starts, it needs to know how many players will play and the name of each player. Each player gets a turn, and then continues round-robin. A player must be alive to have a turn. When a player’s turn comes up, he is asked the name of the player he wants to attack. If a player attempts to attack himself, he forfeits his turn and it goes to the next player in line. A player should not be allowed to attack another player who’s already dead. Once an opponent has been chosen, the program generates a random number between 0 and 20. If the value falls between 0 and 9, the attack is considered a “miss” and the opponent suffers no damage. If the value falls between 10 and 20, decrease the opponent’s health by that amount. Afer damage is inflicted, and before the next player’s turn, the program should display the condition of all the players, whether alive or not. The program should never show the number of health points lost in an attack or that remain for the player. The only way of gauging the impact of an attack is by looking at the condition of the player. Afer a player’s turn has taken place and the attack results have been made, the turn goes to the next player in line who is alive. If only one player remains who is still alive, the game is over and the last player wins.
This question has been asked before by someone else but the answer given to them was completely wrong.
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
HashMap<String, Integer> map=new HashMap<>(); // hash map to store score
Scanner sc=new Scanner(System.in);
int pcount=sc.nextInt();
ArrayList<String> mlist = new ArrayList<String>(pcount);
for (int i = 0; i < pcount; i++) { // scan all players name
String name=sc.next();
map.put(name, 100);
mlist.add(name);
}
int start = 0;
int removecount = 0;
while (removecount != pcount - 1) { // run till only one player remains
if ((int)map.get(mlist.get(start)) <= 0) {
// if a player score is <= 0 continue with next player from list
start += 1;
start = start % mlist.size();
continue;
}
boolean flag = true;
while (flag) {
System.out.println("Enter opponent name");
String // get opponent name
if (map.get(oname) != null) {
if ((int)map.get(oname) <= 0) { // if selected a dead player, get another player
System.out.println("Player is dead");
} else {
if (oname != mlist.get(start)) { // if opponent name is not same as player
Random rand = new Random();
int n = rand.nextInt(21);
if (n > 9) map.put(oname, (int)map.get(oname) - n);// if random val is more then 9, decrease opponent score
if ((int)map.get(oname) <= 0) removecount++;
}
flag = false;
}
} else {
System.out.println("Name doesnot exist");
}
}
for(Map.Entry m:map.entrySet()){
String str = "dead";
if ((int)m.getValue() > 0) str = "alive";
System.out.println(m.getKey()+" "+ str); // print status of player
}
start += 1;
start = start % mlist.size();
}
for(Map.Entry m:map.entrySet()) { // Search in map player who have +ve score
if ((int)m.getValue() > 0) {
System.out.println( "Winner is " + " " + m.getKey()); // print winner name
break;
}
}
}
}
Input:
2
a
b
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
b
a
a
b
a
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.