public ShotResult ProcessShot(Coordinate coordinate) { if (!PlayerGrid.Search(coordinate.Row, coordinate.Column).IsOccupied) { return(ShotResult.Miss); } Ship ship = Ships.First(x => x.State == PlayerGrid.Search(coordinate.Row, coordinate.Column).State); ship.Hits++; return(ship.IsSunk ? ShotResult.Sunk : ShotResult.Hit); }
void Start() { if (g == null) { g = this; } tiles = grid.tiles; currentCoordinate = GetCoordinate(gameObject); surroundingTiles = new Vector2[9]; GetSurroundingTiles(currentCoordinate); //ToggleObjects(); }
private void Start() { grid = GetComponentInParent <ReadCSV>().gridGenerator; agent = GetComponent <NavMeshAgent>(); anim = GetComponent <Animator>(); player = GameObject.FindGameObjectWithTag("Player").GetComponent <PlayerGrid>(); transform.position = GetRandomPositionInTile(); gameObject.layer = 12; }
public virtual int[] RandomizeDeployment() { int[] newXY = new int[15]; int position = 0; bool placementSuccessful = false; Direction heading = default(Direction); //for each ship to deploy in shipist foreach (ShipName shipToPlace in Enum.GetValues(typeof(ShipName))) { if (shipToPlace == ShipName.None) { continue; } placementSuccessful = false; //generate random position until the ship can be placed do { int dir = _Random.Next(2); int x = _Random.Next(0, 11); int y = _Random.Next(0, 11); if (dir == 0) { heading = Direction.UpDown; } else { heading = Direction.LeftRight; } //try to place ship, if position unplaceable, generate new coordinates try { PlayerGrid.MoveShip(x, y, shipToPlace, heading); placementSuccessful = true; newXY[position] = x; newXY[position + 1] = y; newXY[position + 2] = dir; position += 3; } catch { placementSuccessful = false; } } while (!placementSuccessful); } Console.WriteLine(newXY[0]); return(newXY); }
public bool HasAnyShips() { var squares = PlayerGrid.GetSquares(); foreach (var square in squares) { if (square == SquareStates.Ship) { return(true); } } return(false); }
/// <summary> /// inits a tetromino with a specific color /// </summary> /// <param name="type"></param> public void Initialize(PlayerGrid playerGrid, TetroMinoColor type) { disabling = false; minos.Clear(); gameObject.SetActive(true); isInitialized = true; this._playerGrid = playerGrid; this.type = type; SetTetroMinoMatrix(type); if (tetrominoMatrix != null) { DrawTetroMino(); } }
public virtual void AlignDeployment() { //Tug = 1, //Submarine = 2, //Destroyer = 3, //Battleship = 4, //AircraftCarrier = 5 PlayerGrid.MoveShip(0, 0, ShipName.Tug, Direction.LeftRight); PlayerGrid.MoveShip(1, 0, ShipName.Submarine, Direction.LeftRight); PlayerGrid.MoveShip(2, 0, ShipName.Destroyer, Direction.LeftRight); PlayerGrid.MoveShip(3, 0, ShipName.Battleship, Direction.LeftRight); PlayerGrid.MoveShip(4, 0, ShipName.AircraftCarrier, Direction.LeftRight); }
private bool CheckPlacement(Orientation orientation, ShipType shipType, int headX, int headY) { //check boundaries switch (orientation) { case Orientation.HORIZONAL: if (headX + (int)shipType <= 9 && PlayerGrid.GetGridSquare(headY, headX) == null) { break; } return(false); case Orientation.VERTICAL: if (headY + (int)shipType <= 9 && PlayerGrid.GetGridSquare(headY, headX) == null) { break; } return(false); } //check proximity for (int i = 0; i < (int)shipType; i++) { if (PlayerGrid.GetGridSquare(headY - 1, headX) == null && PlayerGrid.GetGridSquare(headY + 1, headX) == null && PlayerGrid.GetGridSquare(headY, headX - 1) == null && PlayerGrid.GetGridSquare(headY, headX + 1) == null ) { switch (orientation) { case Orientation.HORIZONAL: headX++; break; case Orientation.VERTICAL: headY++; break; } } else { return(false); } } return(true); }
public virtual void RandomizeDeployment() { bool placementSuccessful = false; Direction heading = default(Direction); //for each ship to deploy in shipist foreach (ShipName shipToPlace in Enum.GetValues(typeof(ShipName))) { if (shipToPlace == ShipName.None) { continue; } placementSuccessful = false; //generate random position until the ship can be placed do { int dir = _Random.Next(2); int x = _Random.Next(0, 11); int y = _Random.Next(0, 11); if (dir == 0) { heading = Direction.UpDown; } else { heading = Direction.LeftRight; } //try to place ship, if position unplaceable, generate new coordinates try { PlayerGrid.MoveShip(x, y, shipToPlace, heading); placementSuccessful = true; } catch { placementSuccessful = false; } } while (!placementSuccessful); } }
public void AddPlayers() { //ignoring num of players for now GameObject newPlayerObject = Instantiate(pfPlayer, new Vector2(0, 0), Quaternion.identity); GameObject gridObject = Instantiate(pfGrid, playerOneGrid.position, Quaternion.identity); playerOneGridRelativeSpawnPoint = new Vector2(4, 21); if (!addedPlayers) { Player player = newPlayerObject.GetComponent <Player>(); PlayerGrid playerGrid = gridObject.GetComponent <PlayerGrid>(); player.Initialize(1, playerTwoGrid); playerGrid.Initialize(player, playerOneNextTetrominoPlaceHolder.position, playerOneGridRelativeSpawnPoint, 10, 21); players.Add(player); } }
public bool isInitialized = false; // if the TetrominoMovement is initialized from outside public void Initialize(PlayerGrid playerGrid, Tetromino tetromino) { isInitialized = true; enabled = true; this.colided = false; this.grid = playerGrid; this.tetromino = tetromino; this.playerInput = playerGrid.player.input; automaticFallRate = 0.45f - (playerGrid.player.currentRound * 0.05f); if (automaticFallRate < 0.05f) { automaticFallRate = 0.05f; } nextAutomaticFall = Time.time + automaticFallRate; nextHorizontalMove = Time.time + nextHorizontalMoveTime; nextMoveDownTime = Time.time + moveDownCurrentRate; ResetDropRate(); }
public Player(User user) : base(user.ConnectionId, user.Name) { IdUser = user.IdUser; Rank = user.Rank; Avatar = user.Avatar; Victory = user.Victory; Defeat = user.Defeat; Ships = new List <Ship> { new Destroyer(), new Submarine(), new Cruiser(), new Carrier(), new Battleship() }; PlayerGrid = new PlayerGrid(); ShotGrid = new PlayerGrid(); IsTurn = false; }
public virtual void RandomizeDeployment() { bool placementSuccessful; Direction heading; // for each ship to deploy in shipist foreach (ShipName shipToPlace in Enum.GetValues(typeof(ShipName))) { if ((shipToPlace == ShipName.None)) { // TODO: Continue For... Warning!!! not translated } placementSuccessful = false; for (; !placementSuccessful;) { int dir = _Random.Next(2); int x = _Random.Next(0, 11); int y = _Random.Next(0, 11); if ((dir == 0)) { heading = Direction.UpDown; } else { heading = Direction.LeftRight; } // try to place ship, if position unplaceable, generate new coordinates try { PlayerGrid.MoveShip(x, y, shipToPlace, heading); placementSuccessful = true; } catch (System.Exception) { placementSuccessful = false; } } } }
public void CreateShip(ShipType shipType) { Ship ship = new Ship(shipType); int headX = random.Next(0, 10); int headY = random.Next(0, 10); int test = random.Next(0, 2); Orientation orientation = (Orientation)test; if (CheckPlacement(orientation, shipType, headX, headY)) { HullComponent hullComponent; for (int i = 0; i < (int)shipType; i++) { switch (orientation) { case Orientation.HORIZONAL: hullComponent = new HullComponent(headY, headX++, ship); ship.AddHullComponent(hullComponent); PlayerGrid.AddGridSquare(hullComponent); break; case Orientation.VERTICAL: hullComponent = new HullComponent(headY++, headX, ship); ship.AddHullComponent(hullComponent); PlayerGrid.AddGridSquare(hullComponent); break; } } } else { CreateShip(shipType); return; } ships.Add(ship); }
protected override void Dispose(bool disposing) { base.Dispose(disposing); if (disposing) { IsTrading = false; PlayerItems = null; if (UserLabel != null) { if (!UserLabel.IsDisposed) { UserLabel.Dispose(); } UserLabel = null; } if (PlayerLabel != null) { if (!PlayerLabel.IsDisposed) { PlayerLabel.Dispose(); } PlayerLabel = null; } if (UserGrid != null) { if (!UserGrid.IsDisposed) { UserGrid.Dispose(); } UserGrid = null; } if (PlayerGrid != null) { if (!PlayerGrid.IsDisposed) { PlayerGrid.Dispose(); } PlayerGrid = null; } if (UserGoldLabel != null) { if (!UserGoldLabel.IsDisposed) { UserGoldLabel.Dispose(); } UserGoldLabel = null; } if (PlayerGoldLabel != null) { if (!PlayerGoldLabel.IsDisposed) { PlayerGoldLabel.Dispose(); } PlayerGoldLabel = null; } if (ConfirmButton != null) { if (!ConfirmButton.IsDisposed) { ConfirmButton.Dispose(); } ConfirmButton = null; } } }
private void PlayerGrid_Click(object sender, EventArgs e) { Control control = (Control)sender; switch (selectedShipType) { #region Destroyer case ShipType.Destroyer: if (shipPositions[0, 0] == null) { shipPositions[0, 0] = new GridPosition(PlayerGrid.GetPositionFromControl(control).Column, PlayerGrid.GetPositionFromControl(control).Row); } else if (shipPositions[0, 1] == null) { shipPositions[0, 1] = new GridPosition(PlayerGrid.GetPositionFromControl(control).Column, PlayerGrid.GetPositionFromControl(control).Row); if (PlaceShip(ShipType.Destroyer) == false) { MessageBox.Show(i18n.GetText("invalidPlacement", i18n.GetText("shipName1")), i18n.GetText("invalidPlacementTitle"), MessageBoxButtons.OK); shipPositions[0, 0] = null; shipPositions[0, 1] = null; } else { for (int i = 0; i < destroyer.occupiedSquares.Length; i++) { pictureBox = (PictureBox)PlayerGrid.GetControlFromPosition(destroyer.occupiedSquares[i].x, destroyer.occupiedSquares[i].y); pictureBox.Image = Properties.Resources.Ship; } btnDestroyer.Enabled = false; btnDestroyer.Text = i18n.GetText("shipPlaced"); } } break; #endregion #region Cruiser case ShipType.Cruiser: if (shipPositions[1, 0] == null) { shipPositions[1, 0] = new GridPosition(PlayerGrid.GetPositionFromControl(control).Column, PlayerGrid.GetPositionFromControl(control).Row); } else if (shipPositions[1, 1] == null) { shipPositions[1, 1] = new GridPosition(PlayerGrid.GetPositionFromControl(control).Column, PlayerGrid.GetPositionFromControl(control).Row); if (PlaceShip(ShipType.Cruiser) == false) { MessageBox.Show(i18n.GetText("invalidPlacement", i18n.GetText("shipName2")), i18n.GetText("invalidPlacementTitle"), MessageBoxButtons.OK); shipPositions[1, 0] = null; shipPositions[1, 1] = null; } else { for (int i = 0; i < cruiser.occupiedSquares.Length; i++) { pictureBox = (PictureBox)PlayerGrid.GetControlFromPosition(cruiser.occupiedSquares[i].x, cruiser.occupiedSquares[i].y); pictureBox.Image = Properties.Resources.Ship; } btnCruiser.Enabled = false; btnCruiser.Text = i18n.GetText("shipPlaced"); } } break; #endregion #region Submarine case ShipType.Submarine: if (shipPositions[2, 0] == null) { shipPositions[2, 0] = new GridPosition(PlayerGrid.GetPositionFromControl(control).Column, PlayerGrid.GetPositionFromControl(control).Row); } else if (shipPositions[2, 1] == null) { shipPositions[2, 1] = new GridPosition(PlayerGrid.GetPositionFromControl(control).Column, PlayerGrid.GetPositionFromControl(control).Row); if (PlaceShip(ShipType.Submarine) == false) { MessageBox.Show(i18n.GetText("invalidPlacement", i18n.GetText("shipName3")), i18n.GetText("invalidPlacementTitle"), MessageBoxButtons.OK); shipPositions[2, 0] = null; shipPositions[2, 1] = null; } else { for (int i = 0; i < submarine.occupiedSquares.Length; i++) { pictureBox = (PictureBox)PlayerGrid.GetControlFromPosition(submarine.occupiedSquares[i].x, submarine.occupiedSquares[i].y); pictureBox.Image = Properties.Resources.Ship; } btnSubmarine.Enabled = false; btnSubmarine.Text = i18n.GetText("shipPlaced"); } } break; #endregion #region Battleship case ShipType.Battleship: if (shipPositions[3, 0] == null) { shipPositions[3, 0] = new GridPosition(PlayerGrid.GetPositionFromControl(control).Column, PlayerGrid.GetPositionFromControl(control).Row); } else if (shipPositions[3, 1] == null) { shipPositions[3, 1] = new GridPosition(PlayerGrid.GetPositionFromControl(control).Column, PlayerGrid.GetPositionFromControl(control).Row); if (PlaceShip(ShipType.Battleship) == false) { MessageBox.Show(i18n.GetText("invalidPlacement", i18n.GetText("shipName4")), i18n.GetText("invalidPlacementTitle"), MessageBoxButtons.OK); shipPositions[3, 0] = null; shipPositions[3, 1] = null; } else { for (int i = 0; i < battleship.occupiedSquares.Length; i++) { pictureBox = (PictureBox)PlayerGrid.GetControlFromPosition(battleship.occupiedSquares[i].x, battleship.occupiedSquares[i].y); pictureBox.Image = Properties.Resources.Ship; } btnBattleship.Enabled = false; btnBattleship.Text = i18n.GetText("shipPlaced"); } } break; #endregion #region Carrier case ShipType.Carrier: if (shipPositions[4, 0] == null) { shipPositions[4, 0] = new GridPosition(PlayerGrid.GetPositionFromControl(control).Column, PlayerGrid.GetPositionFromControl(control).Row); } else if (shipPositions[4, 1] == null) { shipPositions[4, 1] = new GridPosition(PlayerGrid.GetPositionFromControl(control).Column, PlayerGrid.GetPositionFromControl(control).Row); if (PlaceShip(ShipType.Carrier) == false) { MessageBox.Show(i18n.GetText("invalidPlacement", i18n.GetText("shipName5")), i18n.GetText("invalidPlacementTitle"), MessageBoxButtons.OK); shipPositions[4, 0] = null; shipPositions[4, 1] = null; } else { for (int i = 0; i < carrier.occupiedSquares.Length; i++) { pictureBox = (PictureBox)PlayerGrid.GetControlFromPosition(carrier.occupiedSquares[i].x, carrier.occupiedSquares[i].y); pictureBox.Image = Properties.Resources.Ship; } btnCarrier.Enabled = false; btnCarrier.Text = i18n.GetText("shipPlaced");; } } break; #endregion } if (carrier != null && battleship != null && submarine != null && cruiser != null && destroyer != null) { btnSubmit.Enabled = true; } }
public void Setup() => Schedule(() => { Child = grid = new PlayerGrid { RelativeSizeAxes = Axes.Both }; });
public SquareStates Shot(Coordinates coordinates) { return(PlayerGrid.ChangeSquareState(coordinates)); }
public void GameCommandRecieved(object sender, CommandEventArgs e) { Console.WriteLine("Game command received. data:{0} type:{1}", e.Command.Data, e.Command.CommandType); if (e.Command.CommandType == CommandType.GameStartInform) { if (e.Command.Data.ToLower() == "true") { myTurn = true; rtbLog.BeginInvoke((MethodInvoker) delegate() { rtbLog.AppendText(i18n.GetText("firstShotMe"));; }); btnFire.BackColor = System.Drawing.Color.Red; btnFire.ForeColor = System.Drawing.Color.White; //btnFire.Enabled = true; } else if (e.Command.Data.ToLower() == "false") { myTurn = false; btnFire.Enabled = false; rtbLog.BeginInvoke((MethodInvoker) delegate() { rtbLog.AppendText(i18n.GetText("firstShotEnemy"));; }); } } if (e.Command.CommandType == CommandType.GameShotResult) { if (e.Command.Data.ToLower().Equals("hit")) { //Deal with hits // Don't block Thread with waiting until Sound finishs new System.Threading.Thread(() => { if (checkBoxSound.Checked) { spFlightBomb.PlaySync(); } rtbLog.BeginInvoke((MethodInvoker) delegate() { rtbLog.AppendText(i18n.GetText("shotHit")); }); pictureBox.BeginInvoke((MethodInvoker) delegate() { pictureBox = (PictureBox)EnemyGrid.GetControlFromPosition(gridTarget.x, gridTarget.y); pictureBox.Image = Properties.Resources.ShipHit; pictureBox.Tag = "ShipHit"; }); btnFire.BeginInvoke((MethodInvoker) delegate() { btnFire.Enabled = false; }); myTurn = false; rtbLog.BeginInvoke((MethodInvoker) delegate() { rtbLog.AppendText(i18n.GetText("waitEnemyShot"));; }); }).Start(); } else if (e.Command.Data.ToLower().Equals("miss")) { //deal with misses // Don't block Thread with waiting until Sound finishs new System.Threading.Thread(() => { if (checkBoxSound.Checked) { spFlightWater.PlaySync(); } rtbLog.BeginInvoke((MethodInvoker) delegate() { rtbLog.AppendText(i18n.GetText("shotMissed")); }); pictureBox.BeginInvoke((MethodInvoker) delegate() { pictureBox = (PictureBox)EnemyGrid.GetControlFromPosition(gridTarget.x, gridTarget.y); pictureBox.Image = Properties.Resources.WaterMiss; pictureBox.Tag = "WaterMiss"; }); btnFire.BeginInvoke((MethodInvoker) delegate() { btnFire.Enabled = false; }); myTurn = false; rtbLog.BeginInvoke((MethodInvoker) delegate() { rtbLog.AppendText(i18n.GetText("waitEnemyShot"));; }); }).Start(); } } if (e.Command.CommandType == CommandType.GameHitInform) { // Don't block Thread with waiting until Sound finishs new System.Threading.Thread(() => { if (checkBoxSound.Checked) { spFlightBomb.PlaySync(); } rtbLog.BeginInvoke((MethodInvoker) delegate() { rtbLog.AppendText(i18n.GetText("oneShipHit")); }); gridTarget.x = int.Parse(e.Command.Data.Split(',')[0]); gridTarget.y = int.Parse(e.Command.Data.Split(',')[1]); pictureBox.BeginInvoke((MethodInvoker) delegate() { pictureBox = (PictureBox)PlayerGrid.GetControlFromPosition(gridTarget.x, gridTarget.y); pictureBox.Image = Properties.Resources.ShipHit; }); myTurn = true; rtbLog.BeginInvoke((MethodInvoker) delegate() { rtbLog.AppendText(i18n.GetText("turnToShot")); }); btnFire.BeginInvoke((MethodInvoker) delegate() { btnFire.BackColor = System.Drawing.Color.Red; btnFire.ForeColor = System.Drawing.Color.White; }); }).Start(); } if (e.Command.CommandType == CommandType.GameMissInform) { // Don't block Thread with waiting until Sound finishs new System.Threading.Thread(() => { if (checkBoxSound.Checked) { spFlightWater.PlaySync(); } rtbLog.BeginInvoke((MethodInvoker) delegate() { rtbLog.AppendText(i18n.GetText("opponentMissedFleet")); }); gridTarget.x = int.Parse(e.Command.Data.Split(',')[0]); gridTarget.y = int.Parse(e.Command.Data.Split(',')[1]); pictureBox.BeginInvoke((MethodInvoker) delegate() { pictureBox = (PictureBox)PlayerGrid.GetControlFromPosition(gridTarget.x, gridTarget.y); pictureBox.Image = Properties.Resources.WaterMiss; }); myTurn = true; rtbLog.BeginInvoke((MethodInvoker) delegate() { rtbLog.AppendText(i18n.GetText("turnToShot")); }); btnFire.BeginInvoke((MethodInvoker) delegate() { btnFire.BackColor = System.Drawing.Color.Red; btnFire.ForeColor = System.Drawing.Color.White; }); }).Start(); } if (e.Command.CommandType == CommandType.GameOverInform) { if (e.Command.Data.ToLower() == "win") { Command cmdInform = new Command(CommandType.GameOverInform, client.ServerIP, gameID + ":" + "win"); cmdInform.TargetPort = client.ServerPort; cmdInform.SenderIP = client.IP; cmdInform.SenderPort = client.Port; cmdInform.SenderName = client.Username; client.Wins++; client.SendCommand(cmdInform); MessageBox.Show(i18n.GetText("wonCongratulations", client.Username), i18n.GetText("winner"), MessageBoxButtons.OK); Close(); } else if (e.Command.Data.ToLower() == "loss") { Command cmdInform = new Command(CommandType.GameOverInform, client.ServerIP, gameID + ":" + "loss"); cmdInform.TargetPort = client.ServerPort; cmdInform.SenderIP = client.IP; cmdInform.SenderPort = client.Port; cmdInform.SenderName = client.Username; client.Losses++; client.SendCommand(cmdInform); MessageBox.Show(i18n.GetText("lossGame", client.Username), i18n.GetText("gameLost"), MessageBoxButtons.OK); Close(); } } }