-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewModel.cs
190 lines (167 loc) · 6.08 KB
/
ViewModel.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
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using inline.Model;
using System.ComponentModel;
using System.Threading;
using System.Windows.Threading;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
namespace inline
{
public class ViewModel : INotifyPropertyChanged
{
private readonly NextItemGetter mNextItemGetter = new NextItemGetter();
public IList<GameItem> NextItems { get; set; }
public GridGameItems Items { get { return _items; } }
public ViewModel()
{
NextItems = new ObservableCollection<GameItem>();
_items = new GridGameItems(7, 7);
for (int i = 0; i < _items.Rows; i++)
{
for (int j = 0; j < _items.Columns; j++)
{
_items[i, j] = new GameItem(i,j, "");
}
}
Result = string.Format("Score: {0}", 0);
}
int total = 0;
public int Total { get { return total; } }
internal void ShowPoints(int incomingPoints)
{
if(incomingPoints>=4)
Result = string.Format("Score: {0} + {1} {2}", total.ToString(), CalculateBonus(incomingPoints),PointsToCheerUp(incomingPoints));
}
private int CalculateBonus(int incomingPoints)
{
if(incomingPoints>=4)
return incomingPoints + (int)Math.Pow((incomingPoints - 4), 2);
return 0;
}
private string PointsToCheerUp(int incomingPoints)
{
switch (incomingPoints)
{
case 4:return "Well done!";
case 5: return "Cool!";
case 6: return "Awesome!";
default:
if (incomingPoints >= 7)
return "Legendary!";
else
return string.Empty;
}
}
public void AddPoints(int value)
{
total += CalculateBonus(value);
Result = string.Format("Score: {0}", total.ToString());
}
public GameItem SelectedItem { get; set; }
private string _result;
public event PropertyChangedEventHandler PropertyChanged;
private GridGameItems _items;
public string Result
{
get { return _result; }
set { _result = value; this.RaisePropertyChanged(PropertyChanged, "Result"); }
}
public bool PlaceNextItems()
{
var emptyCells = _items.Where(x => x.State == State.Empty).ToArray();
if (emptyCells.Length < 3)
{
return false;
}
Random rnd = new Random();
foreach (var item in NextItems)
{
//could be already populated by the previous iteration
while (true)
{
int nextPosition = rnd.Next(emptyCells.Length);
if (emptyCells[nextPosition].State == State.Empty)
{
emptyCells[nextPosition].State = item.State;
emptyCells[nextPosition].Content = item.Content;
break;
}
}
}
NextItems.Clear();
NextItems.Add(mNextItemGetter.GetNext());
NextItems.Add(mNextItemGetter.GetNext());
NextItems.Add(mNextItemGetter.GetNext());
return true;
}
internal void MoveSelectedGameItemTo(GameItem gameItem)
{
gameItem.State = SelectedItem.State;
gameItem.Content = SelectedItem.Content;
SelectedItem.State = State.Empty;
}
internal void ChangeVisualStyleOfEmptyCells(GameItem gameItem)
{
foreach (var item in AccesibleCellsFrom(gameItem))
{
item.State = State.Empty;
}
}
public List<GameItem> AccesibleCellsFrom(GameItem gameItem)
{
List<GameItem> result = new List<GameItem>();
GetAccesibleCellsFrom(result, gameItem.X, gameItem.Y);
return result;
}
private void GetAccesibleCellsFrom(List<GameItem> accesibleItems, int i, int j)
{
if (i + 1 < Items.Rows && Items[i + 1, j].State == State.Empty && !accesibleItems.Contains(Items[i + 1, j]))
{
accesibleItems.Add(Items[i + 1, j]);
GetAccesibleCellsFrom(accesibleItems, i+1, j);
}
if (i - 1 >= 0 && Items[i - 1, j].State == State.Empty && !accesibleItems.Contains(Items[i - 1, j]))
{
accesibleItems.Add(Items[i - 1, j]);
GetAccesibleCellsFrom(accesibleItems, i - 1, j);
}
if (j - 1 >= 0 && Items[i, j - 1].State == State.Empty && !accesibleItems.Contains(Items[i, j - 1]))
{
accesibleItems.Add(Items[i, j - 1]);
GetAccesibleCellsFrom(accesibleItems, i, j - 1);
}
if (j + 1 < Items.Columns && Items[i, j + 1].State == State.Empty && !accesibleItems.Contains(Items[i, j + 1]))
{
accesibleItems.Add(Items[i, j + 1]);
GetAccesibleCellsFrom(accesibleItems, i, j + 1);
}
}
internal void ChangeStyleOfEmptyCellsToNormal()
{
foreach (var item in Items)
if (item.State == State.Empty)
item.State = State.Empty;
}
internal void Reset()
{
foreach (var item in Items)
{
item.State = State.Empty;
}
NextItems.Clear();
NextItems.Add(mNextItemGetter.GetNext());
NextItems.Add(mNextItemGetter.GetNext());
NextItems.Add(mNextItemGetter.GetNext());
}
}
}