Viergewinnt (four-in-a-row) implementation in c++ and QT
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

viergewinnt.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* VierGewinnt - A simple 4-in-a-row network game
  2. *
  3. * Copyright (c) 2008 by Sebastian Lohff, seba@seba-geek.de
  4. * http://www.seba-geek.de
  5. *
  6. * This file is part of VierGewinnt.
  7. *
  8. * VierGewinnt is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * VierGewinnt is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with VierGewinnt. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "viergewinnt.h"
  22. #include "renderarea.h"
  23. VierGewinntWidget::VierGewinntWidget(QTcpSocket *tcpsock, QWidget *parent) : QDialog(parent) {
  24. setFixedSize(640, 480);
  25. setWindowTitle(tr("Sebas Netzwerk-VierGewinnt Client")+" - "+tr("Spielfenster"));
  26. feld = 0;
  27. mysock = tcpsock;
  28. fx = 7;
  29. fy = 6;
  30. // Arrange QT Stuff
  31. status = new QLabel(tr("Spiel ist gestartet"));
  32. rarea = new RenderArea;
  33. connect(rarea, SIGNAL(addStein(int)), this, SLOT(netAdd(int)));
  34. myscore = new QLCDNumber(2);
  35. vs = new QLabel(tr("zu"));
  36. youscore = new QLCDNumber(2);
  37. exit = new QPushButton(tr("Spiel Beenden"));
  38. connect(exit, SIGNAL(clicked()), this, SLOT(closeGame()));
  39. // layout
  40. upperrow = new QHBoxLayout;
  41. upperrow->addStretch();
  42. upperrow->addWidget(status);
  43. upperrow->addStretch();
  44. underrow = new QHBoxLayout;
  45. underrow->addStretch();
  46. underrow->addWidget(myscore);
  47. underrow->addWidget(vs);
  48. underrow->addWidget(youscore);
  49. underrow->addStretch();
  50. underrow->addWidget(exit);
  51. layout = new QVBoxLayout;
  52. layout->addLayout(upperrow);
  53. layout->addWidget(rarea);
  54. layout->addLayout(underrow);
  55. setLayout(layout);
  56. waitforplayer = new PlayerWaitDialog(mysock, this);
  57. newgame = new NewGameDialog(mysock, this);
  58. }
  59. void VierGewinntWidget::init(int _x, int _y) {
  60. qDebug() << "game init(" << _x << ", " << _y << ")\n";
  61. dealloc();
  62. // reset all possible dialogues
  63. // setDisabled(false);
  64. newgame->hide();
  65. waitforplayer->hide();
  66. fx = _x;
  67. fy = _y;
  68. feld = new Stein*[fx];
  69. for(int i=0; i<fx; i++)
  70. feld[i] = new Stein[fy];
  71. setScore(0, 0);
  72. cleanFeld();
  73. rarea->setFeldInfo(fx, fy, feld);
  74. }
  75. void VierGewinntWidget::startGame() {
  76. // this->setDisabled(false);
  77. // clear all dialogues here
  78. qDebug() << "Starting Game";
  79. newgame->hide();
  80. waitforplayer->hide();
  81. cleanFeld();
  82. QTextStream sockstream(mysock);
  83. sockstream << "TURN\n";
  84. sockstream << "SHOWMYSCORE\n";
  85. }
  86. void VierGewinntWidget::add(int row, Stein st) {
  87. for(int i=fy-1; i>=0; i--) {
  88. qDebug() << "doing " << i << " for row " << row << ", adding " << st;
  89. if(feld[row][i]==n) {
  90. feld[row][i] = st;
  91. rarea->update();
  92. setTurn(!myturn);
  93. return;
  94. }
  95. }
  96. }
  97. void VierGewinntWidget::cleanFeld() {
  98. qDebug() << "Cleaning Feld";
  99. if(feld==0)
  100. return;
  101. for(int i=0; i<fx; i++) {
  102. for(int j=0; j<fy; j++) {
  103. feld[i][j] = n;
  104. }
  105. }
  106. rarea->update();
  107. while(!pendingadd.empty())
  108. pendingadd.pop();
  109. }
  110. void VierGewinntWidget::setTurn(bool _myturn) {
  111. myturn = _myturn;
  112. if(myturn) {
  113. status->setText(tr("Du bist am Zug"));
  114. } else {
  115. status->setText(tr("Dein Mitspieler ist am Zug"));
  116. }
  117. }
  118. void VierGewinntWidget::clientExited() {
  119. // pending dialog mit cancel
  120. qDebug() << "Wait for player...";
  121. newgame->hide();
  122. waitforplayer->show();
  123. }
  124. void VierGewinntWidget::ackAdd(bool valid) {
  125. if(valid)
  126. add(pendingadd.front(), x);
  127. pendingadd.pop();
  128. }
  129. void VierGewinntWidget::ackNewGame() {
  130. newgame->netPlayerAck();
  131. }
  132. void VierGewinntWidget::newGame(bool won) {
  133. if(won) {
  134. setScore(myscore->intValue()+1, youscore->intValue());
  135. } else {
  136. setScore(myscore->intValue(), youscore->intValue()+1);
  137. }
  138. // this->setDisabled(true);
  139. newgame->reset(won);
  140. newgame->show();
  141. // exec, abhfangen
  142. }
  143. void VierGewinntWidget::closeGame() {
  144. mysock->close();
  145. }
  146. void VierGewinntWidget::netAdd(int row) {
  147. // if(!myturn)
  148. // return;
  149. pendingadd.push(row);
  150. QTextStream sockstream(mysock);
  151. sockstream << "ADD " << row << "\n";
  152. }
  153. void VierGewinntWidget::setScore(int a, int b) {
  154. myscore->display(a);
  155. youscore->display(b);
  156. }
  157. void VierGewinntWidget::dealloc() {
  158. if(feld==0)
  159. return;
  160. for(int i=0; i<x; i++)
  161. delete[](feld[i]);
  162. delete[](feld);
  163. feld = 0;
  164. }
  165. VierGewinntWidget::~VierGewinntWidget() {
  166. dealloc();
  167. }