예제 #1
0
    private void activateValidPrefab(PlayerSymbol tileType)
    {
        blockerPrefab.SetActive(false);
        player1Pipe4Prefab.SetActive(false);
        player2Pipe4Prefab.SetActive(false);
        WalkerPrefab.SetActive(false);

        switch (tileType)
        {
        case PlayerSymbol.Blocker:
            //powerType = GetRandomPower();
            blockerPrefab.SetActive(true);
            break;

        case PlayerSymbol.P1:
            //player1Pipe4Prefab.SetActive(true);
            break;

        case PlayerSymbol.P2:
            // player2Pipe4Prefab.SetActive(true);
            break;

        case PlayerSymbol.Walkable:
            WalkerPrefab.SetActive(true);
            break;
        }
    }
예제 #2
0
        public int FindMove(PlayerSymbol[] board, PlayerSymbol AI)
        {
            var opponent = AI == PlayerSymbol.X ? PlayerSymbol.O : PlayerSymbol.X;
            int move     = FindTwoInARow(board, AI);

            if (move < 0)
            {
                move = FindTwoInARow(board, opponent);
            }
            if (move < 0)
            {
                move = FindFork(board, AI);
            }
            if (move < 0)
            {
                move = FindFork(board, opponent);
            }
            if (move < 0)
            {
                move = CheckCenter(board);
            }
            if (move < 0)
            {
                move = FindOppositeCorner(board, AI, opponent);
            }
            if (move < 0)
            {
                move = FindEmpty(board);
            }
            return(move);
        }
예제 #3
0
        /// <summary>
        /// Handles player status changes, e.g.: players joining or leaving game and player state updates.
        /// </summary>
        /// <param name="sender">
        /// Object sending the event.
        /// </param>
        /// <param name="e">
        /// Event arguments.
        /// </param>
        private void PlayerStatusChanged(object sender, PlayerStatusEventArgs <Player> e)
        {
            switch (e.Status)
            {
            case PlayerStatus.Joined:
                // As long as there's an available symbol to play with, add new player to game.
                // Otherwise, just ignore this new player.
                PlayerSymbol available = FindAvailableSymbol();
                if (PlayerSymbol.None != available)
                {
                    player2SymbolMap[e.Player]         = available;
                    symbol2PlayerMap[available]        = e.Player;
                    symbol2ViewerMap[available].Player = e.Player;
                }

                UpdateStatusDisplay(null);
                break;

            case PlayerStatus.Left:
                // If player was actively playing the game, stop tracking player and let others join.
                if (player2SymbolMap.ContainsKey(e.Player))
                {
                    var symbol = player2SymbolMap[e.Player];
                    symbol2PlayerMap.Remove(symbol);
                    symbol2ViewerMap[symbol].Player = null;
                    player2SymbolMap.Remove(e.Player);
                }

                UpdateStatusDisplay(null);
                break;
            }
        }
예제 #4
0
        private int NumTwoInARow(PlayerSymbol[] board, PlayerSymbol player)
        {
            var WinningRows = new List <(int, int, int)> {
                (0, 1, 2), (3, 4, 5), (6, 7, 8),
                (0, 3, 6), (1, 4, 7), (2, 5, 8),
                (0, 4, 8), (2, 4, 6)
            };
            int result = 0;

            foreach (var row in WinningRows)
            {
                if (board[row.Item1] == player && board[row.Item2] == player && board[row.Item3] == PlayerSymbol.Empty)
                {
                    ++result;
                }
                if (board[row.Item1] == player && board[row.Item3] == player && board[row.Item2] == PlayerSymbol.Empty)
                {
                    ++result;
                }
                if (board[row.Item2] == player && board[row.Item3] == player && board[row.Item1] == PlayerSymbol.Empty)
                {
                    ++result;
                }
            }
            return(result);
        }
예제 #5
0
        private void CheckForWinner()
        {
            bool isFinished = true;
            bool hasWinner  =
                ((_positions[0] & _positions[1] & _positions[2]) == PlayerTurn) ||
                ((_positions[3] & _positions[4] & _positions[5]) == PlayerTurn) ||
                ((_positions[6] & _positions[7] & _positions[8]) == PlayerTurn) ||

                ((_positions[0] & _positions[3] & _positions[6]) == PlayerTurn) ||
                ((_positions[1] & _positions[4] & _positions[7]) == PlayerTurn) ||
                ((_positions[2] & _positions[5] & _positions[8]) == PlayerTurn) ||

                ((_positions[0] & _positions[4] & _positions[8]) == PlayerTurn) ||
                ((_positions[2] & _positions[4] & _positions[6]) == PlayerTurn);

            for (int i = 0; i < LastPosition; i++)
            {
                if (_positions[i] == PlayerSymbol.None)
                {
                    isFinished = false;
                    break;
                }
            }

            if (hasWinner)
            {
                Winner     = PlayerTurn;
                IsFinished = true;
            }
            else
            {
                IsFinished = isFinished;
            }
        }
예제 #6
0
    private void activateValidPrefab(PlayerSymbol tileType)
    {
        blockerPrefab.SetActive(false);
        player1PipePrefab.SetActive(false);
        player2PipePrefab.SetActive(false);
        WalkerPrefab.SetActive(false);

        switch (tileType)
        {
        case PlayerSymbol.Blocker:
            blockerPrefab.SetActive(true);
            break;

        case PlayerSymbol.P1:
            player1PipePrefab.SetActive(true);
            break;

        case PlayerSymbol.P2:
            player2PipePrefab.SetActive(true);
            break;

        case PlayerSymbol.Walkable:
            WalkerPrefab.SetActive(true);
            break;
        }
    }
예제 #7
0
    /* This function solves the Maze problem
     * using Backtracking. It mainly uses
     * solveMazeUtil() to solve the problem.
     * It returns false if no path is possible,
     * otherwise return true and prints the path
     * in the form of 1s. Please note that there
     * may be more than one solutions, this
     * function prints one of the feasible
     * solutions.*/
    private static bool solveMaze(Tile[,] maze, Vector2Int startingPosition, PlayerSymbol playerSymbol)
    {
        //int sol[N][N] = { { 0, 0, 0, 0 },
        //                  { 0, 0, 0, 0 },
        //                  { 0, 0, 0, 0 },
        //                  { 0, 0, 0, 0 } };
        int[,] sol = new int[mapsize, mapsize];
        for (int i = 0; i < mapsize; i++)
        {
            for (int j = 0; j < mapsize; j++)
            {
                sol[i, j] = 0;
            }
        }

        if (solveMazeUtil(maze, startingPosition, sol, playerSymbol)
            == false)
        {
            // printf("Solution doesn't exist");
            return(false);
        }

        //printSolution(sol);
        return(true);
    }
예제 #8
0
 public void setWalkableData()
 {
     //renderer.material = walkableMaterial;
     tileType = PlayerSymbol.Walkable;
     containsPipeGenerator = false;
     activateValidPrefab(tileType);
 }
예제 #9
0
 public void setPlayer2Data()
 {
     //renderer.material = player2Material;
     tileType = PlayerSymbol.P2;
     containsPipeGenerator = true;
     activateValidPrefab(tileType);
 }
예제 #10
0
 public int?[][] TakeAMove(Move move)
 {
     Board[move.Position.X][move.Position.Y] = (int)move.Player.Symbol;
     NextPlayerSymbol = (PlayerSymbol)((int)PlayerSymbol.Circle + (int)PlayerSymbol.Cross - (int)move.Player.Symbol);
     CurrentPlayer    = move.Player;
     return(Board);
 }
        /// <summary>
        /// Determine if the placement of the specified symbol on the specified square resulted in a victory.
        /// </summary>
        /// <param name="symbol">
        /// Symbol that was just recently placed.
        /// </param>
        /// <param name="square">
        /// Square where symbol was placed
        /// </param>
        /// <param name="victorySquares">
        /// In case of victory, this out parameter will hold the set of squares that participated in this victory.
        /// </param>
        /// <returns>
        /// true if placement was determined to result in victory. false otherwise.
        /// </returns>
        private bool IsPlayerVictory(PlayerSymbol symbol, BoardSquare square, out IEnumerable <BoardSquare> victorySquares)
        {
            victorySquares = null;

            if (symbol != square.Symbol)
            {
                return(false);
            }

            var victorySet = new HashSet <BoardSquare>();

            foreach (var direction in VictoryDirections)
            {
                int numSquaresMatching = 0;

                // Find the edge of the board, in current direction from square
                int beginRow    = square.Row;
                int beginColumn = square.Column;
                while ((beginRow - direction[0] >= 0) && (beginColumn - direction[1] >= 0) &&
                       (beginRow - direction[0] < Board.Size) && (beginColumn - direction[1] < Board.Size))
                {
                    beginRow    -= direction[0];
                    beginColumn -= direction[1];
                }

                // go to the other edge of the board, looking for matching squares
                for (int row = beginRow, column = beginColumn;
                     (row >= 0) && (column >= 0) && (row < Board.Size) && (column < Board.Size);
                     row += direction[0], column += direction[1])
                {
                    if (symbol == board.GetAt(row, column).Symbol)
                    {
                        ++numSquaresMatching;
                    }
                }

                // A full board length of matching squares is required for victory
                if (numSquaresMatching < Board.Size)
                {
                    continue;
                }

                // Add the squares as victory squares, but keep iterating because there might be victory squares in other directions
                for (int row = beginRow, column = beginColumn;
                     (row >= 0) && (column >= 0) && (row < Board.Size) && (column < Board.Size);
                     row += direction[0], column += direction[1])
                {
                    victorySet.Add(board.GetAt(row, column));
                }
            }

            // If victory set is non-empty, then there was victory in at least one direction
            if (0 == victorySet.Count)
            {
                return(false);
            }

            victorySquares = victorySet;
            return(true);
        }
예제 #12
0
        int[,] gameMap;             //herna mapa, 0 = volne pole, 1 = hracovo pole, 2 = protivnikovo pole

        public MainWindow()
        {
            InitializeComponent();
            isGame       = false;
            playerSymbol = PlayerSymbol.X;
            isPlayerTurn = true;
        }
예제 #13
0
    private Tile[,] createTiles()
    {
        generateFishersRandomArray(mapSeed);
        for (int x = 0; x < mapSize.x; x++)
        {
            for (int y = 0; y < mapSize.y; y++)
            {
                PlayerSymbol type     = playerSymbolsGeneratedBlockets[x, y];//GetRandomTileType();
                Tile         tempTile = Instantiate(tilePrefab, new Vector3(x * 2, 0f, y * 2), Quaternion.identity);
                tempTile.gameObject.transform.parent = walkablesParent;
                //tempTile.transform.parent = this.transform;
                tempTile.setInitialData(type, playerSymbolsGeneratedBlocketsPowers[x, y], new Vector2Int(x, y));
                if (x == 0)
                {
                    tempTile.isTilePlayer2EndGoal = true;
                }
                if (y == 0)
                {
                    tempTile.isTilePlayer1EndGoal = true;
                }
                tiles[x, y] = tempTile;
            }
        }
        float half = mapSize.x - 1;

        walkablesParent.position = new Vector3(walkablesParent.position.x - half, walkablesParent.position.y, walkablesParent.position.z - half);
        return(tiles);
    }
예제 #14
0
 public void setPlayer1Data()
 {
     //renderer.material = player1Material;
     tileType = PlayerSymbol.P1;
     containsP1PipeGenerator = true;
     containsP2PipeGenerator = false;
     activateValidPrefab(tileType);
 }
예제 #15
0
        public TicTacToe(PlayerSymbol startByPlayer = PlayerSymbol.Circle)
        {
            if (!Enum.IsDefined(startByPlayer) || startByPlayer == PlayerSymbol.None)
            {
                throw new InvalidEnumArgumentException($"Player should be {PlayerSymbol.Circle} or {PlayerSymbol.Cross}.");
            }

            PlayerTurn = startByPlayer;
        }
예제 #16
0
 // constructor using reference
 public GamePlayer(GamePlayer reference)
 {
     if (reference != null)
     {
         playerSymbol = reference.playerSymbol;
         playerType   = reference.playerType;
         playerSprite = reference.playerSprite;
     }
 }
        /// <summary>
        /// Clears previous game state and begins a new Tic-Tac-Toe game.
        /// </summary>
        /// <returns>
        /// Tic tac toa board holding game state.
        /// </returns>
        public Board StartGame()
        {
            this.board.Clear();
            this.board.PopulateDefaultIds();

            this.currentSymbol = SymbolOrder[0];
            this.hasEnded      = false;

            return(this.board);
        }
예제 #18
0
    /* A utility function to check if x,
     * y is valid index for N*N maze */
    private static bool isSafe(Tile[,] maze, int x, int y, PlayerSymbol playerSymbol)
    {
        // if (x, y outside maze) return false
        if (maze[x, y].tileType == playerSymbol)
        {
            return(true);
        }

        return(false);
    }
예제 #19
0
 public GameBoard(PlayerSymbol firstPlayerToStartGame, List <baseRule> rules)
 {
     InitializeGameBoard();
     CurrentErrors    = new List <GameErrorTypes>();
     NextPlayerSymbol = firstPlayerToStartGame;
     GameEnd          = false;
     CurrentPlayer    = null;
     Winner           = null;
     _winningRules    = rules;
 }
예제 #20
0
        public void SetPlayerSymbol(Position position, PlayerSymbol value)
        {
            EnsurePositionIsCorrect(position);

            if (_busyCells.ContainsKey(position))
            {
                throw new InvalidOperationException($"Position {position} is already busy");
            }

            _busyCells[position] = value;
        }
예제 #21
0
        public void SetPlayerSymbol(Position position, PlayerSymbol value)
        {
            EnsurePositionIsCorrect(position);

            if (_busyCells.ContainsKey(position))
            {
                throw new InvalidOperationException($"Position {position} is already busy");
            }

            _busyCells[position] = value;
        }
        /// <summary>
        /// Attempt to place specified symbol in specified board square.
        /// </summary>
        /// <param name="symbol">
        /// Symbol to be placed.
        /// </param>
        /// <param name="square">
        /// Square where placement will be attempted.
        /// </param>
        /// <returns>
        /// PlacementStatus indicating whether the move attempted was valid or invalid.
        /// </returns>
        public PlacingStatus PlaceSymbol(PlayerSymbol symbol, BoardSquare square)
        {
            if (null == square)
            {
                throw new ArgumentException(
                          Properties.Resources.InvalidPlacingSquare, "square");
            }

            // If game has already ended, let player know
            if (this.hasEnded)
            {
                return(PlacingStatus.GameEnded);
            }

            // If square is already occupied, let player know
            if (PlayerSymbol.None != square.Symbol)
            {
                return(PlacingStatus.SquareOccupied);
            }

            // If it's currently not the turn of the player that spoke, let them know
            if (currentSymbol != symbol)
            {
                return(PlacingStatus.OutOfTurn);
            }

            // Move was valid, so place symbol in square
            square.Symbol = symbol;

            // Check if player has won
            IEnumerable <BoardSquare> victorySquares;

            if (IsPlayerVictory(symbol, square, out victorySquares))
            {
                foreach (BoardSquare victorySquare in victorySquares)
                {
                    victorySquare.IsHighlighted = true;
                }

                this.hasEnded = true;
                return(PlacingStatus.Victory);
            }

            if (this.board.IsFull())
            {
                this.hasEnded = true;
                return(PlacingStatus.Draw);
            }

            // If player did not win, it's the other player's turn
            this.currentSymbol = this.currentSymbol.Opponent();

            return(PlacingStatus.Valid);
        }
        /// <summary>
        /// Get symbol-appropriate display text similar to "Player X, say a number".
        /// </summary>
        /// <param name="symbol">
        /// Symbol of interest.
        /// </param>
        /// <returns>
        /// A display-ready string corresponding to the specified symbol. May be null.
        /// </returns>
        public static string GetSayNumberText(this PlayerSymbol symbol)
        {
            switch (symbol)
            {
            case PlayerSymbol.XSymbol:
                return(Properties.Resources.XSayNumber);

            case PlayerSymbol.OSymbol:
                return(Properties.Resources.OSayNumber);
            }

            return(null);
        }
        /// <summary>
        /// Get symbol-appropriate display text similar to "It's "X's" turn".
        /// </summary>
        /// <param name="symbol">
        /// Symbol of interest.
        /// </param>
        /// <returns>
        /// A display-ready string corresponding to the specified symbol. May be null.
        /// </returns>
        public static string GetTurnWarningText(this PlayerSymbol symbol)
        {
            switch (symbol)
            {
            case PlayerSymbol.XSymbol:
                return(Properties.Resources.XTurn);

            case PlayerSymbol.OSymbol:
                return(Properties.Resources.OTurn);
            }

            return(null);
        }
        /// <summary>
        /// Get symbol-appropriate display text similar to "X Won!".
        /// </summary>
        /// <param name="symbol">
        /// Symbol of interest.
        /// </param>
        /// <returns>
        /// A display-ready string corresponding to the specified symbol. May be null.
        /// </returns>
        public static string GetWinAnnouncementText(this PlayerSymbol symbol)
        {
            switch (symbol)
            {
            case PlayerSymbol.XSymbol:
                return(Properties.Resources.XWon);

            case PlayerSymbol.OSymbol:
                return(Properties.Resources.OWon);
            }

            return(null);
        }
        /// <summary>
        /// Draw specified symbol in specified DrawingContext.
        /// </summary>
        /// <param name="symbol">
        /// Symbol to draw.
        /// </param>
        /// <param name="pen">
        /// Pen used to draw symbol.
        /// </param>
        /// <param name="rect">
        /// Bounding rectangle where symbol will be drawn.
        /// </param>
        /// <param name="dc">
        /// DrawingContext used to draw symbol.
        /// </param>
        public static void Draw(this PlayerSymbol symbol, Pen pen, Rect rect, DrawingContext dc)
        {
            switch (symbol)
            {
            case PlayerSymbol.XSymbol:
                DrawX(pen, rect, dc);
                break;

            case PlayerSymbol.OSymbol:
                DrawO(pen, rect, dc);
                break;
            }
        }
        /// <summary>
        /// Returns the opponent corresponding to the specified symbol.
        /// </summary>
        /// <param name="symbol">
        /// Symbol of interest.
        /// </param>
        /// <returns>
        /// The opponent for specified symbol.
        /// </returns>
        public static PlayerSymbol Opponent(this PlayerSymbol symbol)
        {
            switch (symbol)
            {
            case PlayerSymbol.XSymbol:
                return(PlayerSymbol.OSymbol);

            case PlayerSymbol.OSymbol:
                return(PlayerSymbol.XSymbol);
            }

            return(PlayerSymbol.None);
        }
        /// <summary>
        /// The recommended relative rendered thickness for specified symbol.
        /// </summary>
        /// <param name="symbol">
        /// Symbol of interest.
        /// </param>
        /// <returns>
        /// A number in [0.0, 1.0] interval, where 0.0 corresponds to no thickness
        /// and 1.0 corresponds to a thickness equal to the rendered size of symbol.
        /// </returns>
        public static double GetRecommendedThickness(this PlayerSymbol symbol)
        {
            switch (symbol)
            {
            case PlayerSymbol.XSymbol:
                return(RecommendedXThickness);

            case PlayerSymbol.OSymbol:
                return(RecommendedOThickness);
            }

            return(0.0);
        }
예제 #29
0
        /// <summary>
        /// Find the next available symbol to be associated with a player that recently joined.
        /// </summary>
        /// <returns>
        /// First symbol available, in play order, or PlayerSymbol.None if all symbols are
        /// already in use.
        /// </returns>
        private PlayerSymbol FindAvailableSymbol()
        {
            PlayerSymbol available = PlayerSymbol.None;

            foreach (PlayerSymbol symbol in GameLogic.SymbolOrder)
            {
                if (!symbol2PlayerMap.ContainsKey(symbol))
                {
                    available = symbol;
                    break;
                }
            }

            return(available);
        }
예제 #30
0
 private Tile[,] createTiles()
 {
     for (int x = 0; x < mapSize.x; x++)
     {
         for (int y = 0; y < mapSize.y; y++)
         {
             PlayerSymbol type     = GetRandomTileType();
             Tile         tempTile = Instantiate(tilePrefab, new Vector3(x * 2, 0f, y * 2), Quaternion.identity);
             //tempTile.transform.parent = this.transform;
             tempTile.setInitialData(type, new Vector2Int(x, y));
             tiles[x, y] = tempTile;
         }
     }
     return(tiles);
 }
예제 #31
0
    public void ResetGameBoard()
    {
        gameOver();

        for (int i = 0; i < cellList.Length; i++)
        {
            cellList[i].ResetCell();
        }

        ResetWinLine();
        currentPlayerSymbol = PlayerSymbol.X;
        ai.ResetAI();
        currentGameState = GameState.RESUME;
        EnableCellInputs(true);
    }
예제 #32
0
        /// <summary>
        /// Mark a space on the board with the current
        /// player's symbol.
        /// spaceNumber - the index of the space to mark (0-8)
        /// Returns true if the mark was placed, false if the
        /// mark could not be placed (game over, or space already
        /// occupied).
        /// </summary>
        /// <param name="spaceNumber"></param>
        /// <returns></returns>
        public bool Mark(int spaceNumber)
        {
            // Ensure it's a valid move
            if (spaceNumber < 0 || spaceNumber >= SpaceCount || spaces[spaceNumber] != PlayerSymbol.NoOne || IsTied() || GetWinner() != PlayerSymbol.NoOne)
            {
                return false;
            }

            // Mark the space
            spaces[spaceNumber] = turn;

            // See if the game has ended
            PlayerSymbol winner = GetWinner();
            if (IsTied() && Tie != null)
            {
                Tie();
            }
            else if (winner != PlayerSymbol.NoOne && Win != null)
            {
                Win(turn);
            }

            // Switch turns
            if (turn == PlayerSymbol.X)
            {
                turn = PlayerSymbol.O;
            }
            else
            {
                turn = PlayerSymbol.X;
            }
            return true;
        }
예제 #33
0
 /// <summary>
 /// Constructor
 /// </summary>
 public TicTacToeLogic()
 {
     // Start a new game, with X going first
     turn = PlayerSymbol.X;
     Reset();
 }