|
|
大概一年半以前闲着无聊,于是就写了一个俄罗斯方块自娱自乐,程序运行如下图1所示:
; o# M7 I) i0 p. A图 17 R$ E* `5 g7 c% m" a* o8 u
完整的source code可以在附件下载。该程序使用WTL实现的,代码本身非常简单,无非是一些碰撞,变形等情况的判断调整。比较有意思的是程序使用了Factory Method用于创建方块,控制方块的变形。创建方块部分的设计类图如下所示:
+ m$ m2 ]$ S0 c5 N& ?* x
6 W+ L- I% u- B" R" Q/ G7 m9 Z
2 I, j5 k+ u$ }Factory的code如下:
9 Y" t( h9 H9 D, x1 ]3 E- }$ g& H2 N# ~ ) Z& _* U0 c! e) I. y
/*************************************$ m! {" |2 k- Y. f1 C
4 B) W6 |- i( o( u
* author:hgf_starlovecpp@hotmail.com
! W5 Z' i' H. }- ?% r3 Y$ Y8 _
' u2 E1 g, Y6 `* ]7 F2 l3 Q5 }* version:1.0
) [" K r, Z3 _6 c; d* g) ?' k& D' ~1 O0 P' y, Y5 @2 J: ^( g
* description: just for fun) X, Z6 ~. k6 X* w" _
# z X! ~6 I2 I/ A8 B
* todo: hard code exists need to fixed
' d( T! c7 s2 e- {) i5 c* L9 o/ g; E8 p4 T0 K7 c
**************************************/' R; _+ T& C7 P5 n4 s& f
: B: ]. q4 X2 X9 v#pragma once% _' G' h3 ~3 ~' F
#include <map>$ Z( @- v( i' R$ D( S9 [% `$ ?; B, B
#include "Tetris.h"! k) }1 X( M5 s4 ]
#include "OneTetris.h": U: Y: D+ s6 Y' T. L _
#include "TwoTetris.h"
* x. @6 D! z' x3 B#include "ThreeTetris.h"5 c! W, g# v/ P3 V
#include "FourTetris.h"
$ o1 `& R" t% y& L5 P& k ]#include "FiveTetris.h"! u6 R1 o% H; E1 D3 M
#include "SixTetris.h" w7 P A% }& u" g- f, l
#include "SevenTetris.h"; S* ]5 @! l3 B5 l$ e
2 v0 U: l% G8 @* [
class TetrisFactory; N+ u0 S9 Z' z; G+ ^
{
: P# t8 h6 f9 S. V9 N2 V5 Xpublic:
$ i4 S4 d& O, b. S " G( U$ R- @/ b, [$ l: {1 a) K& B
* M/ g" W4 ?' }5 [; fTetrisFactory(void)
# e! r9 g% H& ~+ G
5 \6 U; {" P" X* c{
7 |, q, s; V& x7 O; S! s
7 I; F( F$ E2 y2 |m_sel2TetrisMap[0] = &TetrisFactory::CreateOneTetris;/ X4 d% G9 l9 G, F( t
% i: c* ]5 f5 D% D7 f) }m_sel2TetrisMap[1] = &TetrisFactory::CreateTwoTetris;# A; R$ J }4 p- S
, A4 n4 a3 P9 `/ C1 b2 x* x# Rm_sel2TetrisMap[2] = &TetrisFactory::CreateThreeTetris;1 [9 Z7 }9 @, R9 C
; s/ B- Q' C' S: M0 F/ Im_sel2TetrisMap[3] = &TetrisFactory::CreateFourTetris;
1 s8 B9 B! a. Q: s' W6 |9 w' U& m8 Q
m_sel2TetrisMap[4] = &TetrisFactory::CreateFiveTetris;3 d% N/ r1 m; `% L0 n) c' l. t9 S
\7 r+ Z6 \% \, X9 j9 v
m_sel2TetrisMap[5] = &TetrisFactory::CreateSixTetris;
H; U+ [- U, N7 u/ S4 E
- v+ u& q0 q* T) Ym_sel2TetrisMap[6] = &TetrisFactory::CreateSevenTetris;
. Z# R3 t" h9 Z4 t$ _# c' Q
# H P! X& R+ d; ^/ h% |, S}' }( G/ k$ K, e
public:# Z# _' G( E, @8 g8 k) Y
8 t: t) y3 H! [8 i; b4 `: W5 h% [2 d/ T+ d7 V) a) K+ D& s
~TetrisFactory(void)
+ S3 l; `* w9 P* N+ j
9 X0 D6 q& q, D8 n% K{/ u8 C: f! y. v8 z
- u, D6 x4 ?: J! U% k
}
+ h* A) l; d6 w$ i$ U5 n% E& K4 ]
3 L' L7 r: y: P9 kTetris* CreateTetris(char sel)
# t6 k& M0 R4 W/ X+ r) S0 l7 E. }& t
{0 ], G5 d X5 d4 c0 y/ H. B
return (this->*(m_sel2TetrisMap.find(sel)->second))();* m& p1 M) p, Q8 J# ~$ M2 K
6 U7 r4 X& i6 q2 M8 @, d}! M6 N% y5 U, h* I4 S
private:
5 D2 U; |9 |- c6 S, X/ R4 o) }/ p! U$ K+ e9 i" {$ u& ^+ I
6 d" I& |$ O6 c9 v
( F Y2 l% V4 k5 N( gTetris* CreateOneTetris()& j) M) m6 z3 B
9 ^2 o* }1 h. l% }5 s/ S, n+ _
{
( r5 P/ A' e d, E0 r static OneTetris tetris;
- C1 G! r. p3 F5 U1 O' o
6 K2 K# y0 a# m9 q- k6 Htetris.InitPos();& v' _2 R% c$ t/ A A
return & tetris;
6 p! ^/ Y$ J3 T* B, ]6 q4 M) o5 w
% I( K" b! ?, L3 S* V) f}
& Q4 `7 _' |6 ~4 ]) \3 t- q+ h, k7 N4 W2 H
# ?. M- X. T8 Y( J
% y0 {+ G T: ~+ M$ TTetris* CreateTwoTetris()* _( A8 G/ m4 [$ Y
; M `' Z' U3 V7 E) ?* w
{
* T! _# X1 L+ H& D6 e( m+ y static TwoTetris tetris;) C2 R) f+ P$ f7 t
5 C- ~) f8 ~0 z) o: _! Gtetris.InitPos();
' |% ` ~# t7 x. R% {; g& X return & tetris;
8 x+ E6 f' C0 r* w4 c) e' T9 l1 g. Y: \1 N
}6 \" v3 H5 v: ^0 ~8 a8 t/ Z) N L
% N/ v" C `8 g. n- k# ~ N a! R3 A4 z% p! T. R- r" R
+ w5 h( K+ G; RTetris* CreateThreeTetris(): y: H" E/ q2 |9 b3 K7 h- F! V
: _9 ~9 Y5 }+ U$ T2 ^4 L; t# e
{/ a2 N2 e# f6 |( \# h( G
static ThreeTetris tetris;
: j7 M4 w( y2 x! S2 `0 T( y0 {, b- L( E! P% N$ v3 t- V
tetris.InitPos();; h. x6 N% q o- f1 c0 H
, d* ?. e5 U% L) V% r
8 }6 H$ o' f3 y! b% m/ T1 i- X Y9 areturn & tetris; C( Q" u0 L" e( S# r
% S& P) d y+ e L P}8 c, _% X+ Y, h7 G4 o; t, K4 w1 c& c
% ~ t; X" ?# Y
$ x; M1 M {6 w, s* J0 q" q: Y
0 R$ c+ _ X- f
Tetris* CreateFourTetris()
0 s# n' ]! J, T6 c" w4 a' U1 {8 R+ E1 ~8 W* C
{+ O; A5 z k! j5 g8 c) N. ^
static FourTetris tetris;6 B1 R0 ]" j) \. z1 `; K
: d2 M& p/ A# F2 l& Q5 i
tetris.InitPos();* E1 M% b1 b/ O3 H( W# Q" R. N4 \
return & tetris;+ i6 ^: v- J% d! T' J* ?' Z @
1 _6 S& h4 z: b3 K" e0 {
}
' X# ], x, a0 [0 l) Q7 _
2 d3 x; O4 ~8 u9 l3 E9 B) K: q4 {% O& h* p& r$ V& H6 I2 W
& l' t! L9 r' f( I' C
Tetris* CreateFiveTetris(); M! S3 Y# Y& z, Z! }
' l( g8 r# }& \( b4 R{
$ F7 T! q3 S4 p+ a) I e" ?- I. E static FiveTetris tetris;" }9 c) H: _6 t# f
# x) `3 _, C3 q9 ktetris.InitPos();
1 o) n+ o& e3 Q return & tetris;' r' U4 ~. |2 @5 W6 {$ J* \' |# x6 s
3 n8 A! }/ Z: J1 X' B/ j% v+ `# u
}- a( d' E3 @0 P. ?, l M
! Q+ T: B" X6 A8 _. R1 c' y
) e8 s. q3 d, g7 n4 L: f; n
Tetris* CreateSixTetris()
0 |0 q9 m5 Y. {/ K7 r9 P- W# c$ }/ ~) H* T1 V. ? M R7 o
{
$ }3 Y) U: X0 \% O static SixTetris tetris;0 i3 S1 Z4 y3 q8 j9 [( s' o
8 b( A% c( e' e$ u9 E" I
tetris.InitPos();, D* |1 m: r6 E! _3 _0 g9 Y
return & tetris;- w$ P7 F! m" o s% y
& K+ z B5 I6 q$ r6 w; U}$ d% u+ o: m" L* c: h
% ]" h; o( c; S6 H* |
( |; {( N) j# d+ V% I
" _) X4 Z7 o) w& W1 h( [4 L8 j
- D5 f: _9 C8 c, JTetris* CreateSevenTetris()
2 j& m, i8 \! Y3 y& ~
, W, `0 l% N" R; R8 X8 U; }5 u( C{
5 |% m w: l7 w& N0 s% | static SevenTetris tetris; {& I/ l7 o1 C' G* ^5 a7 ]
8 m) |" e% J% Y" P/ Mtetris.InitPos();) X0 }4 p" R3 ]# G9 F, f3 N7 A9 ]
return & tetris;
; D7 o% x5 K( v) w) Z* C# ]4 F8 h( t1 c( R' V; O3 N; w/ |
}
2 \, k0 j( k% ]$ F9 X
4 o5 {) F' I0 F2 ?8 F( Z' M/ [private:
1 }, z1 R S: O1 k/ J 1 o7 h/ g3 i0 F8 [
typedef Tetris*(TetrisFactory::*PMA)();/ w1 O- H0 ~5 p% u8 k0 R+ O0 T4 s
' p! [$ m2 A" v8 _7 f
std::map<char,PMA> m_sel2TetrisMap;! D5 w; s/ c% H
$ ?# F6 k; x/ j, W- A/ {5 x; M9 D! M. e5 h/ y
};
1 n; _! n" D. N& q) P7 n: m 2 } E1 w/ s4 M
有兴趣的朋友可以下下来改改玩玩。- E0 M5 Y Z' Z# ^6 d9 f
3 i* m! l# w, C7 o4 a
That’s all!# u0 t8 S# m5 |) X( m
1 F! d9 j# q: g7 a* z/ DPeter 7 Y' y3 C, r2 s$ z& ^, O
& }, X& |3 U' v) d; F5 {[ 本帖最后由 peterhu 于 2009-5-20 09:51 编辑 ] |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?加入计匠网
×
|