|
大概一年半以前闲着无聊,于是就写了一个俄罗斯方块自娱自乐,程序运行如下图1所示: $ }. s/ v, q9 k* b
图 19 n2 U6 P- f; |+ ]' o: z
完整的source code可以在附件下载。该程序使用WTL实现的,代码本身非常简单,无非是一些碰撞,变形等情况的判断调整。比较有意思的是程序使用了Factory Method用于创建方块,控制方块的变形。创建方块部分的设计类图如下所示:
6 o) d4 v" B3 X& }, m1 O8 B3 b! f. c. M8 y0 t
+ M( k4 D* T# B* CFactory的code如下:
0 Z" }" F z7 s0 e( @2 n) \ u5 g
% S' k0 U5 d4 T* L6 }/*************************************
2 V0 X" V+ V$ H! ^
7 z( x+ J3 Q l* a: X4 t1 a* author:hgf_starlovecpp@hotmail.com9 l# p* |! O4 W
9 |8 F/ c" N2 Y; Y9 X( C" d! R
* version:1.03 W, W. S. R& D' K2 `* t9 Q! p
; e4 q$ e' z) }' z0 c- I* description: just for fun' @ b1 |. O* x9 f j
( y: S+ B' h2 S
* todo: hard code exists need to fixed
! W% o* ?2 { z) Q
& r4 s [! X7 @* t**************************************/
m7 S9 n ?! Y+ V
% _5 D8 C/ u6 F8 {3 u% a; A#pragma once
* w& z+ [) K/ d# U- U#include <map>, X7 N* C' x7 E' n" R
#include "Tetris.h"" p0 X; ]( H1 z$ G, C6 @
#include "OneTetris.h"+ U* Z: w! P% q' h& P$ q/ N4 F
#include "TwoTetris.h"
; R3 f; ]# t$ K- T! G2 y2 A. D#include "ThreeTetris.h"
( K2 T" v9 p. `" ?9 l( o8 [ ?" q#include "FourTetris.h"" M6 L: B; @2 ~, ?
#include "FiveTetris.h"
) q! C4 ?: q, Z6 V#include "SixTetris.h"9 l/ I: F% K! H8 Y8 T- b
#include "SevenTetris.h"! b7 y6 I8 M1 {4 Y( L2 b G! X
8 @. U& [1 j# b7 t
class TetrisFactory% Z0 ^& p# ?; A; U e
{
1 f8 G R4 i4 b6 K4 T9 P) F( V; Vpublic:1 A! t& J6 X" ?9 ?# X8 L
) D# L' O% T# X T! L& J7 U! q$ q/ j7 S1 q4 M6 ]- @# Y
TetrisFactory(void)
R# A: v; A( Z5 I' r
% Y$ H' @+ h; K( u! R* S, _{
- h7 T0 b- j, T& z' I. y; ~) l% }9 W
# f8 \( E5 q% z7 t. A0 Mm_sel2TetrisMap[0] = &TetrisFactory::CreateOneTetris;
. o0 E: u. o; U, ?( T0 @; R& O8 x& I+ x2 L
m_sel2TetrisMap[1] = &TetrisFactory::CreateTwoTetris;& a. U( H; N( B- o/ D
2 o' c* z! `% K% f7 F( D: Tm_sel2TetrisMap[2] = &TetrisFactory::CreateThreeTetris;7 s. o7 }8 c( j+ {" [: v$ z
6 |- @) L( c, A% o3 Im_sel2TetrisMap[3] = &TetrisFactory::CreateFourTetris;
( F: P# j) z2 R( N* x; A2 e# r4 }
) Q! d: e% @4 }( P e* X( sm_sel2TetrisMap[4] = &TetrisFactory::CreateFiveTetris;% n/ l7 h/ W7 J9 m! |( j. r$ c
Q* H. Y4 ^1 P8 W% M- Vm_sel2TetrisMap[5] = &TetrisFactory::CreateSixTetris;3 i" U7 D; p5 W5 R+ {7 p, _* p# ]
]" l; }3 u$ L! F/ R
m_sel2TetrisMap[6] = &TetrisFactory::CreateSevenTetris;% ~( S, @% k4 S+ h$ E
0 q6 k( o0 o0 T( c6 f
}
! T: b5 B. g; a' a* x7 ipublic:
8 D! K% D3 P! C6 q8 h7 h5 t ; @! g2 G; k1 Z, |( p
# F5 ]5 W# x4 D G, |: F~TetrisFactory(void)
9 C! i* W! h5 f0 b, q
K' f4 T9 z6 X$ R2 ?{
! @+ M0 v/ ?4 o$ M; `) _1 t% _7 o% R) ]' x. c" D2 i
}
1 T$ }9 p: H, m1 G5 x: q9 S4 p% m5 s
Tetris* CreateTetris(char sel)
" L& J# v* n; R
2 |' d" E2 M$ f, n6 R2 `4 @{7 F, w3 v, o5 u9 p9 p
return (this->*(m_sel2TetrisMap.find(sel)->second))();) [8 s9 a5 p- Q6 b; V
" R. Z$ q( w, o4 n8 h: d8 w}
$ I1 z) z- u C- H: b1 Hprivate:
- P s* o# [. B% t" K" v7 @! T. \3 R5 E0 h& R* W# r
) {2 ^: Y, ]$ R; g) T" \
0 v7 o6 d; K2 h' }Tetris* CreateOneTetris()
0 W5 l- a+ X- X% r+ Q& X' f
, M: b5 k8 z+ h! z{/ h9 e# m. M8 i& S
static OneTetris tetris;
E5 d, O7 Z& U' s
I+ e8 Y) L: R) H" B1 c0 e0 \7 wtetris.InitPos();
. P1 F3 J5 Y' K, G& G: j1 M2 l return & tetris;
, j' L, a y" B2 D5 u
9 O8 a) f0 j" C6 R' z. R/ C}: S0 ~/ B& m7 n/ a, V2 {! A
+ e2 G& n! ?3 [" T6 v6 F/ Q8 M6 l! g) j
6 ^2 ^" c5 z3 ?
Tetris* CreateTwoTetris()
$ j; X4 ]0 u2 X0 ]' g" U8 {2 Q: `0 c: }$ H7 e* m2 t- ]5 v! \
{3 x) `, c8 z' x6 h
static TwoTetris tetris;9 Z- p* `, c& W
# V6 p& f0 D1 m9 T0 Etetris.InitPos();
( O# }7 ~; Q [# s return & tetris;1 O J: v8 b- y1 ~5 Y9 v
$ I/ s& |* s0 `3 s}
$ E- J$ S+ T# T7 H+ V2 w$ f
% H0 N* ?) D3 M/ r4 L
3 s( b' ^3 [5 h& d9 G$ {7 C
* T& L' }& Z9 B; U. wTetris* CreateThreeTetris()- B5 T8 y0 v, _9 w# Y2 c
( r' H+ Y# i5 Y1 e3 C{) e7 @, U+ S: d; K% m
static ThreeTetris tetris;
5 {9 }, q+ l% Z1 | J/ @* t n5 d2 n. c# ^9 w4 O( V* C6 Q: C
tetris.InitPos();
( W6 V# j- X$ S* |9 f: I5 | E- }4 p7 z! [
6 Q* @% Q9 a% e' Treturn & tetris;
* p5 \7 {5 R- z; P' S1 B6 w1 c1 ?# Q4 t7 y8 g5 @
}
' k% N2 J( @! P- W2 T# G7 d+ P b/ x& B' Y, v
: f# ^. |4 [- E, d9 O$ z" T& S1 {
5 V. g& S* e" E) v4 q/ aTetris* CreateFourTetris()
; ]; ^! X) `1 i% H" X9 {2 d# N. O: U2 _7 ~6 n6 _# ~
{
+ q. |, k; l6 t$ S% w1 Y% | static FourTetris tetris;2 |$ d* a4 k& U2 L' O
% v2 q, K) {* l0 Htetris.InitPos();
$ D+ [ z: R1 [2 N return & tetris;, I0 ]* F% H( w; S& a% K9 Q& |
3 p: I" {) G3 u+ ]) s2 l9 e
}
6 j7 g4 ] D6 X+ y: |. h: ^+ }, j6 H2 P
; V' e$ z6 r# k: c: ~# |- n
+ k- p+ G* G* Z: A2 d
Tetris* CreateFiveTetris()8 h9 r. J3 j* I" G
/ r" R* M: [; |
{; H1 z9 r4 z9 U6 t
static FiveTetris tetris;
7 B* }1 m& Y$ T0 L, Y4 X5 b8 w7 ~% _2 X6 c
tetris.InitPos();2 O/ n8 J2 y- d
return & tetris;: \; ^$ y0 C1 r' w5 s5 C* }' I' p5 I
4 x( a9 U1 x( I' e/ m
}
! F' D2 e. U. O3 d$ d* L
) N( ?# X% U* V6 q% b8 G
8 E2 i$ s( z, NTetris* CreateSixTetris()5 u. O! @# K6 i. n0 ^! F
8 \* V: P1 X' b& G( Q8 y8 Z& `
{ D1 ]0 ^. z* d- L) }
static SixTetris tetris;
8 g1 b$ C3 E: W6 I; v# l0 m. B% j1 l4 t- N$ M& z" v. j+ v
tetris.InitPos();
: C6 q. B& S' g: K. A( w return & tetris;
$ B# W# y* v4 q- y& d$ }7 d( D. q3 r. z! m5 S# D
}
. v# C3 L4 f+ Z' W% c4 C
. d% P/ i7 N: m( H
6 X' g7 c; l! B4 C* I. ^2 B' [: n( i- I3 V% W( g
" S M; _- o# `0 {) v3 ?. _! r
Tetris* CreateSevenTetris()" f: |! k2 f) R0 D
: s8 ^ h* L% G$ j: j* g
{
# P! K& C, O% b, {# K; w& u! K static SevenTetris tetris;
9 `- ? S3 p/ L2 a$ d4 Q
5 p' A5 ]) Z/ }% {tetris.InitPos();
. K9 B; R5 H$ a+ d1 s return & tetris;
( \- \5 ~0 G: o# T8 k: G& S& s, ]+ Y+ x+ Y8 P
}: _: | L. v5 l$ ?) ~4 G# j/ ?/ B8 b
. w! B1 e$ { ~( E
private:
6 W2 B: F; D/ U5 ~) b 7 x( e5 H; P2 V W y
typedef Tetris*(TetrisFactory::*PMA)();: I+ ^# r9 s1 ~; c9 c! N7 e" ^
3 H8 p7 |: [. I3 n' @0 L
std::map<char,PMA> m_sel2TetrisMap;" Q, Y" f/ J. M- N, ^& s$ |' T
- Q; z! @- h4 A
' @/ K1 D& t8 ^1 h5 j
};
q" s- J- M8 t$ H* p. ~# b
% S8 B2 Z% p- R/ L3 a& h+ V$ Q) g U有兴趣的朋友可以下下来改改玩玩。
& v1 P$ I2 ]8 u $ n. B; {7 I P% |) K% |
That’s all!2 a# r* \1 r" i( s
$ z7 J1 y5 N+ Y, }Peter
) i6 Z# ?1 l+ u7 `. D) {+ E3 t F8 F- H# q" i: v% }
[ 本帖最后由 peterhu 于 2009-5-20 09:51 编辑 ] |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?加入计匠网
×
|