// CMPU-101-51, Spring 2007
// Asmt 7 
// Solutions

// Import java.util package to make using the Arrays class easier.
import java.util.*;

// ---------------------------
// The Yahtzee class
// ---------------------------

class Yahtzee {
  
  // ----------------------------------
  // object-oriented data structure
  // ----------------------------------
  
  private int[] scoreCard = new int[6];
  private int numTurnsLeft;
  private YahtzeeHand hand;
  
  // ----------------------------------
  // constructor 
  // ----------------------------------
  
  Yahtzee() {
    for (int i=0; i<6; i++) this.scoreCard[i] = -1;
    this.numTurnsLeft = 6;
    // System.out.println("This Yahtzee object brought to you by CMPU-245!");
  }
  
  // ----------------------------------
  // object-oriented methods
  // ----------------------------------
  
  //  isValidCategory
  // ----------------------------
  
  boolean isValidCategory(int cat) {
    return this.scoreCard[cat-1] == -1;
  }
  
  //  toString
  // ----------------------------
  
  public String toString() {
    String str = "Yahtzee: Scores = [";
    for (int i=0; i<5; i++) {
      if (this.scoreCard[i] == -1) str += "_, ";
      else str += this.scoreCard[i] + ", ";
    }
    if (this.scoreCard[5] == -1) str += "_";
    else str += this.scoreCard[5];
    return str + "], total score = " + this.computeTotalScore()
      + ", num turns left = " + this.numTurnsLeft;
  }
  
  //  helper function for toString
  
  int computeTotalScore() {
    int skore = 0;
    for (int i=0; i<6; i++) {
      if (this.scoreCard[i] != -1)
        skore += this.scoreCard[i];
    }
    return skore;
  }
    
  //  getHandForNextTurn
  // ----------------------------
  
  YahtzeeHand getHandForNextTurn() {
    if (this.numTurnsLeft <= 0) {
      System.out.println("Hey!  Game over already!");
      this.hand = null;
    }
    else if (this.hand == null) {
      this.hand = new YahtzeeHand();
    }
    else {
      System.out.println("Hey!  Must play the hand you're given!"); 
    }
    return this.hand;     
  }
  
  //  scoreHand
  // ----------------------------

  Yahtzee scoreHand(int category) {
    int scoreIndex = category-1;
    if (this.scoreCard[scoreIndex] == -1) {
      this.scoreCard[scoreIndex] = this.hand.score(category);
      this.numTurnsLeft -= 1;
      this.hand = null;
    }
    else {
      System.out.println("Hey!  Pick an EMPTY category!");
    }
    return this;    
  }
  
  //  availCategories
  // ----------------------------

  void availCategories() {
    String str = "The following categories are still available: ";
    for (int i=0; i<6; i++) {
      int cat = i+1;
      if (this.scoreCard[i] == -1) 
        str += cat + " ";
    }
    System.out.println(str);
  }
  
  //  stuff added for Asmt 9
  // ----------------------------

  //  getDie
  // ----------------------------
  //  Simply asks the YahzteeHand object to report the
  //  current value of the indicated die.
  
  int getDie(int indy) {
    if (hand != null) return hand.getDie(indy);
    else return -1;
  }
  
  //  getCatScore
  // -----------------------------
  //  Returns current score in given category.
  
  int getCatScore(int cat) {
    return scoreCard[cat-1];
  }
 
  //  resetGame
  // ------------------------------
  //  Resets game in preparation for starting a new game
  
  void resetGame() {
    for (int i=0; i<6; i++) this.scoreCard[i] = -1;
    this.numTurnsLeft = 6;
    hand = null;
  }
}
