private void PlayerCheckHitOrMiss(string button) { if (PlayerTurn == true) { int buttonToNumber = int.Parse(button); if (HasBeenShotAtAlready(buttonToNumber)) { MessageBox.Show("Du har redan skjutit där!"); } else if (HitComputerShip(buttonToNumber)) { AddHitOnComputerBoard(buttonToNumber); ChangePlayerTurn(); if (MyGameEngine.PlayerHasWon()) { Highscore myHighscore = MyGameEngine.AddNewHighscore(true, Global.MyPlayer.Id); ShowWinDialogueBox(myHighscore); } else { Task.Delay(1000).ContinueWith(t => ComputerHitOrMiss()); } } else { AddCloseOrMissOnComputerBoard(buttonToNumber); ChangePlayerTurn(); Task.Delay(1000).ContinueWith(t => ComputerHitOrMiss()); } } }
private void RandomPlacePlayerShips() { if (Ships == 3) { int[] longitudeShips = MyGameEngine.GetLongitudesForRandomShip(); int[] latitudeShips = MyGameEngine.GetLatitudesForRandomShip(); foreach (var button in PlayerButtonsInGame) { if (button.Longitude == longitudeShips[0] && button.Latitude == latitudeShips[0]) { ChangePlayerGridToSingleBoat(button); } else if (button.Longitude == longitudeShips[1] && button.Latitude == latitudeShips[1]) { ChangePlayerGridToBattleShip(button.Longitude, button.Latitude, button); } else if (button.Longitude == longitudeShips[2] && button.Latitude == latitudeShips[2]) { ChangePlayerGridToSubmarine(button.Longitude, button.Latitude, button); } } Ships = 0; ChangePlayerTurn(); MessageBox.Show("Nu kan spelet börja, du spelar på den högra spelplanen."); } }
public void PlayerPlaceBattleShip(string button) { int buttonToNumber = int.Parse(button); if (MyGameEngine.FillPlayerBattleShip(PlayerButtonsInGame[buttonToNumber].Longitude, PlayerButtonsInGame[buttonToNumber].Latitude) == true) { Ships--; BattleShip.PlacedBoats--; foreach (var myPlayerButton in PlayerButtonsInGame) { if (PlayerButtonsInGame[buttonToNumber].Longitude == myPlayerButton.Longitude && PlayerButtonsInGame[buttonToNumber].Latitude == myPlayerButton.Latitude) { ChangePlayerGridToBattleShip(myPlayerButton.Longitude, myPlayerButton.Latitude, myPlayerButton); } } if (Ships == 0) { ChangePlayerTurn(); MessageBox.Show("Nu kan spelet börja, du spelar på den högra skärmen"); } } else { MessageBox.Show("Du har redan placerat ett skepp där"); } }
public void ComputerHitOrMiss() { int[] shoot = MyGameEngine.ComputerRandomShotFired(); if (WasCloseToShip == false && ComputerHitShip == false && MyGameEngine.CheckIfAPlayerShipHasBeenHit() == false) { if (MyGameEngine.ComputerCheckHitOrMiss(shoot[0], shoot[1])) { foreach (var myPlayerButton in PlayerButtonsInGame) { if (myPlayerButton.Longitude == shoot[0] && myPlayerButton.Latitude == shoot[1]) { CoordinatesHitShip = new int[] { shoot[0], shoot[1] }; myPlayerButton.HitOrMiss = "Träff!"; ChangeGridSquareToExplosionImage(myPlayerButton); ComputerHitShip = true; myPlayerButton.IsClicked = true; } } if (MyGameEngine.PlayerHasLost()) { MyGameEngine.AddNewHighscore(false, Global.MyPlayer.Id); ShowLosingDialogueBox(); } ChangePlayerTurn(); } else if (MyGameEngine.ComputerCheckCloseOrNot(shoot[0], shoot[1])) { CoordinatesCloseToShip = new int[] { shoot[0], shoot[1] }; AddCloseOnPlayerBoard(shoot[0], shoot[1]); } else { foreach (var myPlayerButton in PlayerButtonsInGame) { if (myPlayerButton.Longitude == shoot[0] && myPlayerButton.Latitude == shoot[1]) { myPlayerButton.HitOrMiss = "Miss!"; ChangeToSplashImage(myPlayerButton); myPlayerButton.IsClicked = true; } } ChangePlayerTurn(); } } else if (WasCloseToShip == true && ComputerHitShip == false && MyGameEngine.CheckIfAPlayerShipHasBeenHit() == false) { ComputerShootAroundSplashSonar(); } else if (ComputerHitShip == true && MyGameEngine.CheckIfAPlayerShipHasBeenHit() == false) { ComputerShootToSinkShip(CoordinatesHitShip); } else if (MyGameEngine.CheckIfAPlayerShipHasBeenHit() == true) { ShootCloseToAShipAlreadyHit(); } }
private bool PlayerHasShipsLeftToPlace(int buttonToNumber) { bool result = false; if (MyGameEngine.FillPlayerShips(PlayerButtonsInGame[buttonToNumber].Longitude, PlayerButtonsInGame[buttonToNumber].Latitude)) { result = true; } return(result); }
private bool HitComputerShip(int buttonToNumber) { if (MyGameEngine.PlayerCheckHitOrMiss(ComputerButtonsInGame[buttonToNumber].Longitude, ComputerButtonsInGame[buttonToNumber].Latitude)) { return(true); } else { return(false); } }
public void ShootCloseToAShipAlreadyHit() { int[] shot = MyGameEngine.GetCoordinatesOfPlayerShipAlreadyHit(); if (MyGameEngine.ComputerCheckIfShipStillFloating(shot[0], shot[1]) == true) { int[] newShot = MyGameEngine.ComputerShootToSinkShip(shot[0], shot[1]); if (MyGameEngine.ComputerCheckHitOrMiss(newShot[0], newShot[1])) { foreach (var myPlayerButton in PlayerButtonsInGame) { if (myPlayerButton.Longitude == newShot[0] && myPlayerButton.Latitude == newShot[1]) { myPlayerButton.HitOrMiss = "Träff!"; CoordinatesHitShip = new int[] { shot[0], shot[1] }; ChangeGridSquareToExplosionImage(myPlayerButton); myPlayerButton.IsClicked = true; WasCloseToShip = false; } } if (MyGameEngine.PlayerHasLost()) { MyGameEngine.AddNewHighscore(false, Global.MyPlayer.Id); ShowLosingDialogueBox(); } ChangePlayerTurn(); } else if (MyGameEngine.ComputerCheckCloseOrNot(newShot[0], newShot[1])) { AddCloseOnPlayerBoard(newShot[0], newShot[1]); } else { foreach (var myPlayerButton in PlayerButtonsInGame) { if (myPlayerButton.Longitude == newShot[0] && myPlayerButton.Latitude == newShot[1]) { myPlayerButton.HitOrMiss = "Miss!"; ChangeToSplashImage(myPlayerButton); myPlayerButton.IsClicked = true; } } ChangePlayerTurn(); } } else if (MyGameEngine.ComputerCheckIfShipStillFloating(shot[0], shot[1]) == false) { ComputerHitShip = false; ComputerHitOrMiss(); } }
private void AddCloseOrMissOnComputerBoard(int buttonToNumber) { UpdateNumberOfMovesOnGameboard(); PlayerShotsFired.Add(buttonToNumber); if (MyGameEngine.PlayerCheckCloseOrNot(ComputerButtonsInGame[buttonToNumber].Longitude, ComputerButtonsInGame[buttonToNumber].Latitude) == true) { ComputerButtonsInGame[buttonToNumber].HitOrMiss = "Nära"; ChangeGridSquareToSplashSonarImage(ComputerButtonsInGame[buttonToNumber]); } else { ComputerButtonsInGame[buttonToNumber].HitOrMiss = "Miss"; ChangeToSplashImage(ComputerButtonsInGame[buttonToNumber]); } }
private void AddCloseOnPlayerBoard(int longitude, int latitude) { if (MyGameEngine.ComputerCheckCloseOrNot(longitude, latitude) == true) { foreach (var myPlayerButton in PlayerButtonsInGame) { if (myPlayerButton.Longitude == longitude && myPlayerButton.Latitude == latitude) { ChangeGridSquareToSplashSonarImage(myPlayerButton); WasCloseToShip = true; ChangePlayerTurn(); myPlayerButton.IsClicked = true; } } } }
private void ComputerShootAroundSplashSonar() { int[] shoot = MyGameEngine.ComputerShotCloseToSplashSonar(CoordinatesCloseToShip[0], CoordinatesCloseToShip[1]); if (MyGameEngine.ComputerCheckHitOrMiss(shoot[0], shoot[1])) { foreach (var myPlayerButton in PlayerButtonsInGame) { if (myPlayerButton.Longitude == shoot[0] && myPlayerButton.Latitude == shoot[1]) { myPlayerButton.HitOrMiss = "Träff!"; CoordinatesHitShip = new int[] { shoot[0], shoot[1] }; ChangeGridSquareToExplosionImage(myPlayerButton); myPlayerButton.IsClicked = true; WasCloseToShip = false; ComputerHitShip = true; } } if (MyGameEngine.PlayerHasLost()) { MyGameEngine.AddNewHighscore(false, Global.MyPlayer.Id); ShowLosingDialogueBox(); } ChangePlayerTurn(); } else if (MyGameEngine.ComputerCheckCloseOrNot(shoot[0], shoot[1])) { AddCloseOnPlayerBoard(shoot[0], shoot[1]); } else { foreach (var myPlayerButton in PlayerButtonsInGame) { if (myPlayerButton.Longitude == shoot[0] && myPlayerButton.Latitude == shoot[1]) { myPlayerButton.HitOrMiss = "Miss!"; ChangeToSplashImage(myPlayerButton); myPlayerButton.IsClicked = true; } } ChangePlayerTurn(); } }