bool Engine::playRound() { if (!mDeck) { // no cards set return false; } if (mGeneratedIds.size() < 2) { // too few players - at least two should be set return false; } if (gameEnded()) { return false; } bool defended = playCurrentRound(); lock(); mDefendFailed = false; mRoundIndex++; // if attack failed "next move" goes to defender // or to next player after the defender otherwise mCurrentPlayer = defended ? mDefender : Rules::pickNext(mGeneratedIds, mDefender, &mPlayersCards); // cleanup mAttackers.clear(); mDefender = NULL; mTableCards.clear(); unlock(); return !gameEnded(); }
void GameManager::gameEnding() { QString message; if (playersNumber == 2) { int score1 = player1.getScore(); int score2 = player2.getScore(); if (score1 > score2) { message = tr("First player won"); } else if (score1 == score2){ message = tr("Draw"); } else { message = tr("Second player won"); } } else { int score1 = player1.getScore(); int score2 = bot.getScore(); if (score1 > score2) { message = tr("Player won"); } else if (score1 == score2){ message = tr("Draw"); } else { message = tr("Computer wins"); } } emit gameEnded(message); }
/* ==================================================================== */ WdgSingle::WdgSingle(QWidget *parent, Game *g, PlayerColor myClr) : WdgGame(parent, g), botThinks(false) { static size_t num = 1; static std::shared_ptr<Ai> ai_rand = std::make_shared<AiRandom>(); setWindowTitle(QString("PvE #%1").arg(num++)); emit updateBoard(); PlayerColor botClr = PlayerColor::White == myClr ? PlayerColor::Black : PlayerColor::White; gamebot.reset(new Game_bot(g, botClr)); gamebot->setAi(ai_rand); connect(this, SIGNAL(makingMove(Turn)), this, SLOT(onMakingMove(Turn))); connect(this, SIGNAL(gameEnded(PlayerColor)), this, SLOT(onGameEnded(PlayerColor))); if (g->pulls() == botClr) { botThinks = true; QTimer::singleShot(2000, this, SLOT(onBotThinkTimeout()));
bool CTicTacToe::hasFreeFields() { if (getPlayableFields() == 0) { gameEnded(true); return false; } return true; }
bool CTicTacToe::checkThreeInARow( QObject* sender) { int player = getPlayerPlay(); setCheckBoxCheckedForPlayer(sender, player); if (hasThreeInARow(player)) { gameEnded(); return true; } setPlayableFields(getPlayableFields()-1); return false; }
void GameScreen::decrementLives() { QString t = ui->livesLbl->text(); int livesLeft = t.remove(0, t.length() - 2).toInt(); ui->livesLbl->setText("Lives Left: " + QString::number(--livesLeft)); time->stop(); decScore(500); if(livesLeft == 0) { emit(gameEnded()); } else { createPaddle(); createBall(); spacePressed = false; } }
GameScreen::GameScreen(QWidget *parent) : QMainWindow(parent), ui(new Ui::GameScreen) { ui->setupUi(this); tmp = new QMainWindow(this); paddleIcon = new QPixmap("C:/Users/TAYLOR/Documents/C++ Files/BrickBreak/paddle.png"); ballIcon = new QPixmap("C:/Users/TAYLOR/Documents/C++ Files/BrickBreak/white_ball.png"); gameOverIcon = new QPixmap("C:/Users/TAYLOR/Documents/C++ Files/BrickBreak/gameover.png"); gameOverlay = new QPixmap("C:/Users/TAYLOR/Documents/C++ Files/BrickBreak/background.jpg"); gameWonIcon = new QPixmap("C:/Users/TAYLOR/Documents/C++ Files/BrickBreak/youwin.jpg"); overlayLbl = new QLabel(this); overlayLbl->setPixmap(*gameOverlay); overlayLbl->setGeometry(0, ui->menuBar->height(), this->width() - (this->width() - ui->rightLine->pos().x()), this->height() - (this->height() - ui->bottomLine->pos().y())); connect(this, SIGNAL(eventTriggered(QString)), this, SLOT(writeEvent(QString))); connect(this, SIGNAL(gameEnded()), this, SLOT(gameOver())); diff = EASY; level = 1; newLevel(level); createActions(); createMenus(); createPaddle(); createBall(); numLives = 3; time = new Timer(this, "time"); connect(time, SIGNAL(timeout()), this, SLOT(decrementTimer())); cheatModeActive = false; spacePressed = false; score = 0; createBlocks(); eventLogFullSizePos = new QPoint(310, 10); eventLogHalfSizePos = new QPoint(310, 200); hideCheatMode(); setLivesText(numLives); setTimeText(timeLeft); setLevelText(level); setScoreText(score); }
void GameEngine::loadLevel() { deleteAllObjects(); m_remainingBricks = 0; m_levelLoader->loadLevel( m_bricks ); if (m_bricks.isEmpty()) { if (m_levelLoader->level() == 1) { // No level in the levelset kError() << "Invalid levelset " << m_levelLoader->levelset() << endl; } else { // No more levels: game won m_gameWon = true; kDebug() << m_score; addScore( GAME_WON_SCORE + m_lives.size() * LIFE_SCORE ); kDebug() << m_lives.size() << m_score; showMessage( i18n("Well done! You won the game") ); emit gameEnded(m_score, -1, m_elapsedTime); deleteMovingObjects(); } } m_balls.append(new Ball); moveBar(m_bar.center()); m_bar.reset(); updateAttachedBalls(); m_gameTimer.setInterval(REPAINT_INTERVAL); m_speed = 1.8; m_repaintInterval = 1; m_levelInfo.setLevel(m_level); if (gameIsPaused()) { resume(); } showMessage(i18n("Level %1", m_level)); QTimer::singleShot(2000, this, SLOT(hideMessage())); showFireBallMessage(); }
void board_2048::keyPressEvent(QKeyEvent * event) { QString* ArrayNumber[4][4]; for (int i = 0; i < 4; ++i) for (int j = 0; j < 4; ++j) ArrayNumber[i][j] = new QString(labels[i][j]->text()); if(gameEnded()) return; switch (event->key()) { case Qt::Key_Up: scroll_Up(); break; case Qt::Key_Down: scroll_Down(); break; case Qt::Key_Left: scroll_Left(); break; case Qt::Key_Right: scroll_Right(); break; default: QWidget::keyPressEvent(event); } bool flag = false; for (int i = 0; i < 4; ++i) for (int j = 0; j < 4; ++j) if (ArrayNumber[i][j] != labels[i][j]->text()) flag = true; if (flag) random_Tiles(); saveFile(); }
int main(int argc, char *argv[]) { uint8_t board[SIZE][SIZE]; char c; bool success; setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stdin, NULL, _IONBF, 0); if (argc == 2 && strcmp(argv[1],"test")==0) { return test(); } if (argc == 2 && strcmp(argv[1],"blackwhite")==0) { scheme = 1; } if (argc == 2 && strcmp(argv[1],"bluered")==0) { scheme = 2; } printf("\033[?25l\033[2J"); // register signal handler for when ctrl-c is pressed signal(SIGINT, signal_callback_handler); initBoard(board); while (true) { char input[20]; if (scanf("%20s", &input) < 0) err(1, "failed to get an input"); c = input[0]; switch(c) { case 97: // 'a' key case 68: // left arrow success = moveLeft(board); break; case 100: // 'd' key case 67: // right arrow success = moveRight(board); break; case 119: // 'w' key case 65: // up arrow success = moveUp(board); break; case 115: // 's' key case 66: // down arrow success = moveDown(board); break; case 'q': case 'r': break; default: { char msg[100]; snprintf(msg, sizeof(msg), "> unknown cmd: %s\n", input); printf(msg); printf(" \033[A\033[A"); // one line up success = false; } } if (success) { drawBoard(board); usleep(150000); addRandom(board); drawBoard(board); if (gameEnded(board)) { printf(" GAME OVER \n"); break; } // ref. https://en.wikipedia.org/wiki/2048_(video_game) if (score > 3932156) { printf("Got %d! You are deserved to have a flag!\n", score); FILE *fp = fopen("/proc/flag", "r"); if (!fp) exit(1); while (1) { size_t len = fread(flag, 1, sizeof(flag)-1, fp); flag[len] = '\0'; printf("%s", flag); if (len < sizeof(flag)-1) break; } printf("\n"); fclose(fp); exit(0); } } if (c=='q') { printf(" QUIT? (y/n) \n"); c=getchar(); if (c=='y') { break; } drawBoard(board); } if (c=='r') { printf(" RESTART? (y/n) \n"); c=getchar(); if (c=='y') { initBoard(board); } drawBoard(board); } } printf("\033[?25h\033[m"); return EXIT_SUCCESS; }
int main() { uint16_t board[SIZE][SIZE]; char c; bool success; //创建句柄,详细句柄知识,请百度一下或查MSDN HANDLE consolehwnd; //实例化句柄 consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(consolehwnd, DEFAULT_COLOR); system("title 2048.c"); system("mode con cols=32 lines=23"); system("cls"); memset(board, 0, sizeof(board)); addRandom(board); addRandom(board); system("cls"); drawBoard(board,consolehwnd); while (true) { c = _getch(); switch (c) { case 97: //a case 104://h case 65: //A case 72: //H success = moveLeft(board); break; case 100://d case 108://l case 68: //D case 76: //L success = moveRight(board); break; case 119://w case 107://k case 87: //W case 75: //K success = moveUp(board); break; case 115://s case 106://j case 83: //S case 74: //J success = moveDown(board); break; default: success = false; break; } if (success) { //drawBoard(board, consolehwnd); Sleep(50); addRandom(board); system("cls"); drawBoard(board, consolehwnd); if (gameEnded(board)) { printf(" GAME OVER! \n"); Sleep(2000); break; } } if (c == 'q') { printf(" QUIT? (y/n):"); while (true) { c = getchar(); if (c == 'y') { system("cls"); exit(0); } else if (c == 'n') { system("cls"); drawBoard(board, consolehwnd); break; } } } } return 0; }
void SimpleBallGamePlugin::onEndGame() {//we don't store any resources so we don't need to free them, we don't have a summary generator yet. emit gameEnded(); }
void CMainWindow::connectSignals() { CChatManager *Manager = CChatManager::instance(); CNetworkClient *Network = CNetworkClient::instance(); CBattleManager *BattleManager = CBattleManager::instance(); connect(ui->ChatTabWidget->tabBar(), SIGNAL(tabCloseRequested(int)), Manager, SLOT(closeChannel(int))); connect(ui->ChatTabWidget->tabBar(), SIGNAL(currentChanged(int)), Manager, SLOT(changeCurrentChannel(int))); connect(ui->ChatTabWidget->tabBar(), SIGNAL(tabMoved(int,int)), Manager, SLOT(moveChannel(int,int))); connect(Manager, SIGNAL(joined(CChannel*)), this, SLOT(createTab(CChannel*))); connect(Manager, SIGNAL(closeTab(int)), this, SLOT(removeTab(int))); connect(Manager, SIGNAL(currentChanged(CChannel*)), ui->ChatTabWidget->chatView(), SLOT(loadChannel(CChannel*))); connect(ui->ChannelsView, SIGNAL(doubleClicked(QModelIndex)), CChatManager::instance(), SLOT(doubleClicked(QModelIndex))); connect(ui->ChannelUserView, SIGNAL(doubleClicked(QModelIndex)), CUserManager::instance()->chatModel(), SLOT(doubleClicked(QModelIndex))); connect(ui->actionChatSend, SIGNAL(triggered()), this, SLOT(sendChat())); connect(ui->actionBattleSend, SIGNAL(triggered()), this, SLOT(sendBattle())); connect(Manager, SIGNAL(currentChanged(int)), ui->ChatTabWidget->tabBar(), SLOT(setCurrentIndex(int))); connect(ui->BattleListView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), CBattleManager::instance(), SLOT(battleSelected(QModelIndex))); connect(ui->BattleListView, SIGNAL(doubleClicked(QModelIndex)), CBattleManager::instance(), SLOT(doubleClicked(QModelIndex))); connect(ui->BattlePreviewView, SIGNAL(doubleClicked(QModelIndex)), CBattleManager::instance()->battlePreviewModel(), SLOT(doubleClicked(QModelIndex))); connect(ui->actionConnect, SIGNAL(triggered()), this, SLOT(showConnectDialog())); connect(&ConnectDialog, SIGNAL(connect(QString,int,QString,QString)), Network, SLOT(connectToServer(QString,int,QString,QString))); connect(Network, SIGNAL(disconnected()), this, SLOT(networkDisconnected())); connect(Network, SIGNAL(connected()), this, SLOT(networkConnected())); connect(Network, SIGNAL(multiplayerDisabled()), this, SLOT(disableMultiplayerGUI())); connect(Network, SIGNAL(multiplayerEnabled()), this, SLOT(enableMultiplayerGUI())); connect(ui->actionDisconnect, SIGNAL(triggered()), Network, SLOT(disconnect())); connect(CBattleManager::instance(), SIGNAL(currentMapChanged(CMap*)), ui->MapInfo, SLOT(setMap(CMap*))); connect(CBattleroomManager::instance(), SIGNAL(mapChanged(CMap*)), ui->BattleMapInfo, SLOT(setMap(CMap*))); connect(CBattleroomManager::instance(), SIGNAL(updateChat(CChannel*)), ui->BattleChatText, SLOT(loadChannel(CChannel*))); connect(CBattleroomManager::instance(), SIGNAL(error(int,QString)), this, SLOT(error(int,QString))); connect(ui->LeaveBattleButton, SIGNAL(clicked()), CBattleroomManager::instance(), SLOT(leaveBattle())); connect(ui->DownloadButton, SIGNAL(clicked()), this, SLOT(execDownload())); connect(ui->actionDeleteDownload, SIGNAL(triggered()), this, SLOT(removeDownload())); connect(ui->actionDownloadMap, SIGNAL(triggered()), BattleManager, SLOT(downloadMapForBattle())); connect(ui->actionDownloadMod, SIGNAL(triggered()), BattleManager, SLOT(downloadModForBattle())); connect(ui->DownloadView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showDownloadContextMenu(QPoint))); connect(ui->BattleListView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showBattleContextMenu(QPoint))); connect(Network, SIGNAL(error(int,QString)), this, SLOT(error(int,QString))); connect(CDownloadManager::instance(), SIGNAL(error(int,QString)), this, SLOT(error(int,QString))); connect(Manager, SIGNAL(error(int,QString)), this, SLOT(error(int,QString))); connect(CDownloadManager::instance(), SIGNAL(error(int,QString)), this, SLOT(error(int,QString))); connect(Network, SIGNAL(agreement(QString)), this, SLOT(showAgreement(QString))); connect(ui->actionReloadUnitSync, SIGNAL(triggered()), CUnitSync::instance(), SLOT(reload())); connect(CUnitSync::instance(), SIGNAL(error(int,QString)), this, SLOT(error(int,QString))); connect(CUserManager::instance(), SIGNAL(lobbyUserBattleStatusChanged(CBattleStatus*)), this, SLOT(updateBattleStatus(CBattleStatus*))); connect(CUnitSync::instance(), SIGNAL(loaded()), this, SLOT(unitsyncLoaded())); connect(CUnitSync::instance(), SIGNAL(unloaded()), this, SLOT(unitsyncUnloaded())); connect(ui->actionUpdateStatus, SIGNAL(triggered()), this, SLOT(changeBattleStatus())); connect(CBattleroomManager::instance(), SIGNAL(battleJoined(CBattle*)), this, SLOT(enableBattleroom(CBattle*))); connect(CBattleroomManager::instance(), SIGNAL(battleClosed()), this, SLOT(disableBattleroom())); connect(CBattleroomManager::instance(), SIGNAL(gameStarted()), this, SLOT(lockBattleroom())); connect(CBattleroomManager::instance(), SIGNAL(gameEnded()), this, SLOT(unlockBattleroom())); connect(CBattleroomManager::instance(), SIGNAL(battleStarted()), this, SLOT(onBattleStarted())); connect(CBattleroomManager::instance(), SIGNAL(battleEnded()), this, SLOT(onBattleEnded())); connect(ui->SelectColorButton, SIGNAL(clicked()), this, SLOT(selectColor())); connect(ui->StartBattleButton, SIGNAL(clicked()), CBattleroomManager::instance(), SLOT(startGame())); connect(&ColorDialog, SIGNAL(colorSelected(QColor)), this, SLOT(colorSelected(QColor))); }