Ejemplo n.º 1
0
 public PieceMove(ChessNode start, ChessNode end)
 {
     this.board  = start.board;
     this.start  = start.GetCord();
     this.end    = end.GetCord();
     playerColor = start.piece.color;
 }
Ejemplo n.º 2
0
 public ChessPiece(ChessNode node, PieceType type, PlayerColor player, bool moved = false)
 {
     this.node  = node;
     this.type  = type;
     this.color = player;
     this.moved = moved;
 }
Ejemplo n.º 3
0
 public ChessPiece(ChessPiece piece)
 {
     this.node  = piece.node;
     this.type  = piece.type;
     this.color = piece.color;
     this.moved = piece.moved;
 }
Ejemplo n.º 4
0
 public ComplexMove(ChessNode start, ChessNode end,
                    ChessNode secondaryStart, ChessNode secondaryEnd, bool valid) : base(start, end)
 {
     this.secondaryEnd   = secondaryEnd.GetCord();
     this.secondaryStart = secondaryStart.GetCord();
     this.valid          = true;
     this.validated      = true;
 }
Ejemplo n.º 5
0
 public static bool CheckOverlap(List <PieceMove> moves, ChessNode node)
 {
     foreach (var move in moves)
     {
         if (move.CheckOverlap(node))
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 6
0
    public bool ReplaceWithAnother(ChessNode node)
    {
        if (piece != null)
        {
            board.pieces[piece.color].Remove(piece);
        }
        piece      = node.piece;
        piece.node = this;

        node.piece = null;
        return(true);
    }
Ejemplo n.º 7
0
    public int AddMove(List <PieceMove> list, int offset_x, int offset_y)
    {
        // Add the given move into the list,
        // if the node exist, and it doesnt contain an ally on it. ( we dont wanna eat an ally )
        ChessNode current = node.GetNodeFrom(offset_x, offset_y);

        if (current == null)
        {
            return(0);
        }
        //Debug.Log (current.x + " , " + current.y + " ,,," + offset_x + " , " + offset_y);
        if (current != node && !IsAlly(current.piece))
        {
            list.Add(new PieceMove(node, current));
            return(current.piece == null ? 1 : -1);
        }
        return(0);
    }
Ejemplo n.º 8
0
 public ChessBoard()
 {
     // Create a plain new Board
     pieces.Add(white, new List <ChessPiece> ());
     pieces.Add(black, new List <ChessPiece> ());
     for (int y = 0; y < 8; y++)
     {
         for (int x = 0; x < 8; x++)
         {
             nodes[x, y] = new ChessNode(this, x, y);
         }
     }
     for (int i = 0; i < 8; i++)
     {
         nodes[i, 6].InitializePiece(PieceType.Pawn, black, this);
         nodes[i, 1].InitializePiece(PieceType.Pawn, white, this);
     }
     initSide(7, black);
     initSide(0, white);
 }
Ejemplo n.º 9
0
        public override Action ChooseMove()
        {
            elapsedTime = Time.realtimeSinceStartup;

            while(true){

                computerTeam.Sort();
                ChessNode act = Search(oldRoot, d);

                if(Time.realtimeSinceStartup - elapsedTime > maxTime){
                    break;
                }
                d++;
                cResult = FindNextAction(act);

                oldRoot = new ChessNode();

            }
            oldRoot = new ChessNode();
            d = 2;
            return cResult;
        }
Ejemplo n.º 10
0
    public bool IsChecked(PlayerColor color)
    {
        var       enemyColor = color == PlayerColor.white ? PlayerColor.black : PlayerColor.white;
        ChessNode node       = kings[color].node;

        foreach (PieceType type in System.Enum.GetValues(typeof(PieceType)))
        {
            if (type == PieceType.none)
            {
                continue;
            }
            var piece = new ChessPiece(node, type, color, true);
            foreach (var move in piece.GetMoves())
            {
                var current = GetPieceAt(move.end.x, move.end.y);
                if (current != null && current.type == piece.type && current.color == enemyColor)
                {
                    return(true);
                }
            }
        }
        return(false);
    }
Ejemplo n.º 11
0
    public ChessBoard(Dictionary <PlayerColor, List <ChessPiece> > copy, PlayerColor nextTurn)
    {
        // Generate a copy of a board
        this.currentPlayer = nextTurn;
        this.pieces.Add(white, new List <ChessPiece> ());
        this.pieces.Add(black, new List <ChessPiece> ());
        for (int y = 0; y < 8; y++)
        {
            for (int x = 0; x < 8; x++)
            {
                nodes[x, y] = new ChessNode(this, x, y);
            }
        }

        foreach (var piece in copy[black])
        {
            nodes[piece.node.x, piece.node.y].InitializePiece(piece.type, piece.color, this, piece.moved);
        }
        foreach (var piece in copy[white])
        {
            nodes[piece.node.x, piece.node.y].InitializePiece(piece.type, piece.color, this, piece.moved);
        }
    }
Ejemplo n.º 12
0
        Action FindNextAction(ChessNode node)
        {
            while(node.Father().Father() != null){
                node = (ChessNode)node.Father();
            }

            return node.GetData();
        }
Ejemplo n.º 13
0
 public bool Overlaps(ChessNode node)
 {
     return(node.x == x && node.y == y);
 }
Ejemplo n.º 14
0
    /// <summary>
    /// Abstract function used to get all the possible children of the node
    /// </summary>
    /// <returns>All child nodes</returns>
    public override List<MinMaxNode> getChildren()
    {
        //System.Diagnostics.Stopwatch stp = new System.Diagnostics.Stopwatch();
        //stp.Start();

        ChessPiece playerMax = TurnManager.Singleton.CurrentTurn.PlayerColor;
        ChessPiece playerMin = playerMax == ChessPiece.WHITE ? ChessPiece.BLACK : ChessPiece.WHITE;

        ChessPiece color = ChessPiece.NONE;
        color = NodeType == global::NodeType.MAX ? playerMax : playerMin;

        List<BoardStatus> childrenBoards = m_board.getAllBoardMovements(color);

        List<MinMaxNode> res = new List<MinMaxNode>();
        ChessNode node = null;
        for (int i = 0; i < childrenBoards.Count; ++i)
        {
            node = new ChessNode(childrenBoards[i], Depth - 1, this, NodeType == global::NodeType.MIN ? global::NodeType.MAX : global::NodeType.MIN);
            res.Add(node);
        }

        //stp.Stop();
        //DataTable data = StorageMgr.Blackboard.Get<DataTable>("Tiempos");
        //DataTable depthData = (DataTable)data[Depth];
        //depthData.Add(stp.ElapsedMilliseconds);

        return res;
    }
Ejemplo n.º 15
0
 /// <summary>
 /// Node constructor
 /// </summary>
 /// <param name="board">Board status</param>
 /// <param name="depth">Depth from the origin node</param>
 /// <param name="parentNode">Parent node</param>
 /// <param name="nodeType">Node type</param>
 public ChessNode(BoardStatus board, int depth, ChessNode parentNode, NodeType nodeType)
     : base(depth, parentNode, nodeType)
 {
     m_board = board;
 }
Ejemplo n.º 16
0
 private ChessNode Min(ChessNode a, ChessNode b)
 {
     if(a==null) return b;
     if(b==null) return a;
     return (a.Utility()<=b.Utility())?a:b;
 }
Ejemplo n.º 17
0
 public bool CheckOverlap(ChessNode node)
 {
     return(end.Overlaps(node));
 }
Ejemplo n.º 18
0
        ChessNode MinSearch(ChessNode node, int level, int alpha, int beta)
        {
            if(node.GetData() != null){
                if(_board.TerminalState(node.GetData())){
                    node.SetUtility(node.Utility() + level);
                    return node;
                }
                else if(level == 1){
                    node.SetUtility(Util () - level);
                    return node;
                }
            }

            if(Time.realtimeSinceStartup - elapsedTime > maxTime){
                return node;
            }

            int v = int.MaxValue;
            ChessNode result = null;

            foreach(Action cAction in GetPossibleMove(1)){
                ChessNode cNode = new ChessNode(node, cAction);
                _board.ApplyAction(cAction);
                result = Min (result, MaxSearch(cNode, level-1,alpha, beta));
                _board.RevertAction(cAction);
                if(result != null)
                    v = result.Utility();
                beta = Math.Min(beta, v);
                if(beta <= alpha){
                    return result;
                }
            }

            return result;
        }
Ejemplo n.º 19
0
 public bool Overlaps(ChessNode node)
 {
     return(this.x == node.x && this.y == node.y);
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Class constructor
 /// </summary>
 /// <param name="depth">Depth from the origin node</param>
 /// <param name="parentNode">Parent node</param>
 /// <param name="nodeType">Node type</param>
 public MinMaxNode(int depth, ChessNode parentNode, NodeType nodeType)
 {
     m_Depth = depth;
     m_parent = parentNode;
     m_NodeType = nodeType;
 }
Ejemplo n.º 21
0
 ChessNode Search(ChessNode root, int level)
 {
     return MaxSearch(root, level,int.MinValue, int.MaxValue);
 }
Ejemplo n.º 22
0
 public void SetNode(ChessNode node)
 {
     this.node = node;
     UpdateImage();
     move = null;
 }