Peaceful chess queen armies: Difference between revisions

Added FreeBASIC
m (→‎{{header|Wren}}: Minor tidy)
(Added FreeBASIC)
Line 3,999:
 
It would be instructive to save and examine the generated '''peaceful_queens_elements.f90''' files. I leave that as an exercise for the reader. :)
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">Type posicion
x As Integer
y As Integer
End Type
 
Type pieza
empty As Integer
black As Integer
white As Integer
End Type
 
Function isAttacking(q As posicion, posic As posicion) As Integer
Return (q.x = posic.x Or q.y = posic.y Or Abs(q.x - posic.x) = Abs(q.y - posic.y))
End Function
 
Sub place(m As Integer, n As Integer, blackQueens() As posicion, whiteQueens() As posicion, Byref result As Integer)
If m = 0 Then
result = -1
Exit Sub
End If
Dim As Integer placingBlack = -1
Dim As Integer i, j, k, equalposicion
Dim As Boolean inner
For i = 0 To n-1
For j = 0 To n-1
Dim As posicion posic = Type<posicion>(i, j)
inner = False
For k = Lbound(blackQueens) To Ubound(blackQueens)
equalposicion = (blackQueens(k).x = posic.x And blackQueens(k).y = posic.y)
If equalposicion Or (Not placingBlack And isAttacking(blackQueens(k), posic)) Then
inner = True
Exit For
End If
Next
If Not inner Then
For k = Lbound(whiteQueens) To Ubound(whiteQueens)
equalposicion = (whiteQueens(k).x = posic.x And whiteQueens(k).y = posic.y)
If equalposicion Or (placingBlack And isAttacking(whiteQueens(k), posic)) Then
inner = True
Exit For
End If
Next
If Not inner Then
If placingBlack Then
Redim Preserve blackQueens(Ubound(blackQueens) + 1)
blackQueens(Ubound(blackQueens)) = posic
placingBlack = 0
Else
Redim Preserve whiteQueens(Ubound(whiteQueens) + 1)
whiteQueens(Ubound(whiteQueens)) = posic
place(m-1, n, blackQueens(), whiteQueens(), result)
If result Then Exit Sub
Redim Preserve blackQueens(Ubound(blackQueens) - 1)
Redim Preserve whiteQueens(Ubound(whiteQueens) - 1)
placingBlack = -1
End If
End If
End If
Next
Next
If Not placingBlack Then Redim Preserve blackQueens(Ubound(blackQueens) - 1)
result = 0
End Sub
 
Sub printBoard(n As Integer, blackQueens() As posicion, whiteQueens() As posicion)
Dim As Integer board(n * n)
Dim As Integer i, j, k
For i = Lbound(blackQueens) To Ubound(blackQueens)
board(blackQueens(i).x * n + blackQueens(i).y) = 1
Next
For i = Lbound(whiteQueens) To Ubound(whiteQueens)
board(whiteQueens(i).x * n + whiteQueens(i).y) = 2
Next
For i = 0 To n*n-1
If i Mod n = 0 And i <> 0 Then Print
Select Case board(i)
Case 1
Print "B ";
Case 2
Print "W ";
Case Else
j = i \ n
k = i - j * n
If j Mod 2 = k Mod 2 Then
Print Chr(253); " ";
Else
Print Chr(252); " ";
End If
End Select
Next i
Print
End Sub
 
Dim As posicion nms(23) = { _
Type<posicion>(2, 1), Type<posicion>(3, 1), Type<posicion>(3, 2), Type<posicion>(4, 1), Type<posicion>(4, 2), Type<posicion>(4, 3), _
Type<posicion>(5, 1), Type<posicion>(5, 2), Type<posicion>(5, 3), Type<posicion>(5, 4), Type<posicion>(5, 5), _
Type<posicion>(6, 1), Type<posicion>(6, 2), Type<posicion>(6, 3), Type<posicion>(6, 4), Type<posicion>(6, 5), Type<posicion>(6, 6), _
Type<posicion>(7, 1), Type<posicion>(7, 2), Type<posicion>(7, 3), Type<posicion>(7, 4), Type<posicion>(7, 5), Type<posicion>(7, 6), Type<posicion>(7, 7) }
 
For i As Integer = Lbound(nms) To Ubound(nms)
Print Chr(10); nms(i).y; " black and "; nms(i).y; " white queens on a "; nms(i).x; " x "; nms(i).x; " board:"
Dim As posicion blackQueens(0)
Dim As posicion whiteQueens(0)
Dim As Integer result
place(nms(i).y, nms(i).x, blackQueens(), whiteQueens(), result)
If result Then
printBoard(nms(i).x, blackQueens(), whiteQueens())
Else
Print "No solution exists."
End If
Next i
 
Sleep</syntaxhighlight>
{{out}}
<pre>
1 black and 1 white queens on a 2 x 2 board:
No solution exists.
 
1 black and 1 white queens on a 3 x 3 board:
² ³ ²
³ ² B
W ³ ²
 
2 black and 2 white queens on a 3 x 3 board:
² B ²
³ ² ³
² ³ W
 
1 black and 1 white queens on a 4 x 4 board:
² ³ ² ³
³ ² B ²
W ³ ² ³
³ ² ³ ²
 
2 black and 2 white queens on a 4 x 4 board:
² B ² ³
³ ² ³ W
² ³ ² ³
³ ² ³ ²
 
3 black and 3 white queens on a 4 x 4 board:
B ³ ² ³
³ ² W ²
² ³ ² ³
³ ² ³ ²
 
1 black and 1 white queens on a 5 x 5 board:
² ³ ² ³ ²
³ ² B ² ³
W ³ ² ³ ²
³ ² ³ ² ³
² ³ ² ³ ²
 
2 black and 2 white queens on a 5 x 5 board:
² B ² ³ ²
³ ² ³ W ³
² ³ ² ³ ²
³ ² ³ ² ³
² ³ ² ³ ²
 
3 black and 3 white queens on a 5 x 5 board:
B ³ ² ³ ²
³ ² W ² ³
² ³ ² ³ ²
³ ² ³ ² ³
² ³ ² ³ ²
 
4 black and 4 white queens on a 5 x 5 board:
² ³ ² ³ B
W ² ³ ² ³
² ³ ² ³ ²
³ ² ³ ² ³
² ³ ² ³ ²
 
5 black and 5 white queens on a 5 x 5 board:
² ³ B ³ ²
³ ² ³ ² W
² ³ ² ³ ²
³ ² ³ ² ³
² ³ ² ³ ²
 
1 black and 1 white queens on a 6 x 6 board:
² ³ ² ³ ² ³
³ ² B ² ³ ²
W ³ ² ³ ² ³
³ ² ³ ² ³ ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
 
2 black and 2 white queens on a 6 x 6 board:
² B ² ³ ² ³
³ ² ³ W ³ ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
 
3 black and 3 white queens on a 6 x 6 board:
B ³ ² ³ ² ³
³ ² W ² ³ ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
 
4 black and 4 white queens on a 6 x 6 board:
² ³ ² ³ B ³
W ² ³ ² ³ ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
 
5 black and 5 white queens on a 6 x 6 board:
² ³ B ³ ² ³
³ ² ³ ² W ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
 
6 black and 6 white queens on a 6 x 6 board:
B ³ ² ³ ² ³
³ ² W ² ³ ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
 
1 black and 1 white queens on a 7 x 7 board:
² ³ ² ³ ² ³ ²
³ ² B ² ³ ² ³
W ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
 
2 black and 2 white queens on a 7 x 7 board:
² B ² ³ ² ³ ²
³ ² ³ W ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
 
3 black and 3 white queens on a 7 x 7 board:
B ³ ² ³ ² ³ ²
³ ² W ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
 
4 black and 4 white queens on a 7 x 7 board:
² ³ ² ³ B ³ ²
W ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
 
5 black and 5 white queens on a 7 x 7 board:
² ³ B ³ ² ³ ²
³ ² ³ ² W ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
 
6 black and 6 white queens on a 7 x 7 board:
B ³ ² ³ ² ³ ²
³ ² W ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
 
7 black and 7 white queens on a 7 x 7 board:
² ³ ² ³ B ³ ²
W ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²</pre>
 
=={{header|Go}}==
2,122

edits