public Boolean isValidMove(Move t) { PieceMove.Directions? dir = t.getMoveType(); if(dir == null) return false; PieceMove.Directions d = (PieceMove.Directions)dir; switch (this.PieceType) { case Type.p: return this.isValidPawnDirection(d); case Type.k: return this.isValidKingDirection(d); case Type.n: return this.isValidKnightDirection(d); case Type.q: return this.isValidQueenDirection(d); case Type.b: return this.isValidBishopDirection(d); case Type.r: return this.isValidRookDirection(d); } return false; }
public Move findBestMoveForCurrentPlayerNoDoUndo(int sec) { Move m0 = null, result = null; if (this.CurrentTurn < 2 && this.CurrentColor == Piece.Color.White) { if (rand.Next(1, 10) <= 3) return new Move("a2-a3"); return new Move("b2-b3"); } if (this.CurrentTurn < 2 && this.CurrentColor == Piece.Color.Black) { if (lastMoves.Count > 0) { PreviousMove pm = lastMoves.Pop(); switch (pm.Move.ToString()) { case "a2-a3": case "d2-d3": m0 = new Move("d5-d4"); break; default: m0 = new Move("a5-a4"); break; } lastMoves.Push(pm); return m0; } } if (!cache.HasLoadedFile) { cache.TryASyncLoad(); } TimeSpan t = TimeSpan.FromSeconds(sec); int d0 = 2, a0 = 0, v0 = 0, v = 0, negaresult = 0; List<Move> moves = this.getMoves(this.CurrentColor); DateTime start = DateTime.Now; Board board; while (t > DateTime.Now - start) { a0 = -100000; v = -100000; foreach (Move m in moves) { if (t < DateTime.Now - start) { if (result == null) //if we didn't even have time to evaluate one level down, panic and just pick one. result = m; Console.WriteLine("Depth: {0}", d0); return result; } board = new Board(this); if (board.makeMove(m)) { if (board.State != BoardState.Playing) { if (board.State == BoardState.BlackWinner || board.State == BoardState.WhiteWinner) { Console.WriteLine("Depth: {0}", d0); return m; //this move is a winner, pick it! } } negaresult = AI.negaMaxNodoUndo(board, d0, -100000, -a0, (t - (DateTime.Now - start)).TotalSeconds); v0 = Math.Max(v, -negaresult); a0 = Math.Max(a0, v0); if (v0 >= v) m0 = m; v = Math.Max(v, v0); } } ++d0; result = m0; } Console.WriteLine("Depth: {0}", d0); return result; }
public Boolean makeMove(Move move) { if (move == null) return false; if (!isValidSquare(move.To) || !isValidSquare(move.From)) return false; Piece from = this.getPiece(move.From); if (from == null) return false; if (from.PieceColor != CurrentColor) return false; Piece to = this.getPiece(move.To); if (to != null && to.PieceColor == CurrentColor) return false; if (from.isValidMove(move)) { this.board[move.To.x, move.To.y] = from; this.board[move.From.x, move.From.y] = null; PreviousMove lastMove = new PreviousMove(); lastMove.Move = move; lastMove.FromPiece = from; lastMove.ToPiece = to; lastMove.CurrentState = this.State; lastMove.CurrentColor = this.CurrentColor; lastMove.CurrentTurn = this.CurrentTurn; lastMove.CurrentHash = this.Hash; this.lastMoves.Push(lastMove); if (from.PieceType == Piece.Type.p) { if (from.PieceColor == Piece.Color.Black) { if (move.To.x == HEIGHT - 1) { Piece nq = new Piece(Piece.Type.q, from.PieceColor); this.board[move.To.x, move.To.y] = nq; } } else { if (move.To.x == 0) { Piece nq = new Piece(Piece.Type.q, from.PieceColor); this.board[move.To.x, move.To.y] = nq; } } } this.Hash = 0; if (to != null && to.PieceType == Piece.Type.k) this.State = from.PieceColor == Piece.Color.Black ? BoardState.BlackWinner : BoardState.WhiteWinner; else { this.CurrentColor = this.CurrentColor == Piece.Color.Black ? Piece.Color.White : Piece.Color.Black; if (this.CurrentColor == Piece.Color.White) ++this.CurrentTurn; if (CurrentTurn > 40) this.State = BoardState.Draw; } return true; } return false; }
static void Main(string[] args) { if (args.Length > 0) { foreach (string arg in args) { if (arg.Contains("-c:")) { switch (arg.Replace("-c:", "")) { case "B": player = Mode.AIBlack; break; case "A": player = Mode.AIBoth; break; case "N": player = Mode.AINone; break; case "W": default: player = Mode.AIWhite; break; } } if (arg.Contains("-i")) { outputICMS = true; } if (arg.Contains("-t:")) { int usrtime; if (int.TryParse(arg.Replace("-t:", ""), out usrtime)) moveTime = usrtime; } if (arg.Contains("-s")) showBoard = true; if (arg.Contains("-b")) stepOutput = true; } } Board board = new Board(); board.resetBoard(); Random rand = new Random(); Move m = null; Boolean t = false; while (board.State == Board.BoardState.Playing) { if (showBoard) { board.printToStdOut(); Console.WriteLine(); } if (player == Mode.AIBoth || (player == Mode.AIBlack && board.CurrentColor == Piece.Color.Black) || (player == Mode.AIWhite && board.CurrentColor == Piece.Color.White)) { while (!t) { m = board.findBestMoveForCurrentPlayer(moveTime); t = board.makeMove(m); } t = false; if (outputICMS) { Console.WriteLine("Giving my move to skirmish"); Console.Write("! "); } Console.WriteLine(m); } else { while (!t) { if (outputICMS) Console.WriteLine("Getting move from skirmish"); else Console.WriteLine("Please enter your move"); string play = Console.ReadLine(); if (string.IsNullOrEmpty(play)) continue; play = play.Replace("!", ""); play = play.Trim(); m = new Move(play); t = board.makeMove(m); } t = false; } if (stepOutput && !outputICMS) Console.ReadLine(); } if (outputICMS) Console.Write("= "); Console.WriteLine(Board.getStateString(board.State)); AI.saveTTable(); Board.saveCache(); if(stepOutput && !outputICMS) Console.ReadLine(); }