Example #1
0
void Pong::slotDataAvailable()
{
    QByteArray array = mPort->readAll();

    switch(array[0]) {
        case InitGame:
        {
            mSeed = array.right(array.size() - 1).toUInt();

            qsrand(mSeed);

            mThisPIsLeftP = false;
            slotNewGame(false);
            break;
        }
        case StartGame:
        {
            slotStartGame(false);
            break;
        }
        case NewGame:
        {
            slotNewGame(false);
            break;
        }
        case StopGame:
        {
            slotStopGame(false);
            break;
        }
        case MoveSecondPlayerUp:
        {
            if (mThisPIsLeftP) {
                mRightP->r->moveBy(0, -mPaddleInc);
            } else {
                mLeftP->r->moveBy(0, -mPaddleInc);
            }
            checkIfPlayersGoOutOfMap();
            break;
        }
        case MoveSecondPlayerDown:
        {
            if (mThisPIsLeftP) {
                mRightP->r->moveBy(0, mPaddleInc);
            } else {
                mLeftP->r->moveBy(0, mPaddleInc);
            }
            checkIfPlayersGoOutOfMap();
            break;
        }
    }
}
// Constructor
Battleship::Battleship(QWidget *parent) : QMainWindow(parent)
{
	QApplication::setStyle("plastique");
	setupUi(this);
	gridLayoutHuman->sizePolicy().setHeightForWidth(true);
	gridLayoutEnemy->sizePolicy().setHeightForWidth(true);
	labelStatus = new QLabel(QString::fromUtf8("Press Ön to start the Game"), statusbar);
	statusbar->setSizeGripEnabled(true);
	statusbar->addWidget(labelStatus, 3);
	imageDialog = new ImageDialog(this);
	alignment = h;	// set default alignment
	enemyPlayer = new AI(enemy, this);		// Create players and their fields
	setupField(enemyPlayer);
	humanPlayer = new Human(human, this);
	setupField(humanPlayer);

	round = 0;		// for first round

	gridLayoutHuman->installEventFilter(this);	// EventFilter for right mouse button

	gridLayoutEnemy->setCursor(QCursor(Qt::ForbiddenCursor));
	gridLayoutHuman->setCursor(QCursor(Qt::ForbiddenCursor));


	// Buttons:
	connect(pushButtonEwo, SIGNAL(toggled(bool)), this, SLOT(slotSetEwoMode(bool)));
	connect(pushButtonStartGame, SIGNAL(clicked()), this, SLOT(slotStartGame()));

	// Place fleet & ships
	connect(this, SIGNAL(sigHumanPlaceShip(int, int, int)), humanPlayer, SLOT(slotPlaceShip(int, int, int)));
	connect(this, SIGNAL(sigPlaceFleet()), humanPlayer, SLOT(slotPlaceFleet()));
	connect(this, SIGNAL(sigPlaceFleet()), enemyPlayer, SLOT(slotPlaceFleet()));
	connect(humanPlayer, SIGNAL(sigFleetComplete()), this, SLOT(slotPlayerReady()));
	connect(enemyPlayer, SIGNAL(sigFleetComplete()), this, SLOT(slotPlayerReady()));

	// players get shot
	connect(this, SIGNAL(sigShotAtEnemy(int, int)), enemyPlayer, SLOT(slotShotAt(int, int)));
	connect(enemyPlayer, SIGNAL(sigShotAtHuman(int, int)), humanPlayer, SLOT(slotShotAt(int, int)));

	// toggle player turn
	connect(enemyPlayer, SIGNAL(sigDone(PlayerT)), this, SLOT(slotPlayerTurn(PlayerT)));
	connect(humanPlayer, SIGNAL(sigDone(PlayerT)), this, SLOT(slotPlayerTurn(PlayerT)));

	// AI shall shoot
	connect(this, SIGNAL(sigRequestShotFromEnemy()), enemyPlayer, SLOT(slotFire()));
	connect(humanPlayer, SIGNAL(sigEnemyTryAgain()), this, SLOT(slotRequestShotNow()));

	//Feedback of shots for AI
	connect(humanPlayer, SIGNAL(sigFireFeedback(int, int, ConditionT)), enemyPlayer, SLOT(slotFireFeedback(int, int, ConditionT)));

	// Game Over
	connect(enemyPlayer, SIGNAL(sigGameOver(PlayerT)), this, SLOT(slotGameOver(PlayerT)));
	connect(humanPlayer, SIGNAL(sigGameOver(PlayerT)), this, SLOT(slotGameOver(PlayerT)));

	// console output
	connect(humanPlayer, SIGNAL(sigPrint(QString)), this, SLOT(slotPrintToConsole(QString)));
	connect(enemyPlayer, SIGNAL(sigPrint(QString)), this, SLOT(slotPrintToConsole(QString)));

	//sound output
	connect(humanPlayer, SIGNAL(sigPlaySound(QString)), this, SLOT(slotPlaySound(QString)));
	connect(enemyPlayer, SIGNAL(sigPlaySound(QString)), this, SLOT(slotPlaySound(QString)));
	connect(humanPlayer, SIGNAL(sigPlayDelayedSound(QString, int)), this, SLOT(slotPlayDelayedSound(QString, int)));
	connect(enemyPlayer, SIGNAL(sigPlayDelayedSound(QString, int)), this, SLOT(slotPlayDelayedSound(QString, int)));

	// help menu -> display info
	infoDialog = new InfoDialog(this);
	connect(actionManual, SIGNAL(triggered()), infoDialog, SLOT(slotLoadTutorial()));
	connect(actionAbout, SIGNAL(triggered()), infoDialog, SLOT(slotLoadAbout()));
	connect(actionLicense, SIGNAL(triggered()), infoDialog, SLOT(slotLoadLicense()));

	//options menu
	connect(actionSound_on, SIGNAL(triggered()), this, SLOT(slotSoundOn()));

	if(QSound::isAvailable()) {
		soundAvailable = true;
		soundOn = true;
		slotPrintToConsole("Sound is availiable\n");
	} else {
		soundAvailable = false;
		soundOn = false;
		slotPrintToConsole("Sound is not availiable\n");
	}
}