Skip to content

C# Dice Game

james wiley

Asked May 4, 2009, 12:17 PM EDT1 answer
Share
LinkedInX
Hey all I've got this dice game that i'm trying to make. It basically a dice version of golf. Each player rolls 3 dice until they roll a double. Each roll before the double is achieved counts as a stroke. Once a double is rolled i want the score for that hole to be displayed on the console. At the moment im still working with notepad and the basic SDK command prompt as this is all i understand. I've attempted some of the code so far but would really appreciate some help finishing it off (in simple terms if possible) Thanks in advance! CODE using System; namespace dicegame1 { public static void Main() { Random ran = new Random(); int player1 = 0; int player2 = 0; int player1RoundScore = 0; int player2RoundScore = 0; bool player1RoundScore = false; bool player2RoundScore = false; int roundNumber = 1; // Random dice rolling // { int diceOne = ran.Next(1,7); int diceTwo = ran.Next(1,7); int diceThree = ran.Next(1,7); } { if (diceOne == diceTwo || diceTwo == diceThree || diceOne == diceThree) } } }

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    The following will do what you need but you'll need to fix the random number seeding if you want to run it more than once...   

    class Program
            {
            public static void Main(string[] args)
                {
                GolfGame game = new GolfGame(18);
                Player player1 = new Player("Fred");
                Player player2 = new Player("George");
                game.AddPlayer(player1);
                game.AddPlayer(player2);
                game.Play();

                Console.Write("\nPress any key to continue...");
                Console.ReadKey();
                }


            }

        public class GolfGame
            {
            ArrayList players = new ArrayList();
            int numberOfHoles = 0;

            public GolfGame(int holes)
                {
                numberOfHoles = holes;
                }

            public void AddPlayer(Player p)
                {
                if (players.Count >= 2)
                    {
                    Console.Write("Error: cannot add player {0} there are already 2 players signed up", p.Name);
                    }
                else
                    {
                    players.Add(p);
                    }
                }

            public void Play()
                {
                for (int hole = 1; hole <= numberOfHoles; ++hole)
                    {
                    bool stillPlayingTheHole = true;
                    while (stillPlayingTheHole)
                        {
                        stillPlayingTheHole = false;
                        foreach (Player player in players)
                            {
                            if (player.ShotsTaken == 0)
                                {
                                stillPlayingTheHole = true;
                                player.TeeOff();
                                if (player.Scored)
                                    {
                                    Console.Write("Player {0} scored a hole-in-one !\n", player.Name);
                                    }
                                }
                            else
                                {
                                if (!player.Scored)
                                    {
                                    stillPlayingTheHole = true;
                                    player.TakeShot();
                                    if (player.Scored)
                                        {
                                        Console.Write("Player {0} scored in {1} shots\n", player.Name, player.ShotsTaken);
                                        }
                                    }
                                }
                            }
                        }

                    foreach (Player player in players)
                        {
                        player.ShotsTaken = 0;
                        }

                    }
                foreach (Player player in players)
                    {
                    Console.Write("After {0} rounds, {1} has a total score of {2}\n", numberOfHoles, player.Name, player.AccumulatedScore);
                    }
                }
            }

        public class Player
            {
            private string name;
            private int accumulatedScore = 0;
            private int shotsTaken = 0;
            private bool scored = false;
            Random ran;

            public Player(string playerName)
                {
                name = playerName;
                ran = new Random(name.Length);
                }

            public string Name
                {
                get { return name; }
                set { name = value; }
                }

            public bool Scored
                {
                get { return scored; }
                set { scored = value; }
                }

            public int ShotsTaken
                {
                get { return shotsTaken; }
                set { shotsTaken = value; }
                }
            public int AccumulatedScore
                {
                get { return accumulatedScore; }
                set { accumulatedScore = value; }
                }
            public void TeeOff()
                {
                shotsTaken = 0;
                scored = false;
                Roll();
                }
            public void TakeShot()
                {
                Roll();
                }

            private void Roll()
                {
                int diceOne = ran.Next(1, 7);
                int diceTwo = ran.Next(1, 7);
                int diceThree = ran.Next(1, 7);
                ++shotsTaken;
                Console.Write("{0} got [{1}] [{2}] [{3}]\n", name, diceOne, diceTwo, diceThree);

                if (diceOne == diceTwo || diceTwo == diceThree || diceOne == diceThree)
                    {
                    scored = true;
                    accumulatedScore += shotsTaken;
                    }
                }


            }
    0