From 684befeb511028193de1cf75a7a9fe0d005fe312 Mon Sep 17 00:00:00 2001 From: jakteknikdotcom Date: Wed, 15 Apr 2026 21:56:49 +0700 Subject: [PATCH] add GuessTheDice Windows Forms app --- Form1.cs | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/Form1.cs b/Form1.cs index fbbea7f..9a69774 100644 --- a/Form1.cs +++ b/Form1.cs @@ -1,10 +1,99 @@ +using System; +using System.Windows.Forms; + namespace GuessTheDice { public partial class Form1 : Form { + private int score = 0; + + private Label lblTitle = null!; + private Label lblGuess = null!; + private Label lblResult = null!; + private Label lblScore = null!; + private NumericUpDown nudGuess = null!; + private Button btnRoll = null!; + public Form1() { InitializeComponent(); + BuildUI(); + } + + private void BuildUI() + { + this.Text = "Guess The Dice"; + this.Size = new System.Drawing.Size(400, 350); + this.StartPosition = FormStartPosition.CenterScreen; + + lblTitle = new Label() + { + Text = "🎲 Guess The Dice", + Font = new System.Drawing.Font("Arial", 16, System.Drawing.FontStyle.Bold), + TextAlign = System.Drawing.ContentAlignment.MiddleCenter, + Dock = DockStyle.Top, + Height = 60 + }; + + lblGuess = new Label() + { + Text = "Tebak angka dadu (1-6):", + Left = 80, Top = 80, Width = 200 + }; + + nudGuess = new NumericUpDown() + { + Minimum = 1, Maximum = 6, Value = 1, + Left = 80, Top = 105, Width = 80 + }; + + btnRoll = new Button() + { + Text = "Roll Dice!", + Left = 80, Top = 145, Width = 120, Height = 40, + Font = new System.Drawing.Font("Arial", 11) + }; + btnRoll.Click += BtnRoll_Click; + + lblResult = new Label() + { + Text = "", + Left = 80, Top = 200, Width = 280, Height = 40, + Font = new System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Bold) + }; + + lblScore = new Label() + { + Text = "Skor: 0", + Left = 80, Top = 245, Width = 200, + Font = new System.Drawing.Font("Arial", 11) + }; + + this.Controls.AddRange(new Control[] + { + lblTitle, lblGuess, nudGuess, btnRoll, lblResult, lblScore + }); + } + + private void BtnRoll_Click(object? sender, EventArgs e) + { + Random rng = new Random(); + int diceResult = rng.Next(1, 7); + int guess = (int)nudGuess.Value; + + if (guess == diceResult) + { + score++; + lblResult.Text = $"🎯 Benar! Dadu: {diceResult}"; + lblResult.ForeColor = System.Drawing.Color.Green; + } + else + { + lblResult.Text = $"❌ Salah! Dadu: {diceResult}"; + lblResult.ForeColor = System.Drawing.Color.Red; + } + + lblScore.Text = $"Skor: {score}"; } } } \ No newline at end of file