polymorphing

BlitzMax Forums/BlitzMax Beginners Area/polymorphing

this is the silly code I was working on. not yet fixed or complete. it is a math drill program for my second grade daughter to better her skills. just use your own font :
'math program
SuperStrict 

Type tmath
	Field x%,y%
	Field operand%
	Field operator%
	Field answer%
	Field text$
	Field txt$
	Field width%
	Field sign$
	Field correct%
	Field wrong%
	Field index% 
	Field density%[] = [10,20,30,40,50]
	Global level% = 0
	Global completed%[4]
	Method create() Abstract
	
	Method Inputs:String(x1% = -1,y1% = -1)
		If x1 = -1 Then x1 = x
		If y1 = -1 Then y1 = y 
		Local c:String = Chr(GetChar())
		If txt.length < width Then If c=>"0" And c <="9" Then txt=txt+c
		If KeyHit(KEY_BACKSPACE) And SizeOf(txt)>1 Then  txt = Left$(txt,txt.length-1) 
		SetColor 0,255,0
		DrawText txt,x,y
		If KeyHit(KEY_ENTER) Local name:String = txt; txt = Null ;Return name
	End Method
	Method generate()
		
		operand = Rand(0,9)
		operator = Rand (0,9)
		If operand < operator
			Local temp% = operand
			operand = operator
			operator = temp
		EndIf
	End Method	

	Method display()
		Local d$ = RSet(String(operand),4)
		Local r$ = sign+RSet(String(operator),3)
		Local tx1% = (GraphicsWidth()-TextWidth(text))
		Local rx1% = (GraphicsWidth()-TextWidth(r$))
		Local dx1% = (GraphicsWidth()-TextWidth(d$))
		Local sx%  = (GraphicsWidth()-TextWidth("____"))
		Local h%   = TextHeight("X")
		Local w%   = TextWidth("____")
		Local th%  = (GraphicsHeight()-h)
		SetColor 200,50,0
		DrawText text,tx1/2,th/8
		SetColor 50,200,0
		DrawText d$,dx1/2,th/4
		SetColor 0,50,200
		DrawText r$,rx1/2,th/4+h
		SetColor 200,50,50
		DrawText "____",sx/2,th/4 + h+10
		DrawRect  sx/2,th/4 + h*3,w,h
		x = sx/2+w - TextWidth(String(answer))
		y = th/4+h*3
	End Method
	
End Type

Type Taddition Extends Tmath	 
	Method create()
		text = "ADDITION"
		sign = "+"
		generate()
		answer = operand + operator
		width = String(answer).length
	End Method
	
End Type

Type Tsubtraction Extends Tmath
	Method create()
		text$ = "SUBTRACTION"
		sign$ = "-"
		generate()
		answer = operand - operator
		width = String(answer).length	
	End Method

End Type

Type Tmultiplication Extends Tmath
	Method create()
		text$ = "MULTIPLICATION"
		sign$ = "X"
		generate()
		answer = operand * operator
		width = String(answer).length
	End Method
	

End Type

Type Tdivision Extends Tmath
	Method create()
		text$ = "DIVISION"
		sign$ = "/"
		Repeat	
			generate()
		Until operator > 0
		answer = operand * operator
		Local temp% = answer
		answer = operand
		operand = temp
		width = String(answer).length
	End Method
	
	Method display()
		Local d$ = String(operand)
		Local r$ = String(operator)
		Local tx1% = (GraphicsWidth()-TextWidth(text))
		Local rx1% = (GraphicsWidth()-TextWidth(r$))
		Local dx1% = (GraphicsWidth()-TextWidth(d$))
		Local sx%  = (GraphicsWidth()-TextWidth("____"))
		Local h%   = TextHeight("X")
		Local w%   = TextWidth("____")
		Local th%  = (GraphicsHeight()-h)
		SetColor 200,50,0
		DrawText text,tx1/2,th/8
		SetColor 50,200,0
		DrawText "____",rx1/2,th/4+h
		SetColor 0,50,200
		DrawText r$,rx1/2-TextWidth("X"),th/4+h*2-12
		SetColor 50,200,0
		DrawText "|",rx1/2-12,th/4+h*2-12
		SetColor 200,50,50
		DrawText d$,rx1/2-12+TextWidth("|"),th/4+h*2-12
		DrawRect rx1/2,th/4+h-12,w,h
		x = rx1/2+12
		y = th/4+h
		
	End Method
End Type

Function lesson%(Math:tmath Var,z% Var )
	
	If z Then 
		Math.create()
		z=0
	Else
		DrawText " Correct = "+math.correct,0,0
		DrawText "   Wrong = "+math.wrong,400,0
		If tsubtraction(math) Then DrawText "index = "+tsubtraction(math).index,0,200
		
		Math.display()
		Local c$ = math.inputs()
		If c$ 
			Local check% =Int(c$)
	    	If check = Math.answer
				z = 1
				math.correct:+1
			Else
				math.wrong :+ 1 
			EndIf
			If math.density[math.level]<math.correct Then 
				math.completed[math.index] = True
				math = Null
				FlushKeys
			EndIf  
		EndIf
	EndIf

End Function	
Function menu:tmath(z% Var)
	DrawText "   MENU",200,100
	DrawText " 1 addition",200,200
	DrawText " 2 subtraction",200,200+TextHeight("X")
	DrawText " 3 division ",200,200+TextHeight("X")*2
	DrawText " 4 multiplication",200,200+TextHeight("X")*3
	Local M:tmath

	Select 1
		Case KeyHit(KEY_1)
			z = 1
			FlushKeys()
			M= New taddition			
			If m.completed[0] Then Return Null
			Return M
		Case KeyHit(KEY_2)
			z = 1
			FlushKeys ()
			M = New Tsubtraction
			If m.completed[1] Then Return Null
			Return M
		Case KeyHit(KEY_3)
			z = 1
			FlushKeys()
			M = New Tdivision
			If m.completed[2] Then Return Null
			Return M
		Case KeyHit(KEY_4)
			z = 1
			FlushKeys()
			M = New Tmultiplication
			If M.completed[3] Then Return Null
			Return M
		End Select
			
End Function



Graphics 800,600,16 '640,480,16
Local font:timagefont = LoadImageFont("TT1001M_.TTF",48,SMOOTHFONT)
SetImageFont(font)
Local math:Tmath 

SeedRnd MilliSecs()

'addition.create()
'subtraction.create()
'multiplication.create()
Local z%

Repeat
	If math 
		lesson%(math,z) 
	Else 
		math=menu(z)
		If math
			math.correct= 0
			math.wrong = 0
			math.level= 0
		EndIf
	EndIf
	Flip()
	Cls()
Until KeyDown(KEY_ESCAPE)