Question

Can someone help me with this HW problem. It's a C# assignment.

Two-Up (10 points) Two-up is an old gambling game using two coins. Before the coins are tossed, the player would guess whethe

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Create C# windows application with the name TwoUpGame. Add the below code in respective files

Form1.Designer.cs

namespace TwoUpGame
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.lblHeader = new System.Windows.Forms.Label();
            this.coinPicture1 = new System.Windows.Forms.PictureBox();
            this.coinPicture2 = new System.Windows.Forms.PictureBox();
            this.grpGuess = new System.Windows.Forms.GroupBox();
            this.rdbtnObserve = new System.Windows.Forms.RadioButton();
            this.rdbtnReverse = new System.Windows.Forms.RadioButton();
            this.rdbtnEwan = new System.Windows.Forms.RadioButton();
            this.btnSpin = new System.Windows.Forms.Button();
            this.btnClear = new System.Windows.Forms.Button();
            this.btnExit = new System.Windows.Forms.Button();
            this.lblResult = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.coinPicture1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.coinPicture2)).BeginInit();
            this.grpGuess.SuspendLayout();
            this.SuspendLayout();
            //
            // lblHeader
            //
            this.lblHeader.AutoSize = true;
            this.lblHeader.Font = new System.Drawing.Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
                ((byte)(0)));
            this.lblHeader.Location = new System.Drawing.Point(171, 9);
            this.lblHeader.Name = "lblHeader";
            this.lblHeader.Size = new System.Drawing.Size(124, 31);
            this.lblHeader.TabIndex = 0;
            this.lblHeader.Text = "Two - Up";
            //
            // coinPicture1
            //
            this.coinPicture1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.coinPicture1.Location = new System.Drawing.Point(29, 57);
            this.coinPicture1.Name = "coinPicture1";
            this.coinPicture1.Size = new System.Drawing.Size(100, 100);
            this.coinPicture1.TabIndex = 1;
            this.coinPicture1.TabStop = false;
            //
            // coinPicture2
            //
            this.coinPicture2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.coinPicture2.Location = new System.Drawing.Point(177, 57);
            this.coinPicture2.Name = "coinPicture2";
            this.coinPicture2.Size = new System.Drawing.Size(100, 100);
            this.coinPicture2.TabIndex = 2;
            this.coinPicture2.TabStop = false;
            //
            // grpGuess
            //
            this.grpGuess.Controls.Add(this.rdbtnEwan);
            this.grpGuess.Controls.Add(this.rdbtnReverse);
            this.grpGuess.Controls.Add(this.rdbtnObserve);
            this.grpGuess.Location = new System.Drawing.Point(321, 57);
            this.grpGuess.Name = "grpGuess";
            this.grpGuess.Size = new System.Drawing.Size(174, 100);
            this.grpGuess.TabIndex = 3;
            this.grpGuess.TabStop = false;
            this.grpGuess.Text = "Guess";
            //
            // rdbtnObserve
            //
            this.rdbtnObserve.AutoSize = true;
            this.rdbtnObserve.Location = new System.Drawing.Point(11, 19);
            this.rdbtnObserve.Name = "rdbtnObserve";
            this.rdbtnObserve.Size = new System.Drawing.Size(127, 17);
            this.rdbtnObserve.TabIndex = 0;
            this.rdbtnObserve.Text = "Observe (both heads)";
            this.rdbtnObserve.UseVisualStyleBackColor = true;
            //
            // rdbtnReverse
            //
            this.rdbtnReverse.AutoSize = true;
            this.rdbtnReverse.Location = new System.Drawing.Point(11, 47);
            this.rdbtnReverse.Name = "rdbtnReverse";
            this.rdbtnReverse.Size = new System.Drawing.Size(116, 17);
            this.rdbtnReverse.TabIndex = 1;
            this.rdbtnReverse.Text = "Reverse (both tails)";
            this.rdbtnReverse.UseVisualStyleBackColor = true;
            //
            // rdbtnEwan
            //
            this.rdbtnEwan.AutoSize = true;
            this.rdbtnEwan.Location = new System.Drawing.Point(11, 77);
            this.rdbtnEwan.Name = "rdbtnEwan";
            this.rdbtnEwan.Size = new System.Drawing.Size(146, 17);
            this.rdbtnEwan.TabIndex = 2;
            this.rdbtnEwan.Text = "Ewan (one head, one tail)";
            this.rdbtnEwan.UseVisualStyleBackColor = true;
            //
            // btnSpin
            //
            this.btnSpin.Location = new System.Drawing.Point(29, 196);
            this.btnSpin.Name = "btnSpin";
            this.btnSpin.Size = new System.Drawing.Size(75, 35);
            this.btnSpin.TabIndex = 4;
            this.btnSpin.Text = "Spin";
            this.btnSpin.UseVisualStyleBackColor = true;
            this.btnSpin.Click += new System.EventHandler(this.btnSpin_Click);
            //
            // btnClear
            //
            this.btnClear.Location = new System.Drawing.Point(220, 196);
            this.btnClear.Name = "btnClear";
            this.btnClear.Size = new System.Drawing.Size(75, 35);
            this.btnClear.TabIndex = 5;
            this.btnClear.Text = "Clear";
            this.btnClear.UseVisualStyleBackColor = true;
            this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
            //
            // btnExit
            //
            this.btnExit.Location = new System.Drawing.Point(420, 196);
            this.btnExit.Name = "btnExit";
            this.btnExit.Size = new System.Drawing.Size(75, 35);
            this.btnExit.TabIndex = 6;
            this.btnExit.Text = "Exit";
            this.btnExit.UseVisualStyleBackColor = true;
            this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
            //
            // lblResult
            //
            this.lblResult.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lblResult.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblResult.Location = new System.Drawing.Point(26, 275);
            this.lblResult.Name = "lblResult";
            this.lblResult.Size = new System.Drawing.Size(469, 30);
            this.lblResult.TabIndex = 7;
            this.lblResult.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(529, 342);
            this.Controls.Add(this.lblResult);
            this.Controls.Add(this.btnExit);
            this.Controls.Add(this.btnClear);
            this.Controls.Add(this.btnSpin);
            this.Controls.Add(this.grpGuess);
            this.Controls.Add(this.coinPicture2);
            this.Controls.Add(this.coinPicture1);
            this.Controls.Add(this.lblHeader);
            this.Name = "Form1";
            this.Text = "Two-Up";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.coinPicture1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.coinPicture2)).EndInit();
            this.grpGuess.ResumeLayout(false);
            this.grpGuess.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label lblHeader;
        private System.Windows.Forms.PictureBox coinPicture1;
        private System.Windows.Forms.PictureBox coinPicture2;
        private System.Windows.Forms.GroupBox grpGuess;
        private System.Windows.Forms.RadioButton rdbtnEwan;
        private System.Windows.Forms.RadioButton rdbtnReverse;
        private System.Windows.Forms.RadioButton rdbtnObserve;
        private System.Windows.Forms.Button btnSpin;
        private System.Windows.Forms.Button btnClear;
        private System.Windows.Forms.Button btnExit;
        private System.Windows.Forms.Label lblResult;
    }
}

Form1.cs

using System;
using System.Drawing;
using System.Windows.Forms;

namespace TwoUpGame
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lblResult.Visible = false;
        }

        private void btnSpin_Click(object sender, EventArgs e)
        {
            if(rdbtnObserve.Checked || rdbtnReverse.Checked || rdbtnEwan.Checked)
            {
                Random random = new Random();
                //generate two random numbers for two coins
                int r1 = random.Next(2);
                int r2 = random.Next(2);
                //display the images for each number generates
                if (r1 == 0)
                    //replace path with path of your image
                    coinPicture1.Image = Image.FromFile(@"E:\DotNet\TwoUpGame\TwoUpGame\images\head.png");
                else
                    //replace path with path of your image
                    coinPicture1.Image = Image.FromFile(@"E:\DotNet\TwoUpGame\TwoUpGame\images\tail.png");
                if (r2 == 0)
                    //replace path with path of your image
                    coinPicture2.Image = Image.FromFile(@"E:\DotNet\TwoUpGame\TwoUpGame\images\head.png");
                else
                    //replace path with path of your image
                    coinPicture2.Image = Image.FromFile(@"E:\DotNet\TwoUpGame\TwoUpGame\images\tail.png");
                //if the guess is Observe
                if (rdbtnObserve.Checked)
                {
                    //print win message if both r1 and r2 are 0
                    if(r1 == 0 && r2 == 0)
                    {
                        lblResult.Text = "Congratulations - You win";
                        lblResult.Visible = true;
                    }
                    //else print lose message
                    else
                    {
                        lblResult.Text = "Sorry - You lose";
                        lblResult.Visible = true;
                    }
                }
                //if the guess is Reverse
                else if (rdbtnReverse.Checked)
                {
                    //print win message if both r1 and r2 are 1
                    if (r1 == 1 && r2 == 1)
                    {
                        lblResult.Text = "Congratulations - You win";
                        lblResult.Visible = true;
                    }
                    //else print lose message
                    else
                    {
                        lblResult.Text = "Sorry - You lose";
                        lblResult.Visible = true;
                    }
                }
                //if guess is Edwan
                else
                {
                    //print win message if both r1 and r2 are different
                    if (r1 != r2)
                    {
                        lblResult.Text = "Congratulations - You win";
                        lblResult.Visible = true;
                    }
                    //else print lose message
                    else
                    {
                        lblResult.Text = "Sorry - You lose";
                        lblResult.Visible = true;
                    }
                }
            }
            //display error message if spin is clicked without making any guess
            else
            {
                lblResult.Text = "You must guess before spinning";
                lblResult.Visible = true;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            //clear all the controls
            coinPicture1.Image = null;
            coinPicture2.Image = null;
            lblResult.Visible = false;
            rdbtnObserve.Checked = false;
            rdbtnReverse.Checked = false;
            rdbtnEwan.Checked = false;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            //close the application
            this.Close();
        }
    }
}

Output:

lwo-Up Two - Up Guess O Observe (both heads) O Reverse (both tails) O Ewan (one head, one tail) Spin Exit Clear You must gueslwo-Up Two - Up Guess Observe(both heads) O Reverse (both tails) O Ewan (one head, one tail) Spin Exit Clear Sorry - You loselwo-Up Two - Up Guess Observe(both heads) O Reverse (both tails) O Ewan (one head, one tail) Spin Exit Clear Congratulations

Images used:

Program Screenshots:

Form1.Designer.cs x TwoUpGame Form1.cs Form1.cs [Design] TwoUpGame.Form1 Dispose(bool disposing) pace TwoUpGame names 3 referFormi.Designer.cs Form1.cs Form1.cs [Design 1 reference private void InitializeComponent () this.lblHeader new System.WindowsForm1.cs [Design Form1.Designer.cs x Form1.cs TwoUpGame.Form1 zeComponent0 圈TwoupGame 57 // coinPicture1 58 59 this.coinPictuForm1.Designer.cs x CH TwoUpGame Form1.cs TwoUpGame.Form1 87 // rdbtnObserve 90 this.rdbtnObserve.AutoSize true; this.rdbtnobForm1.cs [Design -InitializeComponent0 // btnSpin 118 119 this.btnSpin.Location new System.Drawing.Point (29, 196); this.btnSForm1.cs [Design] Form1.cs InitializeComponent0 Form1.Designer.cs х TwoUpGame. Form 1 147 // 1blResult this.lblResult.BorderSForm1.cs Form1.Designer.csX Form1.cs [Design] -Ί Q. Initial!zeComponent0 this.Controls.Add(this.coinPicture1); this.Controls.Form1.Desiqner.cs Form1.cs » x Form1.cs [Design TwoUpGame.Form TwoUpGame Form10 using System using System.Drawing using SysteForm1.cs IDesianl Form1.Designer.cs Form1.cs a x Form10 TwoUpGame /Idisplay the images for each number generates if (ri - e)Form1.Designer.cs Forml.cs ㄨ Form1.cs [Design] Form1 Load(object sender, EventArgs e //if the guess is Reverse else if (rdbtnForm1.Designer.cs Form1.cs X Form1.cs [Design] -Form1 Load(object sender, EventArgs e) TwoUpGame.Form1 87 //display error mes

Add a comment
Know the answer?
Add Answer to:
Can someone help me with this HW problem. It's a C# assignment. Two-Up (10 points) Two-up is an old gambling game u...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT