選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

196 行
4.4 KiB

/* VierGewinnt - A simple 4-in-a-row network game
*
* Copyright (c) 2008 by Sebastian Lohff, seba@seba-geek.de
* http://www.seba-geek.de
*
* This file is part of VierGewinnt.
*
* VierGewinnt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* VierGewinnt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VierGewinnt. If not, see <http://www.gnu.org/licenses/>.
*/
#include "viergewinnt.h"
#include "renderarea.h"
VierGewinntWidget::VierGewinntWidget(QTcpSocket *tcpsock, QWidget *parent) : QDialog(parent) {
setFixedSize(640, 480);
setWindowTitle(tr("Sebas Netzwerk-VierGewinnt Client")+" - "+tr("Spielfenster"));
feld = 0;
mysock = tcpsock;
fx = 7;
fy = 6;
// Arrange QT Stuff
status = new QLabel(tr("Spiel ist gestartet"));
rarea = new RenderArea;
connect(rarea, SIGNAL(addStein(int)), this, SLOT(netAdd(int)));
myscore = new QLCDNumber(2);
vs = new QLabel(tr("zu"));
youscore = new QLCDNumber(2);
exit = new QPushButton(tr("Spiel Beenden"));
connect(exit, SIGNAL(clicked()), this, SLOT(closeGame()));
// layout
upperrow = new QHBoxLayout;
upperrow->addStretch();
upperrow->addWidget(status);
upperrow->addStretch();
underrow = new QHBoxLayout;
underrow->addStretch();
underrow->addWidget(myscore);
underrow->addWidget(vs);
underrow->addWidget(youscore);
underrow->addStretch();
underrow->addWidget(exit);
layout = new QVBoxLayout;
layout->addLayout(upperrow);
layout->addWidget(rarea);
layout->addLayout(underrow);
setLayout(layout);
waitforplayer = new PlayerWaitDialog(mysock, this);
newgame = new NewGameDialog(mysock, this);
}
void VierGewinntWidget::init(int _x, int _y) {
qDebug() << "game init(" << _x << ", " << _y << ")\n";
dealloc();
// reset all possible dialogues
// setDisabled(false);
newgame->hide();
waitforplayer->hide();
fx = _x;
fy = _y;
feld = new Stein*[fx];
for(int i=0; i<fx; i++)
feld[i] = new Stein[fy];
setScore(0, 0);
cleanFeld();
rarea->setFeldInfo(fx, fy, feld);
}
void VierGewinntWidget::startGame() {
// this->setDisabled(false);
// clear all dialogues here
qDebug() << "Starting Game";
newgame->hide();
waitforplayer->hide();
cleanFeld();
QTextStream sockstream(mysock);
sockstream << "TURN\n";
sockstream << "SHOWMYSCORE\n";
}
void VierGewinntWidget::add(int row, Stein st) {
for(int i=fy-1; i>=0; i--) {
qDebug() << "doing " << i << " for row " << row << ", adding " << st;
if(feld[row][i]==n) {
feld[row][i] = st;
rarea->update();
setTurn(!myturn);
return;
}
}
}
void VierGewinntWidget::cleanFeld() {
qDebug() << "Cleaning Feld";
if(feld==0)
return;
for(int i=0; i<fx; i++) {
for(int j=0; j<fy; j++) {
feld[i][j] = n;
}
}
rarea->update();
while(!pendingadd.empty())
pendingadd.pop();
}
void VierGewinntWidget::setTurn(bool _myturn) {
myturn = _myturn;
if(myturn) {
status->setText(tr("Du bist am Zug"));
} else {
status->setText(tr("Dein Mitspieler ist am Zug"));
}
}
void VierGewinntWidget::clientExited() {
// pending dialog mit cancel
qDebug() << "Wait for player...";
newgame->hide();
waitforplayer->show();
}
void VierGewinntWidget::ackAdd(bool valid) {
if(valid)
add(pendingadd.front(), x);
pendingadd.pop();
}
void VierGewinntWidget::ackNewGame() {
newgame->netPlayerAck();
}
void VierGewinntWidget::newGame(bool won) {
if(won) {
setScore(myscore->intValue()+1, youscore->intValue());
} else {
setScore(myscore->intValue(), youscore->intValue()+1);
}
// this->setDisabled(true);
newgame->reset(won);
newgame->show();
// exec, abhfangen
}
void VierGewinntWidget::closeGame() {
mysock->close();
}
void VierGewinntWidget::netAdd(int row) {
// if(!myturn)
// return;
pendingadd.push(row);
QTextStream sockstream(mysock);
sockstream << "ADD " << row << "\n";
}
void VierGewinntWidget::setScore(int a, int b) {
myscore->display(a);
youscore->display(b);
}
void VierGewinntWidget::dealloc() {
if(feld==0)
return;
for(int i=0; i<x; i++)
delete[](feld[i]);
delete[](feld);
feld = 0;
}
VierGewinntWidget::~VierGewinntWidget() {
dealloc();
}