mainwindow.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QtWidgets/QMessageBox>
  4. #include <QtWidgets/QInputDialog>
  5. #include <QSettings>
  6. #include <QNetworkInterface>
  7. #include "gamesettingsdialog.h"
  8. MainWindow::MainWindow(QWidget *parent) :
  9. QMainWindow(parent),
  10. ui(new Ui::MainWindow)
  11. {
  12. ui->setupUi(this);
  13. board = new QTicTacToeBoard(ui->scrollArea, 2);
  14. ui->scrollArea->setWidget(board);
  15. ui->actionNew_game->setIcon(QIcon::fromTheme("document-new"));
  16. ui->actionGame_settings->setIcon(QIcon::fromTheme("document-properties"));
  17. game = 0;
  18. tcpServer = 0;
  19. tcpSocket = 0;
  20. clientSocket = 0;
  21. isServer = false;
  22. isClient = false;
  23. isLocalGame = true;
  24. newGame();
  25. }
  26. void MainWindow::newGame()
  27. {
  28. if (isClient)
  29. {
  30. QMessageBox::information(this,tr("QTicTacToe"),tr("You connected to server! Only server can start game!"));
  31. return;
  32. }
  33. if (game) delete game;
  34. QSettings settings("Home inc.","TicTacToe",this);
  35. settings.beginGroup("gameSettings");
  36. boardSize = settings.value("boardSize",3).toInt();
  37. countToWin = settings.value("countToWin",3).toInt();
  38. game = new QTicTacToeGame(boardSize,countToWin,this);
  39. board->setGame(game);
  40. connect(game,SIGNAL(gameOver(int)), this, SLOT(gameOver(int)));
  41. connect(game,SIGNAL(itemPuted(int,int,int)), this, SLOT(turn(int,int,int)));
  42. if (isServer)
  43. {
  44. clientSocket->write(QString("settings:%1:%2").arg(boardSize).arg(countToWin).toUtf8());
  45. }
  46. }
  47. MainWindow::~MainWindow()
  48. {
  49. delete ui;
  50. }
  51. void MainWindow::changeEvent(QEvent *e)
  52. {
  53. QMainWindow::changeEvent(e);
  54. switch (e->type()) {
  55. case QEvent::LanguageChange:
  56. ui->retranslateUi(this);
  57. break;
  58. default:
  59. break;
  60. }
  61. }
  62. void MainWindow::gameOver(int player)
  63. {
  64. QString mess(player?tr("Game over! Player %1 win!").arg(player):tr("Game over! No win!"));
  65. QMessageBox::information(this,tr("Game over"),mess);
  66. game->clear();
  67. }
  68. void MainWindow::turn(int x, int y, int player)
  69. {
  70. QString data = QString("%1:%2:%3").arg(x).arg(y).arg(player);
  71. if (isServer)
  72. {
  73. clientSocket->write(data.toUtf8());
  74. }
  75. if (isClient)
  76. {
  77. tcpSocket->write(data.toUtf8());
  78. }
  79. this->statusBar()->showMessage(tr("Opponent turn"));
  80. }
  81. void MainWindow::readTurn()
  82. {
  83. QString data = QString::fromUtf8(isClient?tcpSocket->readAll():clientSocket->readAll());
  84. QStringList t = data.split(":");
  85. if (t.at(0) == "settings")
  86. {
  87. delete game;
  88. game = new QTicTacToeGame(t.at(1).toInt(), t.at(2).toInt(), this);
  89. board->setGame(game);
  90. connect(game,SIGNAL(gameOver(int)), this, SLOT(gameOver(int)));
  91. connect(game,SIGNAL(itemPuted(int,int,int)), this, SLOT(turn(int,int,int)));
  92. ui->actionStart_server->setEnabled(false);
  93. ui->actionConnect_to_Server->setEnabled(false);
  94. ui->actionDisconnect->setEnabled(true);
  95. this->statusBar()->showMessage(tr("Connected to server!"), 5000);
  96. } else
  97. {
  98. if (game->put(t.at(0).toInt(), t.at(1).toInt(), t.at(2).toInt()))
  99. {
  100. this->statusBar()->showMessage(tr("Your turn"));
  101. }
  102. }
  103. }
  104. void MainWindow::on_actionNew_game_triggered()
  105. {
  106. newGame();
  107. }
  108. void MainWindow::on_actionGame_settings_triggered()
  109. {
  110. GameSettingsDialog *dialog = new GameSettingsDialog(this);
  111. dialog->setBoardSize(boardSize);
  112. dialog->setCountToWin(countToWin);
  113. if (dialog->exec())
  114. {
  115. QSettings settings("Home inc.","TicTacToe",this);
  116. settings.beginGroup("gameSettings");
  117. settings.setValue("boardSize",dialog->getBoardSize());
  118. settings.setValue("countToWin", dialog->getCountToWin());
  119. newGame();
  120. }
  121. delete dialog;
  122. }
  123. void MainWindow::on_actionStart_server_triggered()
  124. {
  125. tcpServer = new QTcpServer(this);
  126. tcpServer->setMaxPendingConnections(1);
  127. connect(tcpServer,SIGNAL(newConnection()),this,SLOT(newConnection()));
  128. if (!tcpServer->listen(QHostAddress::Any, 1515)) {
  129. QMessageBox::critical(this, tr("QTicTacToe Server"), tr("Unable to start the server: %1.").arg(tcpServer->errorString()));
  130. return;
  131. }
  132. board->setBoardPlayer(1);
  133. this->statusBar()->showMessage(tr("Waiting for connection"));
  134. ui->actionStart_server->setEnabled(false);
  135. ui->actionConnect_to_Server->setEnabled(false);
  136. ui->actionDisconnect->setEnabled(true);
  137. board->setEnabled(false);
  138. isClient = false;
  139. isLocalGame = false;
  140. isServer = true;
  141. }
  142. void MainWindow::newConnection()
  143. {
  144. this->statusBar()->showMessage(tr("New player connected!"), 5000);
  145. clientSocket = tcpServer->nextPendingConnection();
  146. connect(clientSocket,SIGNAL(readyRead()),this, SLOT(readTurn()));
  147. connect(clientSocket,SIGNAL(disconnected()),clientSocket,SLOT(deleteLater()));
  148. connect(clientSocket,SIGNAL(disconnected()),this,SLOT(on_actionDisconnect_triggered()));
  149. newGame();
  150. board->setEnabled(true);
  151. }
  152. void MainWindow::on_actionConnect_to_Server_triggered()
  153. {
  154. tcpSocket = new QTcpSocket(this);
  155. tcpSocket->abort();
  156. QString address = QInputDialog::getText(this,tr("QTicTacToe"),tr("Address"));
  157. if (address.length()>0) {
  158. tcpSocket->connectToHost(address,1515);
  159. isClient = true;
  160. isLocalGame = false;
  161. isServer = false;
  162. connect(tcpSocket,SIGNAL(readyRead()),this, SLOT(readTurn()));
  163. connect(tcpSocket,SIGNAL(disconnected()),tcpSocket,SLOT(deleteLater()));
  164. connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(on_actionDisconnect_triggered()));
  165. board->setBoardPlayer(2);
  166. }
  167. }
  168. void MainWindow::on_actionDisconnect_triggered()
  169. {
  170. if (isServer)
  171. {
  172. disconnect(clientSocket,SIGNAL(disconnected()),this,SLOT(on_actionDisconnect_triggered()));
  173. clientSocket->disconnectFromHost();
  174. tcpServer->close();
  175. }
  176. if (isClient)
  177. {
  178. disconnect(tcpSocket,SIGNAL(disconnected()),this,SLOT(on_actionDisconnect_triggered()));
  179. tcpSocket->disconnectFromHost();
  180. }
  181. isClient = false;
  182. isLocalGame = true;
  183. isServer = false;
  184. ui->actionDisconnect->setEnabled(false);
  185. ui->actionConnect_to_Server->setEnabled(true);
  186. ui->actionStart_server->setEnabled(true);
  187. board->setEnabled(true);
  188. board->setBoardPlayer(0);
  189. newGame();
  190. this->statusBar()->clearMessage();
  191. this->statusBar()->showMessage(tr("Player disconnected!"), 5000);
  192. }
  193. void MainWindow::on_actionAbout_triggered()
  194. {
  195. QMessageBox::about(this, tr("About"), tr("QTicTacToe v 0.1"));
  196. }