## Magic Square Math Puzzle Solver

Game development specific discussions.
neil
Posts: 594
Joined: Mar 17, 2022 23:26

### Magic Square Math Puzzle Solver

The object is that every row, column, and diagonal has to add up to the same value. Only using numbers 1 to 9.
This is solved by brute force. This is qbasic code, and it compiles fine using #lang "qb" header. It took 57 seconds to solve on my PC.

Code: Select all

``````#lang "qb"
Dim As integer SquareA,SquareB,SquareC,SquareD,SquareE
Dim As integer SquareF,SquareG,SquareH,SquareI
SquareA = 1: SquareB = 2: SquareC = 3
SquareD = 4: SquareE = 5: SquareF = 6
SquareG = 7: SquareH = 8: SquareI = 9
SquareA = 1: SquareB = 2: SquareC = 3
SquareD = 4: SquareE = 5: SquareF = 6
SquareG = 7: SquareH = 8: SquareI = 9
GOSUB DoScreen
GOSUB FillSquare
Time1\$ = TIME\$

DO
GOSUB Validate
LOOP WHILE INKEY\$ = ""
End

DoScreen:

COLOR 14,0
CLS
LOCATE 5, 29: PRINT "######################"
LOCATE 6, 29: PRINT "#                    #"
LOCATE 7, 29: PRINT "#   #############    #"
LOCATE 8, 29: PRINT "#   #   #   #   #    #"
LOCATE 9, 29: PRINT "#   #############    #"
LOCATE 10, 29: PRINT "#   #   #   #   #    #"
LOCATE 11, 29: PRINT "#   #############    #"
LOCATE 12, 29: PRINT "#   #   #   #   #    #"
LOCATE 13, 29: PRINT "#   #############    #"
LOCATE 14, 29: PRINT "#                    #"
LOCATE 15, 29: PRINT "######################"
RETURN

SquareI = SquareI + 1
IF SquareI > 9 THEN SquareI = 1: SquareH = SquareH + 1
IF SquareH > 9 THEN SquareH = 1: SquareG = SquareG + 1
IF SquareG > 9 THEN SquareG = 1: SquareF = SquareF + 1
IF SquareF > 9 THEN SquareF = 1: SquareE = SquareE + 1
IF SquareE > 9 THEN SquareE = 1: SquareD = SquareD + 1
IF SquareD > 9 THEN SquareD = 1: SquareC = SquareC + 1
IF SquareC > 9 THEN SquareC = 1: SquareB = SquareB + 1
IF SquareB > 9 THEN SquareB = 1: SquareA = SquareA + 1
IF SquareA > 9 THEN SquareA = 1
RETURN

Validate:  'validate current iteration ( no repeated Squares )
IF SquareI = SquareH THEN RETURN
IF SquareI = SquareG THEN RETURN
IF SquareI = SquareF THEN RETURN
IF SquareI = SquareE THEN RETURN
IF SquareI = SquareD THEN RETURN
IF SquareI = SquareC THEN RETURN
IF SquareI = SquareB THEN RETURN
IF SquareI = SquareA THEN RETURN
IF SquareH = SquareG THEN RETURN
IF SquareH = SquareF THEN RETURN
IF SquareH = SquareE THEN RETURN
IF SquareH = SquareD THEN RETURN
IF SquareH = SquareC THEN RETURN
IF SquareH = SquareB THEN RETURN
IF SquareH = SquareA THEN RETURN
IF SquareG = SquareF THEN RETURN
IF SquareG = SquareE THEN RETURN
IF SquareG = SquareD THEN RETURN
IF SquareG = SquareC THEN RETURN
IF SquareG = SquareB THEN RETURN
IF SquareG = SquareA THEN RETURN
IF SquareF = SquareE THEN RETURN
IF SquareF = SquareD THEN RETURN
IF SquareF = SquareC THEN RETURN
IF SquareF = SquareB THEN RETURN
IF SquareF = SquareA THEN RETURN
IF SquareE = SquareD THEN RETURN
IF SquareE = SquareC THEN RETURN
IF SquareE = SquareB THEN RETURN
IF SquareE = SquareA THEN RETURN
IF SquareD = SquareC THEN RETURN
IF SquareD = SquareB THEN RETURN
IF SquareD = SquareA THEN RETURN
IF SquareC = SquareB THEN RETURN
IF SquareC = SquareA THEN RETURN
IF SquareB = SquareA THEN RETURN
Permutations& = Permutations& + 1
GOSUB FillSquare
RETURN

FillSquare:
COLOR 11
LOCATE 8, 34: PRINT SquareA:  LOCATE 8, 38: PRINT SquareB:  LOCATE 8, 42: PRINT SquareC
LOCATE 10, 34: PRINT SquareD: LOCATE 10, 38: PRINT SquareE: LOCATE 10, 42: PRINT SquareF
LOCATE 12, 34: PRINT SquareG: LOCATE 12, 38: PRINT SquareH: LOCATE 12, 42: PRINT SquareI
Sum1 = SquareA + SquareB + SquareC
Sum2 = SquareD + SquareE + SquareF
Sum3 = SquareG + SquareH + SquareI
Sum4 = SquareA + SquareD + SquareG
Sum5 = SquareB + SquareE + SquareH
Sum6 = SquareC + SquareF + SquareI
Sum7 = SquareA + SquareE + SquareI
Sum8 = SquareC + SquareE + SquareG
COLOR 15
LOCATE 8, 46: PRINT Sum1
LOCATE 10, 46: PRINT Sum2
LOCATE 12, 46: PRINT Sum3
LOCATE 14, 33: PRINT Sum4
LOCATE 14, 37: PRINT Sum5
LOCATE 14, 41: PRINT Sum6
LOCATE 6, 46: PRINT Sum8
LOCATE 14, 46: PRINT Sum7
GOSUB TestSums
RETURN

TestSums:  'test sums
IF Sum1 <> Sum2 THEN RETURN
IF Sum2 <> Sum3 THEN RETURN
IF Sum3 <> Sum4 THEN RETURN
IF Sum4 <> Sum5 THEN RETURN
IF Sum5 <> Sum6 THEN RETURN
IF Sum6 <> Sum7 THEN RETURN
IF Sum7 <> Sum8 THEN RETURN
Time2\$ = TIME\$
COLOR 12
LOCATE 17, 29: PRINT " Puzzle solved!       " 'print answer
LOCATE 18, 29: PRINT " Permutations:        ": LOCATE 18, 43: PRINT Permutations&
LOCATE 19, 29: PRINT " Start time: "; Time1\$; " "
LOCATE 20, 29: PRINT " End time:   "; Time2\$; " "
DO: LOOP WHILE INKEY\$ = "": SYSTEM
RETURN``````
3622
Posts: 24
Joined: Mar 14, 2015 23:53

### Re: Magic Square Math Puzzle Solver

Here's my take on it, but with no timer.

Code: Select all

``````

'		Magic Square 3 x 3

screen 18

randomize

dim as integer n(8), i, r

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

function wincheck (n() as integer) as integer

dim as integer total

total = n(0) + n(1) + n(2)

if n(3) + n(4) + n(5) <> total then return 0
if n(6) + n(7) + n(8) <> total then return 0
if n(0) + n(3) + n(6) <> total then return 0
if n(1) + n(4) + n(7) <> total then return 0
if n(2) + n(5) + n(8) <> total then return 0
if n(0) + n(4) + n(8) <> total then return 0
if n(2) + n(4) + n(6) <> total then return 0

return 1

end function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

for i = 0 to 8
n(i) = i + 1
next

do
for i = 0 to 8
r = rnd * 8
swap n(i), n(r)
next
if wincheck(n()) then exit do
loop

locate 10, 10 : print n(0); "    "; n(1); "    "; n(2)
locate 12, 10 : print n(3); "    "; n(4); "    "; n(5)
locate 14, 10 : print n(6); "    "; n(7); "    "; n(8)

locate 18, 10 : print "Magic Number = "; n(0) + n(1) + n(2)

sleep

``````
neil
Posts: 594
Joined: Mar 17, 2022 23:26

### Re: Magic Square Math Puzzle Solver

@3622 Solved very fast. Nice one.