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 character from this music video. 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 power level. The simulation will consists of multiple rounds, where two random challengers are selected from the linked list. The challenger with the higher power level will win, and the loser is eliminated and removed from the linked list. The battle is over when only one challenger remains (it will always be Godzilla).

Part 1: Loading the Linked List

You will read in information about the challengers from a file. This file only contains 8 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 BufferedReader, 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. Each line in the file represents a Node in the circular linked list. As you are reading in from the file, you will need to add nodes to the linked list. Remember that this is a circular doubly linked list, so the head node and tail node should be connected, and you need to keep track of the next node and the previousnode. 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' power level. Whoever has the higher power level wins, 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. If both challengers have the same power level, then it is a tie and nothing should happen. 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.


The loser 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. We wrote quite a bit of code in class that might be helpful for program 2.


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

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.

You can use Integer.parseInt(some_string) to convert a string to an integer.

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 10
Your program correctly simulates one round. j and k are generated, and correctly select two challengers from the circular linked list 20
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






program 2 solution