import java.util.Scanner;
public class SinglyLinkedList<E> {
public void main(String[]args) {
// output message
System.out.println("Please input the shuffled deck in order starting from the top: ");
// get input from user
Scanner cards = new Scanner(System.in);
SinglyLinkedList player1 = new SinglyLinkedList();
SinglyLinkedList player2 = new SinglyLinkedList();
SinglyLinkedList temp = new SinglyLinkedList();
// add first element to the linked lists
player1.addFirst(cards.nextLine());
player2.addFirst(cards.nextLine());
// build initial hands
for (int k = 1; k < 26; k++) {
player1.addLast(cards.nextLine());
player2.addLast(cards.nextLine());
}
//create an array with strings to compare to the elements
String[] cardValue = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
int counter = 0;
int card1 = 0;
int card2 = 0;
while (counter <= 10000){
counter ++;
if (player1.first() == null) {
System.out.println("Player 2 wins after " + counter + " rounds.");
break;
}
if (player2.first() == null) {
System.out.println("Player 1 wins after " + counter + " rounds.");
break;
}
for (int n = 0; n < 13; n++) {
if(player1.first() == cardValue[n])
card1 = n;
if(player2.first() == cardValue[n])
card2 = n;
}
if (card1 < card2) {
// player2 wins
player2.addLast(player1.first());
player2.addLast(player2.first());
player1.removeFirst();
player2.removeFirst();
} else if (card2 < card1) {
// player1 wins
player1.addLast(player1.first());
player1.addLast(player2.first());
player1.removeFirst();
player2.removeFirst();
} else if (card1 == card2) {
// put the equal card in the temp list and remove from the players hand
temp.addLast(card1);
temp.addLast(card2);
player1.removeFirst();
player2.removeFirst();
// Check to see if any hands are null
if (player1.first() == null){
System.out.println("Player 2 wins after " + counter + " rounds.");
break;
}
else if (player2.first() == null){
System.out.println("Player 1 wins after " + counter + " rounds.");
break;
}
else {
// put the face down cards in the temp list and remove from the players hands
temp.addLast(player1.first());
temp.addLast(player2.first());
player1.removeFirst();
player2.removeFirst();
}
// assign values to the cards
for (int n = 0; n < 13; n++) {
if(player1.first().equals(cardValue[n]))
card1 = n;
if(player2.first().equals(cardValue[n]))
card2 = n;
}
if (card1 < card2) {
// player2 wins
temp.addLast(player1.first());
temp.addLast(player2.first());
player1.removeFirst();
player2.removeFirst();
while (temp.first() != null)
player2.addLast(temp.first());
temp.removeFirst();
}
} else if (card2 < card1) {
// player1 wins
temp.addLast(player1.first());
temp.addLast(player2.first());
player1.removeFirst();
player2.removeFirst();
while (temp.first() != null){
player1.addLast(temp.first());
temp.removeFirst();
}
}
if (counter == 10000) {
System.out.println("There was a draw after 10,000 rounds.");
}
}
}
static class Node<E> {
private E element;
private Node<E> nextNode;
public Node(E e, Node<E> n){ //define current node
element = e; //define element stored in node
nextNode = n; //define next node in list
}
public E getElement(){ //return the element stored in the current node
return element;
}
public Node<E> getNextNode() { //return the reference location of the next node
return nextNode;
}
public void setNextNode(Node<E> n){ //set the new reference location of the next node to input "n"
nextNode = n;
}
}
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
public SinglyLinkedList() {}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public E first() {
if (isEmpty())
return null;
return head.getElement();
}
public E last() {
if (isEmpty())
return null;
return tail.getElement();
}
public void addFirst(E e) {
head = new Node<>(e, head);
if (size == 0)
tail = head;
size ++;
}
public void addLast(E e) {
Node<E> newest = new Node<>(e, null);
if (isEmpty())
head = newest;
else
tail.setNextNode(newest);
tail = newest;
size ++;
}
public E removeFirst() {
if (isEmpty())
return null;
E answer = head.getElement();
head = head.getNextNode();
size--;
if (size == 0)
tail = null;
return answer;
}
}
That's my actual code... I am pretty sure an issue is resulting when I try and compare them but the program isn't outputting anything... It just says it is terminating