|
|
大概一年半以前闲着无聊,于是就写了一个俄罗斯方块自娱自乐,程序运行如下图1所示: ) e. L, I. |2 \& ]6 L7 }( ?
图 1
% K4 `) X7 q1 \+ D+ f3 h' q1 y- l完整的source code可以在附件下载。该程序使用WTL实现的,代码本身非常简单,无非是一些碰撞,变形等情况的判断调整。比较有意思的是程序使用了Factory Method用于创建方块,控制方块的变形。创建方块部分的设计类图如下所示:
; i+ W; w B5 d3 i
5 W$ E2 W& t9 ?& g5 k
4 X3 \4 N) e- m: fFactory的code如下:& Z; b; [# ]' J# r# o' J: T
F! ?8 G% g! Y7 p( J7 Y2 ^# z
/*************************************
8 o7 E: t4 `( u9 n9 O { f3 J% M% `/ [$ E/ @4 X
* author:hgf_starlovecpp@hotmail.com1 A% R9 [& l: q0 k! `5 f9 t9 H
* v: ]6 V7 p- h. F. v! \8 k1 I
* version:1.0
9 r f' ?4 R! Y0 m- P* @6 j5 b# {
* description: just for fun
. q' P6 X5 N3 o+ U, I2 @5 E
W- d. y) C) Z/ |' ^* todo: hard code exists need to fixed
! Y' G$ T& N( b& v
5 ]3 t, i* \( y8 m/ S) @1 U**************************************/( D7 k" u' X* i3 l/ _* o0 E/ h0 Z# J
& i% }+ J1 Q0 j( d8 ]" X) j( i5 q9 }- M7 C
#pragma once5 {3 A' j5 S4 C5 \
#include <map>
, Y5 T4 b. G0 E1 H6 h#include "Tetris.h"
2 S" }/ {7 C: o9 u0 I* ]#include "OneTetris.h"8 v2 `" \# J; W) b7 u$ J
#include "TwoTetris.h"
/ T( K/ p: \. S/ o7 P; _, g#include "ThreeTetris.h"
0 n) w- O) \$ V#include "FourTetris.h": G# \- l. a! ~3 ?
#include "FiveTetris.h"% O% N/ s( ~0 o. Z1 |2 L
#include "SixTetris.h". S1 C7 w+ |" G( E1 N; u. d
#include "SevenTetris.h"
. t& J2 E% M( t: _1 `
. \3 @2 v% ^$ m: P/ ~class TetrisFactory, }# R% u( y+ p: e2 _/ b
{+ f! k+ p& {6 ]( K
public:
. s" z- l4 [/ k7 t - v- c! B& e: k8 I) g! R: C5 w
9 ]% r* N0 \4 XTetrisFactory(void)
^. ^5 V- m# g, l6 e. i5 I7 v
, M; ^; L4 e4 [, Z{
- ?# U% y0 S! e6 |+ q6 g2 y+ J
9 i M/ b( J# b& c8 Z" o8 `m_sel2TetrisMap[0] = &TetrisFactory::CreateOneTetris;
; H- M/ J: D( B4 S9 @1 {: s; ^. @7 x. i6 I& H5 W% g
m_sel2TetrisMap[1] = &TetrisFactory::CreateTwoTetris;
. @4 D; T( O5 ~4 h$ t) M* O3 c f7 P
5 W D" D; R5 Lm_sel2TetrisMap[2] = &TetrisFactory::CreateThreeTetris;4 W. w! @+ x& r( i/ S
8 m2 K6 W& U+ P" D3 ~& b1 vm_sel2TetrisMap[3] = &TetrisFactory::CreateFourTetris;
7 `( h$ Q, h9 B# V# a. `; c; [
% T. `' r6 f+ q K/ xm_sel2TetrisMap[4] = &TetrisFactory::CreateFiveTetris;9 T" M B$ \) I: }' B: M1 U8 y
: @5 I2 p7 M) P7 a4 l; O, Km_sel2TetrisMap[5] = &TetrisFactory::CreateSixTetris;4 ~; P) g5 `" ]
( @( {1 t( f: X$ K
m_sel2TetrisMap[6] = &TetrisFactory::CreateSevenTetris;. R* S' Y# b# j
2 V; v) L' H2 t" K: H/ j
}- o" l, m, F. D% H
public:
5 G2 m8 H! S' p
- ^7 q% P# F) l$ r/ l' H) v& A3 F& C7 D& L R" g
~TetrisFactory(void)* ?7 l S g+ x d$ w% b. R
4 `- r9 S/ I5 e
{" f: u' M0 X( p: R1 F' v
3 b6 F1 P T, ~' _}) C7 p! k7 p1 {2 x" x0 S0 X, c$ P
0 N! q: V# u6 X1 B. L
Tetris* CreateTetris(char sel)% ?9 m1 |& J. ^- `: E- S! f% E A
+ _( b" A7 ~6 _4 `{$ q* U; Z/ U/ G7 L v" {
return (this->*(m_sel2TetrisMap.find(sel)->second))();( y, C/ l2 k4 B- Y: B- e
. z$ {/ Z: i0 |1 Q2 Z8 l}6 w H9 S# _2 ?9 ?% o
private:
) L( u9 L, r# i+ R# M- z
9 M2 i8 z- w7 |- c( B+ x* N* y9 v/ S; @6 \" d- m6 p
9 [) ]3 A* Y" j
Tetris* CreateOneTetris()7 W1 E5 d X$ v0 s
4 O8 _4 F+ V) R" t{
1 {, G' z. w' k/ J7 e6 l9 M5 `% ^ static OneTetris tetris;, y+ F9 z) w+ w1 B8 k9 {3 [+ ~4 s
" ^7 i }# _" p8 s& i4 Y8 p0 F
tetris.InitPos();. \9 d# u. h5 \7 e
return & tetris;
) n4 [4 W' G/ L; z
( H' F" a, L, \& W0 P8 n}
+ S3 Y4 Z' a' m( @* G' x
! ~* A0 R; w. w) L, L I, I5 ]4 ~& n! F% c5 m! u( @7 d6 j; s
3 U: w8 B- x( E
Tetris* CreateTwoTetris()
0 J5 z: y# g( n; m
8 w5 x) z$ `2 a( w& v; o{
# O: T: N6 O7 B ` static TwoTetris tetris;
; i: F% {, l/ p1 H+ N i
& s4 I3 w/ A% a, j$ _tetris.InitPos();* A+ v, s ~- ?% i8 K U7 X0 X3 m
return & tetris;0 q, N0 d- a( t9 L- ]! g" R5 `! p
: d/ |) Y1 a& N$ ^
}
7 R0 T, ?1 R7 v: v! D0 Y: L) A+ ?
7 C# @2 u- _( {; z' W6 C' B/ Z1 w' s9 I B8 @! y: _2 \
% ~) V# Z z/ d; x D8 Z; DTetris* CreateThreeTetris()1 o1 h, c; i( P* E( T, u( j
9 l3 H1 b, [$ r# w{! U2 g# J& k; r# o3 z
static ThreeTetris tetris;. n' P5 H! D2 i- L" g
9 W7 K+ n b; e& _& N' Stetris.InitPos();
2 E; [' a2 p+ I6 a7 ]6 T- k6 ~1 L; ]* D
; U1 ]4 _9 n0 w) Y' U ~; Sreturn & tetris;
$ K4 r2 p1 V# |/ t) C; P- m. j; r w' u
}5 D' I2 |+ h* Q
6 L5 P8 X) [7 K* P
4 I2 r# d$ f3 ~8 K+ G u' Q
3 M- H a' L: g1 ]6 R* i# Q* q2 RTetris* CreateFourTetris(), M8 @& A/ l: B. ^
/ Z5 ~! ?' Z8 F
{2 Y( r- _3 C% B- u
static FourTetris tetris; Z* o( G$ n( e6 { y
- W& N2 F% j5 H! B6 Ltetris.InitPos();
, }. `7 w7 w& c return & tetris;* E/ Q3 j) @8 q Q! O
# b+ F/ {( b; @, M( Z6 x8 n
}
# f" ^: t0 u o. U% Y9 P( T/ z7 }- Q6 Y5 L
- g* w z6 S- a2 u' B9 t9 e. @; }
7 k: O$ _# M; T6 V( OTetris* CreateFiveTetris()
* A7 \4 K h2 S G- @& Q1 o! } U) f: I; w" B
{# k1 R4 m# O0 }2 M* ~ ?
static FiveTetris tetris;
; x8 s, k+ C y+ l. s* W! @+ \* ^4 s4 A& L1 d$ j
tetris.InitPos();# s: } j/ V+ g% P# c; v
return & tetris;
4 X% R' \% Y; P- c. z% P& W* F
( x. B: ^- |& x2 x}
& E* ?$ _2 O2 h # H+ |5 j1 U3 J2 U7 Z2 Q. T. k
9 j# o: [: Q9 D9 ]* D
Tetris* CreateSixTetris()
t" D% t) D9 }2 x: _9 `( }+ a8 p
{
3 O( t* s2 _3 O! [8 o. o static SixTetris tetris;
9 ^9 b* |* K, p2 P e5 g& |& e# Q0 X, D2 G
tetris.InitPos();
' X) e& y" k/ q( f; C* u( Q return & tetris;
+ q- {& B, |3 f0 I! x
d0 d5 Q. u1 W6 [}
, I0 X5 o" E- q
/ F; x, j4 }" l
! w7 E; B, Q; V6 \! s: o
* g/ k9 r3 S! c* Y9 b7 y* i7 A7 V# b# i
Tetris* CreateSevenTetris()
+ {$ Z0 P$ _+ G5 e1 T5 B5 N) I+ O8 x( D! x, l9 A* g0 v
{
`1 e+ |, ^' B1 [/ M. O h) \ static SevenTetris tetris;
1 e& m# v' L o6 q4 x# i1 s
Q+ Y8 G# W+ b4 L& dtetris.InitPos();
6 X) b9 r! t# y' o return & tetris;7 w; H" h0 _0 @ @7 {9 S2 m
7 \$ j! \, ~) F/ B8 B! h D/ F}
' x, u% t* h$ e! `. G% B P5 t/ S0 c8 B 6 w& @6 n0 N: g& `* A9 u
private:! ?- l" Q; T& @% I% n
0 ?7 z: E* X" Y+ f S typedef Tetris*(TetrisFactory::*PMA)();1 G! Y) s8 q a5 H2 `
- k) m) ]# }% z# N: Estd::map<char,PMA> m_sel2TetrisMap;2 }9 W+ x4 B; O# ^: T
4 O4 @% |; d8 c" R+ N0 x6 ?
6 ~2 Q2 w6 o3 _! S! T$ K};: s6 ^/ c* a1 ~# f
3 P$ ?! g, B- p7 F8 ~
有兴趣的朋友可以下下来改改玩玩。. M' m" n+ U# h: _" B& O6 _% I/ K
' j; M o2 y& s1 K4 n. Q* l
That’s all!
|/ W$ j5 N8 V' _$ c
# a6 ]; H; A8 J- R5 y6 CPeter
# P& ~9 U: ~1 w: S3 L6 o" A% N; C( E5 q% A( I2 A% ]3 N; q
[ 本帖最后由 peterhu 于 2009-5-20 09:51 编辑 ] |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?加入计匠网
×
|