File indexing completed on 2022-09-27 13:19:29

0001 /*
0002     SPDX-FileCopyrightText: 2006 Matthew Williams <matt@milliams.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "ksquaresgame.h"
0008 
0009 #include <QDebug>
0010 
0011 //generated
0012 #include "settings.h"
0013 
0014 KSquaresGame::KSquaresGame()
0015 {
0016     //qDebug() << "Constructing Game";
0017     gameInProgress = false;
0018 }
0019 
0020 KSquaresGame::~KSquaresGame()
0021 {
0022     //qDebug() << "Destroying game";
0023     gameInProgress = false;
0024 }
0025 
0026 void KSquaresGame::createGame(const QVector<KSquaresPlayer> &startPlayers, int startWidth, int startHeight)
0027 {
0028     resetEverything();  //reset everything
0029     //qDebug() << "Creating Game with" << startPlayers.size() << "player(s)";
0030 
0031     //BEGIN Initialisation
0032     width = startWidth;
0033     height = startHeight;
0034     for (int i = 0; i < startPlayers.size(); i++) {
0035         players.append(startPlayers[i]);
0036     }
0037     for (int i = 0; i < (2 * width * height + width + height); i++) {
0038         lineList.append(false);
0039     }
0040     for (int i = 0; i < (width * height); i++) {
0041         squareOwnerTable.append(-1);
0042     }
0043     //END Initialisation
0044 
0045     //qDebug() << "Game Starting";
0046     nextPlayer();
0047 }
0048 
0049 void KSquaresGame::switchPlayer()
0050 {
0051     anotherGo = false;
0052     currentPlayerId() >= (players.size() - 1) ? i_currentPlayerId = 0 : i_currentPlayerId++;
0053 }
0054 
0055 int KSquaresGame::nextPlayer()
0056 {
0057     anotherGo = false;  //just to reset the variable
0058     currentPlayerId() >= (players.size() - 1) ? i_currentPlayerId = 0 : i_currentPlayerId++;
0059     //qDebug()<< "- Moving to next player:" << currentPlayer()->name() << "(" << currentPlayerId() << ")";
0060     //qDebug() << "-";
0061     Q_EMIT takeTurnSig(currentPlayer());
0062 
0063     return currentPlayerId();
0064 }
0065 
0066 void KSquaresGame::playerSquareComplete(int index)
0067 {
0068     //qDebug() << "- - " << currentPlayer()->name() << "(" << currentPlayerId() << ") has completed a square";
0069     anotherGo = true;
0070 
0071     squareOwnerTable[index] = currentPlayerId();    //add square to index
0072     Q_EMIT drawSquare(index, currentPlayer()->colour());
0073     currentPlayer()->incScore();
0074 
0075     int totalPoints = 0;
0076     for (int i = 0; i < players.size(); i++) {
0077         totalPoints += players.at(i).score();
0078     }
0079     //qDebug() << "- - Square Completed";
0080     if (totalPoints >= width * height) { //if the board is full
0081         //qDebug() << "Game Over";
0082         gameInProgress = false;
0083         Q_EMIT gameOver(players);
0084     }
0085 }
0086 
0087 void KSquaresGame::tryEndGo()
0088 {
0089     //qDebug() << "- - Trying to end go";
0090     if (anotherGo) {
0091         if (gameInProgress) {
0092             //qDebug() << "- - - Having another go";
0093             //qDebug() << "-";
0094             anotherGo = false;
0095             Q_EMIT takeTurnSig(currentPlayer());
0096         }
0097     } else {
0098         //qDebug() << "- - - Go ending";
0099         if (!currentPlayer()->isHuman()) {
0100             Q_EMIT highlightMove(lastLine);
0101         }
0102         nextPlayer();
0103     }
0104 }
0105 
0106 void KSquaresGame::resetEverything()
0107 {
0108     //qDebug() << "Game Values Resetting";
0109     numOfPlayers = 0;
0110     players.resize(0);
0111     lineList.clear();
0112     squareOwnerTable.clear();
0113     width = 0;
0114     height = 0;
0115     i_currentPlayerId = -1;
0116     anotherGo = false;
0117     gameInProgress = false;
0118     lastLine = -1;
0119 }
0120 
0121 void KSquaresGame::addLineToIndex(int index)
0122 {
0123     if (lineList[index] == true) {  //if there is already a line
0124         qWarning() << "KSquaresGame::addLineToIndex():"
0125                    << "trying to add line already there!";
0126         return;
0127     }
0128     lineList[index] = true;
0129     lastLine = index;
0130 
0131     Q_EMIT drawLine(index, Settings::lineColor());
0132 
0133     if (gameInProgress) {
0134         checkForNewSquares();
0135     }
0136 }
0137 
0138 void KSquaresGame::checkForNewSquares()
0139 {
0140     for (int i = 0; i < (width * height); i++) { //cycle through every box..
0141         if (squareOwnerTable.at(i) == -1) { //..checking it if there is no current owner
0142             //indices of the lines surrounding the box; Correlates to "QVector<bool> lineList"
0143             int index1 = (i / width) * ((2 * width) + 1) + (i % width);
0144             int index2 = index1 + width;
0145             int index3 = index2 + 1;
0146             int index4 = index3 + width;
0147             //cout << index1 << "," << index2 << "," << index3 << "," << index4 << " - " << lineList.size() << endl;
0148             if (lineList.at(index1) && lineList.at(index2) && lineList.at(index3) && lineList.at(index4)) {
0149                 //qDebug() << "- - Square" << i << "completed.";
0150                 playerSquareComplete(i);
0151             }
0152         }
0153     }
0154     //Q_EMIT lineDrawnSig();
0155     tryEndGo();
0156 }
0157