File indexing completed on 2024-04-21 04:02:06

0001 /***************************************************************************
0002 *   KBlocks, a falling blocks game by KDE                                *
0003 *   SPDX-FileCopyrightText: 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com>       *
0004 *                                                                         *
0005 *   SPDX-License-Identifier: GPL-2.0-or-later
0006 ***************************************************************************/
0007 #include "KBlocksDummyAI.h"
0008 
0009 #include <QRandomGenerator>
0010 
0011 KBlocksDummyAI::KBlocksDummyAI()
0012 {
0013     mPauseFlag = false;
0014 
0015     mFieldWidth = 0;
0016     mRotateCount = 0;
0017 }
0018 
0019 KBlocksDummyAI::~KBlocksDummyAI()
0020 {
0021 }
0022 
0023 void KBlocksDummyAI::startGame(SingleGameInterface *p)
0024 {
0025     mpGame = p;
0026     mPauseFlag = false;
0027 
0028     mFieldWidth = mpGame->getField()->getWidth();
0029     mRotateCount = mpGame->getPiece(0)->getRotationCount();
0030 }
0031 
0032 void KBlocksDummyAI::stopGame()
0033 {
0034     mpGame = nullptr;
0035 }
0036 
0037 void KBlocksDummyAI::pauseGame(bool flag)
0038 {
0039     mPauseFlag = flag;
0040 }
0041 
0042 void KBlocksDummyAI::think(GamePlayer_ActionList *actionList)
0043 {
0044     if (mPauseFlag) {
0045         return;
0046     }
0047 
0048     auto random = QRandomGenerator::global();
0049     int rotation = random->bounded(mRotateCount + 1) - mRotateCount / 2;
0050     int motionx = random->bounded(mFieldWidth + 1) - mFieldWidth / 2;
0051 
0052     if (rotation > 0) {
0053         for (int i = 0; i < rotation; i++) {
0054             actionList->push_back(PlayerAction_Rotate_CW);
0055         }
0056     } else {
0057         rotation = -rotation;
0058         for (int i = 0; i < rotation; i++) {
0059             actionList->push_back(PlayerAction_Rotate_CCW);
0060         }
0061     }
0062 
0063     if (motionx > 0) {
0064         for (int i = 0; i < motionx; i++) {
0065             actionList->push_back(PlayerAction_Move_Right);
0066         }
0067     } else {
0068         motionx = -motionx;
0069         for (int i = 0; i < motionx; i++) {
0070             actionList->push_back(PlayerAction_Move_Left);
0071         }
0072     }
0073 
0074     //actionList->push_back(PlayerAction_Push_Down);
0075 }