In C#: Use the provided project and add the following features to the app: Use M
ID: 3704473 • Letter: I
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 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;
}
}
}
Explanation / Answer
//Form1.cs code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int[,] array = new int[10, 10]; // col, row
TableLayoutPanel grid = new TableLayoutPanel();
int totalMoves = 0;
int missCount = 0;
int hitCount = 0;
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))
{
totalMoves++;
if (array[col, row] == 1) // ship
{
b.BackColor = Color.Red;
hitCount++;
}
else if (array[col, row] == 0) // no ship
{
b.BackColor = Color.DodgerBlue;
missCount++;
if (missCount == 96)
{
MessageBox.Show("The ship got sunk. Score: Total moves: " + totalMoves + " Total hits: " + hitCount);
reset();
}
}
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;
}
private void reset()
{
for (int row = 0; row < grid.RowCount; row++)
{
for (int col = 0; col < grid.ColumnCount; col++)
{
var btn = grid.GetControlFromPosition(col, row);
array[col, row] = 0;
btn.BackColor = Color.SkyBlue;
}
}
totalMoves = missCount = hitCount = 0;
placeShips();
}
}
}