Hey All

I want to create a simple dice golf game in C#

The basic idea is:

A player rolls 3 random dice until they roll a double
The number of rolls taken until a double is rolled is calculated and displayed on the console
This is run 18 times until all 18 holes are completed
Final score is displayed on the console

I started some of the code so far but i'm not really sure where i should be heading or what i should be doing..

(The code is a bit of a mess and doesn't really anything, ive just been noting down things i might need to include as i go along and havent organised them yet)

I hope you can help me :)

using System;

namespace dicegame1
{
public class RandDice
{
public static void Main()
{
	Random ran = new Random();
	
	int player1;
	int player2;
	int player1RoundScore;
	int player2RoundScore;
	
	bool player1RoundScore = false;
	bool player2RoundScore = false;
	
	int roundNumber;
}
	// Random dice rolling //
	{
	int diceOne = ran.Next(1,7);
	int diceTwo = ran.Next(1,7);
	int diceThree = ran.Next(1,7);
	
	Console.write(ran.Next(1,7) + " ");
	Console.write(ran.Next(1,7) + " ");
	Console.write(ran.Next(1,7));
	}
	{
	if (diceOne == diceTwo || diceTwo == diceThree || diceOne == diceThree)

	}
	
	
}
}
}

Recommended Answers

All 8 Replies

I might help You on this one,
but first I have some questions:
"This is run 18 times until all 18 holes are completed"
what holes?
Let me see if I understood Your game:
Player one rolls 3 dices until he gets a double. and on each roll You count a score(bad score) ,so we just count how many time he rolled.
player 2 does the same.
this is repeated 18 times .
In the end , the One with the highest count of rolls lost?

You should organize things up , I would make a function (1 for player one, and 1 for player 2) that return how many time the dices were rolled.
summon them both in a function of 18 iterations, while i add up their scores,now the game will end really fast XD
some question on your code:
" bool player1RoundScore = false;
bool player2RoundScore = false;
"
what did You mean to do with these variables ?

" Console.write(ran.Next(1,7) + " ");
Console.write(ran.Next(1,7) + " ");
Console.write(ran.Next(1,7));
"
I guess You wanted to print dice One dice Two and dice Three,but You're randomizing numbers again :S

well If that' what You need I can help.
Good luck

Hey,

Thats exactly what i want to do, you've hit the nail on the head :)

I'm not really sure where to head with what i've got thats the problem.

I've put together bits and pieces that i thought would work but cant seem to get a finished product

Any code you can provide me would be greatly appreciated as this is starting to annoy me now

Cheers! :)

ps. (im still fairly new to c# so i want to try and keep the code simple if possible)

Thanks again

About the holes.. i dont need to have seperate holes i just need the process to loop 18 times then finish :)

i tried a different attempt at it as well but again i don't really know where i'm heading and this code only displays 0 0 0 instead of 3 random numbers? :s

using System;
using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication1
{

    class Program
    {
        static void Main(string[] args)
        {
            
            RollingDice rd = new RollingDice();
            Console.WriteLine("Rolling count:{0}", rd.rollingCount);
            Console.Write(rd.first);
            Console.Write(rd.second);
            Console.Write(rd.third);
            Console.ReadKey();
        }


    }
    class RollingDice
    {
        public int first;
        public int second;
        public int third;
        public int rollingCount;
        Random ran = new Random();

        public void CallBackFunction(Object stateInfo)
        {
            
            first = ran.Next(1,7);
            second = ran.Next(1,7);
            third = ran.Next(1,7);
            Console.Write(first);
            Console.Write(second);
            Console.Write(third);
            Console.Write("");
            rollingCount++;

            if (first == second || first == third || second == third)
            {
                Console.Write("you scored:");

            }

            if (CheckIfExistTwoNumberEqual())
            {
                
            }

        }
        bool CheckIfExistTwoNumberEqual()
        {
            return (first == second || first == third || second == third);
        }
    }


}

Multiply classes is not a good idea for start in C#.
Functions are very easy and powerful tool that You can use, which can divide your work so you can conquer it.
I think I understand what code You'd like to see, a basic You can start from, so this is what can get you started(this is in the class program:)

public static bool CheckIfExistTwoNumberEqual(int first,int second,int third)
{
if ((first==second)||(second==third)||(third==first))
return true;
return false;
}

public static int num_of_rolls(int player_num)
{
Random r = new Random();
int count=1;
  int first=r.Next(6)+1; //exactly like (1,7)
int second=r.Next(6)+1;
int third=r.Next(6)+1;

Console.WriteLine("Player {0} rolled dices and got :{1},{2},{3}",player_num,first,second,third);
while (CheckIfExistTwoNumberEqual(first,second,third)==false)
{
count++;
first=r.Next(6)+1; //exactly like (1,7)
second=r.Next(6)+1;
third=r.Next(6)+1;
}
return count;
}
now in the main function:

static void Main(string[] args)
{
int sum1=0;
int sum2=0;
for(int i=1; i<=18;i++)
{
sum1+=num_of_rolls(1); //sum+= is the same as sum=sum+X
sum2+=num_of_rolls(2); 
}
if (sum2>sum1)
Console.WriteLine("congrats Player 1, You're the lucky winner !");
else
Console.WriteLine("congrats Player 2, You're the lucky winner !");
Console.ReadLine();
}

Hope that helps, anyway I'm going to sleep(hope my tiredness didn't kill my code XD) , and tomorrow I'll check here for very short cause i have an exam in two days(not about computers) and i need to learn a lot more XDDD
so good luck, You're welcome to ask any question , it's vital for me to help You understand, cause this is how I was treated here :D
good luck

Hey

Thank you ever so much for you help, I really do appreciate it!

While the football was on (Man Utd won - Get in!!) haha I had another crack at the code..

I've managed to come up with this..

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

namespace ConsoleApplication1
{

    class Program
    {
        static void Main(string[] args)
        {
            //Create 2 rollingdice objects that represent the scores and dice for each player.
            RollingDice rd = new RollingDice();
            rd.player_number = 1;
            RollingDice rd2 = new RollingDice();
            rd2.player_number = 2;
            //rd.RollDice();
            //Console.WriteLine("Rolling count:{0}", rd.rollingCount);
            //Console.Write(rd.first);
            //Console.Write(rd.second);
            //Console.Write(rd.third);


            while (true)
            {
                rd.RollDice();
                Console.WriteLine("Rolling player 1");
                Console.WriteLine(rd.first + " " + rd.second + " " + rd.third);
                if (rd.first == rd.second || rd.first == rd.third || rd.second == rd.third)
                {
                    Console.WriteLine("Player {0} has finished the hole!", rd.player_number);
                    rd.round_count++;
                }
                else
                {
                    Console.WriteLine("Player {0} took a stroke but did not get it in the hole. Score is now : {1}", rd.player_number, rd.rollingCount);
                }
                Console.ReadKey();

                rd2.RollDice();
                Console.WriteLine("Rolling player 2");
                Console.WriteLine(rd2.first + " " + rd2.second + " " + rd2.third);

                if (rd2.first == rd2.second || rd2.first == rd2.third || rd2.second == rd2.third)
                {
                    Console.WriteLine("Player {0} has finished the hole!", rd2.player_number);
                    rd2.round_count++;
                }
                else
                {
                    Console.WriteLine("Player {0} took a stroke but did not get it in the hole. Score is now : {1}", rd2.player_number, rd2.rollingCount);
                }

                if (rd.round_count > 18 || rd2.round_count > 18)
                {
                    Console.WriteLine("Game has finished!");

                    if (rd.rollingCount < rd2.rollingCount)
                    {
                        Console.WriteLine("Player 1 wins with a score of " + rd.rollingCount + "!");
                    }
                    else if (rd.rollingCount > rd2.rollingCount)
                    {
                        Console.WriteLine("Player 2 wins with a score of " + rd2.rollingCount + "!");

                    }


                    goto out_of_loop;
                }
                Console.ReadKey();
            }
        out_of_loop:
            Console.WriteLine("Play again? Y/N:");
            string answer = Console.ReadLine();
            Console.WriteLine("Not implemented.");
            Console.ReadLine();
        }


    }
    class RollingDice
    {
        public int player_number;
        public int first;
        public int second;
        public int third;
        public int rollingCount = 0;

        public int round_count = 1;


        public void RollDice()
        {
            Random random = new Random();
            first = random.Next(1, 7);
            second = random.Next(1, 7);
            third = random.Next(1, 7);
            //Console.Write(first);
           // Console.Write(second);
            //Console.Write(third);
           // Console.Write("");
            rollingCount++;


        }



    }
}

The only problem is, everytime i run it the result always comes out as a draw, is there anyway to solve this so one of the players wins each time?

Thanks :)

Well,
First some things on my code:
I didn't think about a draw , in the end You should add else if and else for that.
Now Your code, looks ok , and I can't find the problem, one thing is that
(rd.round_count > 18 || rd2.round_count > 18)
seems that one of them won't reach 18 holes , You should make in the while loop , in the first if :
if (rd2.round_count == 18)
rd2.round_count = 18;
else
rd2.round_count++;

and than in the end of the loop if ((rd.round_count==18) && (rd2.round_count == 18))

but that's minor, well seems like both players have the same rolling count, cause they both in the same loop,so I think You should add a condition like if (rd.round_count != 18) and if (rd2.round_count != 18)
Before each rd.RollDice, maybe that will help, the problem is that each time 1 of them rolls a dice, the second one will as well, and YOu count how much times each one rolled which is exactly the same.
I don't really know how to work with classes, I would've done it without classes, Do You know to use arrays?
anyway, Man UTD were really good but lucky as well,ronaldo with that shot was amazing, but I'd really like to see Barca-Manchester in the finals :D
well good luck, I think I'll just reply tomorrow cause tomorrow I have a test so I need to study.

Well here is my take at this game take it or leave it. I added some colors to the program to make it look more eye friendly. But none the less this took me about 30 minutes to code as I just pasted my color class in their. Granted the code looks sloppy but just create an empty Console document and try it out. I liked how it turned out. The only thing that I've noticed is that Player 1 seems to always win. Not too sure why that is the case. Perhaps Ill add my own Random Number generator in their tomorrow. Well untill then I hope you like it. Adios


Also please let me know if you tried it and how you liked/disliked it as well.

-Poab9200(Jd_Logic)

using System;

namespace Dice_Golf_Game
{
    class Program
    {
        static void Main(string[] args)
        {
            Fg_Colors fg = new Fg_Colors();
            Random r = new Random();
            int nHoles1 = 1, nHoles2 = 1;
            int nRolls1 = 1, nRolls2 = 1;
            int nScore1 = 0, nScore2 = 0;
            decimal mAvg1, mAvg2;
            string sPlayer1, sPlayer2;
            Console.Title = "Dice Golf Game : Created By: Jd_Logic(Poab9200)";
            fg.Yellow();
            Console.WriteLine("Dice Golf Game : Created By: Jd_Logic(Poab9200)\n\n");
            fg.White();
            Console.Write("Enter a name for Player 1 : ");
            fg.Green();
            sPlayer1 = Console.ReadLine();
            fg.White();
            Console.Write("Enter a name for Player 2 : ");
            fg.Blue();
            sPlayer2 = Console.ReadLine();
            while (true)
            {
                fg.Yellow();
                Console.WriteLine("\n\nCurrent Hole : {0}", nHoles1);
                while (nHoles1 != 19)
                {
                    int nDice1 = r.Next(1, 7);
                    int nDice2 = r.Next(1, 7);
                    int nDice3 = r.Next(1, 7);
                    fg.White();
                    Console.WriteLine("Roll #{3}: {0} {1} {2}", nDice1, nDice2, nDice3, nRolls1);
                    if (nDice1 == nDice2 || nDice1 == nDice3 || nDice2 == nDice3)
                    {
                        nScore1 = nScore1 + nRolls1;
                        nHoles1++;
                        nRolls1 = 1;
                        if (nHoles1 == 19)
                        {
                            decimal mHoles = Convert.ToDecimal(nHoles1);
                            decimal mScore = Convert.ToDecimal(nScore1);
                            mAvg1 = mScore / mHoles;
                            fg.Cyan();
                            Console.WriteLine("\n\n          {0}' Score", sPlayer1);
                            fg.White();
                            Console.WriteLine("---------------------------------");
                            fg.Cyan();
                            Console.WriteLine("    Your Score Is : {0}", nScore1);
                            Console.WriteLine("  Your Average Is : {0:F3}", mAvg1);
                            fg.White();
                            Console.WriteLine("---------------------------------\n\n");
                            break;
                        }
                        else
                        {
                            fg.Yellow();
                            Console.WriteLine("\nCurrent Hole : {0}", nHoles1);
                        }
                    }
                    else
                    {
                        nRolls1++;
                    }
                }
                fg.Yellow();
                Console.WriteLine("\n\nCurrent Hole : {0}", nHoles2);
                while (nHoles2 != 19)
                {
                    int nDice1 = r.Next(1, 7);
                    int nDice2 = r.Next(1, 7);
                    int nDice3 = r.Next(1, 7);
                    fg.White();
                    Console.WriteLine("Roll #{3}: {0} {1} {2}", nDice1, nDice2, nDice3, nRolls2);
                    if (nDice1 == nDice2 || nDice1 == nDice3 || nDice2 == nDice3)
                    {
                        nScore2 = nScore2 + nRolls2;
                        nHoles2++;
                        nRolls2 = 2;
                        if (nHoles2 == 19)
                        {
                            decimal mHoles = Convert.ToDecimal(nHoles2);
                            decimal mScore = Convert.ToDecimal(nScore2);
                            mAvg2 = mScore / mHoles;
                            fg.Cyan();
                            Console.WriteLine("\n\n          {0}' Score", sPlayer2);
                            fg.White();
                            Console.WriteLine("---------------------------------");
                            fg.Cyan();
                            Console.WriteLine("    Your Score Is : {0}", nScore2);
                            Console.WriteLine("  Your Average Is : {0:F3}", mAvg2);
                            fg.White();
                            Console.WriteLine("---------------------------------");
                            break;
                        }
                        else
                        {
                            fg.Yellow();
                            Console.WriteLine("\nCurrent Hole : {0}", nHoles2);
                        }
                    }
                    else
                    {
                        nRolls2++;
                    }
                }
                decimal mHoles2 = Convert.ToDecimal(nHoles2);
                decimal mScore2 = Convert.ToDecimal(nScore2);
                mAvg2 = mScore2 / mHoles2;
                decimal mHoles1 = Convert.ToDecimal(nHoles1);
                decimal mScore1 = Convert.ToDecimal(nScore1);
                mAvg1 = mScore1 / mHoles1;
                fg.Cyan();
                Console.WriteLine("\n          Score Totals");
                Console.WriteLine("---------------------------------");
                fg.Green();
                Console.WriteLine("{1}'s Score   : {0}", nScore1, sPlayer1);
                Console.WriteLine("{1}'s Average : {0:F3}", mAvg1, sPlayer1);
                fg.Blue();
                Console.WriteLine("{1}'s Score   : {0}", nScore2, sPlayer2);
                Console.WriteLine("{1}'s Average : {0:F3}", mAvg2, sPlayer2);
                fg.White();
                Console.WriteLine("---------------------------------\n");
                if (mAvg1 < mAvg2)
                {
                    fg.Green();
                    Console.WriteLine("{0} has won the game.\n\n", sPlayer1);
                }
                else
                {
                    fg.Blue();
                    Console.WriteLine("{0} has won the game.\n\n", sPlayer2);
                }
                fg.Cyan();
                Console.WriteLine("Thank you for playing Jd_Logic's Dice Golf");
                fg.Gray();
                break;
            }
            Console.Read();
        }
    }
    class Fg_Colors
    {
        public void Black()
        {
            Console.ForegroundColor = ConsoleColor.Black;
        }
        public void Blue()
        {
            Console.ForegroundColor = ConsoleColor.Blue;
        }
        public void Cyan()
        {
            Console.ForegroundColor = ConsoleColor.Cyan;
        }
        public void DarkBlue()
        {
            Console.ForegroundColor = ConsoleColor.DarkBlue;
        }
        public void DarkCyan()
        {
            Console.ForegroundColor = ConsoleColor.DarkCyan;
        }
        public void DarkGrey()
        {
            Console.ForegroundColor = ConsoleColor.DarkGray;
        }
        public void DarkGreen()
        {
            Console.ForegroundColor = ConsoleColor.DarkGreen;
        }
        public void DarkMagenta()
        {
            Console.ForegroundColor = ConsoleColor.DarkMagenta;
        }
        public void DarkRed()
        {
            Console.ForegroundColor = ConsoleColor.DarkRed;
        }
        public void DarkYellow()
        {
            Console.ForegroundColor = ConsoleColor.DarkYellow;
        }
        public void Gray()
        {
            Console.ForegroundColor = ConsoleColor.Gray;
        }
        public void Green()
        {
            Console.ForegroundColor = ConsoleColor.Green;
        }
        public void Magenta()
        {
            Console.ForegroundColor = ConsoleColor.Magenta;
        }
        public void Red()
        {
            Console.ForegroundColor = ConsoleColor.Red;
        }
        public void White()
        {
            Console.ForegroundColor = ConsoleColor.White;
        }
        public void Yellow()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
        }
    }
}
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.