|
大概一年半以前闲着无聊,于是就写了一个俄罗斯方块自娱自乐,程序运行如下图1所示:
& Y& h a4 A: g图 1$ q, n" i* I" {3 c( }# A# Z
完整的source code可以在附件下载。该程序使用WTL实现的,代码本身非常简单,无非是一些碰撞,变形等情况的判断调整。比较有意思的是程序使用了Factory Method用于创建方块,控制方块的变形。创建方块部分的设计类图如下所示:
" ]2 e( W6 X4 D
8 y! t. L0 R+ ]$ _/ B6 b
; v* s- P% ?9 JFactory的code如下:
" x2 Q2 A2 K3 m% M; y. r8 d" ^ 5 E/ b% R2 s9 _* U' O% \
/*************************************
5 x( v1 } n6 t0 ] I+ I
0 w3 F5 E/ `/ G! s* author:hgf_starlovecpp@hotmail.com8 J! \& o0 Q; G# [" e+ d4 |
5 c, Z% Z, z) y; @2 \% J
* version:1.0/ \# V0 W( y9 i. q
& n$ L9 D6 z3 o1 }
* description: just for fun4 `5 W- y# Q' L' R a% |
6 l( A% H$ E/ @" u( t
* todo: hard code exists need to fixed
8 V4 X" Z' }; l( R/ [' V4 D1 g" r, ~) K
**************************************/! ?8 X9 B9 @" Z3 l1 e
& k7 x T3 ~( Y! G3 u5 u5 I4 a
#pragma once: W# u) {# @# d& r
#include <map>
$ j% H% M5 A$ }0 G: n4 B#include "Tetris.h"' ~: d7 Q& `; {4 n
#include "OneTetris.h"0 ?/ Y N: W/ |7 R. d
#include "TwoTetris.h"8 e% M: q8 a- g0 n! O
#include "ThreeTetris.h"6 k% d. u N* p8 Z' d
#include "FourTetris.h"
% ?+ `: A1 b: z5 x! I4 q" B: d: p#include "FiveTetris.h"; y- g& D, t a( E7 @
#include "SixTetris.h"
0 V7 G4 C& B x# A9 F' L1 C2 v#include "SevenTetris.h"8 k: i$ D: h) f' N: X: R' h7 n
1 f0 n* u# F8 c6 k# u; C6 [
class TetrisFactory% ^ y0 j4 q# R$ v* s
{
8 W' Z3 M. e2 qpublic:
0 Q7 M8 ~( v$ ?. h( m2 ~ - v* k! H; r! \7 y. e! v `& H
K5 y1 w" [& X% d. e+ U9 H% CTetrisFactory(void)0 n! ^# s% M& R. @
! z B( q; n8 i3 s
{
4 \' ?1 m7 x( k& W5 l3 f% A
( b: x* k7 e: k0 y2 c% mm_sel2TetrisMap[0] = &TetrisFactory::CreateOneTetris;
$ g ^5 o. }3 Y
1 a* w8 I( d# k, S/ b7 Pm_sel2TetrisMap[1] = &TetrisFactory::CreateTwoTetris;
`' C% u5 g: W: x" g1 q- m I+ V: ^" w
m_sel2TetrisMap[2] = &TetrisFactory::CreateThreeTetris;
, x: z8 w, e: ~1 `& V8 H1 Z. Y: c1 G: n' V* c$ H) q# }
m_sel2TetrisMap[3] = &TetrisFactory::CreateFourTetris;3 ^$ R* B: q7 i) R2 m! M3 F0 \8 Y
/ {2 `( `2 ?4 R# I; t
m_sel2TetrisMap[4] = &TetrisFactory::CreateFiveTetris;) n9 }1 S% {$ h8 v
* D" I+ ~& S$ c& Jm_sel2TetrisMap[5] = &TetrisFactory::CreateSixTetris;
2 l& [% M0 l- c4 z0 A, M) L) Q2 J: E* b: K1 `0 h4 f
m_sel2TetrisMap[6] = &TetrisFactory::CreateSevenTetris;
# K! p1 q& ?" i
4 ]! ~0 [: A0 d% ?- Y% E6 P}
0 J; V5 K6 B5 O& |& e# }% V4 Xpublic:
; Q$ D% o( O/ _, M0 D) _% _
5 l1 s; G( m, i6 q- y% h7 W/ l: N8 }6 c$ r( N5 i
~TetrisFactory(void)
0 @( E$ K' {$ y+ S: X: H: ?, o
0 d5 a1 v ]* B0 g; b! T{
% X. K/ G3 m; N6 m/ p% H
" a# N- p5 h) Z6 `' M) c; \/ g0 o}& c2 Z* }, N9 z; {
6 i, m5 ?4 s% l* S& l# N
Tetris* CreateTetris(char sel)
% p7 n* O, r8 |0 j, N$ G0 w/ x
) Y( d! U2 G+ [6 d# F+ C3 o{
7 P( E( B9 Q% K0 p return (this->*(m_sel2TetrisMap.find(sel)->second))();- ^1 U3 l) {- ~- }/ x* N& B1 }
- P( U# ^/ _" }/ x- }* F$ n& c}
8 U2 k! l2 Y1 g+ d1 A3 X8 U9 [private:
, d( ^. P2 W0 [8 ?8 y* m
2 P6 A" \ V! A/ D `/ p) r
5 z6 u6 j7 F+ H e6 Q+ r+ a0 z& i7 {. M- j7 {; f
Tetris* CreateOneTetris()
! v5 L: z/ X J
# Y. x/ T% j0 ?0 v& |{9 p/ l* \9 M! t( y
static OneTetris tetris;, w9 Y, A/ ~$ d o8 m4 T; B
5 _2 r! a& b4 D% h6 b
tetris.InitPos();$ N* B. Z' F a0 K
return & tetris;
/ n. ~" ?5 t, A2 i% ^5 u1 h# w. z' ?5 f. F" u& a8 |7 r& v" H
}8 g& I/ q9 T5 \( h, k0 Q' Q
6 D9 N" a* \; Z) B! y% W5 x; e2 _2 o! j$ `, z
$ z1 I& X2 l5 A8 P
Tetris* CreateTwoTetris()9 R2 i+ [* Z; @, n' D2 d
* X6 S% t* Z; L7 y/ h
{
2 x3 I. ^: g, A' G) A9 o static TwoTetris tetris; W& K, f( O! u( x
% |% } o1 R- ~tetris.InitPos();
/ `8 m5 h% {$ |4 p ` return & tetris;9 a3 O7 X- O+ I
8 p6 x' p; s' G$ V
}
$ U5 @' V: G0 \
% }: M& g3 D3 w3 \4 u9 d# z4 j. b' y1 U1 M' @
^1 s( Y1 r) v- |6 X& @Tetris* CreateThreeTetris()
! _7 J4 d% T/ i0 a7 o$ a* r8 B/ @, x/ `- A, j3 e* J; `
{
4 O/ P. ?& @% s. [0 |1 e static ThreeTetris tetris;7 y7 t5 F* X0 Q
, a0 A! h+ ?2 i+ L' htetris.InitPos();
, z' K; v- @( e, c, H( F8 i9 J) x# u" K/ n) k+ [$ c
3 a( g, ~* K I& `- Breturn & tetris;
* L9 o% e0 X' {6 b
4 ]& T- c' e) p9 J}5 q1 R# p) m* Z0 b1 y
: }+ _! ?/ z- x$ ^$ _6 z1 v
& C' A: Y: Y: T9 I4 D P6 g2 j9 S) Y6 r% O3 e$ m' ~$ _1 ~0 z
Tetris* CreateFourTetris()
5 Q5 m' u9 e; a: Z! e* I
$ I$ }1 u3 N( K# n) W' Q{
- }, [* V' m6 s; z) R+ h static FourTetris tetris;& m! ^ L" m" i8 v6 N" ^
/ o& [5 R( Q" o; L2 c
tetris.InitPos();6 h! ]- G5 i6 f5 k; |
return & tetris;/ X7 a% ^, i! m
/ Z& z& d+ U6 {& N}7 z8 |6 X! F5 b
; T) _6 p; @9 ]# f V! a: `
/ O+ \$ F, o& l: S3 t4 }
0 |' {" D- Q* F9 M8 kTetris* CreateFiveTetris()
' F% _8 |8 e# x: J% c5 d! s U
1 k" v7 Y. c$ }{* `( n( Z- u9 ]
static FiveTetris tetris;# {/ ]# P# O9 B
: }/ m0 G( `9 R: ^9 \# P+ o
tetris.InitPos();
! \7 l S3 V: @ return & tetris;' ~1 o+ i0 o3 w5 N, O8 f* J0 V
3 N+ C6 A( a" i. S5 z2 j
}4 |# L- P/ b9 \) H3 I. b/ [
% ?" D( ^ i" q; v! |
% y- W, a( w @* C6 u6 `Tetris* CreateSixTetris()
; p, |" s( r1 N* u" @& y4 t2 Q9 M
{
3 w' p) l3 Z$ O- B7 f' { static SixTetris tetris;
0 v; Y( @5 k. |0 H8 v r
+ U# }* T0 T+ h$ U* ^" Stetris.InitPos();
" @/ ~" n, E& D, C; \& `& y* R return & tetris;
" D) n E( d( F& c% N5 K; w* ?; S$ q( C+ v0 E" Y5 E
}, P0 O6 k% j0 O7 T7 S
2 Q$ ~9 h4 D D& ^% W
) i# I. r/ E: R
4 v4 { `3 h- ^7 ?5 s
5 o8 {/ B, a T. STetris* CreateSevenTetris()
: e4 @6 _: I6 [& g9 B0 E
) v. a6 @- H# ?# K# n8 [" e{* C* U E' }+ I6 m( ~3 [/ j0 v
static SevenTetris tetris;
! q- w' T' t( H& o+ j+ X1 E0 b: [$ G
tetris.InitPos();% ^, J" t/ [* v3 X* o: M- ]1 D
return & tetris;/ |5 u1 C3 f9 D$ M$ ?; m
5 q5 {" A: I) U$ W ]}( T* Z% W" k T6 F* W1 {/ w1 p
: C6 X& N6 U# I
private:
! m% p: h$ J( W& b- f3 ~1 U* V' S- _ . \/ P! g( t$ M" Z7 q( n
typedef Tetris*(TetrisFactory::*PMA)();
& o& C/ T1 y+ t; {* A0 x9 e) d* i! L' E& {
std::map<char,PMA> m_sel2TetrisMap;1 b5 |; _0 w4 i+ H1 {' k
3 R; E& o; H r( I0 p) W) j+ v: u+ D5 }$ b W( o% C) a
};; C0 i# f6 p: o* m$ \
$ k! p3 i: ^$ J+ h$ ~
有兴趣的朋友可以下下来改改玩玩。- z+ Z8 E) W1 ~+ B' X1 L8 S4 r
3 A5 O& @6 ^0 X8 v3 [& N
That’s all!9 u1 l2 g5 ^" t& {* v8 V9 J* H: o( g
; K9 N: m. e; W' x9 s, e
Peter
+ z0 ~7 x/ T+ F6 x, q3 ^4 ?* y6 O6 i8 w# T& q. C' }
[ 本帖最后由 peterhu 于 2009-5-20 09:51 编辑 ] |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?加入计匠网
×
|