-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReplicatorGameView.cpp
285 lines (234 loc) · 7.44 KB
/
ReplicatorGameView.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// ReplicatorGameView.cpp : implementation of the CReplicatorGameView class
//
#include "stdafx.h"
#include "ReplicatorGame.h"
#include "World.h"
#include "ReplicatorGameDoc.h"
#include "ReplicatorGameView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CReplicatorGameView
IMPLEMENT_DYNCREATE(CReplicatorGameView, CView)
BEGIN_MESSAGE_MAP(CReplicatorGameView, CView)
//{{AFX_MSG_MAP(CReplicatorGameView)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CReplicatorGameView construction/destruction
CReplicatorGameView::CReplicatorGameView()
{
m_blocksize = CSize(15, 15);
}
CReplicatorGameView::~CReplicatorGameView()
{
}
BOOL CReplicatorGameView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CReplicatorGameView drawing
void CReplicatorGameView::OnDraw(CDC* pDC)
{
CReplicatorGameDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CSize viewport_size = pDC->GetViewportExt();
CWnd* dc_window = pDC->GetWindow();
RECT rect;
dc_window->GetClientRect(&rect);
//Text rendering
CFont thin_font;
thin_font.CreatePointFont(120, "Courier", pDC);
pDC->SelectObject(&thin_font);
CString blue_score;
blue_score.Format("Blue: %d", pDoc->GameScore(1));
CString red_score;
red_score.Format("Red (Computer AI): %d", pDoc->GameScore(2));
CString game_remaining;
int occupied = pDoc->GameScore(1) + pDoc->GameScore(2);
game_remaining.Format("Occupied spaces: %d/%d", occupied, 15*15);
int max_text_width = pDC->GetOutputTextExtent(game_remaining).cx;
int shortest_side = min(rect.right - max_text_width - 10, rect.bottom);
int segment = shortest_side / 15;
m_blocksize.cx = segment;
m_blocksize.cy = segment;
TEXTMETRIC textmetrics;
if (pDC->GetTextMetrics(&textmetrics))
{
int text_height = textmetrics.tmHeight + textmetrics.tmDescent;
pDC->TextOut((pDoc->m_play_field->GetWorldSize()).cx * m_blocksize.cx + 10, 0, blue_score);
pDC->TextOut((pDoc->m_play_field->GetWorldSize()).cx * m_blocksize.cx + 10, text_height, red_score);
pDC->TextOut((pDoc->m_play_field->GetWorldSize()).cx * m_blocksize.cx + 10, text_height * 2, game_remaining);
}
int loop, loop_x;
if (!pDoc->m_play_field->isWorld())
{
return;
}
CSize size_of_data = pDoc->m_play_field->GetWorldSize();
CPoint block_placement;
CPen blue_blockpen, red_blockpen;
blue_blockpen.CreatePen(0, 0, RGB(0,0,200));
red_blockpen.CreatePen(0, 0, RGB(200,0,0));
CPen* pOldPen = pDC->SelectObject(&blue_blockpen);
for (loop = 0; loop < size_of_data.cy; loop++)
{
for (loop_x = 0; loop_x < size_of_data.cx; loop_x++)
{
block_placement = CPoint(loop_x, loop);
int color = pDoc->m_play_field->ReadWorld(block_placement);
switch(color)
{
case 1:
pDC->SelectObject(blue_blockpen);
BlockPlot(block_placement, pDC);
break;
case 2:
pDC->SelectObject(red_blockpen);
BlockPlot(block_placement, pDC);
}
}
}
if (pDoc->m_player_selected != CPoint(-1,-1))
{
CRect temp(CPoint(pDoc->m_player_selected.x * m_blocksize.cx, pDoc->m_player_selected.y * m_blocksize.cy), m_blocksize);
pDC->FrameRect(temp, NULL);
}
//Clean up from OnDraw
pDC->SelectObject(pOldPen);
}
void CReplicatorGameView::BlockPlot(CPoint plotting_at, CDC* pDC)
{
for (int loop = 0; loop < m_blocksize.cy; loop++)
{
pDC->MoveTo(plotting_at.x*m_blocksize.cx, loop + plotting_at.y*m_blocksize.cy);
pDC->LineTo(plotting_at.x*m_blocksize.cx + m_blocksize.cx, loop + plotting_at.y*m_blocksize.cy);
}
}
/////////////////////////////////////////////////////////////////////////////
// CReplicatorGameView printing
BOOL CReplicatorGameView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CReplicatorGameView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CReplicatorGameView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CReplicatorGameView diagnostics
#ifdef _DEBUG
void CReplicatorGameView::AssertValid() const
{
CView::AssertValid();
}
void CReplicatorGameView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CReplicatorGameDoc* CReplicatorGameView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CReplicatorGameDoc)));
return (CReplicatorGameDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CReplicatorGameView message handlers
void CReplicatorGameView::OnLButtonDown(UINT nFlags, CPoint point)
{
CReplicatorGameDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
int x_loc, y_loc;
x_loc = point.x / m_blocksize.cx;
y_loc = point.y / m_blocksize.cy;
CSize world_size = pDoc->m_play_field->GetWorldSize();
if (world_size.cx <= x_loc || world_size.cy <= y_loc)
{
//Out of bounds.
return;
}
//Select the square clicked if it's controlled by player.
if (pDoc->m_player_selected == CPoint(-1,-1) || pDoc->m_play_field->ReadWorld(x_loc, y_loc) == 1)
{
if (pDoc->m_play_field->ReadWorld(x_loc, y_loc) == 1) //Human player is player #1.
{
pDoc->m_player_selected = CPoint(x_loc, y_loc);
Invalidate(false);
}
return;
}
//Take control of empty square.
if (pDoc->m_player_selected != CPoint(-1,-1) && pDoc->m_play_field->ReadWorld(x_loc, y_loc) == 0)
{
if (pDoc->Rules(1, pDoc->m_player_selected, CPoint(x_loc, y_loc)))
{
pDoc->CompleteMove(1, pDoc->m_player_selected, CPoint(x_loc, y_loc));
pDoc->m_player_selected = CPoint(-1,-1);
//Run computer's turn after player's turn is complete.
pDoc->AI();
Invalidate(true);
}
return;
}
if (pDoc->m_player_selected != CPoint(-1,-1))
{
if (pDoc->Rules(1, pDoc->m_player_selected, CPoint(x_loc, y_loc)))
{
Invalidate(false);
pDoc->m_player_selected = CPoint(-1,-1);
}
return;
}
if (pDoc->m_player_selected == CPoint(-1,-1))
{
pDoc->m_player_selected = CPoint(x_loc, y_loc);
}
else
{
if (pDoc->Rules(1, pDoc->m_player_selected, CPoint(x_loc, y_loc)) == false)
{
return;
}
//Copy if neighboring: copy. Move if a 2 spaces away.
int check_y, check_x;
switch(pDoc->m_play_field->ReadWorld(x_loc, y_loc))
{
case 0:
//Empty
for (check_y = max(0, y_loc - 1); check_y < min(y_loc + 1, 15); check_y++)
{
for (check_x = max(0, x_loc - 1); check_x < min(y_loc + 1, 15); check_x++)
{
if (pDoc->m_play_field->ReadWorld(check_x, check_y) == 2)
{
pDoc->m_play_field->WriteWorld(check_x, check_y, 1);
}
}
}
break;
case 1:
pDoc->m_player_selected = CPoint(x_loc, y_loc);
break;
case 2:
//Opponent.
break;
}
}
Invalidate(false);
}