using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace WindowsApplication4 { /// /// Rock-Scissors-Paper Game. /// JL Popyack /// /// Demonstrates GUI tools and Event capture. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.RadioButton Paper_Button; private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.RichTextBox richTextBox1; private System.Windows.Forms.RadioButton Rock_Button; private System.Windows.Forms.RadioButton Scissors_Button; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; /// /// Programmer-supplied variables: /// private ArrayList weapon = new ArrayList() ; private Random RNG ; private int playerChoice ; private int computerChoice ; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // Further initialization (constructor code) // weapon.Add("None") ; weapon.Add("Rock") ; weapon.Add("Scissors") ; weapon.Add("Paper") ; RNG = new Random() ; playerChoice = 0 ; computerChoice = 0 ; } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.Rock_Button = new System.Windows.Forms.RadioButton(); this.Scissors_Button = new System.Windows.Forms.RadioButton(); this.Paper_Button = new System.Windows.Forms.RadioButton(); this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.richTextBox1 = new System.Windows.Forms.RichTextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.BackColor = System.Drawing.Color.Wheat; this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] { this.Paper_Button, this.Scissors_Button, this.Rock_Button}); this.groupBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.groupBox1.Location = new System.Drawing.Point(16, 32); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(208, 176); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Player\'s Choice:"; this.groupBox1.Enter += new System.EventHandler(this.groupBox1_Enter); // // Rock_Button // this.Rock_Button.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.Rock_Button.ForeColor = System.Drawing.Color.Maroon; this.Rock_Button.Location = new System.Drawing.Point(24, 40); this.Rock_Button.Name = "Rock_Button"; this.Rock_Button.TabIndex = 0; this.Rock_Button.Text = "Rock"; this.Rock_Button.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged); // // Scissors_Button // this.Scissors_Button.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.Scissors_Button.ForeColor = System.Drawing.Color.Maroon; this.Scissors_Button.Location = new System.Drawing.Point(24, 80); this.Scissors_Button.Name = "Scissors_Button"; this.Scissors_Button.TabIndex = 1; this.Scissors_Button.Text = "Scissors"; // // Paper_Button // this.Paper_Button.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.Paper_Button.ForeColor = System.Drawing.Color.Maroon; this.Paper_Button.Location = new System.Drawing.Point(24, 128); this.Paper_Button.Name = "Paper_Button"; this.Paper_Button.TabIndex = 2; this.Paper_Button.Text = "Paper"; // // button1 // this.button1.BackColor = System.Drawing.Color.Ivory; this.button1.Font = new System.Drawing.Font("Papyrus", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.button1.ForeColor = System.Drawing.Color.Maroon; this.button1.Location = new System.Drawing.Point(32, 224); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(128, 32); this.button1.TabIndex = 1; this.button1.Text = "Play Game!"; this.button1.Click += new System.EventHandler(this.button1_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(272, 56); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(176, 20); this.textBox1.TabIndex = 2; this.textBox1.Text = ""; this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged); // // richTextBox1 // this.richTextBox1.AutoSize = true; this.richTextBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.richTextBox1.Location = new System.Drawing.Point(272, 112); this.richTextBox1.Name = "richTextBox1"; this.richTextBox1.Size = new System.Drawing.Size(176, 96); this.richTextBox1.TabIndex = 3; this.richTextBox1.Text = ""; this.richTextBox1.TextChanged += new System.EventHandler(this.richTextBox1_TextChanged); // // label1 // this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.label1.Location = new System.Drawing.Point(272, 32); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(176, 16); this.label1.TabIndex = 4; this.label1.Text = "Computer\'s Choice:"; this.label1.Click += new System.EventHandler(this.label1_Click); // // label2 // this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.label2.Location = new System.Drawing.Point(272, 88); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(176, 16); this.label2.TabIndex = 5; this.label2.Text = "Result:"; this.label2.Click += new System.EventHandler(this.label2_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.BackColor = System.Drawing.Color.PaleGreen; this.ClientSize = new System.Drawing.Size(472, 270); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.label2, this.label1, this.richTextBox1, this.textBox1, this.button1, this.groupBox1}); this.Name = "Form1"; this.Text = "Rock-Scissors-Paper Game"; this.Load += new System.EventHandler(this.Form1_Load); this.groupBox1.ResumeLayout(false); this.ResumeLayout(false); } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new Form1()); } private void radioButton1_CheckedChanged(object sender, System.EventArgs e) { } private void groupBox1_Enter(object sender, System.EventArgs e) { } private void button1_Click(object sender, System.EventArgs e) { if( this.Rock_Button.Checked ) playerChoice = 1 ; else if( this.Scissors_Button.Checked ) playerChoice = 2 ; else if( this.Paper_Button.Checked ) playerChoice = 3 ; this.Rock_Button.Checked = false ; this.Scissors_Button.Checked = false ; this.Paper_Button.Checked = false ; computerChoice = RNG.Next(1,4) ; this.textBox1.Text = weapon[computerChoice].ToString() ; this.richTextBox1.Text = "You choose " + weapon[playerChoice].ToString() + "."; this.richTextBox1.Text += "\nI choose " + weapon[computerChoice].ToString() + "." ; if( computerChoice == playerChoice ) this.richTextBox1.Text += "\nWe tie." ; else if( (playerChoice + 3 - computerChoice)%3 == 1 ) // i.e., P=2,C=1 OR P=3,C=2 OR P=1,C=3 { // A Rich Text Box allows subtext to be selected // and formatted: this.richTextBox1.Text += "\nI win.\nBWAHAHAHAAA!!!" ; int startPos = richTextBox1.Find("win.") + 4 ; int endPos = richTextBox1.Find("!!!") + 2 ; this.richTextBox1.Select(startPos,endPos-startPos+1) ; this.richTextBox1.SelectionFont = new Font("Cooper Black", 12); this.richTextBox1.SelectionColor = Color.DarkRed; } else this.richTextBox1.Text += "\nYou win.\nWAAAAAHHHH!!!" ; } private void textBox1_TextChanged(object sender, System.EventArgs e) { } private void label1_Click(object sender, System.EventArgs e) { } private void label2_Click(object sender, System.EventArgs e) { } private void richTextBox1_TextChanged(object sender, System.EventArgs e) { } private void Form1_Load(object sender, System.EventArgs e) { } } }