ksquares
ksquaresgame.cpp
Go to the documentation of this file.00001 /*************************************************************************** 00002 * Copyright (C) 2006 by Matthew Williams <matt@milliams.com> * 00003 * * 00004 * This program is free software; you can redistribute it and/or modify * 00005 * it under the terms of the GNU General Public License as published by * 00006 * the Free Software Foundation; either version 2 of the License, or * 00007 * (at your option) any later version. * 00008 ***************************************************************************/ 00009 00010 #include "ksquaresgame.h" 00011 00012 #include <kdebug.h> 00013 00014 //generated 00015 #include "settings.h" 00016 00017 KSquaresGame::KSquaresGame() 00018 { 00019 kDebug() << "Constructing Game"; 00020 gameInProgress = false; 00021 } 00022 00023 KSquaresGame::~KSquaresGame() 00024 { 00025 kDebug() << "Destroying game"; 00026 gameInProgress = false; 00027 } 00028 00029 void KSquaresGame::createGame(const QVector<KSquaresPlayer> &startPlayers, int startWidth, int startHeight) 00030 { 00031 resetEverything(); //reset everything 00032 kDebug() << "Creating Game with" << startPlayers.size() << "player(s)"; 00033 00034 //BEGIN Initialisation 00035 width = startWidth; 00036 height = startHeight; 00037 for(int i=0; i < startPlayers.size(); i++) 00038 { 00039 players.append(startPlayers[i]); 00040 } 00041 for(int i = 0; i < (2*width*height + width + height); i++) 00042 { 00043 lineList.append(false); 00044 } 00045 for(int i = 0; i < (width*height); i++) 00046 { 00047 squareOwnerTable.append(-1); 00048 } 00049 //END Initialisation 00050 00051 kDebug() << "Game Starting"; 00052 nextPlayer(); 00053 } 00054 00055 int KSquaresGame::nextPlayer() 00056 { 00057 anotherGo = false; //just to reset the variable 00058 currentPlayerId() >= (players.size()-1) ? i_currentPlayerId = 0 : i_currentPlayerId++; 00059 kDebug()<< "- Moving to next player:" << currentPlayer()->name() << "(" << currentPlayerId() << ")"; 00060 kDebug() << "-"; 00061 emit takeTurnSig(currentPlayer()); 00062 00063 return currentPlayerId(); 00064 } 00065 00066 void KSquaresGame::playerSquareComplete(int index) 00067 { 00068 kDebug() << "- - " << currentPlayer()->name() << "(" << currentPlayerId() << ") has completed a square"; 00069 anotherGo = true; 00070 00071 squareOwnerTable[index] = currentPlayerId(); //add square to index 00072 emit drawSquare(index, currentPlayer()->colour()); 00073 currentPlayer()->incScore(); 00074 00075 int totalPoints=0; 00076 for (int i=0; i < players.size(); i++) 00077 { 00078 totalPoints += players.at(i).score(); 00079 } 00080 kDebug() << "- - Square Completed"; 00081 if (totalPoints >= width*height) //if the board is full 00082 { 00083 kDebug() << "Game Over"; 00084 gameInProgress = false; 00085 emit gameOver(players); 00086 } 00087 } 00088 00089 void KSquaresGame::tryEndGo() 00090 { 00091 kDebug() << "- - Trying to end go"; 00092 if (anotherGo) 00093 { 00094 if(gameInProgress) 00095 { 00096 kDebug() << "- - - Having another go"; 00097 kDebug() << "-"; 00098 anotherGo = false; 00099 emit takeTurnSig(currentPlayer()); 00100 } 00101 } 00102 else 00103 { 00104 kDebug() << "- - - Go ending"; 00105 nextPlayer(); 00106 } 00107 } 00108 00109 void KSquaresGame::resetEverything() 00110 { 00111 kDebug() << "Game Values Resetting"; 00112 numOfPlayers = 0; 00113 players.resize(0); 00114 lineList.clear(); 00115 squareOwnerTable.clear(); 00116 width = 0; 00117 height = 0; 00118 i_currentPlayerId = -1; 00119 anotherGo = false; 00120 gameInProgress = false; 00121 } 00122 00123 void KSquaresGame::addLineToIndex(int index) 00124 { 00125 if (lineList[index] == true) //if there is already a line 00126 { 00127 kWarning() << "KSquaresGame::addLineToIndex():" 00128 << "trying to add line already there!"; 00129 return; 00130 } 00131 lineList[index] = true; 00132 00133 emit drawLine(index, Settings::lineColor()); 00134 00135 if (gameInProgress) 00136 checkForNewSquares(); 00137 } 00138 00139 void KSquaresGame::checkForNewSquares() 00140 { 00141 for(int i=0; i < (width*height); i++) //cycle through every box.. 00142 { 00143 if (squareOwnerTable.at(i) == -1) //..checking it if there is no current owner 00144 { 00145 //indices of the lines surrounding the box; Correlates to "QVector<bool> lineList" 00146 int index1 = (i/width) * ((2*width) + 1) + (i%width); 00147 int index2 = index1 + width; 00148 int index3 = index2 + 1; 00149 int index4 = index3 + width; 00150 //cout << index1 << "," << index2 << "," << index3 << "," << index4 << " - " << lineList.size() << endl; 00151 if (lineList.at(index1) && lineList.at(index2) && lineList.at(index3) && lineList.at(index4)) 00152 { 00153 kDebug() << "- - Square" << i << "completed."; 00154 playerSquareComplete(i); 00155 } 00156 } 00157 } 00158 //emit lineDrawnSig(); 00159 tryEndGo(); 00160 } 00161 00162 #include "ksquaresgame.moc"
KDE 4.0 API Reference