equal
deleted
inserted
replaced
14 #include <e32math.h> |
14 #include <e32math.h> |
15 |
15 |
16 using namespace IoUtils; |
16 using namespace IoUtils; |
17 |
17 |
18 _LIT(KSnakeSeg, "*"); |
18 _LIT(KSnakeSeg, "*"); |
|
19 _LIT(KUnicodeSnakeSeg, "\u2588"); // Full block |
19 _LIT(KBait, "$"); |
20 _LIT(KBait, "$"); |
|
21 _LIT(KUnicodeBait, "\u2665"); // heart |
20 |
22 |
21 class CKeyWatcher; |
23 class CKeyWatcher; |
22 |
24 |
23 class CCmdSnake : public CCommandBase |
25 class CCmdSnake : public CCommandBase |
24 { |
26 { |
43 void ConstructL(); |
45 void ConstructL(); |
44 void InitBoardL(); |
46 void InitBoardL(); |
45 void SetBoard(TInt aX, TInt aY, TBool aSet); |
47 void SetBoard(TInt aX, TInt aY, TBool aSet); |
46 TBool GetBoard(TInt aX, TInt aY); |
48 TBool GetBoard(TInt aX, TInt aY); |
47 void DrawBoardL(); |
49 void DrawBoardL(); |
|
50 TUint16 BoardCharacter(TInt aX, TInt aY); |
48 void InitSnakeL(TInt aLen, TPoint aPos); |
51 void InitSnakeL(TInt aLen, TPoint aPos); |
49 void DrawSnakeL(); |
52 void DrawSnakeL(); |
50 void DrawScore(); |
53 void DrawScore(); |
51 void PlaceBait(); |
54 void PlaceBait(); |
52 void Grow(); |
55 void Grow(); |
62 TInt iBoardWidthWords; |
65 TInt iBoardWidthWords; |
63 TPoint iDirection; |
66 TPoint iDirection; |
64 TPoint iBait; |
67 TPoint iBait; |
65 TInt iScore; |
68 TInt iScore; |
66 TInt iSpeed; |
69 TInt iSpeed; |
|
70 TBool iUnicode; |
67 |
71 |
68 CKeyWatcher* iKeys; |
72 CKeyWatcher* iKeys; |
69 CPeriodic* iTimer; |
73 CPeriodic* iTimer; |
70 TInt64 iRandomSeed; |
74 TInt64 iRandomSeed; |
71 TBool iInCollisionGracePeriod; |
75 TBool iInCollisionGracePeriod; |
161 |
165 |
162 void CCmdSnake::OptionsL(RCommandOptionList& aOptions) |
166 void CCmdSnake::OptionsL(RCommandOptionList& aOptions) |
163 { |
167 { |
164 _LIT(KOptSpeed, "speed"); |
168 _LIT(KOptSpeed, "speed"); |
165 aOptions.AppendIntL(iSpeed, KOptSpeed); |
169 aOptions.AppendIntL(iSpeed, KOptSpeed); |
|
170 _LIT(KOptUnicode, "unicode"); |
|
171 aOptions.AppendBoolL(iUnicode, KOptUnicode); |
166 } |
172 } |
167 |
173 |
168 void CCmdSnake::ConstructL() |
174 void CCmdSnake::ConstructL() |
169 { |
175 { |
170 BaseConstructL(); |
176 BaseConstructL(); |
228 |
234 |
229 for (TInt y=0; y<iBoardSize.iHeight; ++y) |
235 for (TInt y=0; y<iBoardSize.iHeight; ++y) |
230 { |
236 { |
231 for (TInt x=0; x<iBoardSize.iWidth; ++x) |
237 for (TInt x=0; x<iBoardSize.iWidth; ++x) |
232 { |
238 { |
233 line[x] = GetBoard(x,y) ? '#' : ' '; |
239 //line[x] = GetBoard(x,y) ? '#' : ' '; |
|
240 line[x] = BoardCharacter(x, y); |
234 } |
241 } |
235 User::LeaveIfError(iCons.SetCursorPosAbs(TPoint(0, y))); |
242 User::LeaveIfError(iCons.SetCursorPosAbs(TPoint(0, y))); |
236 User::LeaveIfError(iCons.Write(line)); |
243 User::LeaveIfError(iCons.Write(line)); |
237 } |
244 } |
238 |
245 |
239 |
246 |
240 CleanupStack::PopAndDestroy(); |
247 CleanupStack::PopAndDestroy(); |
241 } |
248 } |
242 |
249 |
|
250 TUint16 CCmdSnake::BoardCharacter(TInt aX, TInt aY) |
|
251 { |
|
252 if (!GetBoard(aX, aY)) |
|
253 { |
|
254 return ' '; |
|
255 } |
|
256 else if (!iUnicode) |
|
257 { |
|
258 return '#'; |
|
259 } |
|
260 else |
|
261 { |
|
262 // TODO this is too simplistic for anything other than the basic board |
|
263 if (aX == 0) |
|
264 { |
|
265 if (aY == 0) |
|
266 { |
|
267 return 0x2554; // TL |
|
268 } |
|
269 else if (aY == iBoardSize.iHeight - 1) |
|
270 { |
|
271 return 0x255A; // BL |
|
272 } |
|
273 else |
|
274 { |
|
275 return 0x2551; |
|
276 } |
|
277 } |
|
278 else if (aX == iBoardSize.iWidth - 1) |
|
279 { |
|
280 if (aY == 0) |
|
281 { |
|
282 return 0x2557; // TR |
|
283 } |
|
284 else if (aY == iBoardSize.iHeight - 1) |
|
285 { |
|
286 return 0x255D; // BR; |
|
287 } |
|
288 else |
|
289 { |
|
290 return 0x2551; |
|
291 } |
|
292 } |
|
293 else |
|
294 { |
|
295 return 0x2550; |
|
296 } |
|
297 } |
|
298 } |
|
299 |
243 void CCmdSnake::InitSnakeL(TInt aLen, TPoint aPos) |
300 void CCmdSnake::InitSnakeL(TInt aLen, TPoint aPos) |
244 { |
301 { |
245 iSnake.Reset(); |
302 iSnake.Reset(); |
246 for (TInt i=0; i<aLen; ++i) |
303 for (TInt i=0; i<aLen; ++i) |
247 { |
304 { |
253 void CCmdSnake::DrawSnakeL() |
310 void CCmdSnake::DrawSnakeL() |
254 { |
311 { |
255 for (TInt i=0; i<iSnake.Count(); ++i) |
312 for (TInt i=0; i<iSnake.Count(); ++i) |
256 { |
313 { |
257 User::LeaveIfError(iCons.SetCursorPosAbs(iSnake[i])); |
314 User::LeaveIfError(iCons.SetCursorPosAbs(iSnake[i])); |
258 User::LeaveIfError(iCons.Write(KSnakeSeg)); |
315 User::LeaveIfError(iCons.Write(iUnicode ? KUnicodeSnakeSeg : KSnakeSeg)); |
259 } |
316 } |
260 } |
317 } |
261 |
318 |
262 void CCmdSnake::Up() |
319 void CCmdSnake::Up() |
263 { |
320 { |
353 { |
410 { |
354 iCons.SetCursorPosAbs(iSnake[tail]); |
411 iCons.SetCursorPosAbs(iSnake[tail]); |
355 iCons.Write(_L(" ")); |
412 iCons.Write(_L(" ")); |
356 } |
413 } |
357 iCons.SetCursorPosAbs(newHead); |
414 iCons.SetCursorPosAbs(newHead); |
358 iCons.Write(KSnakeSeg); |
415 iCons.Write(iUnicode ? KUnicodeSnakeSeg : KSnakeSeg); |
359 iSnakeHead = (iSnakeHead+1) % iSnake.Count(); |
416 iSnakeHead = (iSnakeHead+1) % iSnake.Count(); |
360 iSnake[iSnakeHead] = newHead; |
417 iSnake[iSnakeHead] = newHead; |
361 |
418 |
362 if (iSnake[iSnakeHead] == iBait) |
419 if (iSnake[iSnakeHead] == iBait) |
363 { |
420 { |
396 if (iSnake[i] == iBait) ok = EFalse; |
453 if (iSnake[i] == iBait) ok = EFalse; |
397 } |
454 } |
398 } |
455 } |
399 } while (!ok); |
456 } while (!ok); |
400 iCons.SetCursorPosAbs(iBait); |
457 iCons.SetCursorPosAbs(iBait); |
401 iCons.Write(KBait); |
458 iCons.Write(iUnicode ? KUnicodeBait : KBait); |
402 } |
459 } |
403 |
460 |
404 void CCmdSnake::Grow() |
461 void CCmdSnake::Grow() |
405 { |
462 { |
406 TInt tail = (iSnakeHead+1)% iSnake.Count(); |
463 TInt tail = (iSnakeHead+1)% iSnake.Count(); |