private void playerBoard_MouseEnter(Object sender, EventArgs e) { if (!gameStarted && selectedShip != null) { gs = (GraphicSquare)sender; markedGraphicSquares = new GraphicSquare[selectedShip.Length()]; if (selectedShip.Rotated) { for (int i = 0; i < selectedShip.Length(); i++) { try { markedGraphicSquares[i] = playerGraphicBoard[gs.GetI() - (selectedShip.Length() / 2) + i, gs.GetJ()]; } catch { markedGraphicSquares[i] = null; } } } else { for (int i = 0; i < selectedShip.Length(); i++) { try { markedGraphicSquares[i] = playerGraphicBoard[gs.GetI(), gs.GetJ() - (selectedShip.Length() / 2) + i]; } catch { markedGraphicSquares[i] = null; } } } if (PossibleToPlaceShip(markedGraphicSquares, !selectedShip.Rotated)) { for (int i = 0; i < markedGraphicSquares.Length; i++) { markedGraphicSquares[i].Mark(false); } possibleToPlaceShip = true; } else { for (int i = 0; i < markedGraphicSquares.Length; i++) { if (markedGraphicSquares[i] != null) { markedGraphicSquares[i].Mark(true); } } } } }
private void DrawBoard() { this.Width = GameSettings.Default.BoardSize * (GameSettings.Default.ButtonSize + GameSettings.Default.SpaceBetweenButtons) + (GameSettings.Default.SpaceBetweenButtons + GameSettings.Default.ButtonSize) * GameSettings.Default.BoardSize + GameSettings.Default.SpaceBetweenBoards + GameSettings.Default.LeftSpacing + GameSettings.Default.RightSpacing + 15; this.Height = GameSettings.Default.BoardSize * (GameSettings.Default.ButtonSize + GameSettings.Default.SpaceBetweenButtons) + GameSettings.Default.TopSpacing + GameSettings.Default.ButtomSpacing + 40; playerGraphicBoard = new GraphicSquare[GameSettings.Default.BoardSize, GameSettings.Default.BoardSize]; computerGraphicBoard = new GraphicSquare[GameSettings.Default.BoardSize, GameSettings.Default.BoardSize]; this.CenterToScreen(); for (int i = 0; i < GameSettings.Default.BoardSize; i++) { for (int j = 0; j < GameSettings.Default.BoardSize; j++) { computerGraphicBoard[i, j] = new GraphicSquare(i, j, computer.GetSquare(i, j), false); computerGraphicBoard[i, j].Click += new EventHandler(computerBoard_Click); computerGraphicBoard[i, j].Left = GameSettings.Default.LeftSpacing + j * (GameSettings.Default.ButtonSize + GameSettings.Default.SpaceBetweenButtons); computerGraphicBoard[i, j].Top = GameSettings.Default.TopSpacing + i * (GameSettings.Default.ButtonSize + GameSettings.Default.SpaceBetweenButtons); playerGraphicBoard[i, j] = new GraphicSquare(i, j, player.GetSquare(i, j), false); playerGraphicBoard[i, j].Click += new EventHandler(playerBoard_Click); playerGraphicBoard[i, j].MouseEnter += new EventHandler(playerBoard_MouseEnter); playerGraphicBoard[i, j].MouseLeave += new EventHandler(playerBoard_MouseLeave); playerGraphicBoard[i, j].Left = GameSettings.Default.LeftSpacing + j * (GameSettings.Default.ButtonSize + GameSettings.Default.SpaceBetweenButtons) + (GameSettings.Default.SpaceBetweenButtons + GameSettings.Default.ButtonSize) * GameSettings.Default.BoardSize + GameSettings.Default.SpaceBetweenBoards; playerGraphicBoard[i, j].Top = GameSettings.Default.TopSpacing + i * (GameSettings.Default.ButtonSize + GameSettings.Default.SpaceBetweenButtons); Controls.Add(playerGraphicBoard[i, j]); Controls.Add(computerGraphicBoard[i, j]); } } DrawShipButtons(); }
private void playerBoard_Click(Object sender, EventArgs e) { gs = (GraphicSquare)sender; MouseEventArgs temp = (MouseEventArgs)e; if (!gameStarted && temp.Button == MouseButtons.Right && selectedShip != null) { selectedShip.Rotate(); } else if (!gameStarted && temp.Button == MouseButtons.Left && selectedShip != null) { if (possibleToPlaceShip) { markedSquares = new Square[markedGraphicSquares.Length]; for (int i = 0; i < markedGraphicSquares.Length; i++) { markedSquares[i] = markedGraphicSquares[i].GetSquare(); markedGraphicSquares[i].Image = Properties.Resources.shipMark; } // A clone is needed because Stack pushes a pointer and not a value. placedShips.Push((GraphicSquare[])markedGraphicSquares); placedShipsBoxes.Push(selectedShip); player.AddShip(new Ship(markedSquares, selectedShip.Type)); selectedShip.Lock(); selectedShip = null; } else { MessageBox.Show("You can't place the ship here.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private async void computerBoard_Click(Object sender, EventArgs e) { gs = (GraphicSquare)sender; MouseEventArgs temp = (MouseEventArgs)e; if (!gameStarted && temp.Button == MouseButtons.Right && selectedShip != null) { selectedShip.Rotate(); } else if (gameStarted && gs.Clickable && !gs.Clicked) { playersResult = gameManager.Attack(player, gs.GetSquare()); if (playersResult != Result.None) { playerTurnsCount++; playersTurnsCountLabel.Text = playerTurnsCount.ToString(); gs.Attack(); if (playersResult == Result.ShipDestroyed || playersResult == Result.Victory) { UpdateShipDestroyed(Player.PlayerType.Player, gs.GetSquare().GetShip()); if (playersResult == Result.Victory) { foreach (GraphicSquare temp2 in computerGraphicBoard) { temp2.Clickable = false; } if (sound) { win.Play(); } GameEnded(player); } } PlaySound(playersResult); #region Computer's Play if (playersResult == Result.Miss) { do { await Delay(); KeyValuePair <Square, Result> values = computer.Attack(); computersAttackSquare = values.Key; computersResult = values.Value; playerGraphicBoard[computersAttackSquare.I, computersAttackSquare.J].Attack(); if (computersResult == Result.ShipDestroyed) { UpdateShipDestroyed(Player.PlayerType.Computer, computersAttackSquare.GetShip()); } PlaySound(computersResult); this.Update(); //? computerTurnsCount++; computersTurnsCountLabel.Text = computerTurnsCount.ToString(); } while (computersResult != Result.Miss && computersResult != Result.Victory); if (computersResult == Result.Victory) { UpdateShipDestroyed(Player.PlayerType.Computer, computersAttackSquare.GetShip()); this.Update(); if (sound) { lose.Play(); } GameEnded(computer); } } #endregion } } }