Program 2: Linked List Battle

Due Date and Submission Requirements


The goal of this lab is:


Background and Directions

The Ultimate Showdown of Ultimate Destiny is a viral music video that depicts a massive battle between various fictional and non-fictional characters from pop culture. In this assignment, you will write a Java program that simulates a very similar battle between various characters from different places of pop culture. We will keep this PG, so nobody technically dies in our java program-- they are "eliminated".

You will use a circular doubly linked list to hold information about all the challengers. Each challenger has a name, and a class. The simulation will consists of multiple rounds, where two random challengers are selected from the linked list. The winner between two challengers is based on their class. There are three types of classes: Warriors, Mages, and Rogues. Think is this as a simple game of rock, paper, scissors.

Warrior beats mage. Mage beats Rogue. Rogue beats Warrior. If two challengers share the same class, the winner is determined in a coin flip (50/50). The loser is eliminated and removed from the linked list. The battle is over when only one challenger remains.

Part 1: Loading the Linked List

You will read in information about the challengers from a file. This file only contains 30 challengers, but your program should allow for any amount of challengers. You can assume the input file will always follow the same format. You can use the FileReader, Scanner, or any other method to read in from the file. You should use the comma (,) as a delimiter as you are reading from the file. The first thing that program will do is ask the user how many challengers they want included in the program. This will be a number N where 3 < N < 20. N challengers will be randomly selected and inserted into the circular doubly linked list (with no duplicates). The head node and tail node should be connected, and you need to keep track of the next node and the previous node. You need to keep track of the head node and tail node. You can either add nodes to the front, or the back of the linked list-- it is your decision.


Part 2: Printing the Linked List

You will need a method (printLinkedList) that will print out the linked list starting from the head to the tail (clockwise). You will need to call this method after each round

Part 3: Simulating one round

I recommend having a method, simulateOneRound(), that simulates one round of the battle. Each round, two random challengers are selected from the linked list to battle each other.

You will need to generate two random values: j and k. These are two random numbers you will generate from a range of [1, N] where N is the size of the current linked list. The first challenger is selected by starting from the head node, moving j spots clockwise and selects whatever challenger it lands on. The second challenger is selected by starting from the head node, moving k spots counter-clockwise and selects whatever challenger it lands on.

You must ensure that j and k do not land on the same nodes. If they do, you can move j or k forward/backwards one spot (this is the easiest way), or reroll j and k.

The winner is determined by the challengers' class. Whoever's class has the advantage should win, and the loser must be removed from the linked list. You should have a removeFromCircle() method that removes a node/challenger from the linked list. There can never be a tie. The battle is over when one challenger remains, and the program should then end. Between each round, you should ask the user if they would like to continue the simulation. If the user does not want to continue, the program should terminate.


Captain America won because he is a Warrior and Eleven from Stranger Things is a Mage. The loser (Eleven) must now be removed from the linked list, and pointers must be updated.

Starting Code and Requirements

You will develop your solution from scratch, however you should have at least a Node.java, LinkedListBattle.java, and a Program2Demo.java class (they do not need to be called those specefic names). We wrote quite a bit of code in class that might be helpful for program 2.


You must comment your code. At a minimum, each method should have a brief comment explaining what's going on. Basic getters and setters do not need a comment.


I have provided a sample input file for your program. I recommend using the provided input file, and then start testing with your own input file when you get it working.

Sample Output

When your program is run, it should look something like this. Obviously, the output will look slightly different each time due to the random values for j and k. Your program does not need to look exactly like this, but your program should clearly print out the initial list of challengers, then for each round it should clearly print out which characters were selected, and who won and who lost. After each round, you should print out all remaining challengers in the linked list.

Optional Hints

You may find that an ArrayList is very helpful for putting N elements into the linked lists with no duplicates.

Do not try to tackle the entire program in one go. First get information loaded into the linked list from the file, then print to linked list to confirm you did part 1 correctly.

I recommend having a method in your linked list class the simulates just one round. It generates j and k, selects the correct nodes, determines the winner, and removes the loser from the linked list. From your demo class, you can call this method until there is one node left in the linked list.



If you get stuck or have a bug in your code, remember that you a rubber duck that can help 😉

Grading (100 points)

Requirement Points
Your program reads in the list of challengers from an external file (participants.txt) 10
Challengers are loaded in and added into a circular linked list 20
The circular linked list is correctly printed out after each round 5
Your program correctly simulates one round. j and k are generated, and correctly select two challengers from the circular linked list 20
Winner is determined correctly and eliminated challengers are removed from the circular linked list 20
Program continues to run until 1 challenger remains, or if the user wants to exit the program 10
Your output clearly shows which challengers were picked, who won, and who gets eliminated 10
Your program has an adequate amount of comments 5






program 2 solution