Question: I have this pokemon cad battle game written in java using stack and linked stack. I need to use a override tostring method in the

I have this pokemon cad battle game written in java using stack and linked stack. I need to use a override tostring method in the linkedstack class which I cannot work out, It works and is right but i need to implement it in correctly. here is my code and please change up the codes with better logic so it looks different than before.

public class LinkedStack implements Stack { private Node head; private Node temp; @Override public void push(T item) throws IllegalStateException { if (head == null) { // stack is empty // linked list is empty // special case head = new Node(item, null); } else { // there's at least one item here Node Head = new Node(item,head); Head.next = head; head = Head; } } @Override public T pop() { // this will return the top item but at the same time also delete it temp = head; head = head.next; return temp.data; } @Override public T peek() { // this will only give the current top item return head.data; } @Override public boolean isEmpty() { return false; } @Override public int length() { return 0; } @Override public String toString() { return head.data.toString(); // this is not implemented correctly^ } private class Node { T data; Node next; public Node(T data, Node next) { this.next = next; this.data = data; } } }

///////////////////////////////////////////////////////////////////

import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class PokemonCardGame { public static void main(String[] args) { List allCards = readCSV("pokemon.csv"); Collections.shuffle(allCards); Stack player = new LinkedStack<>(); Stack computer = new LinkedStack<>(); for (int i = 0; i < 10; i++) { player.push(allCards.get(i)); } for (int i = 10; i < 20; i++) { computer.push ( allCards.get ( i ) ); } for( int i = 0; i<10; i++){ int count = i +1; System.out.println(" Round:"+count); if(player.peek().getAttack() > computer.peek().getDefense()){ System.out.println(" Player wins"); System.out.println("Player :"+ player.peek().toString()); System.out.println("Computer :"+ computer.peek().toString()); player.pop(); computer.pop(); } else if (player.peek().getAttack() < computer.peek().getDefense()) { System.out.println("Computer wins "); System.out.println( " Player : " + player.peek().toString()); System.out.println("Computer :"+ computer.peek().toString()); player.pop(); computer.pop(); } else{ System.out.println(" TIE "); System.out.println("Player :" +player.peek().toString()); System.out.println("Computer :" + computer.peek().toString()); player.pop(); computer.pop(); } } ///////////////////// 
public class Pokemon { int Attack, Defense; String Data; public Pokemon( String data, int attack, int defense ){ this.Data = data; this.Attack = attack; this.Defense = defense; } public int getAttack() { return Attack; } public int getDefense() { return Defense; } @Override public String toString() { return "Pokemon{" + "attack=" + Attack + ", defense=" + Defense + ", data='" + Data + '\'' + '}'; } }
 } static List readCSV(String filename) { File file = new File(filename); List pokemonList = new ArrayList<>(); try { Scanner scan = new Scanner(file); scan.nextLine(); while (scan.hasNextLine()) { String[] info = scan.nextLine().split(","); // #,Name,Type 1,Type 2,Total,HP,Attack,Defense,Sp. Atk,Sp. Def,Speed,Generation,Legendary Pokemon poke = new Pokemon(info[1], Integer.parseInt(info[6]),Integer.parseInt(info[7])); pokemonList.add(poke); } } catch (FileNotFoundException e) { System.out.println("File not found!"); e.printStackTrace(); } return pokemonList; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!