import java.util.Scanner;
import java.io.File;

/**
 * Class that models a maze of caves for the "Hunt the Wumpus" game.
 *   @author Dave Reed
 *   @version 3/3/12
 */
public class CaveMaze {
  private Cave currentCave;
  private Cave[] caves;
  private Die d;
  
  /**
   * Constructs a CaveMaze from the data found in a file.  Initially, there is 1 pit, 1 swarm of bats, and 
   * between 1 and 4 wumpi randomly placed in the maze.  The player starts out in the cave numbered 0 with
   * three times as many grenades as there are wumpi.
   *   @param filename the name of the cave data file
   */
  public CaveMaze(String filename) throws java.io.FileNotFoundException {
      Scanner infile = new Scanner(new File(filename));
      
      int numCaves = infile.nextInt();
      this.caves = new Cave[numCaves];
      
      for (int i = 0; i < numCaves; i++) {
          int num1 = infile.nextInt();
          int num2 = infile.nextInt();
          int num3 = infile.nextInt();
          int num4 = infile.nextInt();
          String name = infile.nextLine().trim();
          this.caves[num1] = new Cave(name, num1, num2, num3, num4);
      }
      
      this.currentCave = this.caves[0];
      this.currentCave.markAsVisited();       
  }
  
  /**
   * Moves the player from the current cave along the specified tunnel.  The new location must be marked
   * as visited and he appropriate action taken if the new location is not empty.
   *   @param tunnel the number of the tunnel to be traversed (1-3)
   */
  public String move(int tunnel) {
      this.currentCave = this.caves[this.currentCave.getAdjNumber(tunnel)];
      this.currentCave.markAsVisited();  
      return "ok";
  }
  
  /**
   * Tosses a grenade (if the player has any) through the specified tunnel.  If there is a wumpus 
   * in the corresponding cave, it is killed.  However, any remaining wumpi adjacent to the player are 
   * alerted by the explosion and may move as a result.
   *   @param tunnel the number of the tunnel to be bombed (1-3)
   */
  public String toss(int tunnel) {
      return "Sorry, not yet implemented.";      
  }  
  
  /**
   * Displays the current cave name and the names of adjacent caves (if those caves have been visited).
   * Caves that have not yet been visited are displayed as "unknown".  Also, warning messages are displayed if
   * the player is adjacent to a wumpi, bats, or a pit.
   */
  public String showLocation() {
      String message = "You are currently in " + this.currentCave.getCaveName();
      for (int i = 1; i <= 3; i++) {
	  Cave adjCave = this.caves[this.currentCave.getAdjNumber(i)];
	  if (adjCave.hasBeenVisited()) {
	      message += "\n    (" + i + ") " + adjCave.getCaveName();
	  }
	  else {
	      message += "\n    (" + i + ") unknown";
	  }
      }
      return message;
  }
  
  /**
   * Reports whether the player is still alive.
   *   @return true if alive, false otherwise
   */
  public boolean stillAlive() {
      return true;
  }

  /**
   * Reports whether there are any wumpi remaining.
   *   @return true if wumpi reamining in the maze, false otherwise
   */
  public boolean stillWumpi() {
      return true;
  }
}