c# - How to restart array once filled on windows form? -
i created form asked user guess number. created array on 100 ints, created method random numbers fill array. working fine required create method resets array 0 once 100 guesses made, , seems let me keep going , going in current set up. how make sure array filling , not restarting every button click, how reset it? again runs great not meeting requirements.
here code array , method , calling at:
int[] rndarray = new int[100]; int wrong = 1; int right = 1; public void getnumbers() { random random = new random(); (int x = 0; x < rndarray.length; x++) { rndarray[x] = random.next(1, 100); } } public form1() { getnumbers(); initializecomponent(); }
then have 2 buttons seeing if guess correct , letting them guess again.
private void guess_click(object sender, eventargs e) { bool num = true; (int x = 0; x < rndarray.length; x++) { if (convert.toint32(textbox1.text) == rndarray[x]) { result.text = "you win!"; num = true; } else { result.text = "sorry - loose; number is: " + rndarray[x]; num = false; } } if (num == true) correct.text = "correct: " + right++; else incorrect.text = "incorrect: " + wrong++; mousehere.enabled = false; guess.enabled = false; nextguess.enabled = true; } private void nextguess_click(object sender, eventargs e) { mousehere.enabled = true; guess.enabled = true; nextguess.enabled = false; hint.text = "??"; textbox1.text = ""; result.text = ""; getnumbers(); }
i called method again not generate same number each guess.
why not create guessinggame
class , raise event when 100th guess made? can reset array , guess counter.
something like:
public class guessinggame { private int[] randomnumbers; private guesscount = 0; // constructor public guessinggame () { this.guesscount = 0; this.randomnumbers = this.randomnumbergenerator(); } public void addguess(int guess) { // increment guesscount // check winner and/or 100th guess // raise correct guess event if correct // raise 100th guess event if 100th guess occurs } private int[] randomnumbergenerator() { // return array of random numbers } ...more stuff... }
you'll want declare couple of simple eventhandler classes whatever need them , fire them guessinggame class. that's bit code you, i'll let on own. should started in right oo direction.
http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx. tutorial on events pretty useful.
Comments
Post a Comment