forked from cpe200-160/twofourzeroeight
/
TwoZeroFourEightView.cs
194 lines (169 loc) · 5.96 KB
/
TwoZeroFourEightView.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace twozerofoureight
{
public partial class TwoZeroFourEightView : Form, View
{
Model model;
Controller controller;
public TwoZeroFourEightView()
{
InitializeComponent();
model = new TwoZeroFourEightModel();
model.AttachObserver(this);
controller = new TwoZeroFourEightController();
controller.AddModel(model);
controller.ActionPerformed(TwoZeroFourEightController.LEFT);
//kpeh = new KeyPressEventHandler(key_Press);
// this.KeyPress += kpeh; //this works only when the contro (form) has focus
keh = new KeyEventHandler(key_Down);
this.KeyDown += keh; //this works only when the control (form) has focus
// this.KeyPress += new KeyPressEventHandler(key_Press); //create object
}
// private KeyPressEventHandler kpeh;
private KeyEventHandler keh;
// protected void key_Press(object sneder , EventArgs e)
// {
// }
//protected void key_Down(object sender , EventArgs e)
// {
// }
//the own method to bevexcuted when the event occurs.
private void key_Press(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == 'w')
{
controller.ActionPerformed(TwoZeroFourEightController.UP);
}
//check if allow key up is pressed then call something to perform UP
//
}
private void key_Down(object sender, KeyEventArgs e)
{
switch (e.KeyData)
{
case Keys.Right:
case Keys.D:
controller.ActionPerformed(TwoZeroFourEightController.RIGHT);
break;
case Keys.Left:
case Keys.A:
controller.ActionPerformed(TwoZeroFourEightController.LEFT);
break;
case Keys.Up:
case Keys.W:
controller.ActionPerformed(TwoZeroFourEightController.UP);
break;
case Keys.Down:
case Keys.S:
controller.ActionPerformed(TwoZeroFourEightController.DOWN);
break;
}
/* if(e.KeyData == Keys.Right)
{
controller.ActionPerformed(TwoZeroFourEightController.RIGHT);
}
else if(e.KeyData == Keys.Down)
{
controller.ActionPerformed(TwoZeroFourEightController.DOWN);
}
else if (e.KeyData == Keys.Left)
{
controller.ActionPerformed(TwoZeroFourEightController.LEFT);
}
else if (e.KeyData == Keys.Up)
{
controller.ActionPerformed(TwoZeroFourEightController.UP);
}
*/
//check if allow key up is down then call something to perform UP
}
public void Notify(Model m)
{
UpdateBoard(((TwoZeroFourEightModel) m).GetBoard()); //call medtod // m model //casting
UpdateScore(((TwoZeroFourEightModel)m).GetScore());
UpdateGameStatus(((TwoZeroFourEightModel)m).IsGameover(((TwoZeroFourEightModel)m).GetBoard())); //add new on board
}
private void UpdateGameStatus(bool b)
{
//if(true) , hide "this form" and show "gameover form "
if(b == true)
{
lblScore.Text = "Gameover"; //print on text score
}
}
private void UpdateScore(int i)
{
lblScore.Text = i.ToString();
}
private void UpdateTile(Label l, int i)
{
if (i != 0)
{
l.Text = Convert.ToString(i);
} else {
l.Text = "";
}
switch (i)
{
case 0:
l.BackColor = Color.Pink;
break;
case 2:
l.BackColor = Color.GreenYellow;
break;
case 4:
l.BackColor = Color.Orange;
break;
case 8:
l.BackColor = Color.Red;
break;
default:
l.BackColor = Color.Green;
break;
}
}
private void UpdateBoard(int[,] board)
{
UpdateTile(lbl00,board[0, 0]);
UpdateTile(lbl01,board[0, 1]);
UpdateTile(lbl02,board[0, 2]);
UpdateTile(lbl03,board[0, 3]);
UpdateTile(lbl10,board[1, 0]);
UpdateTile(lbl11,board[1, 1]);
UpdateTile(lbl12,board[1, 2]);
UpdateTile(lbl13,board[1, 3]);
UpdateTile(lbl20,board[2, 0]);
UpdateTile(lbl21,board[2, 1]);
UpdateTile(lbl22,board[2, 2]);
UpdateTile(lbl23,board[2, 3]);
UpdateTile(lbl30,board[3, 0]);
UpdateTile(lbl31,board[3, 1]);
UpdateTile(lbl32,board[3, 2]);
UpdateTile(lbl33,board[3, 3]);
}
private void btnLeft_Click(object sender, EventArgs e)
{
controller.ActionPerformed(TwoZeroFourEightController.LEFT);
}
private void btnRight_Click(object sender, EventArgs e)
{
controller.ActionPerformed(TwoZeroFourEightController.RIGHT);
}
private void btnUp_Click(object sender, EventArgs e)
{
controller.ActionPerformed(TwoZeroFourEightController.UP);
}
private void btnDown_Click(object sender, EventArgs e)
{
controller.ActionPerformed(TwoZeroFourEightController.DOWN);
}
}
}