|
| 26 Jan 2012 06:23 PM |
For the AP Computer Science class at my school, there is a program to complete that requires testing if a magic square works. I know that I can use looping to do this, but I wanted to try with recursion for practice. And then I failed. Miserably.
Is it even possible to determine if a magic square is magic using recursion? |
|
|
| Report Abuse |
|
Varp
|
  |
| Joined: 18 Nov 2009 |
| Total Posts: 5333 |
|
|
| 26 Jan 2012 06:25 PM |
| It doesn't lend itself to recursion. I can't really think how you'd divide a magic square into nested sub-problems. I mean, summing every row is basically the only way to check. |
|
|
| Report Abuse |
|
yoyoman2
|
  |
| Joined: 07 Mar 2009 |
| Total Posts: 2170 |
|
|
| 27 Jan 2012 01:00 PM |
| its right and up then every 3 down i think |
|
|
| Report Abuse |
|
|
| 27 Jan 2012 01:26 PM |
and diagonals
yeah I don't think there's any shortcut here |
|
|
| Report Abuse |
|
nate890
|
  |
| Joined: 22 Nov 2008 |
| Total Posts: 21686 |
|
|
| 27 Jan 2012 02:33 PM |
Haven't ever heard of the game, just played it (Fairly tricky, I got all to equal 15, except for the diagonals).
If I have time, I'll write a game like that later and you could check the code out.
Also, I don't quite understand what you're asking. If you're trying to check if a player has used a number already, a table would probably be the most efficient way to go.
<'+1 Post. Ujelly?'> |
|
|
| Report Abuse |
|
nate890
|
  |
| Joined: 22 Nov 2008 |
| Total Posts: 21686 |
|
|
| 29 Jan 2012 04:26 PM |
Back from my couple day 'get-away'. I'll write you a demo in a couple of minutes, make it a model and send you a PM. From there you can check out the code and hopefully you'll learn whatever you need to learn.
<'+1 Post. Ujelly?'> |
|
|
| Report Abuse |
|
nate890
|
  |
| Joined: 22 Nov 2008 |
| Total Posts: 21686 |
|
|
| 29 Jan 2012 07:22 PM |
Bah, got around to completing it. Only spent 30 minutes on it and the code is, well, I don't quite know. It was actually fairly difficult to make it efficient (to be honest) because of the manual checking and separate tables for each row. Really increased the amount of lines.
local rows={ h={ --Horizontal [1]={}; [2]={}; [3]={} }; v={ --Vertical [1]={}; [2]={}; [3]={} }; d={ --Diagonal [1]={}; --Top left to bottom right [2]={} --Top right to bottom left } }
local numbers={ [1]=false; [2]=false; [3]=false; [4]=false; [5]=false; [6]=false; [7]=false; [8]=false; [9]=false }
local frame=script.Parent
function valueGUI() --Trying to reduce some lines with the gui creation local value=Instance.new("TextLabel",frame) value.Text=0 value.BackgroundColor3=Color3.new(1,1,1) value.BorderColor3=Color3.new(0,0,0) return value end
function getSum(row) local sum=0 for _,v in pairs(row) do if tonumber(v.Text) then sum=sum+tonumber(v.Text) end end return sum end
function refresh() for i,c in pairs(frame:GetChildren()) do if c.Name:match("xval") then local val=c.Name:sub(1,1) c.Text=getSum(rows.v[tonumber(val)]) elseif c.Name:match("yval") then local val=c.Name:sub(1,1) c.Text=getSum(rows.h[tonumber(val)]) elseif c.Name:match("dval") then local val=c.Name:sub(1,1) c.Text=getSum(rows.d[tonumber(val)]) end end end
function check() local correct,used=0,0 for i,c in pairs(frame:GetChildren()) do if c.Name:find("num") then if tonumber(c.Text) then if not numbers[tonumber(c.Text)] then used=used+1 numbers[tonumber(c.Text)]=true else used=0 break; end end end end for i=1,#numbers do numbers[i]=false end for i,c in pairs(frame:GetChildren()) do if c.Name:find("val") then if tonumber(c.Text) then if tonumber(c.Text)==15 then correct=correct+1 end end end end if correct==8 and used==9 then print"correct" end print(correct,used) end
for y=1,3 do for x=1,3 do local box=Instance.new("TextBox",frame) box.Name="num:"..y..";"..x box.Text=0 box.BackgroundColor3=Color3.new(1,1,1) box.BorderColor3=Color3.new(0,0,0) box.Size=UDim2.new(0.7/3,0,0.7/3) box.Position=UDim2.new(0.7/3*(x-(0.7/2)),0,0.7/3*(y-(0.7/2))) if y==3 then local value=valueGUI() value.Name=x.."; yval" value.Size=UDim2.new(0.7/3,0,0.7/(3*2)) value.Position=UDim2.new(0.7/3*(x-0.7/2),0,0.7/3*(y+(0.7))) end if x==3 then local value=valueGUI() value.Name=y.."; xval" value.Size=UDim2.new(0.7/(3*2),0,0.7/3) value.Position=UDim2.new(0.7/3*(x+0.7),0,0.7/3*(y-(0.7/2))) end if x==3 and y==3 then local value=valueGUI() value.Name="1; dval" value.Size=UDim2.new(0.7/(3*2),0,0.7/(3*2)) value.Position=UDim2.new(0.7/3*(x+0.7),0,0.7/3*(y+0.7)) end if x==3 and y==1 then local value=valueGUI() value.Name="2; dval" value.Size=UDim2.new(0.7/(3*2),0,0.7/(3*2)) value.Position=UDim2.new(0.7/3*(x+0.7),0,0.7/(3*2)*(y-0.7)-0.015) end table.insert(rows.h[x],box) table.insert(rows.v[y],box) if x==1 and y==1 or y==2 and x==2 or y==3 and x==3 then table.insert(rows.d[1],box) end; if x==3 and y==1 or y==2 and x==2 or y==3 and x==1 then table.insert(rows.d[2],box) end end end
local ready=Instance.new("TextButton",frame) ready.Name="CheckButton" ready.Text="Check" ready.BackgroundColor3=Color3.new(1,1,1) ready.BorderColor3=Color3.new(0,0,0) ready.Size=UDim2.new(0.8,0,0.1)
ready.MouseButton1Down:connect(function() refresh() check() end)
|
|
|
| Report Abuse |
|