0

I'm pretty new to IDL and as a way to learn I have tried to create a number guessing game. I have a Widget with three buttons: One that tells the program the number you are thinking of is larger than the one the computer asks about, one if it's smaller and one if it's correct.

My problem is that once you have pushed i.e. the larger button, if you press it again it won't do anything. E.g. the program starts to guess 500, if I press larger it guesses 750. If I now press larger again, the program doesn't respond.

My code is like this:

PRO test1_event, ev

WIDGET_CONTROL, ev.top, GET_UVALUE = stash

minimum = 0
maximum = 1000

IF (ev.Id EQ largerbutton) THEN BEGIN
  minimum = (minimum+maximum)/2
  maximum = maximum
ENDIF
IF (ev.Id EQ smallerbutton) THEN BEGIN
  maximum = (minimum+maximum)/2
  minimum = minimum
ENDIF
IF (ev.Id EQ correctbutton) THEN BEGIN
  help2 = string('I got it!')    ;This prints to a text widget
ENDIF

END

PRO test1

  wBase = WIDGET_BASE(X_SCROLL_SIZE = 512, Y_SCROLL_SIZE = 512)
  ;wDraw = WIDGET_WINDOW(wBase, X_SCROLL_SIZE = 512, Y_SCROLL_SIZE = 512)
  Lower = WIDGET_BUTTON(wBase, VALUE = 'Smaller', XOFFSET = 60, YOFFSET = 250)
  Higher = WIDGET_BUTTON(wBase, VALUE = 'Larger', XOFFSET = 225, YOFFSET = 250)
  Correct = WIDGET_BUTTON(wBase, VALUE = 'Correct', XOFFSET = 380, YOFFSET = 250)

  minimum = 0
  maximum = 1000

  help1 = string('Please think of a number between' + string(minimum) + ' and ' + string(maximum))
  help2 = string('Is your number ' + string((minimum + maximum)/2) + '?')
  wText = WIDGET_TEXT(wBase, VALUE = ['Welcome to my little game. I will now         try and guess a number you are thinking of.'], XSIZE = 63,XOFFSET = 50, YOFFSET = 100)
  wText1 = WIDGET_TEXT(wBase, VALUE = help1, XSIZE = 63,XOFFSET = 50, YOFFSET = 120)
  wText2 =  WIDGET_TEXT(wBase, VALUE = help2, XSIZE = 63,XOFFSET = 50, YOFFSET = 140)

  stash = {text1:wText, text2:wText1, text3:wText2, $
lower:Lower, higher:Higher, correct:Correct, minimum:minimum, maximum:maximum}

  WIDGET_CONTROL, wBase, SET_UVALUE = stash, /REALIZE
  XMANAGER, 'test1', wBase

end

I have tried using a while loop and also REPEAT, but then the program just goes right up to 999 if I press the larger button and to 0 if I press the smaller.

Any ideas to what I can do?

EDIT: Added the rest of the program

sCuper
  • 91
  • 2
  • 8
  • It looks like you're missing some of your code. Are "largerbutton" and "smallerbutton" stored in the "stash"? Are you calling XMANAGER to start event processing? Finally, how are you storing "minimum" and "maximum"? Please post your entire program. – Chris Torrence Aug 18 '17 at 17:33
  • Thanks for the feedback, added the rest of the program. – sCuper Aug 21 '17 at 11:09

1 Answers1

1

I think the buttons are working fine but your event handler doesn't actually do anything. First, I needed to change largerbutton, smallerbutton, and correctbutton to be stash.higher, stash.lower, stash.correct. Then, your code calculates the new minimum & maximum but it doesn't actually do anything with them.

I put a print statement into the event code and it's definitely getting the button presses.

In your event handler you probably want to use widget_control to update the text box with the new guess.

Chris Torrence
  • 452
  • 3
  • 11