-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActions.cs
More file actions
80 lines (79 loc) · 2.29 KB
/
Actions.cs
File metadata and controls
80 lines (79 loc) · 2.29 KB
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
74
75
76
77
78
79
80
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameOfLife
{
interface IAction // ACTION INTERFACE
{
bool DoAction(int x, int y, Creator creator, Cell[,] field);
}
interface ITypeAction<T> // ACTION INTERFACE
{
bool DoAction(int x, int y, Creator creator, Cell[,] field);
}
class MultAction : IAction // ACTION CLASS
{
public bool DoAction(int x, int y, Creator creator, Cell[,] field)
{
Cell[] cells = GetNeighbours.GetCells(x, y, field);
foreach (var i in cells)
{
if (i is EmptyCell)
{
Cell newCell = creator.CreateCell(i.x, i.y);
field[i.x, i.y] = newCell;
return true;
}
}
return false;
}
}
class MoveAction : IAction
{
public bool DoAction(int x, int y, Creator creator, Cell[,] field)
{
Cell[] cells = GetNeighbours.GetCells(x, y, field);
cells = cells.Where(c => c is EmptyCell).ToArray();
if (cells.Length != 0)
{
Random random = new Random();
int rnd = random.Next(cells.Length);
Cell cell = cells[rnd];
if (cell != null)
{
field[cell.x, cell.y] = field[x, y];
field[x, y] = new EmptyCell(x, y);
return true;
}
}
return false;
}
}
class DieAction : IAction
{
public bool DoAction(int x, int y, Creator creator, Cell[,] field)
{
field[x, y] = new EmptyCell(x, y);
return true;
}
}
class EatAction<T> : ITypeAction<T>
{
public bool DoAction(int x, int y, Creator creator, Cell[,] field)
{
Cell[] cells = GetNeighbours.GetCells(x, y, field);
foreach (var i in cells)
{
if (i is T)
{
field[i.x, i.y] = field[x, y];
field[x, y] = new EmptyCell(x, y);
return true;
}
}
return false;
}
}
}