刺客计划Java

时间:2016-02-09 03:07:59

标签: java linked-list

我必须创建一个程序,其中我有一个人名列表,并且必须随机分配谁将跟踪谁将会杀死谁。

我的节目在这里,但我遇到了一些错误。谁杀了谁以及之后的一切将不会出现在输出中,我想知道我是否能得到一些帮助

到目前为止,这是我的计划:

AssassinMain.java:

package Assassin;


import java.io.*;
import java.util.*;


public class AssassinMain {
 
 public static final String INPUT_FILENAME = "names.txt";
 

 public static final boolean RANDOM = false;
 
 /**
  * If not random, use this value to guide the sequence of numbers
  * that will be generated by the Random object.
  */
 public static final int SEED = 42;


 public static void main(String[] args) throws FileNotFoundException {
     
     File inputFile = new File(INPUT_FILENAME);
     if (!inputFile.canRead()) {
         System.out.println("Required input file not found; exiting.\n" + inputFile.getAbsolutePath());
         System.exit(1);
     }
     Scanner input = new Scanner(inputFile);
     
     Set<String> names = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
     while (input.hasNextLine()) {
         String name = input.nextLine().trim().intern();
         if (name.length() > 0) {
             names.add(name);
         }
     }
     ArrayList<String> nameList = new ArrayList<String>(names);
     Random rand = (RANDOM) ? new Random() : new Random(SEED);
     Collections.shuffle(nameList, rand);
     
     AssassinManager manager = new AssassinManager(nameList);

    
     Scanner console = new Scanner(System.in);
     while (!manager.isGameOver()) {
         oneKill(console, manager);
     }

     // report who won
     System.out.println("Game was won by " + manager.winner());
     System.out.println("Final graveyard is as follows:");
     manager.printGraveyard();
 }

 
 public static void oneKill(Scanner console, AssassinManager manager) {
     
     System.out.println("Current kill ring:");
     manager.printKillRing();
     System.out.println("Current graveyard:");
     manager.printGraveyard();
     
     
     System.out.println();
     System.out.print("next victim? ");
     String name = console.nextLine().trim();
     
     
     if (manager.graveyardContains(name)) {
         System.out.println(name + " is already dead.");
     } else if (!manager.killRingContains(name)) {
         System.out.println("Unknown person.");
     } else {
         manager.kill(name);
     }
     System.out.println();
 }
}

AssassinNode.java:

package Assassin;

//CSE 143, Homework 4: Assassin
//
//Instructor-provided support class.  You should not modify this code!

/**
* Each AssassinNode object represents a single node in a linked list
* for a game of Assassin.
*/
public class AssassinNode {
 
public String name;        // this person's name
 public String killer;      // name of who killed this person (null if alive)
 public AssassinNode next;  // next node in the list (null if none)

 
 /**
  * Constructs a new node to store the given name and no next node.
  */
 public AssassinNode(String name) {
     this(name, null);
 }

 /**
  * Constructs a new node to store the given name and a reference
  * to the given next node.
  */
 public AssassinNode(String name, AssassinNode next) {
     this.name = name;
     this.killer = null;
     this.next = next;
 }
}

AssassinManager.java:

package Assassin;

import java.util.ArrayList;


/**
 * AssassinManager that keeps track of who is stalking whom and the history
 * of who killed whom by maintaining two linked lists, a list of players who
 * are currently alive in the "kill ring" and a list of players who are
 * currently dead in the "graveyard".
 *
 */
public class AssassinManager 
{

    /**
     * killRingFront field: reference to the front node of the kill ring
     */
    private AssassinNode killRingFront;

    /**
     * graveyardFront field: reference to the front node of the graveyard (null if empty)
     */
    private AssassinNode graveyardFront;

    /**
     * Initialize a new assassin manager over the given list of people.
     * @param names
     * @throws IllegalArgumentException if the list is null or has a size of 0
     */
    public AssassinManager(ArrayList<String> names) 
    {
        // You receive a list of names (as a parameter).
        // Take the names and build up the kill ring of linked nodes that
        // contains the names in the same order as you received them in
        // the ArrayList. You may assume that the names are non-empty, non-null
        // strings and that there are no duplicates.
        // Note: you receive a list of names as strings. You need to create a new
        // AssassinNode object for each player and put their name into the node
        // and connect the nodes together into a list that killRingFront references.

        // your code goes here
    	int temp = 0;
    	
    	while( temp < names.size())
    	{
    	if( killRingFront == null)
    	{
    		
    		killRingFront = new AssassinNode(names.get(temp));	
    		
    		
    		
    	}
    	else
    	{
    		AssassinNode current = killRingFront;
    		while( current.next != null)
    		{
    			current = current.next;
    		}
    		current.next = new AssassinNode( names.get(temp));
    			
    		
    	}
    	temp++;
    	}
    	
    }

    /**
     * Prints the names of the people in the kill ring, one per line, indented
     * by two spaces, as "name is stalking name". The behavior is unspecified if
     * the game is over.
     */
    public void printKillRing() 
    {
        // your code goes here
    	AssassinNode current = killRingFront;
    	while( current.next != null)
    	{
    		System.out.println("  " +  current.name + " is stalking " + current.next.name ); 
    		current = current.next;
    	}
    	
    	
    }

    /**
     * Prints the names of the people in the graveyard, one per line, with each
     * line indented by two spaces, with output of the form "name was killed by
     * name". It should print the names in the opposite of the order in which
     * they were killed (most recently killed first, then next more recently
     * killed, and so on). It should produce no output if the graveyard is empty.
     */
    public void printGraveyard() 
    {
        // your code goes here
    	AssassinNode current = graveyardFront;
    	
 	
    	if (graveyardFront == null)
        {
          return;
        }
        
    	for(current = graveyardFront ; current.next != null; current = current.next)
    	{
    		
    		System.out.println("  " + current.name + " was killed by " + current.killer );
    		
    		
    	}
    	System.out.println(current.name);	
         
    }

    /**
     * Checks to see if <code>name</code> is in the current kill ring.
     * @param name name to check
     * @return true if the <code>name</code> is in the kill ring and false otherwise
     */
    public boolean killRingContains(String name) 
    {
       
    	AssassinNode current;
    	for(current = killRingFront ; current.next != null; current = current.next)
    	{
    		if( current.name.equals(name))
    	{
    		return true;
    	}
    	}
    	return false; 
    	
    }

    /**
     * Checks to see if <code>name</code> is in the current graveyard.
     * @param name name to check
     * @return true if the <code>name</code> is in the graveyard and false otherwise
     */
    public boolean graveyardContains(String name) 
    {
    	
    	AssassinNode current = graveyardFront;
    	
    	if( graveyardFront == null)
    	{
    		return false;
    	}
    	for(current = graveyardFront ; current.next != null ; current = current.next)
    	{
    		if( current.name.equals(name))
    		{
    			return true;
    		}
    	}
        return false;
    }

    /**
     * Checks to see if the game is over (if the kill ring has only one player
     * remaining).
     * @return true if the game is over and false otherwise
     */
    public boolean isGameOver() 
    {
    	
    	if(killRingFront.next == null)
    	{
    		return true;
    	}
    	else
    	{
    		return false; 
    	}
       
    }

    /**
     * Obtains the name of the winner of the game.
     * @return name of the winner of the game or <code>null</code> if the game
     *         is not over
     */
    public String winner() 
    {
    	String winner;
    	AssassinNode current;
    	
    	if( isGameOver())
    	{
    		for(current = killRingFront ; current.next != null; current = current.next)
        	{
    			winner = current.name;
    			return winner;
        	}
    	}
        return null; // delete this line and replace it with your code for this method
    }

    /**
     * Transfers a player from the kill ring to the front of the graveyard. This
     * operation does not change the relative order of the kill ring. Case is
     * ignored in comparing names.
     * @param name the name of the player to be transferred from the kill ring
     *             to the graveyard
     * @throws IllegalStateException if the game is over
     * @throws IllegalArgumentException if the given name is not part of the kill ring
     */
    public void kill(String name) 
    {
        // your code goes here
    	AssassinNode current, previous;
    	for( current = killRingFront, previous= null; current != null && !current.name.equals(name); current = current.next)
    	{
    		previous = current;
    		
    	}
    	
    	if( previous == null)
    	{
    		AssassinNode temp = killRingFront;
    		killRingFront = killRingFront.next;

    		while( previous.next != null)
    		{
    			previous= previous.next;
    		}
//    		previous.next = current.next;
//    		current.killer = previous.name;
//    		current.next = graveyardFront;
    		addtoGraveyard(temp);
    	}
    	else
    	{ 
    		AssassinNode temp = killRingFront;
    		previous.next = current.next;
   			current.killer = previous.name;
    		current.next = graveyardFront;
    		
    		addtoGraveyard(temp);
    		
    	}
    	
    	
    	
    	
    }
    
    private void addtoGraveyard(AssassinNode dead)
    {
    		
    }

}

Don Knuth
Alan Turing
Ada Lovelace
Charles Babbage
John von Neumann
Grace Hopper
Bill Gates
Tim Berners-Lee
Alan Kay
Linus Torvalds
Alonzo Church

现在我知道这有很多编程但是我的问题是我无法得到我的列表中被杀的人出现在输出中,该列表是假设随着每个人选择的增长和增长,因为下一个受害者是假设去死。我的教授指示我创建一个私有的虚空插件来帮忙。我不是百分之百确定要把它放在哪里才能使其正常工作?我是否正确使用printgraveyard方法?此外,当我查看列表时,我需要让列表中的最后一个人跟踪列表中的第一个人并杀死列表中的第一个人。你们可以帮助我如何在我的程序中完成这项工作吗?我会在哪里输入它。我真的需要一些帮助这些人我只需要完成我的程序。谢谢你们!

1 个答案:

答案 0 :(得分:0)

graveyardFront表示链接列表的头部,其中包含“死亡刺客”。当你添加到墓地时,它基本上就像添加到链接列表一样。

private void addToGraveYard(AssassinNode node) {
    if (graveyardFront == null) {
        graveyardFront = node;
    }
    else {
        node.next = graveyardFront;
        graveyardFront = node;}
    }
}