#MKFiles 2	; to get files for emulator/calculator

;--------------------
; Register definitons
;--------------------

#reg a maxv	; the max value index in radio buttons
#reg b myvalue	; the current player value
#reg c count	; how many guesses
#reg d answer	; the index in radio buttons for answer
#reg e secret	; the value to be found

;--------------------
; Main Menu description
;--------------------

#MENU	; this is menu 0
	#CSTRING 1 "Secret Number"	; title
	#ACTION "Start game !" start	; run "start" program to play
	#STRING 0 "Set max value:"	
	#RADIO "100|1000|10000" maxv	; radio buttons to select max value
	#ACTION "Exit !" progend	; end of program
#ENDMENU

;--------------------
; Sub Menu description
;--------------------

#MENU	; this is menu 1
	#CSTRING 1 "Playing..."			; title
	#OUTPUTD 0 "Step number" 80 count	; count the guesses
	#VINPUTD "Guess :" 80 myvalue guess	; here the player inputs its value
	#RADIO "Too low|EXACT|Too high" answer	; radio buttons for answer
	#LINK "Exit !" 0			; back to main menu
#ENDMENU

;--------------------
; My initializations
;--------------------

0 #STOItoX maxv 3				; init the radio button to max value 100
						; button 0 out of 3 stored in maxv
;--------------------
; The main loop here
;--------------------

#MENUMAINLOOP 7					; manages all with register 7 for indirect jump

;--------------------
; My subprograms
;--------------------

start:						; start game
	#RCLXtoI maxv 3				; recall radio button (0, 1 or 2)
	2 + 10^X 				; and compute 10^(2+button) = 100, 1000 or 10000
	RND * INT STO secret			; and create an random number into secret
	1 STO count				; count = 1
	0 STO myvalue				; player guess reset
	1 #STOItoX answer 3			; first answer is "EXACT" but dummy
	#GOTOMENU 1 0				; display menu 1 with active element 0
	RTN

guess:						; when a value is entered...
	SELECT myvalue
	CASE< @secret				; if too low
		0 ENT 1				; answer = 0 and 1 to increment count
	CASE= @secret				; if exact
		2 SET \ActiveElement		; "Exit" line becomes active
		1 ENT 0				; answer = 1 and 0 (don't change count!)
	CASE>= @secret				; if too high
		2 ENT 1				; answer=2 and 1 to increment count
	ENDSELECT
	SUM count				; increment (or not) count
	x<>y #STOItoX answer 3			; and set correct answer in radio button
	RTN

PROGEND:
	STOP					; end !
