Question
In C#: Use the provided project and add the following features to the app: Use MessageBox.Show to display a message when the ship has been
In C#:
Use the provided project and add the following features to the app:
Use MessageBox.Show to display a message when the ship has been sunk
Add a score to the message that includes how many moves were made by the player
Fix some bugs: a player can re-click on an old hit and it could count as a new hit; a player could re-click on an old miss and it could count as a new miss.
The grand finale: create a reset method that is called after the player dismisses the messagebox.
Code Provided:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms;
namespace WindowsFormsApplication1 { static class Program { ///
/// The main entry point for the application. ///
[STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
namespace WindowsFormsApplication1 { partial class Form1 { ///
/// Required designer variable. ///
private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used. ///
///true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///
private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Text = "Form1"; }
#endregion } }
using System; using System.Drawing; using System.Windows.Forms;
namespace WindowsFormsApplication1 { public partial class Form1 : Form { int[,] array = new int[10, 10]; // col, row TableLayoutPanel grid = new TableLayoutPanel();
public Form1() { InitializeComponent(); placeShips();
this.Size = new Size(320, 340); grid.Size = new Size(300, 300); grid.ColumnCount = 10; grid.RowCount = 10; Controls.Add(grid);
for (int row = 0; row < grid.RowCount; row++) { for (int col = 0; col < grid.ColumnCount; col++) { Button btn1 = new Button(); btn1.Size = new Size(30, 30); btn1.BackColor = Color.SkyBlue; btn1.Margin = new Padding(0); btn1.FlatStyle = new FlatStyle(); btn1.Click += new EventHandler(btnEventHandler); grid.Controls.Add(btn1, col, row); } } }
private void btnEventHandler(object sender, EventArgs e) { Button b = ((Button)sender);
for (int row = 0; row < grid.RowCount; row++) { for (int col = 0; col < grid.ColumnCount; col++) { if (grid.GetControlFromPosition(col, row).Equals(b)) { if (array[col, row] == 1) // ship { b.BackColor = Color.Red; } else if (array[col, row] == 0) // no ship { b.BackColor = Color.DodgerBlue; } return; } } } }
private void placeShips() { Random ran = new Random(); int row = ran.Next(0, 10); int col = ran.Next(0, 6); for (int i = 0; i < 4; i++) array[col + i, row] = 1; }
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started