Esempio n. 1
0
        public static void GenerateSuccessors(Node node)
        {
            //Player 1 prioritizes moving up -> left or right -> Wall -> Down (tentative Move Ordering)
            PlayerInfo[]   playerStatus = board.playerStatus;
            int            p            = node.Player;
            int            nextPlayer   = (p + 1) % 2;
            ActionFunction undo         = new ActionFunction(Board.UndoMovePawn, p, playerStatus[p].x, playerStatus[p].y);

            #region Player 1's Successors
            if (node.Player == PLAYER1)  //first check in all if statemtents is the players position in relation to moving
            {
                //Move up
                if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x - 1, playerStatus[p].y))
                {
                    node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x - 1, playerStatus[p].y), undo, nextPlayer));
                }
                else
                {
                    // going up jumping over 1 player directly
                    if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x - 2, playerStatus[p].y))
                    {
                        node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x - 2, playerStatus[p].y), undo, nextPlayer));
                    }
                    // going Up by 1 and Right by 1
                    if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x - 1, playerStatus[p].y + 1))
                    {
                        node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x - 1, playerStatus[p].y + 1), undo, nextPlayer));
                    }
                    //going Left by 1 AND Up by 1
                    if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x - 1, playerStatus[p].y - 1))
                    {
                        node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x - 1, playerStatus[p].y - 1), undo, nextPlayer));
                    }
                }

                //Move Left
                if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x, playerStatus[p].y - 1))
                {
                    node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x, playerStatus[p].y - 1), undo, nextPlayer));
                }
                // going Left jumping over 1 player directly
                else if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x, playerStatus[p].y - 2))
                {
                    node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x, playerStatus[p].y - 2), undo, nextPlayer));
                }

                //Move Right
                if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x, playerStatus[p].y + 1))
                {
                    node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x, playerStatus[p].y + 1), undo, nextPlayer));
                }
                //going Right by jumping over 1 player directly
                else if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x, playerStatus[p].y + 2))
                {
                    node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x, playerStatus[p].y + 2), undo, nextPlayer));
                }

                //Move Down
                // going down by 1 (checks on location and for walls)
                if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x + 1, playerStatus[p].y))
                {
                    node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x + 1, playerStatus[p].y), undo, nextPlayer));
                }
                else
                {
                    // going down jumping over 1 player directly
                    if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x + 2, playerStatus[p].y))
                    {
                        node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x + 2, playerStatus[p].y), undo, nextPlayer));
                    }
                    // going Down by 1 AND to the Left by 1
                    if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x + 1, playerStatus[p].y - 1))
                    {
                        node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x + 1, playerStatus[p].y - 1), undo, nextPlayer));
                    }
                    // going Right by 1 AND Down by 1
                    if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x - 1, playerStatus[p].y + 1))
                    {
                        node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x - 1, playerStatus[p].y + 1), undo, nextPlayer));
                    }
                }
            }
            #endregion
            #region Player 2's Successors
            else
            {
                //Player 2 prioritizes moving down -> left or right -> Wall -> Up (tentative Move Ordering)
                //Move Down
                // going down by 1 (checks on location and for walls)
                if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x + 1, playerStatus[p].y))
                {
                    node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x + 1, playerStatus[p].y), undo, nextPlayer));
                }
                else
                {
                    // going down jumping over 1 player directly
                    if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x + 2, playerStatus[p].y))
                    {
                        node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x + 2, playerStatus[p].y), undo, nextPlayer));
                    }
                    // going Down by 1 AND to the Left by 1
                    if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x + 1, playerStatus[p].y - 1))
                    {
                        node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x + 1, playerStatus[p].y - 1), undo, nextPlayer));
                    }
                    // going Right by 1 AND Down by 1
                    if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x + 1, playerStatus[p].y + 1))
                    {
                        node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x + 1, playerStatus[p].y + 1), undo, nextPlayer));
                    }
                }


                //Move Left
                //   if (playerStatus[p].y - 1 >= 0)
                //  {
                if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x, playerStatus[p].y - 1))
                {
                    node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x, playerStatus[p].y - 1), undo, nextPlayer));
                }
                // going Left jumping over 1 player directly
                else if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x, playerStatus[p].y - 2))
                {
                    node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x, playerStatus[p].y - 2), undo, nextPlayer));
                }
                //}
                //Move Right
                if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x, playerStatus[p].y + 1))
                {
                    node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x, playerStatus[p].y + 1), undo, nextPlayer));
                }
                //going Right jumping over 1 player directly
                else if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x, playerStatus[p].y + 2))
                {
                    node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x, playerStatus[p].y + 2), undo, nextPlayer));
                }

                //Move up
                if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x - 1, playerStatus[p].y))
                {
                    node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x - 1, playerStatus[p].y), undo, nextPlayer));
                }
                else
                {
                    // going up jumping over 1 player directly
                    if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x - 2, playerStatus[p].y))
                    {
                        node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x - 2, playerStatus[p].y), undo, nextPlayer));
                    }
                    // going Up by 1 and Right by 1
                    if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x - 1, playerStatus[p].y + 1))
                    {
                        node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x - 1, playerStatus[p].y + 1), undo, nextPlayer));
                    }
                    //going Left by 1 AND Up by 1
                    if (board.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, playerStatus[p].x - 1, playerStatus[p].y - 1))
                    {
                        node.Children.Add(new Node(new ActionFunction(Board.MovePawn, p, playerStatus[p].x - 1, playerStatus[p].y - 1), undo, nextPlayer));
                    }
                }
            }
            #endregion

            #region Wall Generation (for both players) taking too long
            if (playerStatus[p].wallsLeft == 0) // player has no wall left
            {
                return;
            }
            ActionFunction undoWall;
            for (int x = 0; x < Board.BOARD_SIZE - 1; x++)
            {
                for (int y = 0; y < Board.BOARD_SIZE - 1; y++)
                {
                    //Horizontal Wall
                    if (board.CheckWallH(x, y))
                    {
                        undoWall = new ActionFunction(Board.UndoPlaceHorizontalWall, p, x, y);
                        node.Children.Add(new Node(new ActionFunction(Board.PlaceHorizontalWall, p, x, y), undoWall, nextPlayer));
                    }
                    //Vertical Wall
                    if (board.CheckWallV(x, y))
                    {
                        undoWall = new ActionFunction(Board.UndoPlaceVerticalWall, p, x, y);
                        node.Children.Add(new Node(new ActionFunction(Board.PlaceVerticalWall, p, x, y), undoWall, nextPlayer));
                    }
                }
            }
            #endregion
        }
Esempio n. 2
0
    private IEnumerator PlayersTurn(int p) // Player Controls
    {
        MessageText.text = "";
        //int p = playerNum - 1;
        //move or choose wall
        playerStatus[p].currentTurn = true;
        playersTurnText.text        = "Player " + (p + 1) + "'s Turn!";
        while (playerStatus[p].currentTurn)
        {
            // Passing Turn
            if (Input.GetKeyUp(KeyCode.X))
            {
                playersTurnText.text        = "Player " + (p + 1) + " Passes!";
                playerStatus[p].currentTurn = false;
                yield return(m_StartWait);
//                Debug.Log("Player " + (p + 1) + "Passes.");
            }

            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            bool       collisionFound = Physics.Raycast(ray, out hit);

            // Moving Handler
            if (Input.GetMouseButtonUp(0))
            {
                GameObject tempObj;
                if (collisionFound)
                {
                    if (hit.transform.tag == "Board")
                    {
                        gameSquareInfo tempSquare;
                        int            xDiff;
                        int            yDiff;
                        tempObj    = hit.collider.gameObject;
                        tempSquare = tempObj.GetComponent <gameSquareInfo>();

                        xDiff = tempSquare.x - playerStatus[p].x;
                        yDiff = tempSquare.y - playerStatus[p].y;

                        //no matter what, chosen board must be available
                        if (tempSquare.isOpen)
                        {
                            // DOWN-----------------------------------------------------------
                            // going down by 1 (checks on location and for walls)
                            if (xDiff == 1 && yDiff == 0 &&
                                MainBoard.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, tempSquare.x, tempSquare.y))
                            {
                                RenderPawnPosition(p, tempSquare.x, tempSquare.y);
                                Board.MovePawn(MainBoard, p, tempSquare.x, tempSquare.y);
                                playerStatus[p].currentTurn = false;
                            }
                            // going down jumping over 1 player directly
                            else if (xDiff == 2 && yDiff == 0 &&
                                     MainBoard.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, tempSquare.x, tempSquare.y))
                            {
                                RenderPawnPosition(p, tempSquare.x, tempSquare.y);
                                Board.MovePawn(MainBoard, p, tempSquare.x, tempSquare.y);
                                playerStatus[p].currentTurn = false;
                            }
                            // going Down by 1 AND to the Left by 1
                            else if (xDiff == 1 && yDiff == -1 &&
                                     MainBoard.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, tempSquare.x, tempSquare.y))
                            {
                                RenderPawnPosition(p, tempSquare.x, tempSquare.y);
                                Board.MovePawn(MainBoard, p, tempSquare.x, tempSquare.y);
                                playerStatus[p].currentTurn = false;
                            }

                            // UP-----------------------------------------------------------------------
                            // going up by 1 (checks on location and for walls)
                            else if (xDiff == -1 && yDiff == 0 &&
                                     MainBoard.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, tempSquare.x, tempSquare.y))
                            {
                                RenderPawnPosition(p, tempSquare.x, tempSquare.y);
                                Board.MovePawn(MainBoard, p, tempSquare.x, tempSquare.y);
                                playerStatus[p].currentTurn = false;
                            }
                            // going up jumping over 1 player directly
                            else if (xDiff == -2 && yDiff == 0 &&
                                     MainBoard.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, tempSquare.x, tempSquare.y))
                            {
                                RenderPawnPosition(p, tempSquare.x, tempSquare.y);
                                Board.MovePawn(MainBoard, p, tempSquare.x, tempSquare.y);
                                playerStatus[p].currentTurn = false;
                            }
                            // going Up by 1 AND Right by 1
                            else if (xDiff == -1 && yDiff == 1 &&
                                     MainBoard.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, tempSquare.x, tempSquare.y))
                            {
                                RenderPawnPosition(p, tempSquare.x, tempSquare.y);
                                Board.MovePawn(MainBoard, p, tempSquare.x, tempSquare.y);
                                playerStatus[p].currentTurn = false;
                            }

                            // LEFT-----------------------------------------------------------------------
                            // going Left by 1 (checks on location and for walls)
                            else if (xDiff == 0 && yDiff == -1 &&
                                     MainBoard.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, tempSquare.x, tempSquare.y))
                            {
                                RenderPawnPosition(p, tempSquare.x, tempSquare.y);
                                Board.MovePawn(MainBoard, p, tempSquare.x, tempSquare.y);
                                playerStatus[p].currentTurn = false;
                            }
                            // going Left jumping over 1 player directly
                            else if (xDiff == 0 && yDiff == -2 &&
                                     MainBoard.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, tempSquare.x, tempSquare.y))
                            {
                                RenderPawnPosition(p, tempSquare.x, tempSquare.y);
                                Board.MovePawn(MainBoard, p, tempSquare.x, tempSquare.y);
                                playerStatus[p].currentTurn = false;
                            }
                            //going Left by 1 AND Up by 1
                            else if (xDiff == -1 && yDiff == -1 &&
                                     MainBoard.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, tempSquare.x, tempSquare.y))
                            {
                                RenderPawnPosition(p, tempSquare.x, tempSquare.y);
                                Board.MovePawn(MainBoard, p, tempSquare.x, tempSquare.y);
                                playerStatus[p].currentTurn = false;
                            }

                            // RIGHT-----------------------------------------------------------------------
                            // going Right by 1 (checks on location and for walls)
                            else if (xDiff == 0 && yDiff == 1 &&
                                     MainBoard.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, tempSquare.x, tempSquare.y))
                            {
                                RenderPawnPosition(p, tempSquare.x, tempSquare.y);
                                Board.MovePawn(MainBoard, p, tempSquare.x, tempSquare.y);
                                playerStatus[p].currentTurn = false;
                            }
                            else if (xDiff == 0 && yDiff == 2 &&
                                     MainBoard.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, tempSquare.x, tempSquare.y))
                            {
                                RenderPawnPosition(p, tempSquare.x, tempSquare.y);
                                Board.MovePawn(MainBoard, p, tempSquare.x, tempSquare.y);
                                playerStatus[p].currentTurn = false;
                            }
                            // going Right by 1 AND Down by 1
                            else if (xDiff == 1 && yDiff == 1 &&
                                     MainBoard.IsPawnMoveLegal(playerStatus[p].x, playerStatus[p].y, tempSquare.x, tempSquare.y))
                            {
                                RenderPawnPosition(p, tempSquare.x, tempSquare.y);
                                Board.MovePawn(MainBoard, p, tempSquare.x, tempSquare.y);
                                playerStatus[p].currentTurn = false;
                            }
                        }
                    } //end of "Board" tag
                      //PLACING HORIZONTAL WALLS via Left-Click--------------------------------------------------
                    if (hit.transform.tag == "Peg")
                    {
                        WallPeg tempPeg;
                        tempObj = hit.collider.gameObject;
                        tempPeg = tempObj.GetComponent <WallPeg>();

                        //if wall can be placed, place it and end turn
                        if (playerStatus[p].wallsLeft > 0 && MainBoard.CheckWallH(tempPeg.x, tempPeg.y))
                        {
                            RenderWall(tempPeg.x, tempPeg.y, true);
                            Board.PlaceHorizontalWall(MainBoard, p, tempPeg.x, tempPeg.y);
                            playerStatus[p].currentTurn = false;
                            UpdateWallRemTxt();
                        }
                    }
                }
            }//end of Left Click if-statement

            //PLACING VERTICAL WALLS via Right-Click---------------------------------------------------
            if (Input.GetMouseButtonUp(1))
            {
                if (collisionFound)
                {
                    GameObject tempObj;
                    if (hit.transform.tag == "Peg")
                    {
                        WallPeg tempPeg;
                        tempObj = hit.collider.gameObject;
                        tempPeg = tempObj.GetComponent <WallPeg>();

                        //if wall can be placed, place it and end turn
                        if (playerStatus[p].wallsLeft > 0 && MainBoard.CheckWallV(tempPeg.x, tempPeg.y))
                        {
                            RenderWall(tempPeg.x, tempPeg.y, false);
                            Board.PlaceVerticalWall(MainBoard, p, tempPeg.x, tempPeg.y);
                            playerStatus[p].currentTurn = false;
                            UpdateWallRemTxt();
                        }
                    }
                }
            }//end of Right Click if statement


            yield return(null);
        }// end of current turn while loop
    }