// constructors // copy constructor public Board(Board b) { playerOnTurn = b.playerOnTurn; board = new BoardCellState[b.board.GetLength(0), b.board.GetLength(1)]; Array.Copy(b.board, board, board.Length); updateScore(); }
public List <Action> MakeMove(int i, int j) { //Debug.Log("Move to " + i + " " + j); // simulate the move Board nextBoard = SimulateMove(i, j); // prepare action messages List <Action> actions = new List <Action>(); ForEachBoardCell((int ii, int jj) => { if (board[ii, jj] != nextBoard.board[ii, jj]) { IntPair cell = new IntPair(ii, jj); if (i == ii && j == jj) { // it is a split, find split cell IntPair splitCell = new IntPair(-1, -1); ForEachCellNeighbour(ii, jj, (int si, int sj) => { if (board[si, sj] == playerOnTurn) { splitCell = new IntPair(si, sj); } }); // if no cell found spawn instead if (splitCell.i == -1) { actions.Add(new Action(ActionType.Spawn, cell)); } else { actions.Add(new Action(ActionType.Split, cell, splitCell)); // split } } else if (board[ii, jj] == BoardCellState.Empty) { actions.Add(new Action(ActionType.Spawn, cell)); // spawn } else { actions.Add(new Action(ActionType.Convert, cell)); // convert } } }); // update the board // just to be sure that I do not have a memory leak copy manually playerOnTurn = nextBoard.playerOnTurn; ForEachBoardCell((int ii, int jj) => { board[ii, jj] = nextBoard.board[ii, jj]; }); // update score updateScore(); return(actions); }
public void SetState(BoardCellState state) { if (this.state == state) { return; } this.state = state; }
public void Appear(BoardCellState _state) { if (state != _state) { state = _state; animator.SetInteger("Player", playerAnimId[(int)state]); PlayAnimation("Idle"); } }
// convert bacteria public void Convert() { state = state.Opponent(); animator.SetInteger("Player", playerAnimId[(int)state]); PlayAnimation("Convert"); GlobalAnimationTimer.AnimationTriggered(convertAnimationClip); // play audio MusicManagerSingleton.Instance.playSound(popAudio, audio); }
/// <summary> /// Check if all elements of the list have the same BoardCellState /// </summary> private bool IsBoardCellList(List <Point> list, BoardCellState boardCell, Board knowBoard) { foreach (Point p in list) { if (knowBoard.GetCellState(p.X, p.Y) != boardCell) { return(false); } } return(true); }
public void setHighlighted(bool highlighted, BoardCellState state = BoardCellState.Empty) { if (highlighted) { gameObject.GetComponent <SpriteRenderer>().color = (state == BoardCellState.Player1 ? Player1Tint : Player2Tint); renderer.enabled = true; } else { renderer.enabled = false; } }
public void Spawn(BoardCellState _state) { if (state != _state) { state = _state; animator.SetInteger("Player", playerAnimId[(int)state]); PlayAnimation("Spawn"); GlobalAnimationTimer.AnimationTriggered(spawnAnimationClip); // play audio MusicManagerSingleton.Instance.playSound(spawnAudio, audio); } }
public void StartNew() { Board = new BoardCellState[3, 3]; for (var i = 0; i < 3; i++) { for (var j = 0; j < 3; j++) { Board[i, j] = BoardCellState.Empty; } } CurrentPlayer = Players.X; }
// called from BoardCellController when creating the board public void Initialize(BoardBuilder parent, int _iPos, int _jPos) { boardBuilder = parent; iPos = _iPos; jPos = _jPos; state = BoardCellState.Empty; cellAnim = gameObject.GetComponentInChildren <CellAnimator>(); animator = cellAnim.GetComponent <Animator>(); cellHighlight = gameObject.GetComponentInChildren <CellHighlight>(); cellBackground = gameObject.GetComponentInChildren <CellBackground>(); cellBackground.Init(iPos, jPos); }
public void SetBoardCell(BoardCell cell) { Vector2Int pos = cell.GetPosition(); int x = pos.x; int y = pos.y; // Board cell BoardCellState cellState = new BoardCellState(); cellState.id = cell.GetObjectID(); cellState.x = x; cellState.y = y; cellState.args = cell.GetArgs(); cells[(x - 1) + (y - 1) * columns] = cellState; }
public Board(int[,] boardSetup, BoardCellState playerOnTurn = BoardCellState.Player1) { this.playerOnTurn = playerOnTurn; // init the board from boardSetup board = new BoardCellState[boardSetup.GetLength(0), boardSetup.GetLength(1)]; for (int i = 0; i < board.GetLength(0); i++) { for (int j = 0; j < board.GetLength(1); j++) { board[i, j] = (BoardCellState)boardSetup[i, j]; } } updateScore(); }
/// <summary> /// Draws a ship on a board /// </summary> /// <param name="ship">Ship to draw</param> /// <param name="state">Ship state to draw</param> /// <param name="force">True to force ship drawing</param> private void DrawShip(Ship ship, BoardCellState state, bool force) { if (!_drawShips && !force) { return; } var points = ship.GetShipRegion().GetPoints(); foreach (var point in points) { if (BoardRegion.Contains(point)) { _cells[point.X, point.Y].State = state; } } }
public static BoardCellState GetStartingPlayer() { startingPlayer = startingPlayer.Opponent(); return(startingPlayer); }
public BoardCell(int x, int y, BoardCellState state = BoardCellState.BOARD_CELL_STATE_NORMAL) { X = x; Y = y; this.State = state; }
// interface for highlighting //public bool getHighlighted() { return cellHighlight.getHighlighted(); } public void setHighlighted(bool highlighted, BoardCellState _state = BoardCellState.Empty) { cellHighlight.setHighlighted(highlighted, _state); }
void Start() { // apply settings switch (GlobalState.gameMode) { case 0: playerVsAI = false; break; case 1: playerVsAI = true; break; default: throw new UnityException("Invalid game mode."); } switch (GlobalState.board) { case 0: boardSetup = board0; break; case 1: boardSetup = board1; break; case 2: boardSetup = board2; break; default: throw new UnityException("Invalid board."); } board = new Board(boardSetup, GlobalState.GetStartingPlayer()); // compute layout vectors // i - rows, j - columns Vector3 iVector = new Vector3(0, -cellDistance, 0); Vector3 jVector = new Vector3(Mathf.Cos(-Mathf.PI / 6) * cellDistance, -Mathf.Sin(-Mathf.PI / 6) * cellDistance, 0); // get center coordinates float iCenter = (boardSetup.GetLength(0) - 1) / 2.0f; float jCenter = (boardSetup.GetLength(1) - 1) / 2.0f; // compute scale factor int standardWidth = 7; float scaleFactor = standardWidth / (float)boardSetup.GetLength(1); //Debug.Log(scaleFactor); // init the board boardCells = new BoardCell[boardSetup.GetLength(0), boardSetup.GetLength(1)]; board.ForEachBoardCell((int i, int j) => { // determine cell state BoardCellState state = board.board[i, j]; // create board cell //Debug.Log("Initializing " + i + " " + j); boardCells[i, j] = ((GameObject)Instantiate(boardCellPrefab)).GetComponent <BoardCell>(); boardCells[i, j].transform.localScale = new Vector3(scaleFactor, scaleFactor, scaleFactor); boardCells[i, j].transform.position = (i - iCenter) * iVector + (j - jCenter) * jVector; boardCells[i, j].transform.position *= scaleFactor; boardCells[i, j].Initialize(this, i, j); if (state != BoardCellState.Empty) { boardCells[i, j].Spawn(state); } }); // initialize highlighted cells & stuff StartCoroutine(nextTurn()); }
public int GetScore(BoardCellState player) { return(board.scoreCount[(int)player]); }
/// <summary> /// Draws a ship on a board /// </summary> /// <param name="ship">Ship to draw</param> /// <param name="state">Ship state to draw</param> private void DrawShip(Ship ship, BoardCellState state) { DrawShip(ship, state, false); }
// Let us say that an area of empty is WEAKLY CONNECTED if cells are // separated by at most one bacteria. A weakly connected area should // be filled if all of its boundary bacteria are of the same type. public void FillClosedAreas() { // detect weakly connected areas by a depth-first search int[,] visited = new int[board.GetLength(0), board.GetLength(1)]; int areaId = 0; ForEachBoardCell((int i, int j) => { // find empty cell which was not visited if (board[i, j] == BoardCellState.Empty && visited[i, j] == 0) { // fill this area areaId++; // store boundary type and if uniform BoardCellState boundary = BoardCellState.Empty; bool isUniform = true; // store positions to visit in a stack for dfs Stack <IntPair> dfs = new Stack <IntPair>(); // kick off the search dfs.Push(new IntPair(i, j)); while (dfs.Count != 0) { IntPair pos = dfs.Pop(); if (visited[pos.i, pos.j] != 0) { continue; } // this cell has not been visited yet // mark as visited // add all neighbours to the queue if empty // add empty neighbours if not empty visited[pos.i, pos.j] = areaId; ForEachCellNeighbour(pos.i, pos.j, (int ii, int jj) => { if (board[pos.i, pos.j] == BoardCellState.Empty || board[ii, jj] == BoardCellState.Empty) { dfs.Push(new IntPair(ii, jj)); } }); // store if we have seen bacteria if (board[pos.i, pos.j] != BoardCellState.Empty) { if (boundary == BoardCellState.Empty) { boundary = board[pos.i, pos.j]; } else if (boundary != board[pos.i, pos.j]) { isUniform = false; } } } // if boundary is uniform, fill the cells from area areaId if (isUniform) { ForEachBoardCell((int ni, int nj) => { if (visited[ni, nj] == areaId) { board[ni, nj] = boundary; } }); } } }); }
static GlobalState() { startingPlayer = BoardCellState.Player1; }
/// <summary> /// Draws a ship on a board /// </summary> /// <param name="ship">Ship to draw</param> /// <param name="state">Ship state to draw</param> /// <param name="force">True to force ship drawing</param> private void DrawShip(Ship ship, BoardCellState state, bool force) { if (!_drawShips && !force) { return; } var points = ship.GetShipRegion().GetPoints(); foreach (var point in points) { if (BoardRegion.Contains(point) && state != BoardCellState.ShowDrowned) { _cells[point.X, point.Y].State = state; } else { if (_cells[point.X, point.Y].State == BoardCellState.ShotShip) { _cells[point.X, point.Y].State = state; } if (point.X != 0 && point.Y != 0) { if (_cells[point.X - 1, point.Y - 1].State == BoardCellState.Normal) { _cells[point.X - 1, point.Y - 1].State = BoardCellState.MissedShot; _cells[point.X - 1, point.Y - 1].Click -= OnCellClick; } } if (point.Y != 0) { if (_cells[point.X, point.Y - 1].State == BoardCellState.Normal) { _cells[point.X, point.Y - 1].State = BoardCellState.MissedShot; _cells[point.X, point.Y - 1].Click -= OnCellClick; } } if (point.X != 9 && point.Y != 0) { if (_cells[point.X + 1, point.Y - 1].State == BoardCellState.Normal) { _cells[point.X + 1, point.Y - 1].State = BoardCellState.MissedShot; _cells[point.X + 1, point.Y - 1].Click -= OnCellClick; } } if (point.X != 0) { if (_cells[point.X - 1, point.Y].State == BoardCellState.Normal) { _cells[point.X - 1, point.Y].State = BoardCellState.MissedShot; _cells[point.X - 1, point.Y].Click -= OnCellClick; } } if (point.X != 0 && point.Y != 9) { if (_cells[point.X - 1, point.Y + 1].State == BoardCellState.Normal) { _cells[point.X - 1, point.Y + 1].State = BoardCellState.MissedShot; _cells[point.X - 1, point.Y + 1].Click -= OnCellClick; } } if (point.X != 9) { if (_cells[point.X + 1, point.Y].State == BoardCellState.Normal) { _cells[point.X + 1, point.Y].State = BoardCellState.MissedShot; _cells[point.X + 1, point.Y].Click -= OnCellClick; } } if (point.Y != 9) { if (_cells[point.X, point.Y + 1].State == BoardCellState.Normal) { _cells[point.X, point.Y + 1].State = BoardCellState.MissedShot; _cells[point.X, point.Y + 1].Click -= OnCellClick; } } if (point.X != 9 && point.Y != 9) { if (_cells[point.X + 1, point.Y + 1].State == BoardCellState.Normal) { _cells[point.X + 1, point.Y + 1].State = BoardCellState.MissedShot; _cells[point.X + 1, point.Y + 1].Click -= OnCellClick; } } } } }
public static BoardCellState Opponent(this BoardCellState player) { return((BoardCellState)(1 - (int)player)); }