Skip to content

Instantly share code, notes, and snippets.

@gempir
Last active May 10, 2016 12:48
Show Gist options
  • Save gempir/109e401c26738aec5ab70e72f26036d2 to your computer and use it in GitHub Desktop.
Save gempir/109e401c26738aec5ab70e72f26036d2 to your computer and use it in GitHub Desktop.
C# part of my battleships wpf application
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
// number code:
// 0 - water
// 1 - ship
// 2 - hit ship
// 3 - hit water
namespace battleships
{
public partial class MainWindow : Window
{
private Random rand;
private int[,] field;
private Regex reg;
private TextBlock output;
private Dictionary<string, Button> ButtonDict = new Dictionary<string, Button>();
private int shots = 0;
private int hits = 0;
private int misses = 0;
public MainWindow()
{
InitializeComponent();
this.rand = new Random();
this.reg = new Regex(@"^\d,\d$", RegexOptions.IgnoreCase);
this.CreateField();
this.FillField();
scoreboard.Content = "shots:\r\n" + this.shots + "\r\nhits:\r\n" + this.hits + "\r\nmisses:\r\n" + this.misses;
}
private void Output_Loaded(object sender, RoutedEventArgs e)
{
this.output = sender as TextBlock;
}
public void handleFieldClick(object sender, RoutedEventArgs e)
{
Button b = (Button)sender;
string[] coords = b.Name.Split('y');
coords[0] = coords[0].Replace("x", "");
// buttons start at 1,1 array at 0,0
int x = int.Parse(coords[0]) - 1;
int y = int.Parse(coords[1]) - 1;
if (this.field[y, x] == 1)
{
this.output.Text = "That's a hit!";
b.Background = Brushes.Green;
this.field[y, x] = 2;
this.hits += 1;
this.shots += 1;
}
else if (this.field[y, x] == 0)
{
b.Background = Brushes.Blue;
this.output.Text = "That was a shot in the water!";
this.misses += 1;
this.field[y, x] = 3;
this.shots += 1;
}
else if (this.field[y, x] == 2 || this.field[y, x] == 3)
{
this.output.Text = "You already shot there.";
}
scoreboard.Content = "shots:\r\n" + this.shots + "\r\nhits:\r\n" + this.hits + "\r\nmisses:\r\n" + this.misses;
}
private void CreateField()
{
// setup empty field
this.field = new int[10, 10] {
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0}
};
}
// place the ships the player is supposed to shoot at in the end
private void FillField()
{
// Patrol boat
this.GenerateShip(2);
// Destroyer
this.GenerateShip(3);
// Submarine
this.GenerateShip(3);
// Battleship
this.GenerateShip(4);
// Aircraft Carrier
this.GenerateShip(5);
}
public bool GetRandomBoolean()
{
return this.rand.Next(0, 2) == 0;
}
private void GenerateShip(int size)
{
int num1 = this.rand.Next(0, 10);
int num2 = this.rand.Next(0, 10);
bool horizontal = this.GetRandomBoolean();
bool valid = false;
while (true)
{
if (horizontal)
{
for (var i = 0; i < size; i++)
{
bool right, left, up, down = false;
try
{
right = this.field[num1 + i + 1, num2] == 1;
}
catch (IndexOutOfRangeException e)
{
right = false;
}
try
{
left = this.field[num1 + i - 1, num2] == 1;
}
catch (IndexOutOfRangeException e)
{
left = false;
}
try
{
up = this.field[num1 + i, num2 + 1] == 1;
}
catch (IndexOutOfRangeException e)
{
up = false;
}
try
{
down = this.field[num1 + i, num2 - 1] == 1;
}
catch (IndexOutOfRangeException e)
{
down = false;
}
if (num1 + i > 9) // index out of range
{
valid = false;
break;
}
else if (this.field[num1 + i, num2] == 1)
{
valid = false;
break;
}
else if (up || down || right || down)
{
valid = false;
break;
}
else if (num1 + i > 8 || num1 + i < 1 || num2 > 8 || num2 < 1)
{
valid = false;
break;
}
valid = true;
}
if (valid)
{
for (var i = 0; i < size; i++)
this.field[num1 + i, num2] = 1;
valid = false;
break;
}
}
else
{
for (var i = 0; i < size; i++)
{
bool right, left, up, down = false;
try
{
right = this.field[num1 + i + 1, num2] == 1;
}
catch (IndexOutOfRangeException e)
{
right = false;
}
try
{
left = this.field[num1 + i - 1, num2] == 1;
}
catch (IndexOutOfRangeException e)
{
left = false;
}
try
{
up = this.field[num1 + i, num2 + 1] == 1;
}
catch (IndexOutOfRangeException e)
{
up = false;
}
try
{
down = this.field[num1 + i, num2 - 1] == 1;
}
catch (IndexOutOfRangeException e)
{
down = false;
}
if (num2 + i > 9) // index out of range
{
valid = false;
break;
}
else if (this.field[num1, num2 + i] == 1)
{
valid = false;
break;
}
else if (up || down || right || down)
{
valid = false;
break;
}
else if (num1 > 8 || num1 < 1 || num2 + i > 8 || num2 + i < 1)
{
valid = false;
break;
}
valid = true;
}
if (valid)
{
for (var i = 0; i < size; i++)
{
this.field[num1, num2 + i] = 1;
}
valid = false;
break;
}
}
num1 = this.rand.Next(0, 10);
num2 = this.rand.Next(0, 10);
horizontal = this.GetRandomBoolean();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment