Order Now  +1 678 648 4277 

Yahtzee Game Assignment Solution using C#

Question:

You will create a simple version of the dice game Yahtzee that will be played by a single player. The game involves rolling 5 dice to complete all the challenges like 3 of a kind, 4 of a kind, 5 of a kind - Yahtzee, full house, straight, etc… The program must allow the user to click a button to roll the dice, see the outcome for each die using PictureBoxes (one PictureBox for each die), and allow them to select which challenge the roll will be applied. The game is over when the user completes all the challenges or decides to quit. The scoring of points for the game will be based on the number of rolls it takes to complete all the challenges to end the game and the points for each type of challenge. The form should also display each challenge and whether it has been completed or not. If the user chooses a challenge and the roll doesn’t qualify for the challenge, the game should not allow the roll to be applied to the challenge.

You must create a class that represents the game, its data, and methods to manage all game operations. When you write this class, keep in mind that this class should be written so that it can be used in any other program regardless of the user interface. This means you shouldn’t rely on specific GUI controls in this class. The class should not write output to the Form controls, nor should it use these Form controls to get input. If the class’s object requires inputs, they are delivered to the object through input parameters to methods or properties. If the object needs to deliver output, this should be done through return statements of methods or properties.

You need to properly name all your controls, variables, the class, and all other elements regarding this project. See the lecture slides on Coding Style. Also, you need to put thought into the user interface design, so it can be used by anyone with little effort. This means that there should be clear instructions on how to use the application.

Solution:

 

    WinExe

    net5.0-windows

    Dice_Game

    true

 

 

   

      True

      True

      Resources.resx

   

 

 

   

      ResXFileCodeGenerator

      Resources.Designer.cs

   

 

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Dice_Game { public partial class Form1 : Form { int roll = 3; int turn= 1; int[] dices= new int [5]; diceClass dc = new diceClass(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { roll--; btnScore.Enabled = true; Random r = new Random(); int rr; string dir = Directory.GetCurrentDirectory() + "\\images\\"; string s; rr=r.Next(6) + 1; dices[0] = rr; s = dir+ rr+ ".jpg"; pictureBox1.ImageLocation = s; rr = r.Next(6) + 1; dices[1] = rr; s = dir + rr + ".jpg"; pictureBox2.ImageLocation = s; rr = r.Next(6) + 1; dices[2] = rr; s = dir + rr + ".jpg"; pictureBox3.ImageLocation = s; rr = r.Next(6) + 1; dices[3] = rr; s = dir + rr + ".jpg"; pictureBox4.ImageLocation = s; rr = r.Next(6) + 1; dices[4] = rr; s = dir + rr + ".jpg"; pictureBox5.ImageLocation = s; if (roll == 0) { button1.Enabled = false; } lblRoll.Text = "Roll: "+roll; lblTurn.Text = "Turn: "+turn; } private void label8_Click(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { fillLabels(); comboBox1.SelectedIndex = 0; } private void fillLabels() { lblUpper.Text = String.Format("Aces: \n Twos: \n Threes: \n Fours \n Fives \n Sixes"); lblLower.Text = String.Format("Three of a Kind: \n Four of a Kind: \n Full House: \n Small Straight \n Large Straight \n Yahtzee \n Chance \n"); } public string ff(int i) { if (i >= 0) { return i.ToString(); } return ""; } private void btnScore_Click(object sender, EventArgs e) { int p = -1; if (comboBox1.SelectedIndex == 0) { p = dc.AcesCalc(dices); } else if (comboBox1.SelectedIndex == 1) { p = dc.TwosCalc(dices); } else if (comboBox1.SelectedIndex == 2) { p = dc.ThreesCalc(dices); } else if (comboBox1.SelectedIndex == 3) { p = dc.FoursCalc(dices); } else if (comboBox1.SelectedIndex == 4) { p = dc.FivesCalc(dices); } else if (comboBox1.SelectedIndex == 5) { p = dc.SixesCalc(dices); } else if (comboBox1.SelectedIndex == 6) { p = dc.ThreeOfaKindCalc(dices); } else if (comboBox1.SelectedIndex == 7) { p = dc.FourOfaKindCalc(dices); } else if (comboBox1.SelectedIndex == 8) { p = dc.FullHouseCalc(dices); } else if (comboBox1.SelectedIndex == 9) { p = dc.SmallStraint(dices); } else if (comboBox1.SelectedIndex == 10) { p = dc.LargeStraint(dices); } else if (comboBox1.SelectedIndex == 11) { p = dc.YahtzeeCalc(dices); } else if (comboBox1.SelectedIndex == 12) { p = dc.ChanceCalc(dices); } else if (comboBox1.SelectedIndex == 13) { p = dc.ThreeOfaKindCalc(dices); } if (p >= 0) { button1.Enabled = true; roll = 3; turn++; lblRoll.Text = "Roll: " + roll; lblTurn.Text = "Turn: " + turn; btnScore.Enabled = false; if (dc.finishCheck()) { MessageBox.Show("You Finished The game"); button1.Enabled = false; btnScore.Enabled = false; } } else { MessageBox.Show("You can not go with this category, try something else"); } lblTotalScore.Text = "Your point is: " + dc.point; lblUpper.Text = String.Format("Aces:{0} \n Twos:{1} \n Threes: {2} \n Fours {3} \n Fives {4}\n Sixes {5}" , ff(dc.Aces), ff(dc.Twos), ff(dc.Threes), ff(dc.Fours), ff(dc.Fives), ff(dc.Sixes)); lblLower.Text = String.Format("Three of a Kind:{0} \n Four of a Kind:{1} \n Full House:{2} \n Small Straight {3}\n Large Straight:{4} \n Yahtzee: {5} \n Chance: {6} \n" , ff(dc.ThreeOfaKind), ff(dc.FourOfaKind), ff(dc.FullHouse), ff(dc.SmallStraight), ff(dc.LargeStraight), ff(dc.Yahtzee), ff(dc.Chance)); } } } public class diceClass { public int point = 0; public bool finishStatus = false; //upper section public int Aces = -1; public int Twos = -1; public int Threes = -1; public int Fours = -1; public int Fives = -1; public int Sixes = -1; public int TotalUpperScore = 0; public int BONUS = 0; //lower section public int ThreeOfaKind = -1; public int FourOfaKind = -1; public int FullHouse = -1; //2 sixes: score 25 public int SmallStraight = -1; //sequence of 4 ; score 30 public int LargeStraight = -1; //sequence of 4; score 40 public int Yahtzee = -1; //first 50 others 100 public int Chance = -1; //any combiniation public int AcesCalc(int[] dd) { if (Aces >= 0) { return -1; } int p = 0; for (int i = 0; i < dd.Length; i++) { if (dd[i] == 1) { p += dd[i]; } } this.Aces = p; this.point += p; return p; } public int TwosCalc(int[] dd) { if (Twos >= 0) { return -1; } int p = 0; for (int i = 0; i < dd.Length; i++) { if (dd[i] == 2) { p += dd[i]; } } this.Twos = p; this.point += p; return p; } public int ThreesCalc(int[] dd) { if (Threes >= 0) { return -1; } int p = 0; for (int i = 0; i < dd.Length; i++) { if (dd[i] == 3) { p += dd[i]; } } this.Threes = p; this.point += p; return p; } public int FoursCalc(int[] dd) { if (Fours >= 0) { return -1; } int p = 0; for (int i = 0; i < dd.Length; i++) { if (dd[i] == 4) { p += dd[i]; } } this.Fours = p; this.point += p; return p; } public int FivesCalc(int[] dd) { if (Fives >= 0) { return -1; } int p = 0; for (int i = 0; i < dd.Length; i++) { if (dd[i] == 5) { p += dd[i]; } } this.Fives = p; this.point += p; return p; } public int SixesCalc(int[] dd) { if (Sixes >= 0) { return -1; } int p = 0; for (int i = 0; i < dd.Length; i++) { if (dd[i] == 6) { p += dd[i]; } } this.Sixes = p; this.point += p; return p; } public int ChanceCalc(int[] dd) { if (Chance >= 0) { return -1; } int p = 0; for (int i = 0; i < dd.Length; i++) { this.Chance += dd[i]; } this.point += this.Chance; return this.Chance; } public int ThreeOfaKindCalc(int[] dd) { if (ThreeOfaKind >= 0) { return -1; } var c1 = Array.FindAll(dd, s => s.Equals(1)); var c2 = Array.FindAll(dd, s => s.Equals(2)); var c3 = Array.FindAll(dd, s => s.Equals(3)); var c4 = Array.FindAll(dd, s => s.Equals(4)); var c5 = Array.FindAll(dd, s => s.Equals(5)); var c6 = Array.FindAll(dd, s => s.Equals(6)); List ar = new List(); ar.Add(new arrays(c1)); ar.Add(new arrays(c2)); ar.Add(new arrays(c3)); ar.Add(new arrays(c4)); ar.Add(new arrays(c5)); ar.Add(new arrays(c6)); for (int i = 0; i < ar.Count; i++) { if (ar[i].ar.Length >= 3) { for (int j = 0; j < ar[i].ar.Length; j++) { this.ThreeOfaKind += ar[i].ar[j]; } this.point += this.ThreeOfaKind; return this.ThreeOfaKind; } } return -1; } public int FourOfaKindCalc(int[] dd) { if (FourOfaKind >= 0) { return -1; } var c1 = Array.FindAll(dd, s => s.Equals(1)); var c2 = Array.FindAll(dd, s => s.Equals(2)); var c3 = Array.FindAll(dd, s => s.Equals(3)); var c4 = Array.FindAll(dd, s => s.Equals(4)); var c5 = Array.FindAll(dd, s => s.Equals(5)); var c6 = Array.FindAll(dd, s => s.Equals(6)); List ar = new List(); ar.Add(new arrays(c1)); ar.Add(new arrays(c2)); ar.Add(new arrays(c3)); ar.Add(new arrays(c4)); ar.Add(new arrays(c5)); ar.Add(new arrays(c6)); for (int i = 0; i < ar.Count; i++) { if (ar[i].ar.Length >= 4) { for (int j = 0; j < ar[i].ar.Length; j++) { this.FourOfaKind += ar[i].ar[j]; } this.point += this.FourOfaKind; return this.FourOfaKind; } } return -1; } public int YahtzeeCalc(int[] dd) { var c1 = Array.FindAll(dd, s => s.Equals(1)); var c2 = Array.FindAll(dd, s => s.Equals(2)); var c3 = Array.FindAll(dd, s => s.Equals(3)); var c4 = Array.FindAll(dd, s => s.Equals(4)); var c5 = Array.FindAll(dd, s => s.Equals(5)); var c6 = Array.FindAll(dd, s => s.Equals(6)); List ar = new List(); ar.Add(new arrays(c1)); ar.Add(new arrays(c2)); ar.Add(new arrays(c3)); ar.Add(new arrays(c4)); ar.Add(new arrays(c5)); ar.Add(new arrays(c6)); for (int i = 0; i < ar.Count; i++) { if (ar[i].ar.Length >= 5) { if (Yahtzee == 0) { this.Yahtzee = 50; this.point += this.Yahtzee; return this.Yahtzee; } else if (Yahtzee > 0) { this.Yahtzee = 100; this.point += this.Yahtzee; return this.Yahtzee; } } } return -1; } public int FullHouseCalc(int[] dd) { if (FullHouse >= 0) { return -1; } var c1 = Array.FindAll(dd, s => s.Equals(1)); var c2 = Array.FindAll(dd, s => s.Equals(2)); var c3 = Array.FindAll(dd, s => s.Equals(3)); var c4 = Array.FindAll(dd, s => s.Equals(4)); var c5 = Array.FindAll(dd, s => s.Equals(5)); var c6 = Array.FindAll(dd, s => s.Equals(6)); List ar = new List(); ar.Add(new arrays(c1)); ar.Add(new arrays(c2)); ar.Add(new arrays(c3)); ar.Add(new arrays(c4)); ar.Add(new arrays(c5)); ar.Add(new arrays(c6)); int i3 = -1, i2 = -1; for (int i = 0; i < ar.Count; i++) { if (ar[i].ar.Length == 3) { i3 = i; } else if (ar[i].ar.Length == 2) { i2 = i; } } if (i3 > 0 && i2 > 0) { this.FullHouse = 25; this.point += this.FullHouse; return this.FullHouse; } return -1; } public int LargeStraint(int[] dd) { if (LargeStraight >= 0) { return -1; } List ls = new List(); for (int i = 0; i < dd.Length; i++) { if (!ls.Contains(dd[i])) { ls.Add(dd[i]); } } if (ls.Contains(1) && ls.Contains(2) && ls.Contains(3) && ls.Contains(4) && ls.Contains(5)) { this.LargeStraight = 30; this.point += this.LargeStraight; return this.LargeStraight; } else if (ls.Contains(5) && ls.Contains(2) && ls.Contains(3) && ls.Contains(4) && ls.Contains(6)) { this.LargeStraight = 30; this.point += this.LargeStraight; return this.LargeStraight; } return -1; } public int SmallStraint(int[] dd) { if (SmallStraight >= 0) { return -1; } int f1 = 0; List ls = new List(); for (int i = 0; i < dd.Length; i++) { if (!ls.Contains(dd[i])) { ls.Add(dd[i]); } } if (ls.Contains(1) && ls.Contains(2) && ls.Contains(3) && ls.Contains(4)) { this.SmallStraight = 30; this.point += this.SmallStraight; return this.SmallStraight; } else if (ls.Contains(5) && ls.Contains(2) && ls.Contains(3) && ls.Contains(4)) { this.SmallStraight = 30; this.point += this.SmallStraight; return this.SmallStraight; } else if (ls.Contains(6) && ls.Contains(5) && ls.Contains(3) && ls.Contains(4)) { this.SmallStraight = 30; this.point += this.SmallStraight; return this.SmallStraight; } return -1; } public bool finishCheck() { if (Aces>=0 && Twos >= 0 && Threes >= 0 && Fours >= 0 && Fives >= 0 && Sixes >= 0 &&ThreeOfaKind >= 0 &&FourOfaKind >= 0 &&FullHouse >= 0 &&SmallStraight >= 0 &&LargeStraight >= 0 &&Chance >= 0) { this.finishStatus = true; } return this.finishStatus; } } class arrays { public int[] ar; public arrays(int[] arr) { this.ar = arr; } }

Output:

Yahtzee 1

Yahtzee 2