16 puzzle game: Difference between revisions

3,802 bytes added ,  10 months ago
Added FreeBasic
m (→‎{{header|Wren}}: Wren-trait -> Wren-iterate)
(Added FreeBasic)
Line 41:
=={{header|AutoHotkey}}==
With Solver, See [[16_puzzle_game/autohotkey]]
 
=={{header|FreeBASIC}}==
{{trans|Wren}}
<syntaxhighlight lang="vb">Const easy = 1, hard = 4
Dim Shared As Byte n(1 To 16)
 
Sub initGrid ()
For i As Byte = 0 To 15
n(i) = i + 1
Next
End Sub
 
Sub rotate (ix() As Byte)
Dim As Byte last = n(ix(3))
For i As Byte = 3 To 1 Step -1
n(ix(i)) = n(ix(i-1))
Next
n(ix(0)) = last
End Sub
 
Function hasWon () As Boolean
For i As Byte = 0 To 15
If n(i) <> i+1 Then Return False
Next
Return True
End Function
 
Sub setDiff (level As Byte)
Dim As Byte moves = Iif(level = easy, 3, 12)
Dim As Byte rc(), j, i
For i = 0 To moves
Redim As Byte rc(20)
Dim As Byte r = Int(Rnd * 2)
Dim As Byte s = Int(Rnd * 4)
If r = 0 Then ' rotate random row
For j = s*4 To (s+1)*4
rc(j) = j
Next
Else ' rotate random column
For j = s To s+16 Step 4
rc(j) = j
Next
End If
rotate(rc())
If hasWon() Then ' do it again
i = -1
End If
i += 1
Next
Print Using !"\nTarget is ## moves."; moves
End Sub
 
Sub drawGrid ()
Print !"\n D1 D2 D3 D4"
Print " +-------------------+"
Print Using "R1 | ## | ## | ## | ## | L1"; n(0); n(1); n(2); n(3)
Print " |----+----+----+----|"
Print Using "R2 | ## | ## | ## | ## | L2"; n(4); n(5); n(6); n(7)
Print " |----+----+----+----|"
Print Using "R3 | ## | ## | ## | ## | L3"; n(8); n(9); n(10); n(11)
Print " |----+----+----+----|"
Print Using "R4 | ## | ## | ## | ## | L4"; n(12); n(13); n(14); n(15)
Print " +-------------------+"
Print !" U1 U2 U3 U4\n"
End Sub
 
'--- Programa Principal ---
Randomize Timer
Dim As Byte level = easy
Dim As String diff
Print "Enter difficulty level easy or hard E/H: ";
Do
Input ; "", diff
Loop Until Instr("eEhH", diff)
If Ucase(diff) = "H" Then level = hard
 
Cls
Print "Sixteen Puzzle"
initGrid()
setDiff(level)
 
Dim As Byte ix(0 To 3)
Print "When entering moves, you can also enter Q to quit or S to start again."
Dim As Byte c, moves = 0
Dim As String*2 move
Do
drawGrid()
If hasWon() Then
Print "Congratulations, you have won the game in"; moves; " moves!!"
While Inkey = "": Wend
Exit Do
End If
Do
Print "Moves so far = "; moves
Input "Enter move : ", move
Select Case Trim(Ucase(move))
Case "D1", "D2", "D3", "D4"
c = Cint(Right(move,1)) - 49
ix(0) = 0 + c
ix(1) = 4 + c
ix(2) = 8 + c
ix(3) = 12 + c
rotate(ix())
moves += 1
Exit Do
Case "L1", "L2", "L3", "L4"
c = Cint(Right(move,1)) - 49
ix(0) = 3 + 4*c
ix(1) = 2 + 4*c
ix(2) = 1 + 4*c
ix(3) = 0 + 4*c
rotate(ix())
moves += 1
Exit Do
Case "U1", "U2", "U3", "U4"
c = Cint(Right(move,1)) - 49
ix(0) = 12 + c
ix(1) = 8 + c
ix(2) = 4 + c
ix(3) = 0 + c
rotate(ix())
moves += 1
Exit Do
Case "R1", "R2", "R3", "R4"
c = Cint(Right(move,1)) - 49
ix(0) = 0 + 4*c
ix(1) = 1 + 4*c
ix(2) = 2 + 4*c
ix(3) = 3 + 4*c
rotate(ix())
moves += 1
Exit Do
Case "Q"
Exit Do, Do
Case "S"
Cls
initGrid()
setDiff(level)
moves = 0
Exit Do
Case Else
Print "Invalid move, try again."
End Select
Loop
Loop
'--------------------------
Sleep</syntaxhighlight>
 
=={{header|Go}}==
2,122

edits