patterncsharpMinor
PseudoBrain FizzBuzz Thoughts
Viewed 0 times
pseudobrainthoughtsfizzbuzz
Problem
Some recent discussions on this site involved the Fizz-Buzz game - I have never actually implemented this, but for the record this is what I put together:
When people implement fizzbuzz, they tend to run a loop going from 1 to 100 and outputting the results to some console. According to Wikipedia, that's not how the game plays:
Players generally sit in a circle. The player designated to go first says the number "1", and each player thenceforth counts one number in turn. However, any number divisible by three is replaced by the word fizz and any divisible by five by the word buzz. Numbers divisible by both become fizz buzz. A player who hesitates or makes a mistake is eliminated from the game.
So I'm thinking of going that way - and since I've never actually coded anything remotely resembling AI since I tweaked some Duke Nukem 3D configuration files in 1997, I'd like to get some feedback and refactoring thoughts about this:
```
public class PseudoBrain
{
private readonly FizzBuzzConverter _converter;
public PseudoBrain(FizzBuzzConverter converter)
{
_converter = converter;
}
public virtual string Think(string lastResult, int value)
{
var random = new Random();
var ai = random.Next(0, 99);
var result = _converter.Convert(value);
if (ai < 85)
{
// thinks straight most of the time.
return result;
}
else
{
Hesitate();
if (ai < 95) return result;
// could be right, c
public class FizzBuzzConverter
{
private readonly int _fizz;
private readonly int _buzz;
public FizzBuzzConverter(int fizz, int buzz)
{
_fizz = fizz;
_buzz = buzz;
}
public string Convert(int value)
{
if (value % (_fizz*_buzz) == 0) return "FizzBuzz";
if (value % _buzz == 0) return "Buzz";
if (value % _fizz == 0) return "Fizz";
return value.ToString();
}
}When people implement fizzbuzz, they tend to run a loop going from 1 to 100 and outputting the results to some console. According to Wikipedia, that's not how the game plays:
Players generally sit in a circle. The player designated to go first says the number "1", and each player thenceforth counts one number in turn. However, any number divisible by three is replaced by the word fizz and any divisible by five by the word buzz. Numbers divisible by both become fizz buzz. A player who hesitates or makes a mistake is eliminated from the game.
So I'm thinking of going that way - and since I've never actually coded anything remotely resembling AI since I tweaked some Duke Nukem 3D configuration files in 1997, I'd like to get some feedback and refactoring thoughts about this:
```
public class PseudoBrain
{
private readonly FizzBuzzConverter _converter;
public PseudoBrain(FizzBuzzConverter converter)
{
_converter = converter;
}
public virtual string Think(string lastResult, int value)
{
var random = new Random();
var ai = random.Next(0, 99);
var result = _converter.Convert(value);
if (ai < 85)
{
// thinks straight most of the time.
return result;
}
else
{
Hesitate();
if (ai < 95) return result;
// could be right, c
Solution
I'd first create an
Then the
This will allow for easier unit testing via mocking or writing and using new Converters.
Lastly, I have a bit of a squicky feeling when I see random number generators created over and over (as
interface that your FizzBuzzConverter class can implement:public interface IConverter
{
string Convert(int value);
}
public class FizzBuzzConverter : IConverter
{
// ...
}Then the
PseudoBrain can have any implementation of the interface injected into it:public class PseudoBrain
{
private readonly IConverter _converter;
public PseudoBrain(IConverter converter)
{
_converter = converter;
}
// ...
}This will allow for easier unit testing via mocking or writing and using new Converters.
Lastly, I have a bit of a squicky feeling when I see random number generators created over and over (as
Think() will be called repeatedly). So I put that at the class level:public class PseudoBrain
{
private readonly Random _random = new Random();
private readonly IConverter _converter;
public PseudoBrain(IConverter converter)
{
_converter = converter;
}
public virtual string Think(string lastResult, int value)
{
var ai = _random.Next(0, 99);
// ...
}
// ...
}Code Snippets
public interface IConverter
{
string Convert(int value);
}
public class FizzBuzzConverter : IConverter
{
// ...
}public class PseudoBrain
{
private readonly IConverter _converter;
public PseudoBrain(IConverter converter)
{
_converter = converter;
}
// ...
}public class PseudoBrain
{
private readonly Random _random = new Random();
private readonly IConverter _converter;
public PseudoBrain(IConverter converter)
{
_converter = converter;
}
public virtual string Think(string lastResult, int value)
{
var ai = _random.Next(0, 99);
// ...
}
// ...
}Context
StackExchange Code Review Q#36750, answer score: 4
Revisions (0)
No revisions yet.