Skip to main content
added 55 characters in body
Source Link
radarbob
  • 8.2k
  • 21
  • 35
public class DiceGame { 
    protected Dice dice;

    public DiceGame(Dice theDice) { dice = theDice; }

    public void Play() { // TBD
         int total = dice.Roll();  rolling all the dice at once
         int total = dice.Roll(3); rolling 3 dies

         // indexer allows individual Rolls.

         int firstDieRoll = dice[0].Roll();
         int secondDieRoll = dice[1].Roll();

         // when I don't care how many there are
         for (int i=0; i<dice.Count; i++) {
             dice[i].Roll();
         }
    }
}
public class DiceGame { 
    protected Dice dice;

    public DiceGame(Dice theDice) { dice = theDice; }

    public void Play() { // TBD
         int total = dice.Roll();  rolling all the dice at once

         // indexer allows individual Rolls.

         int firstDieRoll = dice[0].Roll();
         int secondDieRoll = dice[1].Roll();

         // when I don't care how many there are
         for (int i=0; i<dice.Count; i++) {
             dice[i].Roll();
         }
    }
}
public class DiceGame { 
    protected Dice dice;

    public DiceGame(Dice theDice) { dice = theDice; }

    public void Play() { // TBD
         int total = dice.Roll();  rolling all the dice at once
         int total = dice.Roll(3); rolling 3 dies

         // indexer allows individual Rolls.

         int firstDieRoll = dice[0].Roll();
         int secondDieRoll = dice[1].Roll();

         // when I don't care how many there are
         for (int i=0; i<dice.Count; i++) {
             dice[i].Roll();
         }
    }
}
edited body
Source Link
radarbob
  • 8.2k
  • 21
  • 35
public class Dice {
    List<Die> dice;

    public int Count { get { return dice.Count; }

    public Dice (params Die[] theDice) { // see notes.
        foreach (Die die in theDice) { dice.Add(die); }
    }

    // this is an indexer. see notes.
    public Die this[int i] { get { return dice[i]; } }

    public int Roll() {
        int total = 0;

        foreach (Die die in dice) { total =++= die.Roll(); }

        return total;
    }

    public int Roll(int thisMany) {
        int total = 0;
        if ( thisMany > dice.Count ) return total;
        if ( thisMany <= 0 )         return total;

        for ( i=0; i < thisMany; i++; ) { total =++= dice[i].Roll(); }

        return total;
    }

}
public class Dice {
    List<Die> dice;

    public int Count { get { return dice.Count; }

    public Dice (params Die[] theDice) { // see notes.
        foreach (Die die in theDice) { dice.Add(die); }
    }

    // this is an indexer. see notes.
    public Die this[int i] { get { return dice[i]; } }

    public int Roll() {
        int total = 0;

        foreach (Die die in dice) { total =+ die.Roll(); }

        return total;
    }

    public int Roll(int thisMany) {
        int total = 0;
        if ( thisMany > dice.Count ) return total;
        if ( thisMany <= 0 )         return total;

        for ( i=0; i < thisMany; i++; ) { total =+ dice[i].Roll(); }

        return total;
    }

}
public class Dice {
    List<Die> dice;

    public int Count { get { return dice.Count; }

    public Dice (params Die[] theDice) { // see notes.
        foreach (Die die in theDice) { dice.Add(die); }
    }

    // this is an indexer. see notes.
    public Die this[int i] { get { return dice[i]; } }

    public int Roll() {
        int total = 0;

        foreach (Die die in dice) { total += die.Roll(); }

        return total;
    }

    public int Roll(int thisMany) {
        int total = 0;
        if ( thisMany > dice.Count ) return total;
        if ( thisMany <= 0 )         return total;

        for ( i=0; i < thisMany; i++; ) { total += dice[i].Roll(); }

        return total;
    }

}
added 102 characters in body
Source Link
radarbob
  • 8.2k
  • 21
  • 35
public class Dice {
    List<Die> dice;

    public int Count { get { return dice.Count; }

    public Dice (params Die[] theDice) { // see notes.
        foreach (Die die in theDice) { dice.Add(die); }
    }

    // this is an indexer. see notes.
    public Die this[int i] { get { return dice[i]; } }

    public int Roll() {
        int total = 0;

        foreach (Die die in dice) { total =+ die.Roll(); }

        return total;
    } 

    public int Roll(int thisMany) {
        int total = 0;
        if ( thisMany > dice.Count ) return total;
        if ( thisMany <= 0 )         return total;

        for ( i=0; i < thisMany; i++; ) { total =+ dice[i].Roll(); }

        return total;
    }

}

.

public class Dice {
    public int Roll() { } // don't need to touch this

    public int Roll (int thisMany) {
        if ( this.Count < thisMany ) return 0;

        int total = 0;
        
        for (int i = 0; i < thisMany; i++)
            total += dice[i].Roll();

        return total;
    }
}
public class Dice {
    List<Die> dice;

    public int Count { get { return dice.Count; }

    public Dice (params Die[] theDice) { // see notes.
        foreach (Die die in theDice) { dice.Add(die); }
    }

    // this is an indexer. see notes.
    public Die this[int i] { get { return dice[i]; } }

    public int Roll() {
        int total = 0;

        foreach (Die die in dice) { total =+ die.Roll(); }

        return total;
    }
}

.

public class Dice {
    public int Roll() { } // don't need to touch this

    public int Roll (int thisMany) {
        if ( this.Count < thisMany ) return 0;

        int total = 0;
        
        for (int i = 0; i < thisMany; i++)
            total += dice[i].Roll();

        return total;
    }
}
public class Dice {
    List<Die> dice;

    public int Count { get { return dice.Count; }

    public Dice (params Die[] theDice) { // see notes.
        foreach (Die die in theDice) { dice.Add(die); }
    }

    // this is an indexer. see notes.
    public Die this[int i] { get { return dice[i]; } }

    public int Roll() {
        int total = 0;

        foreach (Die die in dice) { total =+ die.Roll(); }

        return total;
    } 

    public int Roll(int thisMany) {
        int total = 0;
        if ( thisMany > dice.Count ) return total;
        if ( thisMany <= 0 )         return total;

        for ( i=0; i < thisMany; i++; ) { total =+ dice[i].Roll(); }

        return total;
    }

}
Commonmark migration
Source Link
Loading
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link
Loading
deleted 9 characters in body
Source Link
radarbob
  • 8.2k
  • 21
  • 35
Loading
Added Roll(n), with commentary
Source Link
radarbob
  • 8.2k
  • 21
  • 35
Loading
Added Roll(n), with commentary
Source Link
radarbob
  • 8.2k
  • 21
  • 35
Loading
"add a constructor" changed to "Change default constructor"
Source Link
radarbob
  • 8.2k
  • 21
  • 35
Loading
Link to OPs comment on use of .NET Random class
Source Link
radarbob
  • 8.2k
  • 21
  • 35
Loading
Fixed the fix for the indexer
Source Link
radarbob
  • 8.2k
  • 21
  • 35
Loading
Fix return type for Dice indexer
Source Link
radarbob
  • 8.2k
  • 21
  • 35
Loading
added 4 characters in body
Source Link
radarbob
  • 8.2k
  • 21
  • 35
Loading
deleted 74 characters in body
Source Link
radarbob
  • 8.2k
  • 21
  • 35
Loading
added 193 characters in body
Source Link
radarbob
  • 8.2k
  • 21
  • 35
Loading
Source Link
radarbob
  • 8.2k
  • 21
  • 35
Loading