forked from cpe200-160/twofourzeroeight
-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathTwoZeroFourEightModel.cs
196 lines (185 loc) · 5.97 KB
/
TwoZeroFourEightModel.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace twozerofoureight
{
class TwoZeroFourEightModel : Model
{
protected int boardSize; // default is 4
protected int[,] board;
protected Random rand;
protected int[] range;
public TwoZeroFourEightModel() : this(4)
{
// default board size is 4
}
public TwoZeroFourEightModel(int size)
{
boardSize = size;
board = new int[boardSize, boardSize];
range = Enumerable.Range(0, boardSize).ToArray();
foreach (int i in range)
{
foreach (int j in range)
{
board[i, j] = 0;
}
}
rand = new Random();
// initialize board
HandleChanges();
}
public int[,] GetBoard()
{
return board;
}
private void AddRandomSlot()
{
while (true)
{
int x = rand.Next(boardSize);
int y = rand.Next(boardSize);
if (board[x, y] == 0)
{
board[x, y] = 2;
return;
}
}
}
// Perform shift and merge to the left of the given array.
protected bool ShiftAndMerge(int[] buffer)
{
bool changed = false; // whether the array has changed
int pos = 0; // next available slot index
int lastMergedSlot = -1; // last slot that resulted from merging
foreach (int k in range)
{
if (buffer[k] != 0) // nonempty slot
{
// check if we can merge with the previous slot
if (pos > 0 && pos - 1 > lastMergedSlot && buffer[pos - 1] == buffer[k])
{
// merge
buffer[pos - 1] *= 2;
buffer[k] = 0;
lastMergedSlot = pos - 1;
changed = true;
}
else
{
// shift to the next available slot
buffer[pos] = buffer[k];
if (pos != k)
{
buffer[k] = 0;
changed = true;
}
// move the next available slot
pos++;
}
}
}
return changed;
}
protected void HandleChanges(bool changed = true)
{
// if the board has changed, add a new number
// and notify all views
if (changed)
{
AddRandomSlot();
NotifyAll();
}
}
public void PerformDown()
{
bool changed = false; // whether the board has changed
foreach (int i in range)
{
int[] buffer = new int[boardSize];
// extract the current column from bottom to top
foreach (int j in range)
{
buffer[boardSize - j - 1] = board[j, i];
}
// process the extracted array
// also track changes
changed = ShiftAndMerge(buffer) || changed;
// copy back
foreach (int j in range)
{
board[j, i] = buffer[boardSize - j - 1];
}
}
HandleChanges(changed);
}
public void PerformUp()
{
bool changed = false; // whether the board has changed
foreach (int i in range)
{
int[] buffer = new int[boardSize];
// extract the current column from top to bottom
foreach (int j in range)
{
buffer[j] = board[j, i];
}
// process the extracted array
// also track changes
changed = ShiftAndMerge(buffer) || changed;
// copy back
foreach (int j in range)
{
board[j, i] = buffer[j];
}
}
HandleChanges(changed);
}
public void PerformRight()
{
bool changed = false; // whether the board has changed
foreach (int i in range)
{
int[] buffer = new int[boardSize];
// extract the current column from right to left
foreach (int j in range)
{
buffer[boardSize - j - 1] = board[i, j];
}
// process the extracted array
// also track changes
changed = ShiftAndMerge(buffer) || changed;
// copy back
foreach (int j in range)
{
board[i, j] = buffer[boardSize - j - 1];
}
}
HandleChanges(changed);
}
public void PerformLeft()
{
bool changed = false; // whether the board has changed
foreach (int i in range)
{
int[] buffer = new int[boardSize];
// extract the current column from left to right
foreach (int j in range)
{
buffer[j] = board[i, j];
}
// process the extracted array
// also track changes
changed = ShiftAndMerge(buffer) || changed;
// copy back
foreach (int j in range)
{
board[i, j] = buffer[j];
}
}
HandleChanges(changed);
}
}
}