|
大概一年半以前闲着无聊,于是就写了一个俄罗斯方块自娱自乐,程序运行如下图1所示: * v- d9 Z6 @! d7 u3 M$ R! {
图 1. m+ L0 V4 { J
完整的source code可以在附件下载。该程序使用WTL实现的,代码本身非常简单,无非是一些碰撞,变形等情况的判断调整。比较有意思的是程序使用了Factory Method用于创建方块,控制方块的变形。创建方块部分的设计类图如下所示:
) ~& ^$ P' l. X& Y5 c- H6 Y7 Q R; Y: t0 h1 X) x9 y0 A
1 Y) ?4 q- c! P
Factory的code如下:: T) Q& T5 t4 M, X# m, i
& B, f" t5 P; i1 \2 { V4 o
/*************************************3 A& \5 y, T5 g
1 D/ s. u* w% V% d. Z* author:hgf_starlovecpp@hotmail.com$ R) M) L1 g+ Z' o8 K2 c
: n- g9 V& V- J- i# P- p* version:1.0
3 V) g. |# D$ m2 J) D- m. U5 Q- q% Q# \! `
* description: just for fun
8 _( P. P5 M+ l: N. Y2 Z/ E) t* z/ l2 T, C0 b: X
* todo: hard code exists need to fixed F% }/ ~* C3 _! k" Y1 p4 r6 N
% ^2 j/ g; ? v0 L**************************************/) }$ ^; |* h2 D5 a
7 C- t( X$ G) I% C4 F' h/ \
#pragma once& _/ S0 N& D+ I+ [1 K5 i3 _& ~
#include <map>
9 [! X$ w6 f+ p8 \#include "Tetris.h"& ]1 K$ x7 D/ c" S) _
#include "OneTetris.h"
2 i9 P! k% V& h1 X#include "TwoTetris.h" c2 H9 Z, }# i5 i* C; e& v
#include "ThreeTetris.h"% |7 Q2 g! W, d+ F0 O
#include "FourTetris.h"- \9 }# _$ K( @: _' `1 s; I
#include "FiveTetris.h"
% Q' P. x+ ^/ z1 _" d$ d& n#include "SixTetris.h"
4 {# O0 E' x/ f3 T( c% m#include "SevenTetris.h"
/ E9 t) _% L8 o# k; ~ h
8 [* L8 D' _7 b6 v$ kclass TetrisFactory* l: R+ g1 }3 S7 f1 r
{4 }4 S/ [6 X" Y N" O/ ~* f! C
public:
% S; v4 w1 _( e( A# X : e* f% z1 e7 v' f9 s
. H* j+ |- n# RTetrisFactory(void)
6 d4 ?, z0 s2 H1 c( c- e+ N- v' n0 [. n7 K
{
% p% Y+ h$ r% U- d: r H7 ?1 b' I9 |/ i% L1 W4 F
m_sel2TetrisMap[0] = &TetrisFactory::CreateOneTetris;, V+ I8 I+ f0 Z8 c6 ^: R
; K& \3 ~' I, d o% im_sel2TetrisMap[1] = &TetrisFactory::CreateTwoTetris;4 [3 X6 v; n6 N. E: }* S O5 Z
, ]% y. K# m& Y& q: G; _6 ] pm_sel2TetrisMap[2] = &TetrisFactory::CreateThreeTetris;
5 X; D8 ~2 t( D* {) |6 Z& i. ]3 f' i7 [4 u9 K* [- {" C
m_sel2TetrisMap[3] = &TetrisFactory::CreateFourTetris;
- ?$ d- a D3 z/ v# F- m" z- S5 I! b3 G A% O- a# b
m_sel2TetrisMap[4] = &TetrisFactory::CreateFiveTetris;0 }( P2 h( U) h2 B3 L" V) Z$ M; m, N
: U* B9 P z O3 jm_sel2TetrisMap[5] = &TetrisFactory::CreateSixTetris; m j+ ?7 t0 |
- o/ @- p( I$ C- I; c1 Xm_sel2TetrisMap[6] = &TetrisFactory::CreateSevenTetris; Y& k$ U* W$ t
- M1 S! c& l I
}
3 P. F3 [, h$ D) [& U# }public:
# a. I- a7 L! p& b9 Q
E4 j- m3 K" {. `0 g( _2 G. ~1 \. l8 _4 A) L) v
~TetrisFactory(void), |0 P: a) w. c, H$ ~
9 I8 T' g6 E; v$ q. z7 G{
. u, A0 ]9 d) \/ o% w, k" H# b* T1 Y5 q- f/ r2 i5 c0 s
}1 p- p; ~' v% n3 v
! Z3 B) T1 I3 J* y) V' L) K4 y( RTetris* CreateTetris(char sel): a5 f' r4 b- ~! M. h
) d/ H K2 g2 H1 s6 o' h! C& d6 o$ j{/ R7 ^+ }0 u& |" i% P- o, F" s
return (this->*(m_sel2TetrisMap.find(sel)->second))();
" L9 k1 t1 e: f& M6 ~6 V( V. f- q& ]2 N0 f! Q- j- M
}+ [ ?: N8 V& D5 |3 H: ?
private:% j" ^/ {" I! F4 t6 d, ^
8 ^4 h* g" X- X T @/ I( r
: B; o* ^ V3 y$ ~9 ^8 ~: p; V$ h9 u. m/ M9 G n
Tetris* CreateOneTetris()$ ?( [: W" c& T) J; }; N) q
1 Z* U. I6 @2 B* @5 n; |
{
; M- r6 H9 ^- D& ?( k% |1 G static OneTetris tetris;8 g8 l8 b$ g3 h. t- D4 H
9 D& E0 T9 y; W: g7 ^" u
tetris.InitPos();' g8 E2 y# i) T$ o
return & tetris;- ^( C, \% D7 x6 C; T8 ~
3 {& A% k0 b+ ~( k# u
}0 Q6 x" Y+ \ Q6 \3 ?; @" O0 d/ ?
5 \# U r, ~, k# V+ G
$ W8 x, Q7 j3 k; N
' ^ A: A2 n' j; kTetris* CreateTwoTetris()
! ^; P8 l3 v% L: O) s
3 x4 T( }+ d" _& a# b# ^ U{
9 a$ {7 J5 F6 F5 Q( L, ]& j: y static TwoTetris tetris;
" b2 b0 O$ t% j2 H& R4 h
- S/ v+ e- F; I7 Htetris.InitPos();
& n0 m4 V4 X# w. f8 i return & tetris;0 o) I z) n" l1 U1 I
1 Q4 C% z) e, h6 A. S N
}
. d" s b8 Q* J3 X2 q0 x$ F+ J) n9 [4 h4 x
# N N( Z# L- k
7 Q0 L7 m, K9 }$ W" p
Tetris* CreateThreeTetris()
& ^" Q/ J, P: t! \
0 J* w4 G: F% I: N% m1 y/ y# j/ Q{3 v) R5 ?3 p+ X. x2 B9 \3 F
static ThreeTetris tetris;
: K8 x# h2 N7 T+ Q" m+ N8 L
$ z3 G5 h4 X- }% E/ z6 `, h, {% Itetris.InitPos();3 V: x% {. @# z+ Y/ m( C) Z3 }
5 d9 V7 f* H3 o! M( M. S3 d. h
/ [& A3 l6 w+ P1 K5 b4 Yreturn & tetris;( I' W" E# G2 C- n! K% c# Y
; o) C. I0 U9 h9 t}
- z! L6 \' C! B0 f1 _4 ^5 F2 e+ q+ C6 ]: \3 F- D, f
" S% o* U% L3 C) N8 ]# P: N9 M4 N& T" U
Tetris* CreateFourTetris()5 c/ [4 {. ?$ e
4 x9 A0 I8 ]" S/ h& f, r
{
' Y8 F$ N0 ?9 v0 ~7 x0 C$ n static FourTetris tetris;' p( A& R% K2 J8 s0 p: J* i
- {0 _$ y$ v9 s; P
tetris.InitPos();
/ l5 O( Z* \. j return & tetris;
2 t% f( g/ d: M" f* b+ y3 Y) i( G' Z: q' D' l' o! r9 T! ^
}- q# r& z; F/ ]. t& n
: T* I- C+ O( ]7 d
1 W+ L; I% D/ A1 w7 S% a7 B- G: P' Y- O: V. v8 n3 j& Y. b3 O
Tetris* CreateFiveTetris()
7 T( P1 I2 o; N, U# T% C. U: b- x( A
7 b, g5 b3 D5 a0 x3 P{% F# `+ G) b) U3 _" Y4 Q5 Q' n0 N
static FiveTetris tetris;/ o# Z3 U* w+ Y7 e* E& f5 O0 c$ f
6 Q+ k! Q( ~/ Z% A
tetris.InitPos();
3 s$ X" i O) @ k' [ return & tetris;
$ h2 V# x# E' g$ ?1 f9 |: ?5 g) T6 b) \# q0 c- _; Y
}
0 C! X) Z: D/ n: ^5 _8 H- ^
2 ~5 |0 b" l( w/ x7 B. K2 h* Q v" h J/ @ d |. u, W0 Z
Tetris* CreateSixTetris()
: v" ?% F% i" P# d. h
9 J r0 \2 I9 E* h" y{: A8 {! _9 R2 T! F2 a8 H" z
static SixTetris tetris;( g! L5 u n" l: s
; E- o; e% o. ~5 [' l% J! Ltetris.InitPos();
% k% s* K% g( z8 R return & tetris;
( E) f* y/ T. \3 z S! @8 T; Y- j2 o7 Y" p/ k- u" r
}7 V9 P8 P" H+ A
" c. J" W* F; X0 W: f
/ W" V3 T6 e7 b' Z1 ?1 j( H9 H0 n% m2 s G
; i4 k* M& {. o h; N5 l& T8 m
Tetris* CreateSevenTetris()
" N6 v6 e* U ]7 k7 g J% g6 g- }9 v6 z: L) j& A' p
{
" r" h8 g- f x/ t$ @; q static SevenTetris tetris;! [4 f0 E# Z3 Z& k
$ W! S5 B. Y2 _tetris.InitPos();: }5 A: U8 V" ~% B
return & tetris;
+ F, n. R0 E- O, R: f% G4 l% K0 }
8 c _4 T: o9 D+ Y* i8 O" |}
! m* p) r# j. O6 s+ d% M 1 _- P) e) l" v( @3 ]0 F
private: p/ m) r7 C3 v$ [
8 y& c5 k& u z$ j
typedef Tetris*(TetrisFactory::*PMA)();
8 J6 o* {/ W" C9 k2 j/ G+ w `- G
std::map<char,PMA> m_sel2TetrisMap;/ N. f. I4 V+ M/ w. l. ~
5 [/ W; J2 J5 n; m2 i
6 b; i5 j8 n; b& g7 j+ ?};
: l( d$ g9 H6 n: c& J! c / x7 R. A' M$ U( O. k- E+ ^" }" D, q
有兴趣的朋友可以下下来改改玩玩。
" i) L4 y' \0 f0 l. h 9 v3 h5 T! y+ O3 H* Y+ G+ [/ F
That’s all!. }- `0 S; f- @5 M) B) v( F; P
3 B: i4 V' d5 ] D3 w4 [
Peter
7 z8 P, W2 A. \2 [) o" p4 A
8 J. {- B- U6 u2 T+ l2 U- K[ 本帖最后由 peterhu 于 2009-5-20 09:51 编辑 ] |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?加入计匠网
×
|