-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameArrayMap.cs
73 lines (61 loc) · 2.02 KB
/
GameArrayMap.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
namespace MMayinTarlasi
{
class GameArrayMap
{
private static readonly sbyte[] x = { 1, -1, 1, -1, 1, -1, 0, 0};
private static readonly sbyte[] y = { 1, -1, -1, 1, 0, 0, -1, 1};
Random rand = new Random();
private readonly byte mapWidth;
private readonly byte mapHeight;
private readonly byte mapMine;
public byte[,] map;
public GameArrayMap(byte width, byte height, byte mine)
{
this.mapWidth = width;
this.mapHeight = height;
this.mapMine = mine;
SetMapArr();
AddMines(this.mapMine);
}
public void SetMapArr()
{
this.map = new byte[this.mapWidth, this.mapHeight];
}
public void AddMines(byte mineNum) // Matrisin içine rastgele indislere 9 değeri yazılıyor, 9 => mayınlı indisi temsil ediyor.
{
byte randWidth;
byte randHeight;
while (mineNum != 0)
{
randWidth = (byte)rand.Next(this.mapWidth);
randHeight = (byte)rand.Next(this.mapHeight);
if (this.map[randWidth, randHeight] != 9)
{
this.map[randWidth, randHeight] = 9;
MineCounter(randWidth, randHeight);
mineNum--;
}
}
}
//
// X = WIDTH = ROW
// Y = HEIGHT = COLUMN
//
private void MineCounter(byte cordX, byte cordY)
{
for (byte k = 0; k < 8; k++)
{
if
(
cordX + x[k] >= 0
&& cordY + y[k] >= 0
&& cordX + x[k] < this.mapWidth
&& cordY + y[k] < this.mapHeight
&& this.map[(cordX + x[k]), (cordY + y[k])] != 9
)
{ this.map[cordX + x[k], cordY + y[k]]++; }
}
}
}
}