/*
 *  Dice.java
 *  Copyright (C) 2002 Devon D Jones
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  The author of this program grants you the ability to use this code
 *  in conjunction with code that is covered under the Open Gaming License
 *
 */
package org.evilsoft.gaming;

import java.util.*;

public class Dice extends Die {

  public int timesRolled = 0;
  public int total;
  public int num;
  public int sides;
  public int modifier = 0;
  public int rolls[];
  private Random rand = new Random();
  
  public Dice(int num, int sides, int modifier, Random rand) {
    this.num = num;
    this.sides = sides;
    this.modifier = modifier;
    this.rand = rand;
    rolls = new int[num];
    roll();
  }

  public Dice(int num, int sides, int modifier) {
    this.num = num;
    this.sides = sides;
    this.modifier = modifier;
    rolls = new int[num];
    roll();
  }

  public Dice(int num, int sides) {
    this(num, sides, 0);
  }
  
  public Dice(int num, int sides, Random rand) {
    this(num, sides, 0, rand);
  }

  public int roll() {
    int value = 0;
    int i;
    total = 0;
    for(i = 0; i < num; i++) {
      rolls[i] = rand.nextInt(sides) + 1;
      value = rolls[i] + value;
    }
    total = value + modifier;
    timesRolled++;
    return total;
  }

  public int value() {
    return total;
  }
  
  public String toString() {
    if(modifier == 0) {
      return num + "d" + sides;
    }
    else {
      return num + "d" + sides + "+" + modifier;
    }
  }
  
  public void setRandom(Random rand) {
      this.rand = rand;
  }
}
