//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; } }
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; } } }
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); } }
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; } }
//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); } }
public ChessPiece(int x, int y, ChessPieceType p, ref BoardState[,] board) { xCoord = x; yCoord = y; piece = p; board[x, y] = BoardState.Filled; }
//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); }
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; } }
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; } } } }
//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; } } } }
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); }
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; } } }
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; } }
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); }
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; } } }
public static void getKingTouchable(int x, int y, ref BoardState[,] myList) { getRookTouchable(x, y, ref myList, true); getBishopTouchable(x, y, ref myList, true); }
public ChessModel() { //winChessesIndex = new int[5]; board = new BoardState[Board.CROSSCOUNT, Board.CROSSCOUNT]; }
//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); }
public static void getQueenTouchable(int x, int y, ref BoardState[,] myList) { getRookTouchable(x, y, ref myList); getBishopTouchable(x, y, ref myList); }
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; } } }
private static void GetKingTouchable(int x, int y, ref BoardState[,] board) { GetRookTouchable(x, y, ref board, true); GetBishopTouchable(x, y, ref board, true); }
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); }