1 | /*************************************************************************** |
---|---|
2 | * KBlocks, a falling blocks game for KDE * |
3 | * Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> * |
4 | * * |
5 | * This program is free software; you can redistribute it and/or modify * |
6 | * it under the terms of the GNU General Public License as published by * |
7 | * the Free Software Foundation; either version 2 of the License, or * |
8 | * (at your option) any later version. * |
9 | ***************************************************************************/ |
10 | #include "KBlocksDummyAI.h" |
11 | |
12 | #include <stdlib.h> |
13 | |
14 | KBlocksDummyAI::KBlocksDummyAI() |
15 | { |
16 | mpGame = 0; |
17 | mPauseFlag = false; |
18 | |
19 | mFieldWidth = 0; |
20 | mRotateCount = 0; |
21 | } |
22 | |
23 | KBlocksDummyAI::~KBlocksDummyAI() |
24 | { |
25 | } |
26 | |
27 | void KBlocksDummyAI::startGame(SingleGameInterface * p) |
28 | { |
29 | mpGame = p; |
30 | mPauseFlag = false; |
31 | |
32 | mFieldWidth = mpGame->getField()->getWidth(); |
33 | mRotateCount = mpGame->getPiece(0)->getRotationCount(); |
34 | } |
35 | |
36 | void KBlocksDummyAI::stopGame() |
37 | { |
38 | mpGame = 0; |
39 | } |
40 | |
41 | void KBlocksDummyAI::pauseGame(bool flag) |
42 | { |
43 | mPauseFlag = flag; |
44 | } |
45 | |
46 | void KBlocksDummyAI::think(GamePlayer_ActionList * actionList) |
47 | { |
48 | if (mPauseFlag) |
49 | { |
50 | return; |
51 | } |
52 | |
53 | int rotation = rand() % (mRotateCount + 1) - mRotateCount / 2; |
54 | int motionx = rand() % (mFieldWidth + 1) - mFieldWidth / 2; |
55 | |
56 | if (rotation > 0) |
57 | { |
58 | for(int i = 0; i < rotation; i++) |
59 | { |
60 | actionList->push_back(PlayerAction_Rotate_CW); |
61 | } |
62 | } |
63 | else |
64 | { |
65 | rotation = -rotation; |
66 | for(int i = 0; i < rotation; i++) |
67 | { |
68 | actionList->push_back(PlayerAction_Rotate_CCW); |
69 | } |
70 | } |
71 | |
72 | if (motionx > 0) |
73 | { |
74 | for(int i = 0; i < motionx; i++) |
75 | { |
76 | actionList->push_back(PlayerAction_Move_Right); |
77 | } |
78 | } |
79 | else |
80 | { |
81 | motionx = -motionx; |
82 | for(int i = 0; i < motionx; i++) |
83 | { |
84 | actionList->push_back(PlayerAction_Move_Left); |
85 | } |
86 | } |
87 | |
88 | //actionList->push_back(PlayerAction_Push_Down); |
89 | } |
90 |