codingkata.net – warm up test with fizzbuzz

First try and five minutes later I’ve got a passing solution, the Kata really makes you think about what you’re coding a bit more – I’m now hooked and I’ll be completing more Katas! You should try it at: Coding Kata.

using System;

namespace KataFizzbuzz
{
    // 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 fizzbuzz. A player who makes a mistake has to take a drink. 
    // Einstein will choose a random number to start with – for example: 4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz

    public class FizzBuzzKata : IFizzBuzzKata
    {
        static void Main()
        {
            var fb = new FizzBuzzKata();

            var r = new Random();

            while (true)
            {
                var seed = r.Next(0, 1000);

                Console.WriteLine("FizzBuzz number selected {0}", seed);

                var result = fb.Answer(seed);

                Console.WriteLine(result);

                Console.WriteLine("Press q to exit or any other key to play again.");

                var input = Console.ReadLine();

                if (input != null && input.Equals("q"))
                {
                    break;
                }
            }

            Console.ReadKey();
        }

        public string Answer(int i)
        {
            var result = i.ToString();

            if (i % 3 == 0)
                result = "fizz";

            if (i % 5 == 0)
                result = "buzz";

            if (i % 3 == 0 && i % 5 == 0)
                result = "fizzbuzz";

            return result;
        }
    }

    public interface IFizzBuzzKata
    {
        /// <summary>
        /// Give an answer to the current game
        /// </summary>
        /// <param name="number">current number in the game sequence</param>
        /// <returns>appropriate answer to the current number</returns>
        string Answer(int number);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *