25412011first/Form1.cs
2026-04-15 21:56:49 +07:00

99 lines
2.9 KiB
C#

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}";
}
}
}