|
|
大概一年半以前闲着无聊,于是就写了一个俄罗斯方块自娱自乐,程序运行如下图1所示:
: Y; i+ W. X! E6 C1 y* i) G; H- c图 16 z9 {3 ]2 s: h! X
完整的source code可以在附件下载。该程序使用WTL实现的,代码本身非常简单,无非是一些碰撞,变形等情况的判断调整。比较有意思的是程序使用了Factory Method用于创建方块,控制方块的变形。创建方块部分的设计类图如下所示:! [% F. H; \4 S2 w* C" Q
4 [" y0 |; I* x4 ]
% p5 l6 q2 m' q, l+ G) ?8 bFactory的code如下:
3 {5 s c6 y, G( ~8 Q. V* `
0 k3 \) b! u8 U- S) ]! X/************************************** |: b- t$ |1 M- ^3 e% u8 p
$ o1 S2 g- V& P' K- g* author:hgf_starlovecpp@hotmail.com
! S* H4 O9 I4 b1 |- I; h
* i- _3 [6 e9 m& p5 R* version:1.0# B( I# D1 x6 W
. r* b* U3 C# w8 n
* description: just for fun
/ |4 ]- C f$ s+ m4 l0 g5 i' m4 j& i4 T) c7 w D7 u7 {
* todo: hard code exists need to fixed* `$ J& \$ g- ]$ T) s
& R: ?. j* b3 I1 j p1 M7 I! g' r
**************************************/1 m0 v( b3 y3 R5 H
% {2 d! U: r1 D3 _& m#pragma once
' g# P) \( d. f% v+ P' c' h#include <map>
3 b! t1 s) Y! R: f#include "Tetris.h"5 l- Y1 n4 ?& w8 A) j- d& I4 { \
#include "OneTetris.h"& O. z) l# K3 k5 F6 y: W- r# g
#include "TwoTetris.h"+ Q; v, z0 q% J% \! u% w
#include "ThreeTetris.h"
! L* i8 y2 G3 x! y: X- c- z#include "FourTetris.h"3 _0 c4 m* M0 g( m. h
#include "FiveTetris.h" o, D% ?4 x3 O; N1 N# S% c0 P
#include "SixTetris.h"
( P. b. L1 ^/ B#include "SevenTetris.h": O- D3 u! h* {4 Y9 @
3 o: R+ x$ b6 w3 D' p
class TetrisFactory4 w1 k" k" f/ M8 e
{
: g+ e9 A6 t" `2 A3 [public:0 `) c6 v0 H3 U- l& g; ]
, @& w- R) W, f9 V. z
0 I8 o, m! @: S$ @6 j$ e! {TetrisFactory(void)
, Q. b0 P4 a4 d* p5 J+ m! o& x* t' x0 L
{* A3 H( L& c' f4 H. W, j8 u; G
1 b2 l9 L" Z9 om_sel2TetrisMap[0] = &TetrisFactory::CreateOneTetris; O0 v. M* n6 Y% u) E
; w0 Y+ o9 x H1 ~$ ?m_sel2TetrisMap[1] = &TetrisFactory::CreateTwoTetris;3 |( ?& V( j" ~
* |3 U& q9 B& n& T6 F5 A
m_sel2TetrisMap[2] = &TetrisFactory::CreateThreeTetris;& K, x& @* r2 `7 E3 z; Q; M$ R6 J
, Z0 P- `; B/ o1 f7 X7 Xm_sel2TetrisMap[3] = &TetrisFactory::CreateFourTetris;/ ?5 q1 H$ n' L" `0 c, d2 W
) h4 g ~4 V+ b4 D' I
m_sel2TetrisMap[4] = &TetrisFactory::CreateFiveTetris;
/ R3 i2 X+ k8 b9 |6 R# \ S
+ n9 [2 c& P7 t7 hm_sel2TetrisMap[5] = &TetrisFactory::CreateSixTetris;
6 x! Y' N0 S0 ~9 `$ b f$ W+ H ~ x- G. K4 H
m_sel2TetrisMap[6] = &TetrisFactory::CreateSevenTetris;
- \3 a) Y$ |$ E( r: ], i7 z) K7 r$ k+ G& S, B6 Y$ }
}
& ]0 b0 x! b8 a+ Q* d$ Jpublic:
/ }2 L: ~: e( ?& q7 B * u3 F; x+ u# V# e" q9 B2 i# J2 t
$ T% o' f8 o1 g; c$ G9 b
~TetrisFactory(void)
* c( v! B; j A" k$ i0 b+ l- l8 y0 o
{; h! B; ~8 n' X# o! ]
1 t5 E7 ^" k5 @' l}- ~1 G7 {. Z; e! t4 W) [
3 n& {5 ~# q% L- x5 z' c2 v- o' f
Tetris* CreateTetris(char sel)
+ Z% F2 c4 m, p
' J, H3 @' F F7 T' q7 X{1 k- x; Z% p5 a* D7 x
return (this->*(m_sel2TetrisMap.find(sel)->second))();
' Y4 P2 h9 N* P2 P& w* @
8 L9 d1 b. T! e( }- A: t0 k9 H2 N: c}
( r+ ~4 w2 A1 r% m0 |" z; hprivate:( R; a& x% J {- ~0 ?7 q0 K
/ e+ m! S( l# Z* \, J
! j9 v! W" o6 B* Q' T+ C
0 Q; }* P& S2 o2 cTetris* CreateOneTetris(): s+ R! I& M# w k7 E O
( B/ \+ s* C. d8 X. l6 f{1 w G0 ]' F# g1 E+ H
static OneTetris tetris;
" J0 Y; n$ p' s3 z$ F2 [+ ^
# b" L- ]0 {( _$ y2 stetris.InitPos();
! y& E$ P! k; m4 U5 e return & tetris;
/ ~6 ~* N% n m* K4 l0 b4 N- `& _8 O4 p0 g0 ~
}
6 A( T2 k- ]: U( N4 V
8 K* d" i1 ?' y. S, V8 N: A& `" q
+ G* U# i5 u7 A& I- i8 ^/ ~$ l% q+ l/ l$ U, r7 G
Tetris* CreateTwoTetris(). r9 t+ [/ @$ |. J6 m
* P4 F+ x) N/ w1 P1 l, s{
7 a% @, M) J) ~2 [$ x" w: S+ x5 P static TwoTetris tetris;
4 u' A4 G# F/ Z8 f2 i g* q# B, W
* \7 i8 z# w$ c& s% |9 ^) q0 Atetris.InitPos();( i8 b! ~+ W4 c3 y8 N6 {
return & tetris;
7 ~8 d. e! m, O$ I/ w( N
; e% N2 m5 d% b- `/ h. ^0 ^}) \) r5 ^- a1 \+ }$ }, C6 o: E
' O) A6 I7 O( V% }; z b+ a, O3 G8 S
$ t+ ? y# y: P1 v& D4 I
' C. Z s1 ]% R- tTetris* CreateThreeTetris()
& m; b* i/ I- I H0 z. h9 v, z1 _3 \% O1 ]' m" U4 E+ k' A
{1 U. b X6 U) A# c" w( i6 u( I
static ThreeTetris tetris;; S1 I% K9 d) T+ F
2 g# c7 D3 z8 |- n4 Q2 Vtetris.InitPos();" Q8 S" g! V$ p, E' p8 _
1 j, ]) ] u9 q/ O3 C. s6 k% b3 K6 f8 c+ g9 Y8 L) v% I
return & tetris;# C+ U9 d- f: \- e5 T$ s
: E7 D5 v) ~8 v- t4 J8 D3 v}/ L3 `" N5 A! F/ Z7 _# C3 B* R
# I( c6 e& i/ c k' C! P% U
! U( S r! b, |" k8 W5 F) }; O6 j- I* @9 E' [, W# d
Tetris* CreateFourTetris()# Q5 X- [. a5 ^
4 W+ H3 ~% ] D# J
{
3 g# ]7 a! R6 L2 \. \6 e static FourTetris tetris;4 {9 _# d$ |5 `4 B3 ~; ~$ W
. G6 N1 S" c' [tetris.InitPos();
5 H& e6 o$ ?' X8 b& @2 ]3 _0 X0 @0 c Q return & tetris;
+ q7 Q# B& `6 `- c' `8 k7 n1 }$ E, E: t3 C
}
' l0 \, y4 p, ?1 I) l0 h, J
8 O o w$ H; I6 @5 C# T [2 p5 ]+ q! M, N- [; g! c# S, |: I
* w7 F3 l( H( O! a; \5 LTetris* CreateFiveTetris()
! O* O7 d( b& |+ E1 s4 j4 }
' k7 g5 z3 J9 ]- R8 t- F. v" [{
5 o0 B8 i. Y' t8 M! y6 y static FiveTetris tetris;
0 Y" W5 ]. @6 j: P8 O* l, n* J' I5 S" c9 F, \: H2 e7 o' R/ H
tetris.InitPos();% f; ~/ }6 t/ x$ G8 ?
return & tetris;
( \# t5 j% ^- b0 F: A
9 I8 [) D0 I7 p+ A) {1 D}3 a5 z9 w! c" |1 S
; O6 ^( t. ]( [1 c4 J/ f4 r5 s
/ ]; D5 r) E' E+ W4 \
Tetris* CreateSixTetris()# R; q8 e2 L+ c! k' ~" m- X
8 p9 G) {( n { i8 ~7 i
{# \/ V; i: b% M. F
static SixTetris tetris;
# P5 b! l, M! h5 m4 z' c* [$ j6 q9 [' }. O& Q3 W2 W
tetris.InitPos();/ U3 W9 h* W3 D5 e& @# ~
return & tetris;5 p8 g8 w$ Q. K
5 J# ~& Y0 U$ Y4 V
}; ]7 w( [( h! |* E% L& u
; [6 z. S1 p( u; d Q- t/ `# p
; k2 r$ Z U! j R) D
# ]8 f: l6 z b% _1 b! K2 J7 o
4 ]! H/ I, J) V! a. LTetris* CreateSevenTetris()4 U) k' E) w& F" r/ w
) l& G& s) ^1 v; `- ]9 V# K/ E& Y
{4 Y% o l+ g7 X6 F. B
static SevenTetris tetris;5 {1 [3 N4 I' }/ o; |% F
) ~9 X; R3 A0 ^ G# g
tetris.InitPos();- W& V0 |* i0 [" k/ _
return & tetris;1 Q1 s [" X Y! _5 w+ Y
0 ]# ^% K* d1 H- x
}
" P7 N+ J$ K; c) _- s " j1 G* ^! ?- l. e7 {
private:2 Q: j5 d- Q2 M* I2 s
& u2 P; Q0 C8 q& x. y3 n4 Y9 ]
typedef Tetris*(TetrisFactory::*PMA)();
# z3 \% K- L) z: N7 }, F
2 v2 ^( \. m' Z% Ostd::map<char,PMA> m_sel2TetrisMap;
( P7 e! O. a' B! X; w4 A
$ a1 O" M5 B" n6 |, P' \1 b
8 b' N5 u) I. }1 D};
# w2 ?+ }2 ?% A0 U6 y; K8 a8 y4 i " I& U1 [ c( v$ I9 t* j U T
有兴趣的朋友可以下下来改改玩玩。% I; G9 k# L, Z( J9 K$ }! H. p9 P
3 s f6 X- L: [3 V+ c5 l3 l1 bThat’s all!, s$ @2 q' f0 l- V- Y0 a4 Z C
* N, J4 I) U+ Y& V6 c% ]- s0 x8 a. k
Peter
' |0 L* g A, M( R# J' i/ C7 c, C. N+ a: R6 I# J3 f
[ 本帖最后由 peterhu 于 2009-5-20 09:51 编辑 ] |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?加入计匠网
×
|