Program is a Game, user enters name and a Random Score is produced. Im having trouble coming up with a idea on how to produce a random score.
I have implemented the program so the user enters his name and inputs his own score, this is not quite correct though.
import java.util.*;
import java.lang.*;
import java.io.*;
class Game
{
static class Node
{
public int score;
public String name;
public Node next;
public Node(int score, String name)
{
this.score = score;
this.name = name;
this.next = null;
}
}
static class LinkedList
{
public Node head;
public LinkedList()
{
this.head = null;
}
public void add(Node node)
{
this.head = add(node, head);
}
public Node add(Node node, Node head)
{
if (head == null) return node;
if (head.score <= node.score)
{
node.next = head;
return node;
}
return add(node, head.next);
}
public void remove(String name)
{
this.head = remove(name, head);
}
public Node remove(String name, Node head)
{
if (head == null)
return null;
if (name.equals(head.name))
return remove(name, head.next);
head.next = remove(name, head.next);
return head;
}
public void print()
{
Node temp = head;
for (int i = 0; i < 10 && temp != null; i++, temp = temp.next)
{
System.out.println(temp.name + " " + temp.score);
}
}
// print from top 25 for a player
public void print(String name)
{
Node temp = head;
for (int i = 0; i < 25 && temp != null; i++, temp = temp.next)
{
System.out.println(temp.score);
}
}
}
public static void main (String[] args) throws java.lang.Exception
{
File file = new File("scores.txt");
Scanner fileInput;
try
{
fileInput = new Scanner(file);
}
catch (Exception e)
{
System.out.println(e);
return ;
}
LinkedList game = new LinkedList();
// load file
while (fileInput.hasNextLine())
{
String name = fileInput.next();
int score = fileInput.nextInt();
game.add(new Node(score, name));
}
while (true)
{
System.out.println("Menu:- ");
System.out.println("1. Enter your name, and play a game (Randomly simulate the score");
System.out.println("2. Display the current leaderboard (Top 10 scores only)");
System.out.println("3. Search for scores of a player on the top 25 scores using name (single player can be present multipletimes on the leaderboard)");
System.out.println("4. Delete the scores for a specific player (All scores for the player are deleted)");
System.out.println("5. Quit the program");
Scanner input = new Scanner(System.in);
int option = input.nextInt();
if (option == 1)
{
System.out.print("Enter name and score:");
String name = input.next();
int score = input.nextInt();
game.add(new Node(score, name));
}
else if (option == 2)
{
game.print();
}
else if (option == 3)
{
System.out.print("enter player name:");
String name = input.next();
game.print(name);
}
else if (option == 4)
{
System.out.print("enter player name to remove:");
String name = input.next();
game.remove(name);
}
else if (option == 5)
{
PrintWriter output = new PrintWriter(file);
Node temp = game.head;
for (int i = 0; i < 25 && temp != null; i++, temp = temp.next)
{
output.println(temp.name + " " + temp.score);
}
return ;
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire