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

this is the assignment PLEASE BE SURE TO FOLLOW THE INSTRUCTIONS AND TO DESCRIBE

ID: 3582252 • Letter: T

Question

this is the assignment PLEASE BE SURE TO FOLLOW THE INSTRUCTIONS AND TO DESCRIBE WHAT HAVE YOU DONE THROUGHT THE CODE .

You are to make a fairly simple game.

The game will have two players (two humans, no need to try and make a computer player.The game will be a form of combat between the players.Each player should have 3 stats.HP: Their health points, when this reaches 0, they lose.Attack: How much "damage" each of their attacks do when they hit.Defense: The amount of areas they can defend against in an attack.

Attack + Defense should total 10, while the minimum for each stat should be1.

Each player should also be able to enter their name (single word).

Each round of the game will consist of each player acting once as the attacker, and once as the defender.Rounds will be continued until one of (or both of) the players has 0 hp remaining.

Combat works as follows:

The attack choose a number between 1 and 10 inclusive.

The defender then picks spots to defend, again from 1 to 10 inclusive.

So if the player has a defense of 3, they may choose to defend 3 numbers of 1-10.

The attack is defended if the defender picks one of these values.

In this event no damage is done to the defender.

If the attack is a hit (the attacker picked a number that the defender did not)

then the defender loses hp equal to the attack value of the attacker.

For this program you should assume you're working on a system with limited memory (so don't want memory)

You are only allowed to use a single array in your program, so use it wisely (odds are you'll want to use it for temporary storage of the player names).

You should also use functions to set up player info (name, attack and defense levels)

Functions should also be used to resolve combat.

Do not allow invalid stat entry (enforce the sum of attack + defense is 10, and neither stat can be 0)

Before each round you should print the player name and current hp level.

If you want to clear the screen to hide the other players choice, you can use:

Windows: system("cls");

Linux (and probably Mac): system("clear");

Explanation / Answer

Player.java:

package com.chegg;

public class Player {
   private String name;
   private int hp;
  
   public Player(int healthPoints) {
       this.hp = healthPoints;
   }
  
   public void setName(String name) {
       this.name = name;
   }
  
   public String getName() {
       return this.name;
   }
  
   public void setHP(int h) {
       hp += h;
   }
  
   public int getHP() {
       return hp;
   }
}


Combat.java:

package com.chegg;

import java.util.Random;
import java.util.Scanner;

public class Combat {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter 1st player name:");
       String s1 = sc.next();
       System.out.print("Enter 2nd player name:");
       String s2 = sc.next();
       Player p1 = new Player(3);
       Player p2 = new Player(3);
       p1.setName(s1);
       p2.setName(s2);
       int count = 1;
       while(p1.getHP()>0 && p2.getHP()>0){
          
           System.out.print("Player1:");
           int num = sc.nextInt();
           if(!isDefended(num)){
               p1.setHP(num);
               p2.setHP(-num);
           }
           System.out.print("Player2:");
           num = sc.nextInt();
           if(!isDefended(num)){
               p2.setHP(num);
               p1.setHP(-num);
           }
           System.out.println("Round"+count+":");
           System.out.println("------");
           System.out.println("Name   HP");
           System.out.println("---------");
           System.out.println(p1.getName()+" "+p1.getHP());
           System.out.println(p2.getName()+" "+p2.getHP());
           System.out.println();
           count++;
       }
      
       System.out.println();
       int hp1 = p1.getHP();
       int hp2 = p2.getHP();
       if(hp1<=0)
           System.out.println("Winner : "+p2.getName());
       else if(hp2 <= 0)
           System.out.println("Winner : "+p1.getName());
       else
           System.out.println("Match Draw...");
      
       sc.close();
   }

   private static boolean isDefended(int num) {
       boolean flag = false;
       Random rand = new Random();
       for(int i=1;i<=num;i++){
           int randomNum = rand.nextInt((10 - 1) + 1) + 1;
           if( randomNum== num){
               flag = true;
               break;
           }
       }
       return flag;
   }
}