1 using System; 2 using System.Drawing; 3 using System.Collections; 4 using System.ComponentModel; 5 using System.Windows.Forms; 6 using System.Data; 7 8 namespace hiloguess 9 { 10 /// 11 /// Summary description for Form1. 12 13 // Create a higher/lower guessing game using a graphical 14 // user interface. Allow a user to keep guessing until he 15 // guesses the number. Choose two colors for your game: 16 // one should be used to indicate that the value the user 17 // guessed is higher than the target; the other is used to 18 // indicate that the value the user guessed is lower than 19 // the target. With each new guess, change the form color 20 // based on whether the guess is higher than the target or 21 // lower. Keep a count of the number of guesses. when the 22 // the user hits the target, display a MessageBox indicating 23 // the number of guesses it took. Several approaches can be 24 // used to seed the target: one is to generate a random 25 // number by constructing an object of the Random class. 26 // For example, the following stores a random whole number 27 // between 0 and 100 in target: 28 // Random r = new Random(); 29 // int target = r.Next(0, 100); 30 // Turn in your source code for this program. 31 32 /// 33 34 public class Form1 : System.Windows.Forms.Form 35 { 36 private System.Windows.Forms.Label label1; 37 private System.Windows.Forms.Button btnNewGame; 38 private System.Windows.Forms.Button btnExit; 39 private System.Windows.Forms.Button btnGuess; 40 private System.Windows.Forms.TextBox txtGuessNo; 41 private System.Windows.Forms.TextBox txtToGuessNo; 42 private System.Windows.Forms.TextBox txtState; 43 private System.Windows.Forms.TextBox txtResponse; 44 private System.Windows.Forms.TextBox txtGuess; 45 private System.Windows.Forms.ListBox lstResponse; 46 private System.Windows.Forms.Label lblHdr1; 47 private System.Windows.Forms.Label lblHdr2; 48 private System.Windows.Forms.Label lblHdr3; 49 private System.Windows.Forms.PictureBox pictureBox1; 50 /// 51 /// Required designer variable. 52 /// 53 private System.ComponentModel.Container components = null; 54 55 public Form1() 56 { 57 // 58 // Required for Windows Form Designer support 59 // 60 InitializeComponent(); 61 62 // 63 // TODO: Add any constructor code after InitializeComponent call 64 // 65 newGame(); 66 } 67 68 /// 69 /// Clean up any resources being used. 70 /// 71 protected override void Dispose( bool disposing ) 72 { 73 if( disposing ) 74 { 75 if (components != null) 76 { 77 components.Dispose(); 78 } 79 } 80 base.Dispose( disposing ); 81 } 82 83 #region Windows Form Designer generated code 84 /// 85 /// Required method for Designer support - do not modify 86 /// the contents of this method with the code editor. 87 /// 88 private void InitializeComponent() 89 { 90 this.label1 = new System.Windows.Forms.Label(); 91 this.txtGuess = new System.Windows.Forms.TextBox(); 92 this.btnGuess = new System.Windows.Forms.Button(); 93 this.btnNewGame = new System.Windows.Forms.Button(); 94 this.btnExit = new System.Windows.Forms.Button(); 95 this.txtGuessNo = new System.Windows.Forms.TextBox(); 96 this.txtToGuessNo = new System.Windows.Forms.TextBox(); 97 this.txtState = new System.Windows.Forms.TextBox(); 98 this.txtResponse = new System.Windows.Forms.TextBox(); 99 this.lstResponse = new System.Windows.Forms.ListBox(); 100 this.lblHdr1 = new System.Windows.Forms.Label(); 101 this.lblHdr2 = new System.Windows.Forms.Label(); 102 this.lblHdr3 = new System.Windows.Forms.Label(); 103 this.pictureBox1 = new System.Windows.Forms.PictureBox(); 104 this.SuspendLayout(); 105 // 106 // label1 107 // 108 this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); 109 this.label1.Location = new System.Drawing.Point(8, 16); 110 this.label1.Name = "label1"; 111 this.label1.Size = new System.Drawing.Size(208, 24); 112 this.label1.TabIndex = 0; 113 this.label1.Text = "Enter a Guess (1 to 100)"; 114 this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; 115 // 116 // txtGuess 117 // 118 this.txtGuess.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); 119 this.txtGuess.Location = new System.Drawing.Point(216, 16); 120 this.txtGuess.MaxLength = 3; 121 this.txtGuess.Name = "txtGuess"; 122 this.txtGuess.Size = new System.Drawing.Size(40, 26); 123 this.txtGuess.TabIndex = 1; 124 this.txtGuess.Text = "50"; 125 this.txtGuess.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; 126 // 127 // btnGuess 128 // 129 this.btnGuess.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); 130 this.btnGuess.Location = new System.Drawing.Point(272, 16); 131 this.btnGuess.Name = "btnGuess"; 132 this.btnGuess.Size = new System.Drawing.Size(96, 24); 133 this.btnGuess.TabIndex = 2; 134 this.btnGuess.Text = "Guess"; 135 this.btnGuess.Click += new System.EventHandler(this.btnGuess_Click); 136 // 137 // btnNewGame 138 // 139 this.btnNewGame.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); 140 this.btnNewGame.Location = new System.Drawing.Point(16, 280); 141 this.btnNewGame.Name = "btnNewGame"; 142 this.btnNewGame.Size = new System.Drawing.Size(128, 24); 143 this.btnNewGame.TabIndex = 4; 144 this.btnNewGame.Text = "New Game"; 145 this.btnNewGame.Click += new System.EventHandler(this.btnNewGame_Click); 146 // 147 // btnExit 148 // 149 this.btnExit.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); 150 this.btnExit.Location = new System.Drawing.Point(152, 280); 151 this.btnExit.Name = "btnExit"; 152 this.btnExit.Size = new System.Drawing.Size(96, 24); 153 this.btnExit.TabIndex = 5; 154 this.btnExit.Text = "Exit"; 155 this.btnExit.Click += new System.EventHandler(this.btnExit_Click); 156 // 157 // txtGuessNo 158 // 159 this.txtGuessNo.Location = new System.Drawing.Point(296, 280); 160 this.txtGuessNo.MaxLength = 5; 161 this.txtGuessNo.Name = "txtGuessNo"; 162 this.txtGuessNo.ReadOnly = true; 163 this.txtGuessNo.Size = new System.Drawing.Size(32, 20); 164 this.txtGuessNo.TabIndex = 6; 165 this.txtGuessNo.TabStop = false; 166 this.txtGuessNo.Text = "0"; 167 this.txtGuessNo.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; 168 // 169 // txtToGuessNo 170 // 171 this.txtToGuessNo.Location = new System.Drawing.Point(336, 280); 172 this.txtToGuessNo.MaxLength = 5; 173 this.txtToGuessNo.Name = "txtToGuessNo"; 174 this.txtToGuessNo.ReadOnly = true; 175 this.txtToGuessNo.Size = new System.Drawing.Size(32, 20); 176 this.txtToGuessNo.TabIndex = 7; 177 this.txtToGuessNo.TabStop = false; 178 this.txtToGuessNo.Text = "0"; 179 this.txtToGuessNo.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; 180 // 181 // txtState 182 // 183 this.txtState.Location = new System.Drawing.Point(256, 280); 184 this.txtState.MaxLength = 5; 185 this.txtState.Name = "txtState"; 186 this.txtState.ReadOnly = true; 187 this.txtState.Size = new System.Drawing.Size(32, 20); 188 this.txtState.TabIndex = 8; 189 this.txtState.TabStop = false; 190 this.txtState.Text = "Done"; 191 this.txtState.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; 192 // 193 // txtResponse 194 // 195 this.txtResponse.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); 196 this.txtResponse.Location = new System.Drawing.Point(16, 232); 197 this.txtResponse.MaxLength = 100; 198 this.txtResponse.Name = "txtResponse"; 199 this.txtResponse.Size = new System.Drawing.Size(352, 26); 200 this.txtResponse.TabIndex = 9; 201 this.txtResponse.TabStop = false; 202 this.txtResponse.Text = ""; 203 // 204 // lstResponse 205 // 206 this.lstResponse.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); 207 this.lstResponse.ItemHeight = 20; 208 this.lstResponse.Items.AddRange(new object[] { 209 ""}); 210 this.lstResponse.Location = new System.Drawing.Point(16, 48); 211 this.lstResponse.Name = "lstResponse"; 212 this.lstResponse.ScrollAlwaysVisible = true; 213 this.lstResponse.Size = new System.Drawing.Size(352, 184); 214 this.lstResponse.TabIndex = 10; 215 this.lstResponse.TabStop = false; 216 // 217 // lblHdr1 218 // 219 this.lblHdr1.Location = new System.Drawing.Point(256, 264); 220 this.lblHdr1.Name = "lblHdr1"; 221 this.lblHdr1.Size = new System.Drawing.Size(32, 16); 222 this.lblHdr1.TabIndex = 11; 223 this.lblHdr1.Text = "State"; 224 // 225 // lblHdr2 226 // 227 this.lblHdr2.Location = new System.Drawing.Point(296, 264); 228 this.lblHdr2.Name = "lblHdr2"; 229 this.lblHdr2.Size = new System.Drawing.Size(40, 16); 230 this.lblHdr2.TabIndex = 12; 231 this.lblHdr2.Text = "Guess"; 232 // 233 // lblHdr3 234 // 235 this.lblHdr3.Location = new System.Drawing.Point(340, 264); 236 this.lblHdr3.Name = "lblHdr3"; 237 this.lblHdr3.Size = new System.Drawing.Size(24, 16); 238 this.lblHdr3.TabIndex = 13; 239 this.lblHdr3.Text = "Ans"; 240 // 241 // pictureBox1 242 // 243 this.pictureBox1.Location = new System.Drawing.Point(0, 0); 244 this.pictureBox1.Name = "pictureBox1"; 245 this.pictureBox1.Size = new System.Drawing.Size(392, 320); 246 this.pictureBox1.TabIndex = 14; 247 this.pictureBox1.TabStop = false; 248 // 249 // Form1 250 // 251 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 252 this.ClientSize = new System.Drawing.Size(392, 318); 253 this.Controls.Add(this.lblHdr3); 254 this.Controls.Add(this.lblHdr2); 255 this.Controls.Add(this.lblHdr1); 256 this.Controls.Add(this.lstResponse); 257 this.Controls.Add(this.txtResponse); 258 this.Controls.Add(this.txtState); 259 this.Controls.Add(this.txtToGuessNo); 260 this.Controls.Add(this.txtGuessNo); 261 this.Controls.Add(this.btnExit); 262 this.Controls.Add(this.btnNewGame); 263 this.Controls.Add(this.btnGuess); 264 this.Controls.Add(this.txtGuess); 265 this.Controls.Add(this.label1); 266 this.Controls.Add(this.pictureBox1); 267 this.Name = "Form1"; 268 this.Text = "Hi-Lo Guessing Game -- by Pat Moss"; 269 this.ResumeLayout(false); 270 271 } 272 #endregion 273 274 /// 275 /// The main entry point for the application. 276 /// 277 [STAThread] 278 static void Main() 279 { 280 Application.Run(new Form1()); 281 } 282 283 private void btnGuess_Click(object sender, System.EventArgs e) 284 { 285 newGuess(txtGuess.Text); 286 } 287 288 private void btnNewGame_Click(object sender, System.EventArgs e) 289 { 290 newGame(); 291 } 292 293 private void btnExit_Click(object sender, System.EventArgs e) 294 { 295 this.Close(); 296 } 297 298 private void newGame() 299 { 300 Random r = new Random(); 301 int target = r.Next(1,100); 302 txtToGuessNo.Text = target.ToString(); 303 304 txtGuessNo.Text = "0"; 305 306 txtResponse.Text = ""; 307 lstResponse.Items.Clear(); 308 309 txtState.Text = "Run"; 310 311 txtGuess.Text = "50"; 312 txtGuess.Focus(); 313 314 pictureBox1.BackColor = Color.White; 315 } 316 317 private void newGuess(string inpGuess) 318 { 319 int intGuess = Convert.ToInt32(inpGuess); 320 string response = " " + inpGuess + " "; 321 322 if (txtState.Text != "Run") 323 { 324 MessageBox.Show("Game Over. To start new game click on New Game."); 325 return; 326 } 327 328 if ((intGuess < 1) || (intGuess > 100)) 329 { 330 MessageBox.Show ("Sorry -- Guess is out of range 1 to 100."); 331 txtGuess.Focus(); 332 txtGuess.Select(); 333 return; 334 } 335 336 int guessno = Convert.ToInt32(txtGuessNo.Text); 337 guessno++; 338 txtGuessNo.Text = guessno.ToString(); 339 340 int target = Convert.ToInt32(txtToGuessNo.Text); 341 342 if (intGuess < target) 343 { 344 response += "Too low."; 345 updateDisplay(response); 346 txtGuess.Focus(); 347 txtGuess.Select(); 348 pictureBox1.BackColor = Color.LightYellow; 349 } 350 else if (intGuess > target) 351 { 352 response += "Too high."; 353 updateDisplay(response); 354 txtGuess.Focus(); 355 txtGuess.Select(); 356 pictureBox1.BackColor = Color.LightSkyBlue; 357 } 358 else // (intGuess == target) 359 { 360 response += "Right!"; 361 updateDisplay(response); 362 pictureBox1.BackColor = Color.White; 363 txtState.Text = "Done"; 364 MessageBox.Show("Right! You win in " + txtGuessNo.Text + " guesses!"); 365 } 366 } 367 368 private void updateDisplay(string response) 369 { 370 response = "Guess #" + txtGuessNo.Text + ": " + response; 371 lstResponse.Items.Add(response); 372 txtResponse.Text = response; 373 } 374 } 375 }