Spel40: Binär Division

 

binarydiv

Vi programmerar en app för division av binära tal enligt den divisionsalgoritm som utgjorde en höjdpunkt i traditionell skolmatematik, men som inte längre lärs ut eftersom den anses vara för krånglig och svårbegriplig. Lämplig från 5 år.

Titta på ett exempel här. Jämför med Counting på App Store.

Template:

N=3
I=7
I1=I
bitimageg = {}
bitimageg[1] =readImage(“Dropbox:1bitg”)
bitimageg[2] =readImage(“Dropbox:2bitg”)
bitimageg[3] =readImage(“Dropbox:4bitg”)
bitimageg[4] =readImage(“Dropbox:8bitg”)
bitimageg[5] =readImage(“Dropbox:16bitg”)
bitimageg[6] =readImage(“Dropbox:32bitg”)
bitimageg[7] =readImage(“Dropbox:64bitg”)
local bit={}
for n=1,N+3 do
bit[n]={}
end
local aButton1,aButton2,aButton3

function setup()
print(“Hello World!”)
supportedOrientations(LANDSCAPE_ANY)
for n=1,N+3 do
for i=1,I do
bit[n][i] = SpriteObject(bitimageg[i],vec2(800-100*i,100*n+100),0)
end
end
aButton1 = Button(“Cargo Bot:Fast Button Active”,vec2(100,900))
aButton2 = Button(“Cargo Bot:Fast Button Active”,vec2(300,900))
aButton3 = Button(“Cargo Bot:Fast Button Active”,vec2(500,900))
sound(“A Hero’s Quest:Door Open”)
sum={}
init=0
digit=1
sub=0
end

function touched(touch)

for n=1,N+3 do
for i=1,I do
bit[n][i]:touched(touch)
end
end

finger=vec2(touch.x,touch.y)

if init==1 then

for n=N+2,N+3 do
for i=1,I do
d=(bit[n][i].objectStartLocation-finger):len()
if bit[n][i].ID==0 and d<50 then
bit[n][i].ID=1
bit[n-2][i].ID=1
sound(“Game Sounds One:Assembly 6”)
end
end
end

end

if digit==1 then

for i=1,I1 do
d=(bit[1][i].objectStartLocation-finger):len()
if bit[1][i].ID==0 and d<50 then
bit[1][i].ID=1
I1=i
sound(“Game Sounds One:Assembly 6”)
end
end

for j=1,I1 do
if bit[1][j].ID==1 then
for i=j,I do
bit[N-1][i].ID=bit[N+3][i-j+1].ID
end
for i=1,j-1 do
bit[N-1][i].ID=0
end
end
end

end

if sub==1 then

for i=1,I do
if bit[N-1][i].ID>0 then
if (bit[N-1][i]:isTouching(bit[N][i])==true) then
bit[N][i].ID=bit[N][i].ID-bit[N-1][i].ID
bit[N-1][i].ID=0
end
end
end
sound(“Game Sounds One:Assembly 2”)

end

aButton1:touched(touch)
if (aButton1.selected==true) then
init=1
speech.say(“yeeh taal”)
sound(“Game Sounds One:Knock 1”)
end

aButton2:touched(touch)
if (aButton2.selected==true) then
digit=1
init=0
sub=0
speech.say(“set beat”)
sound(“Game Sounds One:Knock 2”)
end

aButton3:touched(touch)
if (aButton3.selected==true) then
sub=1
digit=0
init=0
speech.say(“subtraheera”)
sound(“Game Sounds One:Knock 2”)
end

end

function draw()

background(40, 40, 50)
fill(255, 255, 255, 255)
for n=2,N do
rect(10,100*n+50,800,100)
end
fill(218, 84, 84, 255)
rect(10,150,800,100)
fill(146, 146, 211, 255)
rect(10,100*(N+2)+50,800,100)
fill(207, 204, 136, 252)
rect(10,100*(N+3)+50,800,100)
for n=1,N do
for i=1,I do
bit[n][i]:draw()
end
end
for n=N+2,N+3 do
for i=1,I do
bit[n][i]:draw()
end
end
font(“AmericanTypewriter”)
fontSize(30)
fill(0, 76, 255, 255)
for n=1,N do
for i=1,I do
text(bit[n][i].ID,800-100*i,100*n+100)
end
end
for n=N+2,N+3 do
for i=1,I do
text(bit[n][i].ID,800-100*i,100*n+100)
end
end
aButton1:draw()
aButton2:draw()
aButton3:draw()
text(“Ge tal”,200,900)
text(“Bit”,400,900)
text(“Subtrahera”,630,900)

for i=1,I-1 do
if bit[N][i].ID==-1 then
bit[N][i].ID=1
bit[N][i+1].ID=bit[N][i+1].ID-1
end
end

end

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s