Generate Chess960 starting position: Difference between revisions

m
m (→‎{{header|UNIX Shell}}: minor simplification)
 
(3 intermediate revisions by 3 users not shown)
Line 611:
RNKBNRBQ
QNRBBNKR</pre>
 
 
=={{header|C#}}==
{{trans|Go}}
<syntaxhighlight lang="C#">
using System;
 
class Program
{
struct Symbols
{
public char K, Q, R, B, N;
 
public Symbols(char k, char q, char r, char b, char n)
{
K = k; Q = q; R = r; B = b; N = n;
}
}
 
private static Symbols A = new Symbols('K', 'Q', 'R', 'B', 'N');
private static Symbols W = new Symbols('♔', '♕', '♖', '♗', '♘');
private static Symbols B = new Symbols('♚', '♛', '♜', '♝', '♞');
 
private static string[] krn = new string[]
{
"nnrkr", "nrnkr", "nrknr", "nrkrn",
"rnnkr", "rnknr", "rnkrn",
"rknnr", "rknrn",
"rkrnn"
};
 
private static string Chess960(Symbols sym, int id)
{
char[] pos = new char[8];
int q = id / 4, r = id % 4;
pos[r * 2 + 1] = sym.B;
r = q % 4; q /= 4;
pos[r * 2] = sym.B;
r = q % 6; q /= 6;
int placementIndex = 0; // Adjusted variable name to prevent conflict
for (int i = 0; ; i++)
{
if (pos[i] != '\0') continue;
if (r == 0)
{
pos[i] = sym.Q;
break;
}
r--;
}
while (pos[placementIndex] != '\0') placementIndex++; // Adjusted loop to prevent conflict
foreach (char f in krn[q])
{
while (pos[placementIndex] != '\0') placementIndex++;
switch (f)
{
case 'k':
pos[placementIndex] = sym.K;
break;
case 'r':
pos[placementIndex] = sym.R;
break;
case 'n':
pos[placementIndex] = sym.N;
break;
}
}
return new string(pos);
}
 
static void Main(string[] args)
{
Console.WriteLine(" ID Start position");
foreach (int id in new int[] { 0, 518, 959 })
{
Console.WriteLine($"{id,3} {Chess960(A, id)}");
}
Console.WriteLine("\nRandom");
Random rand = new Random();
for (int i = 0; i < 5; i++)
{
Console.WriteLine(Chess960(W, rand.Next(960)));
}
}
}
</syntaxhighlight>
{{out}}
<pre>
ID Start position
0 BBQNNRKR
518 RNBQKBNR
959 RKRNNQBB
 
Random
♘♕♖♔♗♖♘♗
♗♗♖♘♔♖♕♘
♖♕♘♔♖♗♗♘
♘♖♗♔♘♗♖♕
♖♘♕♗♔♘♗♖
 
</pre>
 
=={{header|C++}}==
Line 835 ⟶ 936:
{{out}}
<pre>QBNNBRKR</pre>
 
=={{header|EasyLang}}==
{{trans|Lua}}
<syntaxhighlight>
len t$[] 8
proc randins c$ l r . pos .
repeat
pos = randint (r - l + 1) + l - 1
until t$[pos] = ""
.
t$[pos] = c$
.
randins "K" 2 7 king
randins "R" 1 (king - 1) h
randins "R" (king + 1) 8 h
randins "B" 1 8 b1
repeat
randins "B" 1 8 b2
until (b2 - b1) mod 2 <> 0
t$[b2] = ""
.
randins "Q" 1 8 b1
randins "N" 1 8 b1
randins "N" 1 8 b1
print strjoin t$[]
</syntaxhighlight>
{{out}}
<pre>
RBBNQNKR
</pre>
 
=={{header|EchoLisp}}==
Line 1,941 ⟶ 2,072:
♖♗♔♕♗♖♘♘
♘♖♔♖♕♗♗♘</pre>
 
=={{header|Prolog}}==
Uses <code lang="prolog">random_permutation/2</code> defined in SWI-Prolog.
 
<syntaxhighlight lang="prolog">
check(Row) :-
nth1(King, Row, ♔),
nth1(Rook1, Row, ♖),
nth1(Rook2, Row, ♖),
nth1(Bishop1, Row, ♗),
nth1(Bishop2, Row, ♗),
Rook1 < King, King < Rook2,
(Bishop1 + Bishop2) mod 2 =:= 1.
 
generate(Row) :-
random_permutation([♖,♘,♗,♕,♔,♗,♘,♖], Row),
check(Row) ; generate(Row).
</syntaxhighlight>
 
Example run:
<syntaxhighlight lang="prolog">
?- generate(X).
X = [♘, ♗, ♖, ♕, ♘, ♔, ♗, ♖] ;
X = [♘, ♗, ♖, ♕, ♘, ♔, ♗, ♖] ;
X = [♘, ♖, ♘, ♕, ♔, ♖, ♗, ♗] ;
X = [♘, ♖, ♘, ♕, ♔, ♖, ♗, ♗]
</syntaxhighlight>
 
=={{header|Python}}==
1,982

edits