private void load() { Grid a = this.Content as Grid; for (int rowIndex = 1; rowIndex < a.RowDefinitions.Count; rowIndex++) { for (int colIndex = 0; colIndex < a.ColumnDefinitions.Count; colIndex++) { List <View> v = a.Children.Where(c => Grid.GetRow(c) == rowIndex && Grid.GetColumn(c) == colIndex).ToList(); if (v.Count > 0) { if (v[0] is Image) { Image img = v[0] as Image; img.Aspect = Aspect.AspectFit; // img.BackgroundColor = Color.Black; var tapGestureRecognizer = new TapGestureRecognizer(); tapGestureRecognizer.Tapped += TapGestureRecognizer_Tapped1; img.GestureRecognizers.Add(tapGestureRecognizer); } } } } }
//code for when a game grid button is clicked private void Button_Clicked(object sender, EventArgs e) { Button btn = (Button)sender; int x = Grid.GetRow(btn); int y = Grid.GetColumn(btn); //validate the player's button choice if (btn.Text == "miss" || btn.Text == "hit") { DisplayAlert("Error.", "Tile already chosen.", "Cancel"); } else { //if the player's chosen tile contains a battleship if (gameGrid[x, y] == 1) { btn.Text = "hit"; hitCount--; turnsLeft--; } //if the player's chosen tile does not contain a battleship else if (gameGrid[x, y] == 0) { btn.Text = "miss"; turnsLeft--; } //display message at end of game for (int col = 0; col < gameGrid.GetLength(0); col++) { //don't display a message if game is already over if (won == true || lost == true) { break; } for (int row = 0; row < gameGrid.GetLength(1); row++) { if (won == true || lost == true) { break; } else if (btn.Text == "hit") { //check if player has sunk all battleships (player wins) if (hitCount == 0) { DisplayAlert("You Win.", "All boats sunk.", "Ok"); won = true; } } else if (btn.Text == "miss") { //check if player has ran out of turns (player loses) if (turnsLeft == 0) { DisplayAlert("You Lose.", "Ran out of turns.", "Ok"); lost = true; } } } } //end of for loop } //end of else path setTurnsLeft(); }//end of Button_CLicked()