Commit 294fd36a authored by Matt Williams's avatar Matt Williams
Browse files

Can no longer place two lines in the same spot

Fix completed square ownership indexing

svn path=/trunk/playground/games/ksquares/; revision=616164
parent a5c794d9
......@@ -17,7 +17,7 @@ using std::endl;
#include "gameboardscene.h"
//#include "lineitem.h"
GameBoardScene::GameBoardScene(int newWidth, int newHeight, QWidget *parent) : QGraphicsScene(parent), width(newWidth), height(newHeight), lineDrawn((2.0*newWidth*newHeight + newWidth + newHeight), false)
GameBoardScene::GameBoardScene(int newWidth, int newHeight, QWidget *parent) : QGraphicsScene(parent), width(newWidth), height(newHeight), lineDrawn((2*newWidth*newHeight + newWidth + newHeight), false)
{
//setObjectName("GameBoardScene");
......@@ -46,7 +46,7 @@ GameBoardScene::GameBoardScene(int newWidth, int newHeight, QWidget *parent) : Q
addItem(indicatorLine);
//lineDrawn.resize(2*width*height + width + height); //now done in constructor
squareOwnerTable.fill(0, (width*height));
squareOwnerTable.fill(-1, (width*height));
QGraphicsEllipseItem tempItem;
QGraphicsEllipseItemType = tempItem.type();
......@@ -101,7 +101,7 @@ bool GameBoardScene::isLineAlready(QList<QGraphicsEllipseItem*> pointPair)
return lineDrawn.at(index);
}
void GameBoardScene::addLineToIndex(QList<QGraphicsEllipseItem*> pointPair)
bool GameBoardScene::addLineToIndex(QList<QGraphicsEllipseItem*> pointPair)
{
int index = -1;
if (pointPair.size() == 2) // if it really is a pair
......@@ -144,18 +144,26 @@ void GameBoardScene::addLineToIndex(QList<QGraphicsEllipseItem*> pointPair)
index = refY * ((2*width)+1) + refX + width;
}
}
if (index == -1)
return;
if (index == -1) //not a valid line since no two unique ends
return false;
lineDrawn[index] = true;
checkForNewSquares();
if (lineDrawn[index] == false) //if there's not a line there yet, do checks
{
lineDrawn[index] = true;
checkForNewSquares();
return true;
}
else //else wait for another line to be drawn
{
return false;
}
}
void GameBoardScene::checkForNewSquares()
{
for(int i=0; i < (width*height); i++) //cycle through every box..
{
if (squareOwnerTable.at(i) == 0) //..checking it if there is no current owner
if (squareOwnerTable.at(i) == -1) //..checking it if there is no current owner
{
//indices of the lines surrounding the box; Correlates to "QVector<bool> lineDrawn"
int index1 = (i/width) * ((2*width) + 1) + (i%width);
......@@ -233,13 +241,13 @@ void GameBoardScene::mouseReleaseEvent (QGraphicsSceneMouseEvent* mouseEvent)
QList<QGraphicsEllipseItem*> connectList = getTwoNearestPoints(mouseEvent->scenePos());
if (connectList.size() == 2)
{
indicatorLine->setPen(QPen(QBrush(QColor(255,255,0,0), Qt::SolidPattern), 2.0)); //just make the pen invisible
QGraphicsLineItem* newLine = new QGraphicsLineItem(QLineF(connectList.at(0)->scenePos(), connectList.at(1)->scenePos()));
newLine->setPen(QPen(QBrush(QColor(0,0,0), Qt::SolidPattern), 2.5));
addItem(newLine);
//actually add the line to the index
addLineToIndex(connectList);
if (addLineToIndex(connectList) == true) //try to add the line to the index
{
indicatorLine->setPen(QPen(QBrush(QColor(255,255,0,0), Qt::SolidPattern), 2.0)); //just make the pen invisible
QGraphicsLineItem* newLine = new QGraphicsLineItem(QLineF(connectList.at(0)->scenePos(), connectList.at(1)->scenePos()));
newLine->setPen(QPen(QBrush(QColor(0,0,0), Qt::SolidPattern), 2.5));
addItem(newLine);
}
}
}
......
......@@ -35,7 +35,7 @@ class GameBoardScene : public QGraphicsScene
int spacing;
bool isLineAlready(QList<QGraphicsEllipseItem*> pointPair);
void addLineToIndex(QList<QGraphicsEllipseItem*> pointPair);
bool addLineToIndex(QList<QGraphicsEllipseItem*> pointPair); //returns true if line is new and was added
void checkForNewSquares();
Qt::MouseButtons buttonPress;
......
......@@ -26,7 +26,7 @@ void GameBoardView::createBoard(int height, int width)
return;
gameScene = new GameBoardScene(height, width, this);
setScene(gameScene);
emit gameStarted();
//emit gameStarted();
}
void GameBoardView::mouseMoveEvent(QMouseEvent* e)
......
......@@ -28,7 +28,7 @@ class GameBoardView : public QGraphicsView
void mouseMoveEvent(QMouseEvent* e);
GameBoardScene* gameScene;
signals:
void gameStarted();
//void gameStarted();
};
#endif // GAMEBOARDVIEW_H
......@@ -30,8 +30,9 @@ using std::endl;
KSquares::KSquares() : KMainWindow(), m_view(new GameBoardView(this))
{
kdDebug() << "4" << endl;
sGame = new KSquaresGame();
connect(m_view, SIGNAL(gameStarted()), sGame, SLOT(startGame()));
//connect(m_view, SIGNAL(gameStarted()), sGame, SLOT(startGame()));
connect(sGame, SIGNAL(playerChangedSig(int)), this, SLOT(playerChanged(int)));
setCentralWidget(m_view);
setupActions();
......@@ -84,14 +85,17 @@ void KSquares::fileNew()
Settings::writeConfig();
QVector<KSquaresPlayer> playerList;
playerList.append(KSquaresPlayer(true));
playerList.append(KSquaresPlayer(true));
playerList.append(KSquaresPlayer(dialog.playerOneName->text(), true));
playerList.append(KSquaresPlayer(dialog.playerTwoName->text(), true));
m_view->createBoard(Settings::boardWidth(), Settings::boardHeight());
//start game etc.
//sGame->createGame(dialog.spinNumOfPlayers->value(), Settings::boardWidth(), Settings::boardHeight());
sGame->createGame(playerList, Settings::boardWidth(), Settings::boardHeight());
sGame->startGame();
m_view->createBoard(Settings::boardWidth(), Settings::boardHeight());
// From here on out, the game is 'controlled' by GameBoardScene and the clicks therein
//cout << "Connecting stuff" << endl;
connect(m_view->scene(), SIGNAL(squareComplete(int)), sGame, SLOT(playerSquareComplete(int)));
connect(m_view->scene(), SIGNAL(lineDrawnSig()), sGame, SLOT(tryEndGo()));
......
......@@ -57,7 +57,7 @@ void KSquaresGame::startGame()
int KSquaresGame::nextPlayer()
{
anotherGo = false;
anotherGo = false; //just ro reset the variable
currentPlayerId >= (players.size()-1) ? currentPlayerId = 0 : currentPlayerId++;
kdDebug() << "- Moving to next player: player " << currentPlayer() << endl;
emit playerChangedSig(currentPlayer());
......
......@@ -27,7 +27,7 @@ class KSquaresGame : public QObject
int currentPlayer() const {return currentPlayerId;}
int nextPlayer();
int nextPlayer(); // moves play control to the next player, looping round when necessary
public slots:
void startGame();
......
......@@ -10,11 +10,16 @@
#ifndef KSQUARESPLAYER_H
#define KSQUARESPLAYER_H
#include <QString>
class KSquaresPlayer
{
public:
KSquaresPlayer(bool isPlayerHuman = true) {human = isPlayerHuman; playerScore = 0;} //defaults to human player
KSquaresPlayer(QString newName = "Player", bool isPlayerHuman = true) {human = isPlayerHuman; playerScore = 0; playerName = newName;} //defaults to human player
//KSquaresPlayer(QString newName, bool isPlayerHuman = true) {human = isPlayerHuman; playerScore = 0; playerName = newName;}
KSquaresPlayer(const KSquaresPlayer& ksp) {human = ksp.isHuman(); playerScore = ksp.score();} //copy constructor
void setName(QString newName) {playerName = newName;}
QString name() {return playerName;}
void setScore(int newScore) {playerScore = newScore;}
int score() const {return playerScore;}
void incScore() {playerScore++;}
......@@ -23,6 +28,7 @@ class KSquaresPlayer
private:
bool human; //is the player a human?
int playerScore;
QString playerName;
};
#endif // KSQUARESPLAYER_H
......@@ -13,6 +13,8 @@
#include <kcmdlineargs.h>
#include <klocale.h>
#include <kdebug.h>
static const char description[] =
I18N_NOOP("Draw lines to make squares");
......@@ -26,12 +28,15 @@ static KCmdLineOptions options[] =
int main(int argc, char **argv)
{
KAboutData about("ksquares", I18N_NOOP("KSquares"), version, description,
kdDebug() << "1" << endl;
KAboutData about("ksquares", I18N_NOOP("KSquares"), version, description,
KAboutData::License_GPL, "(C) 2006 Matt Williams}", 0, 0, "matt@milliams.com");
about.addAuthor( "Matt Williams", 0, "matt@milliams.com" );
KCmdLineArgs::init(argc, argv, &about);
KCmdLineArgs::addCmdLineOptions(options);
KApplication app;
about.addAuthor( "Matt Williams", 0, "matt@milliams.com" );
KCmdLineArgs::init(argc, argv, &about);
KCmdLineArgs::addCmdLineOptions(options);
kdDebug() << "2" << endl;
KApplication app;
kdDebug() << "3" << endl;
KSquares *widget = new KSquares;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment