MultypoWindow::MultypoWindow(QWidget *parent) :
QWidget(parent),
- xiInited(false),
- gameStarted(false)
+ state(Naming),
+ xiInited(false)
{
/* Prepare colors */
setStyleSheet("background-color: black; color: green; font-size: 45pt");
setCursor(QCursor(Qt::BlankCursor));
words.open(stdin,QIODevice::ReadOnly);
-
- typingPlayers = 0;
}
MultypoWindow::~MultypoWindow()
{
}
-void MultypoWindow::resetPlayerText() {
- for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
- it.value()->resetText();
- }
-}
-
void MultypoWindow::nextWord() {
QByteArray tmp = words.readLine().trimmed();
- QString word = QString::fromUtf8(tmp);
- qDebug() << "New word" << word;
- if (word.isEmpty()) { // game over
+ currentWord = QString::fromUtf8(tmp);
+ qDebug() << "New word" << currentWord;
+ if (currentWord.isEmpty()) { // game over
mainLabel->setText("GAME OVER");
for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
- QString tmp = QString ("Spieler %1 hat %2 Punkte.").arg(it.value()->getName()).arg(it.value()->score);
- it.value()->setWaiting(tmp);
+ it.value()->showScore();
}
+ state = Scoring;
} else {
- resetPlayerText();
- mainLabel->setText(word);
- typingPlayers = players.size();
+ for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
+ it.value()->nextWord();
+ }
+ mainLabel->setText(currentWord);
+ state = Playing;
}
}
+bool MultypoWindow::allPlayersWaiting()
+{
+ for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
+ if (it.value()->getState() != Player::Waiting) return false;
+ }
+ return true;
+}
+
+int MultypoWindow::typingPlayers()
+{
+ int n = 0;
+ for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
+ if (it.value()->getState() == Player::Typing) ++n;
+ }
+ return n;
+}
+
void MultypoWindow::handleKeyPress(int device, QString string)
{
qDebug() << "Device" << device << "String" << string;
}
if (!players.contains(device)) {
- if (gameStarted)
+ if (state > Naming)
return;
players[device] = new Player(this);
}
- bool newChar = players[device]->handleKey(string);
-
- if (gameStarted) { // ingame
- qDebug() << "current player line" << players[device]->getCurrentLine();
- qDebug() << "current word" << mainLabel->text();
- if (newChar && players[device]->getCurrentLine() == mainLabel->text()) {
- players[device]->score += typingPlayers;
- QString readyString = QString("READY: %1 points").arg(typingPlayers);
- players[device]->setWaiting(readyString);
- qDebug() << "typingPlayers " << typingPlayers;
- typingPlayers--;
- qDebug() << "typingPlayers " << typingPlayers;
- if (typingPlayers <= 0) {
+ Player *player = players[device];
+ player->handleKey(string);
+
+ if (state == Naming) {
+ // someone's still naming (or nobody's there yet)
+ qDebug() << "checking for game started";
+ if (!players.empty() && allPlayersWaiting()) {
+ nextWord();
+ }
+ }
+ else if (state == Playing) { // all players are waiting or typing
+ qDebug() << "current player line" << player->getCurrentWord();
+ qDebug() << "current word" << currentWord;
+ if (player->getState() == Player::Typing && player->getCurrentWord() == currentWord) {
+ int points = typingPlayers()+1;
+ player->wordComplete(points);
+ QString readyString = QString("READY: %1 points").arg(points);
+ if (allPlayersWaiting()) {
nextWord();
}
}
- } else { // name entering phase
- qDebug() << "checking for game started";
- bool allHaveNames = true;
- for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
- if (! it.value()->hasName()) {
- allHaveNames = false;
- break;
- }
- }
- qDebug() << "Players empty?" << players.empty() << "All have names?" << allHaveNames;
- if (!players.empty() && allHaveNames) {
- gameStarted = true;
- nextWord();
- }
}
}
#include <QLayout>
#include <QDebug>
-static QString colorToString(QColor col)
-{
- return QString("#%1%2%3").arg(col.red(), 2, 16, QChar('0'))
- .arg(col.green(), 2, 16, QChar('0'))
- .arg(col.blue(), 2, 16, QChar('0'));
-}
-
-Player::Player(QWidget* parent) : score(0) {
+Player::Player(QWidget* parent) : score(0), state(Naming) {
theLabel = new QLabel (parent);
parent->layout()->addWidget(theLabel);
- modifyable = true;
qDebug() << "Player created";
}
-bool Player::hasName() {
- return !name.isEmpty();
-}
-
-QString Player::getName() {
- return name;
-}
-
-QString Player::getCurrentLine() {
- return currentLine;
+void Player::handleKey(QString str)
+{
+ // edit "current line"
+ if (str.length() == 1) {
+ currentWord += str;
+ }
+ else if (str == "BackSpace") {
+ currentWord.chop(1);
+ }
+ // see if this does anything useful
+ switch (state) {
+ case Naming:
+ theLabel->setText(currentWord);
+ if (str == "Return") {
+ name = currentWord;
+ state = Waiting;
+ theLabel->setText("<READY>");
+ }
+ break;
+ case Waiting:
+ break;
+ case Typing:
+ theLabel->setText(currentWord);
+ break;
+ }
+}
+
+void Player::wordComplete(int points)
+{
+ score += points;
+ state = Waiting;
+ theLabel->setText("<COMPLETE>");
}
-void Player::setWaiting(QString labeltext) {
- theLabel->setText(labeltext);
- modifyable = false;
+QString Player::getCurrentWord() {
+ Q_ASSERT(state == Typing);
+ return currentWord;
}
-void Player::resetText() {
- currentLine.clear();
- theLabel->setText(currentLine);
- modifyable = true;
+void Player::nextWord() {
+ currentWord = "";
+ theLabel->setText(currentWord);
+ state = Typing;
}
-bool Player::handleKey(QString str) {
- if (!modifyable)
- return false;
- bool newChar = false;
- if (str.length() == 1) {
- currentLine += str;
- newChar = true;
- } else if (!hasName() && str == "Return") {
- // set name
- name = currentLine;
- currentLine = "";
- } else if (str == "BackSpace") {
- currentLine.chop(1);
- }
- theLabel->setText(currentLine);
- return newChar;
+void Player::showScore() {
+ Q_ASSERT(state == Waiting);
+ theLabel->setText(QString ("Spieler %1 hat %2 Punkte.").arg(name).arg(score));
}