刺客程序爪哇



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

我这里有我的程序,但我有一些错误。谁杀了谁会以及之后的一切的列表不会出现在输出中,我想知道我是否可以得到一些帮助

这是我到目前为止的程序:

刺客主.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();
 }
}

刺客节点.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;
 }
}

刺客经理.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

现在我知道这是很多编程,但我的问题是我无法让我的被杀者名单出现在输出中,这个名单应该随着每个人被选为下一个受害者而增长和增长。我的教授指示我创建一个私人的空地来帮忙。我不是 100% 确定在这里到底要放什么才能让它工作? 我是否纠正了与打印墓地方法相同的东西?此外,当我浏览列表时,我需要让列表中的最后一个人跟踪列表中的第一个人并杀死列表中的第一个人。你们能帮我如何在我的程序中做到这一点,以及我将在哪里输入它。我真的需要一些帮助,伙计们,这就是我完成我的程序所需要的一切。谢谢大家!

graveyardFront表示

一个充满"死亡刺客"的链表的头部。 当你添加到墓地时,它本质上就像添加到链表一样。

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

相关内容

  • 没有找到相关文章

最新更新