Microsoft Code Camp
From DojoWiki
[edit]
Petals Around the Rose
Faisal implemented this Game as his first C# program; we spent a couple hours refactoring the code.
Following are three classes (the original solution had all classes in one file, and also had some cumbersome logic control structures).
I commented out the code which actually computes the answer so I wouldn't get in trouble from the official "Petals Around the Rose" governing body.
public class Petals {
static void Main() {
DieList dice = new DieList();
int score = 0;
Petals petals = new Petals() ;
petals.WriteDirections() ;
while (!(6 == score)) {
Console.WriteLine("Rolling Dice...");
dice.roll();
dice.show_dice();
Console.WriteLine("What do you guess? (q to quit.)");
string guessStr = Console.ReadLine();
int result = -1 ;
if (!Int32.TryParse(guessStr, out result) ) {
if (guessStr == "q")
return ;
else {
Console.WriteLine("Bad Input") ;
}
}
else {
if ( dice.score() == result ) {
Console.WriteLine("Correct!");
Console.WriteLine("");
score++;
} else {
Console.WriteLine("Sorry, try again.");
Console.WriteLine("");
score = 0;
}
}
}
if (score >= 6) {
Console.WriteLine("6 times in a row -- you probably know the rule.");
}
}
public void WriteDirections() {
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("PETALS AROUND THE ROSE.");
Console.WriteLine("");
Console.WriteLine("The name of the game is 'Petals Around the Rose.");
Console.WriteLine("Each turn I will roll 5 dice, and you will guess the score.");
Console.WriteLine("I will then tell you if you guessed correctly or not.");
Console.WriteLine("");
Console.WriteLine("Your goal is to figure out how the score is determined.");
Console.WriteLine("");
Console.WriteLine("The score will be an even number between 0 and 20.");
Console.WriteLine("The name of the game is significant.");
Console.WriteLine("");
Console.WriteLine("Good luck.");
Console.WriteLine("");
}
}
public class Die {
public int val;
public void roll(Random rand) {
val = rand.Next(6);
}
public int value {
get {return val;}
}
public int score() {
//The actual Secret Scoring algorithm ;-)
}
public void show() {
Console.Write( Convert.ToString( this.value ) );
Console.Write(" ");
}
}
public class DieBuffer {
string string1;
string string2;
string string3;
public void add(Die die) {
switch (die.value) {
case 1 :
string1 += "[ ] ";
string2 += "[ * ] ";
string3 += "[ ] ";
break ;
case 2 :
string1 += "[* ] ";
string2 += "[ ] ";
string3 += "[ *] ";
break ;
case 3 :
string1 += "[* ] ";
string2 += "[ * ] ";
string3 += "[ *] ";
break ;
case 4 :
string1 += "[* *] ";
string2 += "[ ] ";
string3 += "[* *] ";
break ;
case 5 :
string1 += "[* *] ";
string2 += "[ * ] ";
string3 += "[* *] ";
break ;
case 6 :
string1 += "[* *] ";
string2 += "[* *] ";
string3 += "[* *] ";
break ;
default :
throw new Exception("Die fell off the table") ;
}
}
public void show() {
Console.WriteLine(string1);
Console.WriteLine(string2);
Console.WriteLine(string3);
}
}
public class DieList : List<Die> {
public Random rand = new Random();
public DieList() {
Add(new Die()) ;
Add(new Die()) ;
Add(new Die()) ;
Add(new Die()) ;
Add(new Die()) ;
}
public void roll() {
foreach (Die die in this) {
die.roll(rand) ;
}
}
public void show_dice() {
DieBuffer dieRoll = new DieBuffer();
foreach (Die die in this) {
dieRoll.add(die) ;
}
dieRoll.show();
}
public int score() {
int retVal = 0 ;
foreach (Die die in this) {
retVal += die.score() ;
}
return retVal ;
}
}
