public static List <Piece> Chess960Setup() { List <Piece> shufflePieces = new List <Piece>(); Random rand = new Random(); List <Piece> freePieces = new List <Piece> { Piece.KING, Piece.QUEEN, Piece.ROOK, Piece.ROOK, Piece.BISHOP, Piece.BISHOP, Piece.KNIGHT, Piece.KNIGHT }; int bSet = -1; for (int i = 0; i < 8; i++) { var con = false; while (!con) { var randomPiece = rand.Next(8 - i); if (freePieces[randomPiece] == Piece.ROOK) { if (freePieces.IndexOf(Piece.ROOK) != freePieces.LastIndexOf(Piece.ROOK)) { con = true; } else if (!freePieces.Contains(Piece.KING)) { con = true; } } else if (freePieces[randomPiece] == Piece.KING) { if (freePieces.IndexOf(Piece.ROOK) == freePieces.LastIndexOf(Piece.ROOK)) { con = true; } } else if (freePieces[randomPiece] == Piece.BISHOP) { if (bSet == -1) { con = true; bSet = i; } else { if (bSet % 2 != i % 2) { con = true; } else if (i == 7) { shufflePieces.Insert(0, freePieces[randomPiece]); } } } else { con = true; } if (con) { shufflePieces.Add(freePieces[randomPiece]); freePieces.RemoveAt(randomPiece); } } } return(shufflePieces); }