示例#1
0
        //Switch method to find which method to actually use.
        public void GetTouchable(ref BoardState[,] board)
        {
            switch (piece)
            {
            case ChessPieceType.Queen:
                GetQueenTouchable(xCoord, yCoord, ref board);
                break;

            case ChessPieceType.King:
                GetKingTouchable(xCoord, yCoord, ref board);
                break;

            case ChessPieceType.Bishop:
                GetBishopTouchable(xCoord, yCoord, ref board);
                break;

            case ChessPieceType.Knight:
                GetKnightTouchable(xCoord, yCoord, ref board);
                break;

            case ChessPieceType.Rook:
                GetRookTouchable(xCoord, yCoord, ref board);
                break;
            }
        }
示例#2
0
    public Board(int width, int height)
    {
        Width  = width;
        Height = height;

        numRed = numBlack = 0;

        CurrentPlayer   = BoardState.RED;
        CurrentOpponent = BoardState.BLACK;
        Winner          = null;

        isGameOver = false;

        // initialize an array of all possible tiles
        // the number will represent which player holds the tile
        tilesPerColumn = new int[Width];
        for (int i = 0; i < Width; i++)
        {
            tilesPerColumn[i] = 0;
        }

        theBoard = new BoardState[height, width];
        for (int i = 0; i < Height; i++)
        {
            for (int j = 0; j < Width; j++)
            {
                theBoard[i, j] = BoardState.EMPTY;
            }
        }
    }
示例#3
0
        public static void RandomizeShips(Ship[] ships, BoardState[,] board)
        {
            foreach (Ship s in ships)
            {
                s.IsReady = false;
                int x = 0, y = 0;
                do
                {
                    s.position = (Ship.Position)rand.Next(1, 3);
                    switch (s.position)
                    {
                    case Ship.Position.Vertical:
                        y = rand.Next(1, boardSize);
                        x = rand.Next(1, boardSize - (int)s.type);
                        break;

                    case Ship.Position.Horizontal:
                        x = rand.Next(1, boardSize);
                        y = rand.Next(1, boardSize - (int)s.type);
                        break;
                    }
                } while (!CheckPlaceOnBoard(s, board, x, y));
                s.StartPos = new Point(x, y);
                PlaceShipOnBoard(s, board);
            }
        }
示例#4
0
            public void getTouchable(ref BoardState[,] myList)
            {
                switch (piece)
                {
                case ChessPieceType.Queen:
                    getQueenTouchable(xCoord, yCoord, ref myList);
                    break;

                case ChessPieceType.King:
                    getKingTouchable(xCoord, yCoord, ref myList);
                    break;

                case ChessPieceType.Bishop:
                    getBishopTouchable(xCoord, yCoord, ref myList);
                    break;

                case ChessPieceType.Knight:
                    getKnightTouchable(xCoord, yCoord, ref myList);
                    break;

                case ChessPieceType.Rook:
                    getRookTouchable(xCoord, yCoord, ref myList);
                    break;
                }
            }
示例#5
0
 //Just check spaces around knight
 private static void GetKnightTouchable(int x, int y, ref BoardState[,] board)
 {
     for (int i = 0; i < knightOffsets.GetLength(0); i++)
     {
         TrySetCover(x + knightOffsets[i, 0], y + knightOffsets[i, 1], ref board);
     }
 }
示例#6
0
 public ChessPiece(int x, int y, ChessPieceType p, ref BoardState[,] board)
 {
     xCoord      = x;
     yCoord      = y;
     piece       = p;
     board[x, y] = BoardState.Filled;
 }
示例#7
0
        //Main check, checks if piece is in bounds, and attempts to set 'covered' state
        //Returns true if success, else false
        private static bool TrySetCover(int x, int y, ref BoardState[,] board)
        {
            bool inBounds = x >= 0 && x < board.GetLength(0) && y < board.GetLength(0) && y >= 0;

            if (inBounds && board[x, y] != BoardState.Occupied)
            {
                board[x, y] = BoardState.Covered;
                return(true);
            }
            return(false);
        }
示例#8
0
            public static void getKnightTouchable(int x, int y, ref BoardState[,] myList)
            {
                int newX, newY;

                newX = x + 2;
                newY = y + 1;
                if (newX >= 0 && newX < 6 && newY < 6 && newY >= 0 && myList[newX, newY] == BoardState.Empty)
                {
                    myList[newX, newY] = BoardState.Covered;
                }
                newX = x - 2;
                newY = y + 1;
                if (newX >= 0 && newX < 6 && newY < 6 && newY >= 0 && myList[newX, newY] == BoardState.Empty)
                {
                    myList[newX, newY] = BoardState.Covered;
                }
                newX = x + 2;
                newY = y - 1;
                if (newX >= 0 && newX < 6 && newY < 6 && newY >= 0 && myList[newX, newY] == BoardState.Empty)
                {
                    myList[newX, newY] = BoardState.Covered;
                }
                newX = x - 2;
                newY = y - 1;
                if (newX >= 0 && newX < 6 && newY < 6 && newY >= 0 && myList[newX, newY] == BoardState.Empty)
                {
                    myList[newX, newY] = BoardState.Covered;
                }
                newX = x + 1;
                newY = y + 2;
                if (newX >= 0 && newX < 6 && newY < 6 && newY >= 0 && myList[newX, newY] == BoardState.Empty)
                {
                    myList[newX, newY] = BoardState.Covered;
                }
                newX = x - 1;
                newY = y + 2;
                if (newX >= 0 && newX < 6 && newY < 6 && newY >= 0 && myList[newX, newY] == BoardState.Empty)
                {
                    myList[newX, newY] = BoardState.Covered;
                }
                newX = x + 1;
                newY = y - 2;
                if (newX >= 0 && newX < 6 && newY < 6 && newY >= 0 && myList[newX, newY] == BoardState.Empty)
                {
                    myList[newX, newY] = BoardState.Covered;
                }
                newX = x - 1;
                newY = y - 2;
                if (newX >= 0 && newX < 6 && newY < 6 && newY >= 0 && myList[newX, newY] == BoardState.Empty)
                {
                    myList[newX, newY] = BoardState.Covered;
                }
            }
示例#9
0
 public static void ClearReservedSquares(BoardState[,] board)
 {
     for (int i = 1; i < boardSize; i++)
     {
         for (int j = 1; j < boardSize; j++)
         {
             if (board[i, j] == BoardState.Reserved)
             {
                 board[i, j] = BoardState.Empty;
             }
         }
     }
 }
示例#10
0
 //Use offsets, add to x/y, check space. If occupied or current piece is a king check next set
 private static void GetRookTouchable(int x, int y, ref BoardState[,] board, bool isKing = false)
 {
     for (int i = 0; i < rookOffsets.GetLength(0); i++)
     {
         for (int j = x + rookOffsets[i, 0], k = y + rookOffsets[i, 1]; ; j += rookOffsets[i, 0], k += rookOffsets[i, 1])
         {
             if (!TrySetCover(j, k, ref board) || isKing)
             {
                 break;
             }
         }
     }
 }
示例#11
0
    private void Start()
    {
        player.GetComponent <PlayerScript>().moveController.gameObject.SetActive(true);
        board = null;
        if (PlayerPrefs.HasKey("Level"))
        {
            level = PlayerPrefs.GetInt("Level");
        }
        InitializeBoard();
        plantPlayerAndFinishPoint(player, exit);
        int numberOfKillerTrapsPerLevel  = level;
        int numberOfShooterTrapsPerLevel = Mathf.Clamp(level - 3, 0, int.MaxValue);

        plantTraps(killerTrap, numberOfKillerTrapsPerLevel);
        plantTraps(shooterTrap, numberOfShooterTrapsPerLevel);
    }
示例#12
0
 public static void CreateEmptyBoard(BoardState[,] board)
 {
     board[0, 0] = BoardState.CharOrNum;
     for (int i = 1; i < boardSize; i++)
     {
         board[i, 0] = BoardState.CharOrNum;
         board[0, i] = BoardState.CharOrNum;
     }
     for (int i = 1; i < boardSize; i++)
     {
         for (int j = 1; j < boardSize; j++)
         {
             board[i, j] = BoardState.Empty;
         }
     }
 }
示例#13
0
        public static void PlaceShipOnBoard(Ship ship, BoardState[,] board)
        {
            int x = (int)ship.StartPos.X;
            int y = (int)ship.StartPos.Y;

            switch (ship.position)
            {
            case Ship.Position.Vertical:
                for (int i = -1; i < (int)ship.type + 1; i++)
                {
                    for (int j = -1; j < 2; j++)
                    {
                        if (x + i < boardSize && y + j < boardSize &&
                            x + i >= 1 && y + j >= 1)
                        {
                            board[x + i, y + j] = BoardState.Reserved;
                        }
                    }
                }
                foreach (Point p2 in ship.Coordinates)
                {
                    board[(int)p2.X, (int)p2.Y] = BoardState.Ship;
                }
                ship.IsReady = true;
                break;

            case Ship.Position.Horizontal:
                for (int i = -1; i < 2; i++)
                {
                    for (int j = -1; j < (int)ship.type + 1; j++)
                    {
                        if (x + i < boardSize && y + j < boardSize &&
                            x + i >= 1 && y + j >= 1)
                        {
                            board[x + i, y + j] = BoardState.Reserved;
                        }
                    }
                }
                foreach (Point p2 in ship.Coordinates)
                {
                    board[(int)p2.X, (int)p2.Y] = BoardState.Ship;
                }
                ship.IsReady = true;
                break;
            }
        }
示例#14
0
        public static bool CheckPlaceOnBoard(Ship ship, BoardState[,] board, int x, int y)
        {
            switch (ship.position)
            {
            case Ship.Position.Vertical:     //zamieniome

                for (int i = 0; i < (int)ship.type; i++)
                {
                    if (x + i < boardSize && x + i >= 1)
                    {
                        if (board[x + i, y] == BoardState.Reserved ||
                            board[x + i, y] == BoardState.Ship)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                break;

            case Ship.Position.Horizontal:

                for (int i = 0; i < (int)ship.type; i++)
                {
                    if (y + i < boardSize && y + i >= 1)
                    {
                        if (board[x, y + i] == BoardState.Reserved ||
                            board[x, y + i] == BoardState.Ship)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                break;
            }
            return(true);
        }
示例#15
0
            public static void getRookTouchable(int x, int y, ref BoardState[,] myList, bool tempKing = false)
            {
                for (int i = x + 1; i < 6; i++)
                {
                    if (myList[i, y] == BoardState.Empty)
                    {
                        myList[i, y] = BoardState.Covered;
                    }
                    else if (myList[i, y] == BoardState.Filled)
                    {
                        break;
                    }
                    if (tempKing)
                    {
                        break;
                    }
                }

                for (int i = x - 1; i >= 0; i--)
                {
                    if (myList[i, y] == BoardState.Empty)
                    {
                        myList[i, y] = BoardState.Covered;
                    }
                    else if (myList[i, y] == BoardState.Filled)
                    {
                        break;
                    }
                    if (tempKing)
                    {
                        break;
                    }
                }

                for (int i = y + 1; i < 6; i++)
                {
                    if (myList[x, i] == BoardState.Empty)
                    {
                        myList[x, i] = BoardState.Covered;
                    }
                    else if (myList[x, i] == BoardState.Filled)
                    {
                        break;
                    }
                    if (tempKing)
                    {
                        break;
                    }
                }

                for (int i = y - 1; i >= 0; i--)
                {
                    if (myList[x, i] == BoardState.Empty)
                    {
                        myList[x, i] = BoardState.Covered;
                    }
                    else if (myList[x, i] == BoardState.Filled)
                    {
                        break;
                    }
                    if (tempKing)
                    {
                        break;
                    }
                }
            }
示例#16
0
 public static void getKingTouchable(int x, int y, ref BoardState[,] myList)
 {
     getRookTouchable(x, y, ref myList, true);
     getBishopTouchable(x, y, ref myList, true);
 }
示例#17
0
 public ChessModel()
 {
     //winChessesIndex = new int[5];
     board = new BoardState[Board.CROSSCOUNT, Board.CROSSCOUNT];
 }
示例#18
0
        //Check rook and bishop

        private static void GetQueenTouchable(int x, int y, ref BoardState[,] board)
        {
            GetRookTouchable(x, y, ref board);
            GetBishopTouchable(x, y, ref board);
        }
示例#19
0
 public static void getQueenTouchable(int x, int y, ref BoardState[,] myList)
 {
     getRookTouchable(x, y, ref myList);
     getBishopTouchable(x, y, ref myList);
 }
示例#20
0
            public static void getBishopTouchable(int x, int y, ref BoardState[,] myList, bool tempKing = false)
            {
                for (int i = x + 1, j = y + 1; j < 6 && i < 6; i++, j++)
                {
                    if (myList[i, j] == BoardState.Empty)
                    {
                        myList[i, j] = BoardState.Covered;
                    }
                    else if (myList[i, j] == BoardState.Filled)
                    {
                        break;
                    }
                    if (tempKing)
                    {
                        break;
                    }
                }

                for (int i = x - 1, j = y + 1; j < 6 && i >= 0; i--, j++)
                {
                    if (myList[i, j] == BoardState.Empty)
                    {
                        myList[i, j] = BoardState.Covered;
                    }
                    else if (myList[i, j] == BoardState.Filled)
                    {
                        break;
                    }
                    if (tempKing)
                    {
                        break;
                    }
                }

                for (int i = x + 1, j = y - 1; j >= 0 && i < 6; i++, j--)
                {
                    if (myList[i, j] == BoardState.Empty)
                    {
                        myList[i, j] = BoardState.Covered;
                    }
                    else if (myList[i, j] == BoardState.Filled)
                    {
                        break;
                    }
                    if (tempKing)
                    {
                        break;
                    }
                }

                for (int i = x - 1, j = y - 1; j >= 0 && i >= 0; i--, j--)
                {
                    if (myList[i, j] == BoardState.Empty)
                    {
                        myList[i, j] = BoardState.Covered;
                    }
                    else if (myList[i, j] == BoardState.Filled)
                    {
                        break;
                    }
                    if (tempKing)
                    {
                        break;
                    }
                }
            }
示例#21
0
 private static void GetKingTouchable(int x, int y, ref BoardState[,] board)
 {
     GetRookTouchable(x, y, ref board, true);
     GetBishopTouchable(x, y, ref board, true);
 }
示例#22
0
        public static ShotResult Shot(BoardState[,] board, Ship[] ships, Point hit)
        {
            if (board[(int)hit.X, (int)hit.Y] == BoardState.Hit || board[(int)hit.X, (int)hit.Y] == BoardState.Miss)
            {
                return(new ShotResult(hit, board[(int)hit.X, (int)hit.Y], false, null));
            }
            else if (board[(int)hit.X, (int)hit.Y] == BoardState.Empty)
            {
                board[(int)hit.X, (int)hit.Y] = BoardState.Miss;
                return(new ShotResult(hit, BoardState.Empty, false, null));
            }
            else
            {
                foreach (Ship s in ships)
                {
                    foreach (Point p in s.Coordinates)
                    {
                        if (p == hit)
                        {
                            s.Hit();
                            if (!s.IsAlive)
                            {
                                switch (s.position)
                                {
                                case Ship.Position.Vertical:
                                    for (int i = -1; i < (int)s.type + 1; i++)
                                    {
                                        for (int j = -1; j < 2; j++)
                                        {
                                            if (s.StartPos.X + i < boardSize && s.StartPos.Y + j < boardSize &&
                                                s.StartPos.X + i >= 1 && s.StartPos.Y + j >= 1)
                                            {
                                                board[(int)s.StartPos.X + i, (int)s.StartPos.Y + j] = BoardState.Miss;
                                            }
                                        }
                                    }
                                    foreach (Point p2 in s.Coordinates)
                                    {
                                        board[(int)p2.X, (int)p2.Y] = BoardState.Hit;
                                    }
                                    break;

                                case Ship.Position.Horizontal:
                                    for (int i = -1; i < 2; i++)
                                    {
                                        for (int j = -1; j < (int)s.type + 1; j++)
                                        {
                                            if (s.StartPos.X + i < boardSize && s.StartPos.Y + j < boardSize &&
                                                s.StartPos.X + i >= 1 && s.StartPos.Y + j >= 1)
                                            {
                                                board[(int)s.StartPos.X + i, (int)s.StartPos.Y + j] = BoardState.Miss;
                                            }
                                        }
                                    }
                                    foreach (Point p2 in s.Coordinates)
                                    {
                                        board[(int)p2.X, (int)p2.Y] = BoardState.Hit;
                                    }
                                    break;
                                }
                                return(new ShotResult(hit, BoardState.Ship, true, s));
                            }
                            board[(int)hit.X, (int)hit.Y] = BoardState.Hit;
                            return(new ShotResult(hit, BoardState.Ship, false, s));
                        }
                    }
                }
            }
            return(null);
        }