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

In this assignment you will simulate an online collectible card game where two p

ID: 3684267 • Letter: I

Question

In this assignment you will simulate an online collectible card game where two players duel against each other to see who has the strongest cards. In this particular card game, cards have several attributes: a Name (this can be a word or a phrase that contains spaces), Power (a positive integer), and Health (a positive integer).

Your program will read in data from 2 text files: 1 for each player. Each file contains information aboutthat player and the cards they will be using for this duel. The user of the program will enter thefilenames from a prompt from your program.
The first line of each file will contain the player’s name. The second line will contain an integer n whichrepresents the number of cards that follow. Each subsequent line are the card attributes ordered in thisformat: Name, Power, and Health. It can be assumed that each player has the same number of cards ineach file.

After reading in both files of player information into 2 dynamic arrays of type Card, prompt the user ifthey would like the duel to begin.
Duels occur in the following format: Duels happen between opposing cards in each player’s deck. If each player’s deck has 5 cards, there will be 5 duels. Whichever player wins the most duels, wins the match. If both players win the same amount ofduels, it is a tie. To score a duel, the Power from player A’s card is subtracted from the Health of player B’s cardand vice versa. If a card in the duel is at or below 0 health, that card is defeated. If both cards are defeated, theduel is a draw.

Your program must read in 2 files—the names of each file will be determined by the user Each file will contain the same number of cards for each player The name of players and cards may contain spaces The Power and Health of each card will always be positive integers In addition to the usual getter/setter functions, you will need to overload the == operator tocompare two Card objects for equality and the << operator to display a Card object on thescreen.  You will also need a member function that will compete two Card objects and returnthe address of the winner.  In case of a tie NULL is returned.  Here is the function header for

duel:   Card* duel(Card&);

Explanation / Answer

Comments and output attached

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

/**
*
* @author prmsh
*/
public class cardPlayer {
String name;
int totalCards;
ArrayList<Card> cards;
  
//constructor

public cardPlayer(String name, int totalCards, ArrayList<Card> cards) {
this.name = name;
this.totalCards = totalCards;
cards = cards;
}
  
//getter setter methods

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getTotalCards() {
return totalCards;
}

public void setTotalCards(int totalCards) {
this.totalCards = totalCards;
}

public ArrayList<Card> getCards() {
return cards;
}

public void setCards(ArrayList<Card> cards) {
this.cards = cards;
}
  
  
  
public static cardPlayer readFile(String fileName) {
//reading file
File file = new File("cards.txt");
try {
Scanner sc = new Scanner(file);
//variabled declared
int temp = 0;
String name = "";
int totalCards= 0;
ArrayList<Card> cards = new ArrayList<Card>();
while (sc.hasNextLine()) {
if(temp == 0){ //reading name
name = sc.nextLine();
temp ++;
}
else if(temp == 1){//reading totalcards
totalCards = sc.nextInt();
temp++;
}
else{//reading cards objects
String[] dataLine = sc.nextLine().split(" ");
Card obj = new Card(dataLine[0], Integer.parseInt(dataLine[1]),Integer.parseInt(dataLine[2]));
cards.add(obj);
temp++;
}
}
//creating card object
cardPlayer obj1= new cardPlayer(name,totalCards,cards);
sc.close();
return obj1; //returning object
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}

public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
//reading file names from players
System.out.println("Player1: enter file name");
String fileName1 = sc.nextLine();

System.out.println("Player2: enter file name");
String fileName2 = sc.nextLine();

//calling functions
cardPlayer player1 = readFile(fileName1);
cardPlayer player2 = readFile(fileName2);

//now calculating healths and start duels
System.out.println("Do you like duels to begin: yes or no");
String option = sc.nextLine();

if(option.equals("yes")){
//printing player details
System.out.println(player1);
System.out.println(player2);
int player1Wins = 0, player2Wins = 0;
//start duels
ArrayList<Card> cards1 = player1.getCards();
ArrayList<Card> cards2 = player2.getCards();
for(int i=0;i<player1.getTotalCards();i++){
//check health
if(cards1.get(i).getHealth() > cards2.get(i).getHealth()){
player1Wins++;
}
else{
player2Wins++;
}
}

//printing final results
if(player1Wins > player2Wins){
System.out.println(player2.getName()+" defeated");
}
else if(player1Wins < player2Wins){
System.out.println(player1.getName()+" defeated");
}
else{
System.out.println(player1.getName()+" and " + player2.getName() +" both are alive.");
}
}
System.out.println("Bye.. Thank you");
}
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

Card.java

public class Card {
//class variabels declared
String name;
int power;
int health;
  
//constructor

public Card(String name, int power, int health) {
this.name = name;
this.power = power;
this.health = health;
}
  
//getter setter methods

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getPower() {
return power;
}

public void setPower(int power) {
this.power = power;
}

public int getHealth() {
return health;
}

public void setHealth(int health) {
this.health = health;
}
  
//tostring method

@Override
public String toString() {
return "Card{" + "name=" + name + ", power=" + power + ", health=" + health + '}';
}
  
}