Пример #1
0
    /**
     * determines if the given GridCell is a valid move for the given player
     */
    private bool isValidMove(ReversiGraph graph, ReversiGraph.GridCell given, Player player)
    {
        if (given.State != ReversiGraph.CellState.EMPTY)
        {
            return(false);
        }

        ReversiGraph.CellState seeking = GetSeekingState(player);

        //loop through each possible direction to determine if a move is possible
        foreach (Direction direction in (Direction[])Enum.GetValues(typeof(Direction)))
        {
            if (given.Edges.ContainsKey(direction))
            {
                //traverse the current direction, a move is valid only if we find another same state cell AT LEAST two cells away.
                int depth = graph.Traverse(given, seeking, direction, null, 0);
                if (depth > 1)
                {
                    return(true);
                }
            }
        }

        return(false);
    }
Пример #2
0
    /**
     * performs a move for the given player
     */
    private void performMove(ReversiGraph graph, ReversiGraph.GridCell given, Player player)
    {
        if (given.State != ReversiGraph.CellState.EMPTY)
        {
            return;
        }
        //if ( currentTurn != player ) return;

        //instantiate a new hashset which will be used to keep track of any grid cells which need to be flipped as a result of this move
        HashSet <ReversiGraph.GridCell> flipsNeeded = new HashSet <ReversiGraph.GridCell>();

        //determine which color we are seeking
        ReversiGraph.CellState seeking = GetSeekingState(player);

        //loop through each possible direction and flip any necessary pieces
        foreach (Direction direction in (Direction[])Enum.GetValues(typeof(Direction)))
        {
            //traverse the given direction if needed
            if (given.Edges.ContainsKey(direction))
            {
                //traverse the direction
                graph.Traverse(given, seeking, direction, flipsNeeded, 0);
            }
        }

        //we don't need to flip the current cell (although the code is structured such that this would not cause an issue)
        flipsNeeded.Remove(given);

        //go through and flip each of the cells which need to be flipped
        foreach (ReversiGraph.GridCell cell in flipsNeeded)
        {
            cell.Flip(graph == gameGraph);
        }

        //place a piece at the current position, only if we are working with the actual game board
        if (gameGraph == graph)
        {
            Quaternion rotation = (player == Player.WHITE ? Quaternion.identity : new Quaternion(0f, 0f, 3.1415f, 0f));
            GameObject newPiece = (GameObject)Instantiate(gamePiece, given.SpawnPoint, rotation);
            given.State = seeking;
            given.Model = newPiece;
        }
    }
Пример #3
0
    /**
     * this method will be invoked by the script attached to a square on the Reversi board
     */
    public void itemClicked(string name)
    {
        if (aiVSai || currentTurn == Player.BLACK)
        {
            return;
        }

        ReversiGraph.GridCell selected = gameGraph.Cells[name];

        if (isValidMove(gameGraph, selected, Player.WHITE))
        {
            performMove(gameGraph, selected, Player.WHITE);

            //now process a move for the AI
            currentTurn = Player.BLACK;
            aiMoveReady = true;
        }
        else
        {
            print("NOT VALID LOLZ");
        }
    }