public static square makeSquare(pieceType newType, pieceColour newColour, squarePos newPos) { square toRet ; switch (newType) { case pieceType.none: toRet = new square(newPos); break; case pieceType.pawn: toRet = new pawnSquare(newPos, newColour); break; case pieceType.rook: toRet = new rookSquare(newPos, newColour); break; case pieceType.bishop: toRet = new bishopSquare(newPos, newColour); break; case pieceType.knight: toRet = new knightSquare(newPos, newColour); break; case pieceType.queen: toRet = new queenSquare(newPos, newColour); break; case pieceType.king: toRet = new kingSquare(newPos, newColour); break; default: throw new ArgumentOutOfRangeException(); } return toRet; }
public boardScorer(exampleMiniMaxBoard board, pieceColour viewpoint) { int whiteMaterial = addUpMaterial(board.whitePieceSquares); int blackMaterial = addUpMaterial(board.blackPieceSquares); if (viewpoint == pieceColour.white) score = whiteMaterial - blackMaterial; else score = blackMaterial - whiteMaterial; }
/// <summary> /// Sets the perspective to that of the specified colour /// If the perspective is changed, it has the same effect as rotating the board 180 degrees /// </summary> /// <param name="colour">The perspective to set to</param> private void setPerspective(pieceColour colour) { if (colour == pieceColour.light) { setPerspectiveLight(); } else { setPerspectiveDark(); } }
private void parseActiveColour(string rawCol) { string trimmedCol = rawCol.ToUpper().Trim(); if (trimmedCol == "W") toPlay = pieceColour.white; else if (trimmedCol == "B") toPlay = pieceColour.black; else throw new Exception(); }
/// <summary> /// Has the main threat wait 200 milliseconds, then changes whos turn it is and changes the perspective to /// the new players turn /// </summary> private void nextTurn() { if (this.turn == pieceColour.light) { this.turn = pieceColour.dark; setPerspective(pieceColour.dark); } else { this.turn = pieceColour.light; setPerspective(pieceColour.light); } }
/// <summary> /// Opens the pawn promotion window and return what piece the user chose to promote his pawn to. Does not return /// until the pawn promotion window is closed. /// </summary> /// <param name="colour">The colour of the pawn being promoted</param> /// <returns>What the pawn will be promoted to, or a pawn if there will be no promotion</returns> private pieceType pawnPromotion(pieceColour colour) { pieceType returned = pieceType.pawn; PawnPromotionWindow pawnPromotionWindow = new PawnPromotionWindow(); pawnPromotionWindow.setPromotionPieceColours(colour); pawnPromotionWindow.promotedToPiece += value => returned = value; pawnPromotionWindow.ShowDialog(); pawnPromotionWindow.Close(); return(returned); }
public void play() { toPlay = pieceColour.white; moves = new List<move>(); Debug.WriteLine("--- game start ---"); ChessPlayer playerToPlay = p1; do { doMove(playerToPlay.board); toPlay = baseBoard.getOtherSide(toPlay); playerToPlay = playerToPlay == p1 ? p2 : p1; } while (playerToPlay.board.getGameStatus(toPlay) == gameStatus.inProgress); // See who won and allocate points gameStatus status = p1.board.getGameStatus(toPlay); switch (status) { case gameStatus.won: p1.score++; break; case gameStatus.drawn: p1.score += 0.5f; p2.score += 0.5f; break; case gameStatus.lost: p2.score++; break; default: throw new ArgumentOutOfRangeException(); } // print some output Console.WriteLine("Game over. Final position:"); Console.WriteLine(p1.ToString()); Console.WriteLine("Game transcript:"); foreach (move thisMove in moves) Console.WriteLine(thisMove.ToString(moveStringStyle.chessNotation)); }
public BoardScorer(DoktorChessAIBoard toScore, pieceColour newViewpoint, scoreModifiers newModifiers) { modifiers = newModifiers; viewpoint = newViewpoint; List<square> myPieces = toScore.getPiecesForColour(viewpoint); List<square> enemyPieces = toScore.getPiecesForColour(viewpoint == pieceColour.black ? pieceColour.white : pieceColour.black); parentBoard = toScore; if (viewpoint == pieceColour.black) { _myMaterialAdvantage = toScore.blackMaterialAdvantage; _myMaterialDisadvantage = toScore.whiteMaterialAdvantage; } else if (viewpoint == pieceColour.white) { _myMaterialAdvantage = toScore.whiteMaterialAdvantage; _myMaterialDisadvantage = toScore.blackMaterialAdvantage; } _status = toScore.getGameStatus(myPieces, enemyPieces); }
public void setPromotionPieceColours(pieceColour colour) { this.colour = colour; this.NewQueen.Content = new Image { Source = new BitmapImage(new Uri(FileLocations.getPNGLocation(pieceType.queen, colour), UriKind.Relative)), VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center }; this.NewRook.Content = new Image { Source = new BitmapImage(new Uri(FileLocations.getPNGLocation(pieceType.rook, colour), UriKind.Relative)), VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center }; this.NewBishop.Content = new Image { Source = new BitmapImage(new Uri(FileLocations.getPNGLocation(pieceType.bishop, colour), UriKind.Relative)), VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center }; this.NewKnight.Content = new Image { Source = new BitmapImage(new Uri(FileLocations.getPNGLocation(pieceType.knight, colour), UriKind.Relative)), VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center }; this.OldPawn.Content = new Image { Source = new BitmapImage(new Uri(FileLocations.getPNGLocation(pieceType.pawn, colour), UriKind.Relative)), VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center }; }
/// <summary> /// Is the specified square 'covered' by the specified colour? Note that this is just /// a hint, and may be incorrect under certain circumstances. /// </summary> /// <param name="squareToCheck"></param> /// <param name="sideToExamine"></param> /// <returns></returns> public virtual bool isThreatenedHint(square squareToCheck, pieceColour sideToExamine) { // This base class does not provide an implementation. return false; }
public bishopSquare(squarePos newPos, pieceColour newColour) : base(newPos, newColour) { type = pieceType.bishop; }
private void ThreadStart() { try { ThreadStartInner(); } catch (playerException e) { e.culprit.isErrored = true; e.culprit.errorMessage = e.Message; e.culprit.exception = e.InnerException; isErrored = true; isDraw = false; winningSide = e.culpritCol == pieceColour.white ? pieceColour.black : pieceColour.white; erroredSide = e.culpritCol; isFinished = true; isRunning = false; gameFinished(gameBoardWhite); } }
/// <summary> /// Sets all pieces to their initial locations on the board and clears all other locations /// </summary> private void setInitialBoardState() { this.gameBoard.getSquare(InitialPieceLocations.leftRookInitialPosition(pieceColour.light)).placePiece(new Rook(pieceColour.light)); this.gameBoard.getSquare(InitialPieceLocations.leftKnightInitialPosition(pieceColour.light)).placePiece(new Knight(pieceColour.light)); this.gameBoard.getSquare(InitialPieceLocations.leftBishopInitialPosition(pieceColour.light)).placePiece(new Bishop(pieceColour.light)); this.gameBoard.getSquare(InitialPieceLocations.queenInitialPosition(pieceColour.light)).placePiece(new Queen(pieceColour.light)); this.gameBoard.getSquare(InitialPieceLocations.kingInitialPosition(pieceColour.light)).placePiece(new King(pieceColour.light)); this.gameBoard.getSquare(InitialPieceLocations.rightBishopInitialPosition(pieceColour.light)).placePiece(new Bishop(pieceColour.light)); this.gameBoard.getSquare(InitialPieceLocations.rightKnightInitialPosition(pieceColour.light)).placePiece(new Knight(pieceColour.light)); this.gameBoard.getSquare(InitialPieceLocations.rightRookInitialPosition(pieceColour.light)).placePiece(new Rook(pieceColour.light)); this.gameBoard.getSquare(StaticBoardLocations.A2).placePiece(new Pawn(pieceColour.light)); this.gameBoard.getSquare(StaticBoardLocations.B2).placePiece(new Pawn(pieceColour.light)); this.gameBoard.getSquare(StaticBoardLocations.C2).placePiece(new Pawn(pieceColour.light)); this.gameBoard.getSquare(StaticBoardLocations.D2).placePiece(new Pawn(pieceColour.light)); this.gameBoard.getSquare(StaticBoardLocations.E2).placePiece(new Pawn(pieceColour.light)); this.gameBoard.getSquare(StaticBoardLocations.F2).placePiece(new Pawn(pieceColour.light)); this.gameBoard.getSquare(StaticBoardLocations.G2).placePiece(new Pawn(pieceColour.light)); this.gameBoard.getSquare(StaticBoardLocations.H2).placePiece(new Pawn(pieceColour.light)); this.gameBoard.getSquare(StaticBoardLocations.A3).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.B3).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.C3).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.D3).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.E3).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.F3).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.G3).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.H3).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.A4).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.B4).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.C4).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.D4).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.E4).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.F4).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.G4).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.H4).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.A5).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.B5).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.C5).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.D5).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.E5).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.F5).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.G5).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.H5).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.A6).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.B6).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.C6).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.D6).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.E6).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.F6).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.G6).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.H6).placePiece(null); this.gameBoard.getSquare(StaticBoardLocations.A7).placePiece(new Pawn(pieceColour.dark)); this.gameBoard.getSquare(StaticBoardLocations.B7).placePiece(new Pawn(pieceColour.dark)); this.gameBoard.getSquare(StaticBoardLocations.C7).placePiece(new Pawn(pieceColour.dark)); this.gameBoard.getSquare(StaticBoardLocations.D7).placePiece(new Pawn(pieceColour.dark)); this.gameBoard.getSquare(StaticBoardLocations.E7).placePiece(new Pawn(pieceColour.dark)); this.gameBoard.getSquare(StaticBoardLocations.F7).placePiece(new Pawn(pieceColour.dark)); this.gameBoard.getSquare(StaticBoardLocations.G7).placePiece(new Pawn(pieceColour.dark)); this.gameBoard.getSquare(StaticBoardLocations.H7).placePiece(new Pawn(pieceColour.dark)); this.gameBoard.getSquare(InitialPieceLocations.leftRookInitialPosition(pieceColour.dark)).placePiece(new Rook(pieceColour.dark)); this.gameBoard.getSquare(InitialPieceLocations.leftKnightInitialPosition(pieceColour.dark)).placePiece(new Knight(pieceColour.dark)); this.gameBoard.getSquare(InitialPieceLocations.leftBishopInitialPosition(pieceColour.dark)).placePiece(new Bishop(pieceColour.dark)); this.gameBoard.getSquare(InitialPieceLocations.queenInitialPosition(pieceColour.dark)).placePiece(new Queen(pieceColour.dark)); this.gameBoard.getSquare(InitialPieceLocations.kingInitialPosition(pieceColour.dark)).placePiece(new King(pieceColour.dark)); this.gameBoard.getSquare(InitialPieceLocations.rightBishopInitialPosition(pieceColour.dark)).placePiece(new Bishop(pieceColour.dark)); this.gameBoard.getSquare(InitialPieceLocations.rightKnightInitialPosition(pieceColour.dark)).placePiece(new Knight(pieceColour.dark)); this.gameBoard.getSquare(InitialPieceLocations.rightRookInitialPosition(pieceColour.dark)).placePiece(new Rook(pieceColour.dark)); this.turn = pieceColour.light; }
public knightSquare(squarePos newPos, pieceColour newColour) : base(newPos, newColour) { type = pieceType.knight; }
public playerException(contender newCulprit, pieceColour responsible) { culprit = newCulprit; culpritCol = responsible; }
/// <summary> /// Resets the board to its initial state. Restarts the game. /// </summary> private void resetBoardToInitialState() { initializeBoardObjects(); turn = pieceColour.light; }
public bool containsPieceNotOfColour(pieceColour ourColour) { return ((type != pieceType.none) && (colour != ourColour)); }
public pawnSquare(squarePos newPos, pieceColour newColour) : base(newPos, newColour) { type = pieceType.pawn; }
/// <summary> /// TRUE if the player specified is in check, FALSE otherwise. /// </summary> /// <param name="playerPossiblyInCheck">The colour to examine</param> /// <returns></returns> public bool isPlayerInCheck(pieceColour playerPossiblyInCheck) { // Check does not exist in queen-and-pawns. if (_type == gameType.queenAndPawns) return false; sizableArray<move> moves = getMoves(getOtherSide(playerPossiblyInCheck)); return moves.Exists(a => a.isCapture && a.capturedSquare.type == pieceType.king); }
/// <summary> /// Retrieve squares containing pieces of the specified colour /// </summary> /// <param name="toMoveColour">Colour of piecs to return</param> /// <returns></returns> public List<square> getPiecesForColour(pieceColour toMoveColour) { if (toMoveColour == pieceColour.white) return whitePieceSquares; else if (toMoveColour == pieceColour.black) return blackPieceSquares; else throw new ArgumentException(); }
/// <summary> /// Return a collecetion of all moves one player may be able to make. /// Note that moves in to check will also be returned. /// </summary> /// <param name="toMoveColour">Side to examine</param> /// <returns></returns> public sizableArray<move> getMoves(pieceColour toMoveColour) { List<square> occupiedSquares = getPiecesForColour(toMoveColour); // Generously guess the size of this array sizableArray<move> possibleMoves = new sizableArray<move>(occupiedSquares.Count * 50); // Add all moves from all pieces foreach (square occupiedSquare in occupiedSquares) possibleMoves.AddRange(occupiedSquare.getPossibleMoves(this)); return possibleMoves; }
/// <summary> /// Is the game won/lost/drawn? /// </summary> /// <param name="toPlay">Side to play</param> /// <returns></returns> public gameStatus getGameStatus(pieceColour toPlay) { List<square> myPieces = getPiecesForColour(toPlay); List<square> enemyPieces = getPiecesForColour(getOtherSide(toPlay)); return getGameStatus(myPieces, enemyPieces); }
public queenSquare(squarePos newPos, pieceColour newColour) : base(newPos, newColour) { type = pieceType.queen; }
public virtual void undoMove(move move) { //sanityCheck(); #if DEBUG if (this[move.dstPos].colour == colToMove) throw new ArgumentException("Unmoving peice of wrong colour"); #endif // undo our fifty-move counter fiftyMoveCounter.Pop(); // remove this position from our list of played positions if (positionsSoFar != null) { string oldPos = positionsSoFar.Pop(); #if DEBUG if(oldPos != this.ToString()) throw new Exception("positionsSoFar contains incorrect pos"); #endif } // Revert any promotion if (move.isPawnPromotion) { square promoted = this[move.dstPos]; removePiece(promoted); this[move.dstPos] = promoted.pastLife; addPiece(promoted.pastLife, move.dstPos); } // dec the move counters moveCount--; this[move.dstPos].movedCount--; if (this[move.dstPos].moveNumbers.Count == 0) this[move.dstPos].moveNumbers = this[move.dstPos].moveNumbers; int popped = this[move.dstPos].moveNumbers.Pop(); #if DEBUG if (popped != moveCount) throw new Exception("moveNumbers contains wrong move count"); #endif // Dec the rook's move counter if this is a castling square castlingRook = null; if (move.isACastling) { castlingRook = this[move.castlingRookDstPos]; castlingRook.movedCount--; int poppedRook = castlingRook.moveNumbers.Pop(); #if DEBUG if (moveCount != poppedRook) throw new Exception("moveNumbers contains wrong move count after popping uncastling rook"); #endif } unmovePiece(move.srcPos, move.dstPos); // If a castling, move the rook back too. if (move.isACastling) { removePiece(castlingRook); // ReSharper disable PossibleNullReferenceException // castlingRook cannot be null at this point, as we set it if there is a castling. this[castlingRook.position] = new square(castlingRook.position); // ReSharper restore PossibleNullReferenceException if (castlingRook.position.x == 3) { addPiece(castlingRook, new squarePos(0, castlingRook.position.y)); } else if (castlingRook.position.x == 5) { addPiece(castlingRook, new squarePos(7, castlingRook.position.y)); } else { throw new Exception("While uncastling, could not find castled rook"); } } // Restore any captured piece if (move.isCapture) addPiece(move.capturedSquare, move.capturedSquarePos); colToMove = getOtherSide(colToMove); sanityCheck(); }
private void verifyWonForWhite(DoktorChessAIBoard ourBoard, pieceColour wonCol) { pieceColour lostCol = DoktorChessAIBoard.getOtherSide(wonCol); // The position should be won/lost for white/black, respectively Assert.IsTrue(ourBoard.getGameStatus(wonCol) == gameStatus.won); Assert.IsTrue(ourBoard.getGameStatus(lostCol) == gameStatus.lost); // and this should be reflected in the scores BoardScorer whiteScorer = new BoardScorer(ourBoard, wonCol, new scoreModifiers()); Assert.AreEqual(BoardScorer.highest, whiteScorer.getScore()); // and won for black. BoardScorer blackScorer = new BoardScorer(ourBoard, lostCol, new scoreModifiers()); Assert.AreEqual(BoardScorer.lowest, blackScorer.getScore()); }
/// <summary> /// Would the specified move put the player of the specified colour in to check? /// </summary> /// <param name="playerCol">Colour which may move in ot check</param> /// <param name="playersMove">Move to examine</param> /// <returns></returns> public bool wouldMovePutPlayerInCheck(pieceColour playerCol, move playersMove) { doMove(playersMove); bool toRet = isPlayerInCheck(playerCol); undoMove(playersMove); return toRet; }
// ReSharper disable MemberCanBeProtected.Global public square(squarePos newPos, pieceColour newColour) { movedCount = 0; position = newPos; colour = newColour; }
/// <summary> /// Add a piece to the board /// </summary> /// <param name="newType">Type of piece to add</param> /// <param name="newColour">Colour of piece to add</param> /// <param name="dstPos">Location of piece to add</param> /// <returns></returns> protected virtual square addPiece(pieceType newType, pieceColour newColour, squarePos dstPos) { this[dstPos] = square.makeSquare(newType, newColour, dstPos); addToArrays(this[dstPos]); return this[dstPos]; }
public bool isThreatened(square squareToCheck, pieceColour sideToExamine) { // Return true if the square is covered by at least one enemy piece. Ignore if we are // covering it or not. pieceColour otherSide = DoktorChessAIBoard.getOtherSide(sideToExamine); return piecesWhichThreatenSquare[squareToCheck.position.x, squareToCheck.position.y].Any(sp => _parentBoard[squarePos.unflatten(sp)].colour == otherSide); }
/// <summary> /// Create a board with the FEN-specified layout /// </summary> /// <param name="FENString">FEN of board to create</param> protected void makeFromFEN(string FENString) { parsedFEN fen = new parsedFEN(FENString); // Now create the board from the parsed output. for (int y = 0; y < sizeY; y++) { for (int x = 0; x < sizeX; x++) { switch (fen.boardRepresentation[x, y]) { case 'p': addPiece(pieceType.pawn, pieceColour.black, x, y); break; case 'r': addPiece(pieceType.rook, pieceColour.black, x, y); break; case 'n': addPiece(pieceType.knight, pieceColour.black, x, y); break; case 'b': addPiece(pieceType.bishop, pieceColour.black, x, y); break; case 'q': addPiece(pieceType.queen, pieceColour.black, x, y); break; case 'k': addPiece(pieceType.king, pieceColour.black, x, y); break; case 'P': addPiece(pieceType.pawn, pieceColour.white, x, y); break; case 'R': addPiece(pieceType.rook, pieceColour.white, x, y); break; case 'N': addPiece(pieceType.knight, pieceColour.white, x, y); break; case 'B': addPiece(pieceType.bishop, pieceColour.white, x, y); break; case 'Q': addPiece(pieceType.queen, pieceColour.white, x, y); break; case 'K': addPiece(pieceType.king, pieceColour.white, x, y); break; case ' ': break; default: throw new ArgumentException(); } } } // Pull in castling rights this[7, 7].excludeFromCastling = !fen.blackCanCastleKingside; this[0, 7].excludeFromCastling = !fen.blackCanCastleQueenside; this[7, 0].excludeFromCastling = !fen.whiteCanCastleKingside; this[0, 0].excludeFromCastling = !fen.whiteCanCastleQueenside; positionsSoFar.Clear(); positionsSoFar.Push(this.ToString()); this.colToMove = fen.toPlay; }
public rookSquare(squarePos newPos, pieceColour newColour) : base(newPos, newColour) { type = pieceType.rook; }
private void makeNormalStartPosition() { // Apply two rows of pawns for (int x = 0; x < sizeX; x++) { addPiece(pieceType.pawn, pieceColour.white, x, 1); addPiece(pieceType.pawn, pieceColour.black, x, 6); } // And now fill in the two end ranks. foreach (int y in new[] { 0, 7 }) { pieceColour col = (y == 0 ? pieceColour.white : pieceColour.black); addPiece(pieceType.rook, col, 0, y); addPiece(pieceType.knight, col, 1, y); addPiece(pieceType.bishop, col, 2, y); addPiece(pieceType.queen, col, 3, y); addPiece(pieceType.king, col, 4, y); addPiece(pieceType.bishop, col, 5, y); addPiece(pieceType.knight, col, 6, y); addPiece(pieceType.rook, col, 7, y); } colToMove = pieceColour.white; }
public playerException(contender newCulprit, pieceColour responsible, string why, Exception e) : base(why, e) { culprit = newCulprit; culpritCol = responsible; }
private void makeQueenAndPawnsStartPosition() { for (int x = 0; x < sizeX; x++) this.addPiece(pieceType.pawn, pieceColour.white, x, 1); this.addPiece(pieceType.queen, pieceColour.black, 3, 7); colToMove = pieceColour.white; }
private static string getImageForPiece(pieceType type, pieceColour colour) { string url = @"images/"; switch (type) { case pieceType.none: url += "none"; break; case pieceType.queen: url += "queen"; break; case pieceType.pawn: url += "pawn"; break; case pieceType.bishop: url += "bishop"; break; case pieceType.rook: url += "rook"; break; case pieceType.king: url += "king"; break; case pieceType.knight: url += "knight"; break; default: throw new ArgumentOutOfRangeException("type"); } url += "-" + colour; url += ".png"; return url; }
private void gameThread() { // Make a new AppDomain for each player. appDomainWhite = createAppDomain(white.assemblyPath, "Tournament game AppDomain (white player)"); appDomainBlack = createAppDomain(black.assemblyPath, "Tournament game AppDomain (black player)"); colToMove = pieceColour.white; // Make new players try { gameBoardWhite = white.makeNewBoard(appDomainWhite); } catch (Exception e) { throw new playerException(white, pieceColour.white, "While attempting to make board: ", e); } try { gameBoardBlack = black.makeNewBoard(appDomainBlack); } catch (Exception e) { throw new playerException(black, pieceColour.black, "While attempting to make board: ", e); } // Fill in our board HTML TextWriter ourTextWriter = new StringWriter(); HtmlTextWriter ourHtmlWriter = new HtmlTextWriter(ourTextWriter); utils.makeTableAndEscapeContents(gameBoardWhite).RenderControl(ourHtmlWriter); boardRepresentation = ourTextWriter.ToString(); // Initialise player times (ms) white.timeLeft = black.timeLeft = 5*60*1000; isRunning = true; while (true) { // Do some quick sanity checks to ensure that both AIs have consistant views of // the situation if (gameBoardWhite.colToMove != colToMove) throw new playerException(white, pieceColour.white, "Player has incorrect the 'next player' value"); if (gameBoardBlack.colToMove != colToMove) throw new playerException(black, pieceColour.black, "Player has incorrect the 'next player' value"); // Okay, checks are ok, so lets play a move! baseBoard boardToMove = colToMove == pieceColour.white ? gameBoardWhite : gameBoardBlack; contender player = colToMove == pieceColour.white ? white : black; int timeLeft = colToMove == pieceColour.white ? white.timeLeft : black.timeLeft; move bestMove; try { moveWithTimeout mwo = new moveWithTimeout(); boardToMove.timeLeftMS = timeLeft; lineAndScore bestLine = mwo.findBestMoveWithTimeout(boardToMove, timeLeft); bestMove = bestLine.line[0]; moveList.Add(bestMove); } catch (Exception e) { throw new playerException(player, colToMove, "During move search: ", e); } // Now play the move on both boards foreach (baseBoard thisContener in new[] { gameBoardBlack, gameBoardWhite }) { try { thisContener.doMove(bestMove); } catch (Exception e) { contender culprit = thisContener == gameBoardBlack ? black : white; pieceColour culpritCol = thisContener == gameBoardBlack ? pieceColour.black : pieceColour.white; throw new playerException(culprit, culpritCol, "While playing move as " + culpritCol + ": ", e); } } pieceColour colJustMoved = colToMove; colToMove = colToMove == pieceColour.white ? pieceColour.black : pieceColour.white; // Extract a graphical representation of the board so we don't need to query it // while the game is running TextWriter ourTextWriter2 = new StringWriter(); HtmlTextWriter ourHtmlWriter2 = new HtmlTextWriter(ourTextWriter2); utils.makeTableAndEscapeContents(gameBoardWhite).RenderControl(ourHtmlWriter2); boardRepresentation = ourTextWriter2.ToString(); // Check that game is still in progrss gameStatus statusWhite; gameStatus statusBlack; try { statusWhite = gameBoardWhite.getGameStatus(colJustMoved); } catch (Exception e) { throw new playerException(white, pieceColour.white, "While evaluating game", e); } try { statusBlack = gameBoardBlack.getGameStatus(colJustMoved); } catch (Exception e) { throw new playerException(black, pieceColour.black, "While evaluating game ", e); } if (statusBlack != statusWhite) { // This could be white or black's fault - we can't tell for sure here. throw new playerException(white, pieceColour.white, "While evaluating game ", new Exception("White and Black disagree on game status")); } if (statusWhite != gameStatus.inProgress) { // OK, the game is over! if (statusWhite == gameStatus.drawn) { isDraw = true; } else { isDraw = false; winningSide = colJustMoved; } break; } } isFinished = true; isRunning = false; gameFinished(gameBoardWhite); }