Poab9200 6 Light Poster

Hello I made the following program for someone on this site that had requested the game. I didn't have a problem making it. During some tests I conducted I ran into a problem, at least what had seemed like a problem.

Player 1 will win on average 90-96% of the time. This test is based on executing 9000 'plays' per execution of the program its self. I've ran 5 instances of the program each executing 9000 plays and they all have the same result.

My question is why does Player 1 always win? I mean are not the numbers supposed to be random? It doesn't seem that the Random class isn't truly being random. Also I do know that the Random class is based upon a seed to generate the numbers. I tried using a static version of Random as mentioned in an article on the internet somewhere. The article states that making the Random Class static that it will produce truly random numbers. I also looked at generating Random Numbers with the Cryptographic.RNG(something) method. But I've found that that only generates a random string and not a number.

So if anyone has any ideas to make this game a bit more 'balanced' then that would be great.


EDIT:I found the problem Line#77 resets nRolls2 to 2. Its amazing the simple mistakes that can ruin a program.

Without further ado here is the code:

using System;
using System.Collections.Generic;
using System.Text;

namespace Dice_Golf_Game
{
    using System;
    using System.IO;
    using System.Security.Cryptography;

    namespace Dice_Golf_Game
    {
        class Program
        {
            static void Main(string[] args)
            {
                SaveConsoleData e = new SaveConsoleData();
                PlayerData p = new PlayerData();
                Random r = new Random();
                int nHoles1 = 1, nHoles2 = 1;
                int nRolls1 = 1, nRolls2 = 1;
                int nScore1 = 0, nScore2 = 0;
                int plays = 1, maxplays = 0;
                int p1Won = 0, p2Won = 0;
                string sPlayer1, sPlayer2;

                Console.Title = "Dice Golf Game : Created By: Jd_Logic(Poab9200)";
                Console.WriteLine("Dice Golf Game : Created By: Jd_Logic(Poab9200)\n\n");
                Console.Write("Enter a name for Player 1 : ");
                sPlayer1 = Console.ReadLine();
                Console.Write("Enter a name for Player 2 : ");
                sPlayer2 = Console.ReadLine();
                Console.Write("How many games to play?   : ");
                maxplays = Convert.ToInt32(Console.ReadLine());

                while (true)
                {
                    Console.WriteLine("\n\nCurrent Hole : {0}", nHoles1);
                    while (nHoles1 != 19)
                    {
                        int nDice1 = r.Next(1, 6);
                        int nDice2 = r.Next(1, 6);
                        int nDice3 = r.Next(1, 6);
                        Console.WriteLine("Roll #{3}: {0} {1} {2}", nDice1, nDice2, nDice3, nRolls1);
                        if (nDice1 == nDice2 || nDice1 == nDice3 || nDice2 == nDice3)
                        {
                            nScore1 += nRolls1;
                            nHoles1++;
                            nRolls1 = 1;
                            if (nHoles1 == 19)
                            {
                                p.PlayerScore(sPlayer1, nScore1, false);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("\nCurrent Hole : {0}", nHoles1);
                            }
                        }
                        else
                        {
                            nRolls1++;
                        }
                    }

                    Console.WriteLine("\n\nCurrent Hole : {0}", nHoles2);
                    while (nHoles2 != 19)
                    {
                        int nDice1 = r.Next(1, 6);
                        int nDice2 = r.Next(1, 6);
                        int nDice3 = r.Next(1, 6);
                        Console.WriteLine("Roll #{3}: {0} {1} {2}", nDice1, nDice2, nDice3, nRolls2);
                        if (nDice1 == nDice2 || nDice1 == nDice3 || nDice2 == nDice3)
                        {
                            nScore2 += nRolls2;
                            nHoles2++;
                            nRolls2 = 2;
                            if (nHoles2 == 19)
                            {
                                p.PlayerScore(sPlayer2, nScore2, false);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("\nCurrent Hole : {0}", nHoles2);
                            }
                        }
                        else
                        {
                            nRolls2++;
                        }
                    }

                    // Display how many times each player has won
                    p.PlayerScoreTotal(sPlayer1, sPlayer2, nScore1, nScore2, false);
                    decimal mAvga = Convert.ToDecimal(nScore1) / 18M;
                    decimal mAvgb = Convert.ToDecimal(nScore2) / 18M;
                    if (mAvga < mAvgb)
                    {
                        p1Won++;
                        string ss1 = string.Format("{0} has won game {1}.\n", sPlayer1, plays);
                        e.WriteLine(ss1);
                    }
                    else
                    {
                        p2Won++;
                        string ss2 = string.Format("{0} has won game {1}.\n", sPlayer2, plays);
                        e.WriteLine(ss2);
                    }

                    if (plays == maxplays)
                    {
                        p.PlayerWinTotal(p1Won, p2Won);
                        Console.WriteLine("\nThank you for playing Jd_Logic's Dice Golf");
                        break;
                    }
                    else
                    {
                        nHoles1 = 1;
                        nHoles2 = 1;
                        nRolls1 = 1;
                        nRolls2 = 1;
                        nScore1 = 0;
                        nScore2 = 0;
                        plays++;
                    }
                }
                Console.Read();
            }
        }
        class SaveConsoleData
        {
            public void WriteLine(string text)
            {
                Console.WriteLine(text);
                if (!File.Exists(@"test.txt"))
                {
                    using (StreamWriter sw = File.CreateText(@"test.txt"))
                    {
                        if (text.Contains("\n") || text.Contains("\n\n") || text.Contains("\n\n\n"))
                        {
                            text.Replace("\n", "");
                            sw.WriteLine();
                        }
                        sw.WriteLine(text);
                    }
                }
                using (StreamWriter sw = File.AppendText(@"test.txt"))
                {
                    if (text.Contains("\n") || text.Contains("\n\n") || text.Contains("\n\n\n"))
                    {
                        text.Replace("\n", "");
                        sw.WriteLine();
                    }
                    sw.WriteLine(text);
                }
            }
        }
        class PlayerData
        {
            public void PlayerWinTotal(int p1win, int p2win)
            {
                Console.WriteLine("\n\nPlayer 1 has won {0} games", p1win);
                Console.WriteLine("Player 2 has won {0} games", p2win);
            }
            public void PlayerScore(string playername, int score, bool write)
            {
                SaveConsoleData e = new SaveConsoleData();
                decimal mAvg = Convert.ToDecimal(score) / 18M;
                string s1 = string.Format("\n\n          {0}'s Score", playername);
                string s2 = string.Format("---------------------------------");
                string s3 = string.Format("    Your Score Is : {0}", score);
                string s4 = string.Format("  Your Average Is : {0:F3}", mAvg);
                string s5 = string.Format("---------------------------------\n\n");
                if (write)
                {
                    e.WriteLine(s1);
                    e.WriteLine(s2);
                    e.WriteLine(s3);
                    e.WriteLine(s4);
                    e.WriteLine(s5);
                }
                else
                {
                    Console.WriteLine(s1);
                    Console.WriteLine(s2);
                    Console.WriteLine(s3);
                    Console.WriteLine(s4);
                    Console.WriteLine(s5);
                }
            }
            public void PlayerScoreTotal(string playername1, string playername2, int score1, int score2, bool write)
            {
                SaveConsoleData e = new SaveConsoleData();
                decimal mAvg1 = Convert.ToDecimal(score1) / 18M;
                decimal mAvg2 = Convert.ToDecimal(score2) / 18M;

                string s1 = string.Format("\n          Score Totals");
                string s2 = string.Format("---------------------------------");
                string s3 = string.Format("{0}'s Score   : {1}", playername1, score1);
                string s4 = string.Format("{0}'s Average : {1:F3}", playername1, mAvg1);
                string s5 = string.Format("{0}'s Score   : {1}", playername2, score2);
                string s6 = string.Format("{0}'s Average : {1:F3}", playername2, mAvg2);
                string s7 = string.Format("---------------------------------\n\n\n");
                if (write)
                {
                    e.WriteLine(s1);
                    e.WriteLine(s2);
                    e.WriteLine(s3);
                    e.WriteLine(s4);
                    e.WriteLine(s5);
                    e.WriteLine(s6);
                    e.WriteLine(s7);
                }
                else
                {
                    Console.WriteLine(s1);
                    Console.WriteLine(s2);
                    Console.WriteLine(s3);
                    Console.WriteLine(s4);
                    Console.WriteLine(s5);
                    Console.WriteLine(s6);
                    Console.WriteLine(s7);
                }
            }
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.