Пример #1
0
void gameLoop() { 
    int curPlayer = connA; //socket for the current player
    /*
     * Board represents the curent state of the pegs in the board
     * Each digit corresponds to the number of pegs in that digits row
     * i.e. Row 1 has 1 peg, row 2 has 3 pegs, etc.
     * 9 is prepended to ensure the number always has 5 digits, but is not used
     */
    int board = 91357;  
   while(1) {
        //send Board to player
        char c = 'B';
        write(curPlayer, &c, 1);
        char boardChar[6];
        sprintf(boardChar, "%d", board);
        int left = strlen(boardChar);
        int put = 0;
        int num;
        while (left > 0) {
        if ((num = write(curPlayer, boardChar + put, left)) < 0) {
            perror("nim_server:write");
            exit(0);
        } else {
            left -= num;
        }
        put += num;
        }
        c = ';';
        write(curPlayer, &c, 1);
        //Read player's move
        char buffer[3];
        c = '0';
        read(curPlayer, &c, 1); //Get Type
        if (c != 'M') {
            fprintf(stderr, "nim_match:invalid message type %c\n", c);
            winGame(switchPlayer(curPlayer));
        }
        read(curPlayer, buffer, 1); //read the move TODO: validate in case of network corruption
        read(curPlayer, buffer + 1, 1);
        read(curPlayer, &c, 1);
        if (c != ';') {
            fprintf(stderr, "nim_match:invalid move\n");
            winGame(switchPlayer(curPlayer));          
        }
        board = updateBoard(board, buffer);
        if (board == 90000) {
            winGame(switchPlayer(curPlayer));
        }
        curPlayer = switchPlayer(curPlayer);    
    }
}
Пример #2
0
bool board_2048::gameEnded() {
    bool ended = true;
    if(winGame())
    {
         ui->curScore->setText("WIN");
         return ended;
    }
    if (countEmpty() > 0) {
        return false;
    }
    if (findPairDown()) {
        return false;
    }

    transpose();
    if (findPairDown()) {
        ended = false;
    }
    transpose();
    transpose();
    transpose();
    if(ended==true)
    ui->curScore->setText("END");
    return ended;
}
Пример #3
0
void SpaceShip::checkCollision(Terrain terrain) {
	if (getY() < terrain.terrainHeight) {
		Point *bottomPoints = getPositions();
        
		if (bottomPoints[3].y <= terrain.getHight(bottomPoints[3].x)) {
			loseGame();
		} else if (abs(bottomPoints[0].y - bottomPoints[1].y) < 3) {
			if (bottomPoints[0].y <= terrain.getHight(bottomPoints[0].x)
                || bottomPoints[2].y
                <= terrain.getHight(bottomPoints[2].x)) {
				if (abs(bottomPoints[1].y - terrain.getHight(bottomPoints[1].x))
                    < 2) {
					winGame();
				} else {
					loseGame();
				}
			}
		} else {
			if (bottomPoints[0].y <= terrain.getHight(bottomPoints[0].x)
                || bottomPoints[1].y <= terrain.getHight(bottomPoints[1].x)
                || bottomPoints[2].y
                <= terrain.getHight(bottomPoints[2].x)) {
				loseGame();
			}
		}
	}
}
Пример #4
0
void update() {
	switch (GameObj.currentGameState) {
		case StartGame:
			startGame();
			GameObj.currentGameState = DisplayPattern;
		break;
		case DisplayPattern:
			GameObj.inputIndex = 0; // reset index at beginning of each display so correct entire pattern again
			displayPattern();
			GameObj.currentGameState = UserInput;
		break;
		case UserInput:
			P1OUT &= ~GLED;
			GameObj.userInput[GameObj.inputIndex] =  receiveUserInput();
			GameObj.currentGameState = CheckInput;
		break;
		case CheckInput:
			GameObj.pass = checkInput();
			if (GameObj.pass && GameObj.inputIndex == (PATTERN_LENGTH-1)) { //MAKE SURE TO INCREMENT INDEX
				P1OUT |= GLED;
				GameObj.currentGameState = WinGame;
			} else if(GameObj.pass && (GameObj.inputIndex == GameObj.patternIndex)) {
				P1OUT |= GLED;
				BlinkLEDs();
				GameObj.patternIndex += 2;
				GameObj.currentGameState = DisplayPattern;
			}	else if(GameObj.pass) {
				GameObj.inputIndex++;
				GameObj.currentGameState = UserInput;
			} else {
				P1OUT &= ~GLED;
				GameObj.currentGameState = LoseGame;
			}
		break;
		case WinGame:
			winGame();
		break;
		case LoseGame:
			loseGame();
		break;
	}
}
void try_to_make_move_ai(int player)
{
    lock_gameField();

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (gameField[i][j] == EMPTY && previous_player_move != player) {
                previous_player_move = player;
                gameField[i][j] = player; // ставим в массиве нолик
                drawGameField(i, j);      // отрисовываем интерфейс заново
                unlock_gameField();
                return;
            }
        }
    }

    is_finished_game = winGame(gamePlay(gameField));

    unlock_gameField();
}
int try_to_make_move(int rowField, int colField, int player)
{    
    lock_gameField();

    if (gameField[rowField][colField] == EMPTY && previous_player_move != player) {
        previous_player_move = player;
        gameField[rowField][colField] = player; // ставим в массиве нолик
        drawGameField(rowField, colField);   // отрисовываем интерфейс заново
       
        // проверяем на выигрышную ситуацию (другой клиент проерит в своей потоке)
        is_finished_game = winGame(gamePlay(gameField));

    } else if (gameField[rowField][colField] != EMPTY){
        mi_writeMessage("This place is taken!");
    } else if (previous_player_move == player) {
        mi_writeMessage("CROSS move!");
    }

    unlock_gameField();

    return 0;
}
Пример #7
0
void GameplayScene::subtractEntity() {
    entitiesLeft--;
    if(!entitiesLeft) winGame();
}
Пример #8
0
GameWindow::GameWindow(int sizeX, int sizeY, int minesNumber) :
    QMainWindow(),
    ui(new Ui::GameWindow)
{
    ui->setupUi(this);

    QPixmap background(":/resources/images/options_background.jpg");
        QPalette qPalette;
        qPalette.setBrush(this->backgroundRole(),QBrush(background));
        this->setPalette(qPalette);

    sizeOfFieldX = sizeX;
    sizeOfFieldY = sizeY;
    minesInTheField = minesNumber;

    core = new Field(sizeOfFieldX, sizeOfFieldY, minesInTheField);
    fieldLayout = new QGridLayout;

    backButton = new QPushButton("Back to menu");
    backButton->setStyleSheet(QPushButtonStyle);
    backButton->setFixedSize(175,50);

    refreshButton = new QPushButton("Refresh game");
    refreshButton->setStyleSheet(QPushButtonStyle);
    refreshButton->setFixedSize(175,50);

    QObject::connect(backButton, SIGNAL(clicked(bool)), this, SLOT(backToMenu()));
    QObject::connect(refreshButton, SIGNAL(clicked(bool)), this, SLOT(refreshGame()));

    mainFieldLayout = new QVBoxLayout;
    panelLayout = new QVBoxLayout;
    mainLayout = new QHBoxLayout;

    vector<QMyPushButton*> tmpVect;
    for (int i = 0; i < core->getSizeX(); i++)
    {
        for (int j = 0; j < core->getSizeY(); j++)
        {
            QMyPushButton* newButton;
            tmpVect.push_back(newButton);
        }
        buttons.push_back(tmpVect);
    }

    QLabel *fieldHeight = new QLabel("Height of field:   ");
    fieldHeight->setStyleSheet(QLabelStyle);
    QLabel *fieldLength = new QLabel("Length of field:   ");
    fieldLength->setStyleSheet(QLabelStyle);
    QLabel *allCells = new QLabel("Cells in the field:");
    allCells->setStyleSheet(QLabelStyle);
    QLabel *cellsLeft = new QLabel("Cells left:           ");
    cellsLeft->setStyleSheet(QLabelStyle);
    QLabel *minesLeft = new QLabel("Mines left:         ");
    minesLeft->setStyleSheet(QLabelStyle);
    QLabel *flagsLeft = new QLabel("You have flags:  ");
    flagsLeft->setStyleSheet(QLabelStyle);


    fieldHeightNumber = new QLabel(QString::number(core->getSizeX()));
        fieldHeightNumber->setStyleSheet(QLabelStyle);
    fieldLengthNumber = new QLabel(QString::number(core->getSizeY()));
        fieldLengthNumber->setStyleSheet(QLabelStyle);
    allCellsNumber = new QLabel(QString::number(core->getSizeX() * core->getSizeY()));
        allCellsNumber->setStyleSheet(QLabelStyle);
    cellsLeftNumber = new QLabel(QString::number(core->cellsLeft));
        cellsLeftNumber->setStyleSheet(QLabelStyle);
    minesLeftNumber = new QLabel(QString::number(core->minesLeft));
        minesLeftNumber->setStyleSheet(QLabelStyle);
    flagsLeftNumber = new QLabel(QString::number(core->flagsLeft));
        flagsLeftNumber->setStyleSheet(QLabelStyle);

    QHBoxLayout *panelHorizontal1 = new QHBoxLayout;
    panelHorizontal1->addWidget(fieldHeight);
    panelHorizontal1->addWidget(fieldHeightNumber);
    QHBoxLayout *panelHorizontal2 = new QHBoxLayout;
    panelHorizontal2->addWidget(fieldLength);
    panelHorizontal2->addWidget(fieldLengthNumber);
    QHBoxLayout *panelHorizontal3 = new QHBoxLayout;
    panelHorizontal3->addWidget(allCells);
    panelHorizontal3->addWidget(allCellsNumber);
    QHBoxLayout *panelHorizontal4 = new QHBoxLayout;
    panelHorizontal4->addWidget(cellsLeft);
    panelHorizontal4->addWidget(cellsLeftNumber);
    QHBoxLayout *panelHorizontal5 = new QHBoxLayout;
    panelHorizontal5->addWidget(minesLeft);
    panelHorizontal5->addWidget(minesLeftNumber);
    QHBoxLayout *panelHorizontal6 = new QHBoxLayout;
    panelHorizontal6->addWidget(flagsLeft);
    panelHorizontal6->addWidget(flagsLeftNumber);

    //timer = new QTimer;
    //timer->start(10000);

    panelLayout = new QVBoxLayout;
    mainPanelLayout = new QVBoxLayout;
    panelBox = new QGroupBox;

    panelLayout->addSpacing(5);
    panelLayout->addLayout(panelHorizontal1);
    panelLayout->addLayout(panelHorizontal2);
    panelLayout->addLayout(panelHorizontal3);
    panelLayout->addLayout(panelHorizontal4);
    panelLayout->addLayout(panelHorizontal5);
    panelLayout->addLayout(panelHorizontal6);
    panelBox->setLayout(panelLayout);

    mainPanelLayout->addWidget(panelBox);
    //mainPanelLayout->addWidget(timer);
    mainPanelLayout->addStretch(1);
    mainPanelLayout->addWidget(refreshButton);
    mainPanelLayout->addWidget(backButton);
    mainPanelLayout->setSpacing(20);




    for (int i = 0; i < core->getSizeX(); i++)
        for (int j = 0; j < core->getSizeY(); j++)
        {
            buttons[i][j] = new QMyPushButton;
            fieldButtonSize = new QSize(32,32);
            buttons[i][j]->setStyleSheet(QFieldButtonStyle);
            buttons[i][j]->setFixedSize(*fieldButtonSize);
            //buttons[i][j]->setText("Do it!");
            fieldLayout->addWidget(buttons[i][j], i, j, 1, 1);
            buttons[i][j]->setProperty("coordinates", i * 1000 + j);
            connect(buttons[i][j], SIGNAL(pressed()), this, SLOT(clickedLeft()));
            connect(buttons[i][j], SIGNAL(rClicked()), this, SLOT(setFlag()));
            //QMyPushButton *but = new QMyPushButton;
            //connect(but, SIGNAL())

        }
    //mainFieldLayout->addSpacing(15);
    fieldLayout->setSpacing(0);
    mainFieldLayout->addLayout(fieldLayout);

    //mainLayout->addSpacing(15);
    mainLayout->addLayout(mainFieldLayout);
    mainLayout->addSpacing(25);
    mainLayout->addLayout(mainPanelLayout);
    //mainLayout->addSpacing(15);

    QWidget *centralWidget = new QWidget;
    centralWidget->setLayout(mainLayout);
    this->setCentralWidget(centralWidget);
    this->setWindowTitle("Minesweeper TOP Game");
    this->setWindowIcon(QIcon(":/resources/images/icon.png"));

    connect(this, SIGNAL(allCellsOpen()), this, SLOT(winGame()));
    connect(this, SIGNAL(allah_BABAH()), this, SLOT(loseGame()));


}
Пример #9
0
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow){
    ui->setupUi(this);

    // Initialize Bet
    ui->GuessNumBet->setNum(Config::GuessNumBet);
    ui->BlackJackBet->setNum(Config::BlackjackBet);

    // Initialize server configs
    this->socket = -1;
    this->serverIP = ui->serverIPDisplay->text();
    this->serverPort = ui->serverPortDisplay->text().toInt();

    // Initialize games
    this->guess_num_client = new GuessNumClient(this);
    this->blackjack_client = new BlackJackClient(this);
    this->guess_num_client->setModal(true);
    this->blackjack_client->setModal(true);

    // Initialize keep alive timer
    this->keepAlive = new QTimer();
    this->keepAlive->setInterval(Config::KeepAliveFrequency);

    // Initialize dialog
    this->initializeWaitDialog();
    this->initializeGuessNumDialogs();
    this->initializeBlackjackDialogs();

    *(user.mutable_name()) = "Anomynous";
    ui->name->setText(QString("Anomynous"));
    ui->BlackJackButton->setEnabled(false);
    ui->GuessNumButton->setEnabled(false);

    // Register custom type so QObject::connect can work
    qRegisterMetaType<System::Type>("System::Type");

    QObject::connect(ui->ConfigureServerButton,     SIGNAL(clicked()),                 this, SLOT(showServerConfigDialog()));
    QObject::connect(ui->TryConnectionButton,       SIGNAL(clicked()),                 this, SLOT(tryConnect()));
    QObject::connect(this,                          SIGNAL(connectionInvalid()),       this, SLOT(warnConnection()));
    QObject::connect(this->keepAlive,               SIGNAL(timeout()),                 this, SLOT(keepAlivePokeServer()));

    QObject::connect(this->guess_num_client,        SIGNAL(waitForRival()),            this->guessnum_congrat, SLOT(exec()));
    QObject::connect(this->guess_num_client,        SIGNAL(loseGame()),                this->guessnum_loser, SLOT(exec()));
    QObject::connect(this->guess_num_client,        SIGNAL(winGame()),                 this->guessnum_winner, SLOT(exec()));
    QObject::connect(this->guess_num_client,        SIGNAL(modify_player_money(int)),  this, SLOT(increaseMoney(int)));
    QObject::connect(this->guess_num_client,        SIGNAL(finished(int)),             this->guess_num_client, SLOT(cleanUp()));

    QObject::connect(this->blackjack_client,        SIGNAL(modify_player_money(int)),  this, SLOT(increaseMoney(int)));
    QObject::connect(this->blackjack_client,        SIGNAL(waitForRival()),            this->blackjack_wait, SLOT(exec()));
    QObject::connect(this->blackjack_client,        SIGNAL(showBlackjackResult(int,int)), this->blackjack_result_dialog, SLOT(showResult(int,int)));
    QObject::connect(this->blackjack_client,        SIGNAL(finished(int)),             this->blackjack_client, SLOT(cleanUp()));

    QObject::connect(this->blackjack_result_dialog, SIGNAL(finished(int)),             this->blackjack_client, SLOT(accept()));

    QObject::connect(ui->GuessNumButton,            &QPushButton::clicked,             [this](){ joinGame(System::GUESSNUM); });
    QObject::connect(ui->BlackJackButton,           &QPushButton::clicked,             [this](){ joinGame(System::JACK); });
    QObject::connect(this->guess_num_client,        SIGNAL(rival_die()),  this, SLOT(handleGuessNumPlayerDie()));
    QObject::connect(this->blackjack_client,        SIGNAL(rival_die()),  this, SLOT(handleBlackJackPlayerDie()));

    // Prompt for change name at startup.
    changeName();
}