public void updateGrid() { for (int i = 0; i < this.squares.GetLength(0); i++) { for (int j = 0; j < this.squares.GetLength(1); j++) { bool shot = this.squares[j, i].isShot(); bool ship = this.squares[j, i].isShip(); /* Only show if the square was ever shot */ if (shot) { ((Button)this.grid.Children.Cast <UIElement>().First(f => Grid.GetRow(f) == i && Grid.GetColumn(f) == j)).IsEnabled = false; if (!ship) { ((Button)this.grid.Children.Cast <UIElement>().First(f => Grid.GetRow(f) == i && Grid.GetColumn(f) == j)).Content = "X"; } else if (this.squares[j, i].hasShipSunk()) { ((Button)this.grid.Children.Cast <UIElement>().First(f => Grid.GetRow(f) == i && Grid.GetColumn(f) == j)).Content = "-"; } else { ((Button)this.grid.Children.Cast <UIElement>().First(f => Grid.GetRow(f) == i && Grid.GetColumn(f) == j)).Content = "Q"; } } else if (isGodMode) { if (ship) { ((Button)this.grid.Children.Cast <UIElement>().First(f => Grid.GetRow(f) == i && Grid.GetColumn(f) == j)).Content = "O"; } } } } }
/*Params: int width: Width of the boat we're drawing * int length: Length of the boat we're drawing * char a: char we are going to draw inside the Button * Summary: Draws a ship of the width and length indicated in the Grid * */ private void draw(int boat, bool a) { for (int i = 0; i < length[boat]; i++) { for (int j = 0; j < width[boat]; j++) { Button dynamicButton = new Button(); battleGrid.Children.Add(dynamicButton); Grid.SetRow(dynamicButton, i + posy[boat]); Grid.SetColumn(dynamicButton, j + posx[boat]); BrushConverter bc = new BrushConverter(); if (a) { dynamicButton.Background = (Brush)bc.ConvertFrom("#FF820933"); } else { dynamicButton.Background = (Brush)bc.ConvertFrom("#FF0A0036"); } ((Button)battleGrid.Children.Cast <UIElement>().First(f => Grid.GetRow(f) == i + posy[boat] && Grid.GetColumn(f) == j + posx[boat])).Content = a; } } }
public void Button_Click(object sender, RoutedEventArgs e) { checkWin(); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (((Button)battleGrid.Children.Cast <UIElement>().First(f => Grid.GetRow(f) == i && Grid.GetColumn(f) == j)).Equals((Button)sender)) { if (this.gamePageData.playerBoard.shoot(new Square(j, i)) && !firstship) { Narrator.shipdown(NarratorTxt, null); firstship = true; } this.gamePageData.playerBoard.updateGrid(); this.currIdleTimeLeft = this.gamePageData.boardPlacementData.getIdleTime(); aiMove(); } } } checkWin(); }
/*Void, no params * Summary: Cleans the grid, replacing any Button by "" * */ private void completeClean() { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Button dynamicButton = new Button(); battleGrid.Children.Add(dynamicButton); Grid.SetRow(dynamicButton, i); Grid.SetColumn(dynamicButton, j); BrushConverter bc = new BrushConverter(); dynamicButton.Background = (Brush)bc.ConvertFrom("#FF0A0036"); ((Button)battleGrid.Children.Cast <UIElement>().First(e => Grid.GetRow(e) == i && Grid.GetColumn(e) == j)).Content = (""); } } }