diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2020-09-21 16:13:25 +0200 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2020-09-21 21:38:35 +0200 |
| commit | 3686258d2fa6fdffebb57f7d530952fd4dabf366 (patch) | |
| tree | d0290f2dc79698a4c73beeb58dd8f39b8b718edf | |
| parent | 245a34f1fad0e4fe2fb6401dc1d5352e4914b48a (diff) | |
| download | tddd86-3686258d2fa6fdffebb57f7d530952fd4dabf366.tar.gz | |
Given files L4l4
| -rw-r--r-- | labb4/GameState.cpp | 104 | ||||
| -rw-r--r-- | labb4/GameState.h | 83 | ||||
| -rw-r--r-- | labb4/Hero.cpp | 15 | ||||
| -rw-r--r-- | labb4/Hero.h | 22 | ||||
| -rw-r--r-- | labb4/Junk.cpp | 16 | ||||
| -rw-r--r-- | labb4/Junk.h | 23 | ||||
| -rw-r--r-- | labb4/Robot.cpp | 15 | ||||
| -rw-r--r-- | labb4/Robot.h | 18 | ||||
| -rw-r--r-- | labb4/Robots.pro | 26 | ||||
| -rw-r--r-- | labb4/Unit.cpp | 66 | ||||
| -rw-r--r-- | labb4/Unit.h | 57 | ||||
| -rw-r--r-- | labb4/constants.h | 32 | ||||
| -rw-r--r-- | labb4/mainwindow.cpp | 199 | ||||
| -rw-r--r-- | labb4/mainwindow.h | 58 | ||||
| -rw-r--r-- | labb4/qgameoverwindow.cpp | 42 | ||||
| -rw-r--r-- | labb4/qgameoverwindow.h | 35 | ||||
| -rw-r--r-- | labb4/qresetbutton.cpp | 18 | ||||
| -rw-r--r-- | labb4/qresetbutton.h | 28 | ||||
| -rw-r--r-- | labb4/robotsmain.cpp | 21 | ||||
| -rw-r--r-- | labb4/utilities.cpp | 18 | ||||
| -rw-r--r-- | labb4/utilities.h | 27 |
21 files changed, 923 insertions, 0 deletions
diff --git a/labb4/GameState.cpp b/labb4/GameState.cpp new file mode 100644 index 0000000..581d2f7 --- /dev/null +++ b/labb4/GameState.cpp @@ -0,0 +1,104 @@ +/** + * Copyright (C) David Wolfe, 1999. All rights reserved. + * Ported to Qt and adapted for TDDD86, 2015. + */ + +#include "GameState.h" +#include "utilities.h" +#include "constants.h" + +GameState::GameState(){} + +GameState::GameState(int numberOfRobots) { + for (int i = 0; i < numberOfRobots; i++) { + Robot robot; + do {robot = Robot();} + while (!isEmpty (robot)); + robots.push_back(robot); + } + teleportHero(); +} + +void GameState::draw(QGraphicsScene *scene) const { + scene->clear(); + for (size_t i = 0; i < robots.size(); ++i) + robots[i].draw(scene); + for (size_t i = 0; i < junks.size(); ++i) + junks[i].draw(scene); + hero.draw(scene); +} + +void GameState::teleportHero() { + do hero.teleport(); + while (!isEmpty(hero)); +} + +void GameState::moveRobots() { + for (unsigned int i = 0; i < robots.size(); i++) + robots[i].moveTowards (hero); +} + +int GameState::countCollisions() { + int numberDestroyed = 0; + unsigned int i = 0; + while (i < robots.size()) { + bool hitJunk = junkAt (robots[i]); + bool collision = (countRobotsAt (robots[i]) > 1); + if (hitJunk || collision) { + if (!hitJunk) junks.push_back (Junk(robots[i])); + robots[i] = robots[robots.size()-1]; + robots.pop_back(); + numberDestroyed++; + } else i++; + } + return numberDestroyed; +} + +bool GameState::anyRobotsLeft() const { + return (robots.size() != 0); +} + +bool GameState::heroDead() const { + return !isEmpty(hero); +} + +bool GameState::isSafe(const Unit& unit) const { + for (unsigned int i = 0; i < robots.size(); i++) + if (robots[i].attacks(unit)) return false; + if (junkAt(unit)) return false; + return true; +} + +void GameState::moveHeroTowards(const Unit& dir) { + hero.moveTowards(dir); +} + +Hero GameState::getHero() const {return hero;} + +/* + * Free of robots and junk only + */ +bool GameState::isEmpty(const Unit& unit) const { + return (countRobotsAt(unit) == 0 && !junkAt(unit)); +} + +/* + * Is there junk at unit? + */ +bool GameState::junkAt(const Unit& unit) const { + for (size_t i = 0; i < junks.size(); ++i) + if (junks[i].at(unit)) return true; + return false; +} + +/* + * How many robots are there at unit? + */ +int GameState::countRobotsAt(const Unit& unit) const { + int count = 0; + for (size_t i = 0; i < robots.size(); ++i) { + if (robots[i].at(unit)) + count++; + } + return count; +} diff --git a/labb4/GameState.h b/labb4/GameState.h new file mode 100644 index 0000000..d260c58 --- /dev/null +++ b/labb4/GameState.h @@ -0,0 +1,83 @@ +/** + * Copyright (C) David Wolfe, 1999. All rights reserved. + * Ported to Qt and adapted for TDDD86, 2015. + * + * Maintains the game state: Location of all robots, junk and hero. + * Method isSafe() is used to keep Hero from moving stupidly. + */ + +#ifndef GAMESTATE_H +#define GAMESTATE_H + +#include <string> +#include <vector> +#include <QGraphicsScene> +#include "Unit.h" +#include "Robot.h" +#include "Junk.h" +#include "Hero.h" + +class GameState { +public: + GameState(); + GameState(int numberOfRobots); + + /* + * Clear and redraw entire playing field + */ + void draw(QGraphicsScene* scene) const; // Clear and redraw entire playing field + + /* + * Teleport hero to random location + */ + void teleportHero(); + + /* + * Move robots one step towards hero + */ + void moveRobots(); + + /* Count colliding robots + * Also converts robots to junk while + * checking collisions + */ + int countCollisions (); + + /* + * Check if any robots are left on playing field + */ + bool anyRobotsLeft () const; + + /* + * Is hero in same place as robot or junk? + */ + bool heroDead() const; + + /* + * Can unit safely reside here? + */ + bool isSafe (const Unit& unit) const; // Can unit safely reside here? + + /* + * Move hero towards dir + */ + void moveHeroTowards (const Unit& dir); + + /* + * Return hero + */ + Hero getHero () const; + +private: + std::vector<Robot> robots; // the robots + std::vector<Junk> junks; // robots that have turned to junk + Hero hero; // the hero + + // private helpers + bool isEmpty(const Unit& unit) const; + bool junkAt(const Unit& unit) const; + int countRobotsAt(const Unit& unit) const; + +}; + +#endif // GAMESTATE_H diff --git a/labb4/Hero.cpp b/labb4/Hero.cpp new file mode 100644 index 0000000..2d3b12e --- /dev/null +++ b/labb4/Hero.cpp @@ -0,0 +1,15 @@ +/** + * Copyright (C) David Wolfe, 1999. All rights reserved. + * Ported to Qt and adapted for TDDD86, 2015. + */ + +#include "Hero.h" +#include "constants.h" + +Hero::Hero() : Unit() {} + +void Hero::draw(QGraphicsScene *scene) const { + Point corner = asPoint(); + scene->addRect(QRectF(corner.x * UNIT_WIDTH, corner.y * UNIT_HEIGHT, + UNIT_WIDTH, UNIT_HEIGHT), Qt::NoPen, QBrush(HERO_COLOR)); +} diff --git a/labb4/Hero.h b/labb4/Hero.h new file mode 100644 index 0000000..7750b19 --- /dev/null +++ b/labb4/Hero.h @@ -0,0 +1,22 @@ +/** + * Copyright (C) David Wolfe, 1999. All rights reserved. + * Ported to Qt and adapted for TDDD86, 2015. + */ + +#ifndef HERO_H +#define HERO_H + +#include "Unit.h" +#include <QGraphicsScene> + +class Hero : public Unit { +public: + Hero(); + + /* + * Draws this hero onto the given QGraphicsScene. + */ + void draw(QGraphicsScene *scene) const; +}; + +#endif // HERO_H diff --git a/labb4/Junk.cpp b/labb4/Junk.cpp new file mode 100644 index 0000000..5f6ea29 --- /dev/null +++ b/labb4/Junk.cpp @@ -0,0 +1,16 @@ +/** + * Copyright (C) David Wolfe, 1999. All rights reserved. + * Ported to Qt and adapted for TDDD86, 2015. + */ + +#include "Junk.h" +#include "constants.h" + +Junk::Junk() : Unit() {} +Junk::Junk(Unit c) : Unit(c) {} + +void Junk::draw(QGraphicsScene *scene) const { + Point corner = asPoint(); + scene->addEllipse(QRectF(corner.x * UNIT_WIDTH, corner.y * UNIT_HEIGHT, + JUNK_RADIUS, JUNK_RADIUS), QPen(), QBrush(JUNK_COLOR)); +} diff --git a/labb4/Junk.h b/labb4/Junk.h new file mode 100644 index 0000000..b3b5648 --- /dev/null +++ b/labb4/Junk.h @@ -0,0 +1,23 @@ +/** + * Copyright (C) David Wolfe, 1999. All rights reserved. + * Ported to Qt and adapted for TDDD86, 2015. + */ + +#ifndef JUNK_H +#define JUNK_H + +#include "Unit.h" +#include <QGraphicsScene> + +class Junk : public Unit { +public: + Junk(); + Junk(Unit c); + + /* + * Draws this junk onto the given QGraphicsScene. + */ + void draw(QGraphicsScene* scene) const; +}; + +#endif // JUNK_H diff --git a/labb4/Robot.cpp b/labb4/Robot.cpp new file mode 100644 index 0000000..fa4df91 --- /dev/null +++ b/labb4/Robot.cpp @@ -0,0 +1,15 @@ +/** + * Copyright (C) David Wolfe, 1999. All rights reserved. + * Ported to Qt and adapted for TDDD86, 2015. + */ + +#include "Robot.h" +#include "constants.h" + +Robot::Robot() : Unit() {} + +void Robot::draw(QGraphicsScene *scene) const { + Point corner = asPoint(); + scene->addEllipse(QRectF(corner.x * UNIT_WIDTH, corner.y * UNIT_HEIGHT, + JUNK_RADIUS, JUNK_RADIUS), QPen(), QBrush(ROBOT_COLOR)); +} diff --git a/labb4/Robot.h b/labb4/Robot.h new file mode 100644 index 0000000..953b818 --- /dev/null +++ b/labb4/Robot.h @@ -0,0 +1,18 @@ +/** + * Copyright (C) David Wolfe, 1999. All rights reserved. + * Ported to Qt and adapted for TDDD86, 2015. + */ + +#ifndef ROBOT_H +#define ROBOT_H + +#include "Unit.h" +#include <QGraphicsScene> + +class Robot : public Unit { +public: + Robot(); + void draw(QGraphicsScene* scene) const; +}; + +#endif // ROBOT_H diff --git a/labb4/Robots.pro b/labb4/Robots.pro new file mode 100644 index 0000000..e6db845 --- /dev/null +++ b/labb4/Robots.pro @@ -0,0 +1,26 @@ +TEMPLATE = app +QT += widgets +HEADERS += mainwindow.h \ + utilities.h \ + constants.h \ + Unit.h \ + Junk.h \ + Hero.h \ + Robot.h \ + GameState.h \ + qgameoverwindow.h \ + qresetbutton.h +SOURCES += mainwindow.cpp \ + robotsmain.cpp \ + utilities.cpp \ + Unit.cpp \ + Junk.cpp \ + Hero.cpp \ + Robot.cpp \ + GameState.cpp \ + qgameoverwindow.cpp \ + qresetbutton.cpp +QMAKE_CXXFLAGS += -std=c++11 +macx { + cache() +} diff --git a/labb4/Unit.cpp b/labb4/Unit.cpp new file mode 100644 index 0000000..1492f63 --- /dev/null +++ b/labb4/Unit.cpp @@ -0,0 +1,66 @@ +/** + * Copyright (C) David Wolfe, 1999. All rights reserved. + * Ported to Qt and adapted for TDDD86, 2015. + */ + +#include "Unit.h" +#include "constants.h" +#include "utilities.h" +#include <cstdlib> +#include <cmath> + +Unit::Unit() { + teleport(); +} + +Unit::Unit(const Unit& u) { + x = u.x; + y = u.y; +} + +Unit::Unit(const Point& p) { + x = p.x; + y = p.y; +} + +Point Unit::asPoint() const { + return Point{x, y}; +} + +bool Unit::at(const Unit& u) const { + return (x == u.x && y == u.y); +} + +bool Unit::attacks(const Unit& u) const { + return (abs(x - u.x) <= 1 && + abs(y - u.y) <= 1); +} + +void Unit::moveTowards(const Unit& u) { + if (x > u.x) x--; + if (x < u.x) x++; + if (y > u.y) y--; + if (y < u.y) y++; + checkBounds(); +} + +void Unit::teleport() { + x = rand_int (MIN_X, MAX_X); + y = rand_int (MIN_Y, MAX_Y); +} + +double Unit::distanceTo(const Unit& u) const { + double dx = u.x - x; + double dy = u.y - y; + return sqrt(dx * dx + dy * dy); +} + +/* + * Put this unit inside playing field if outside + */ +void Unit::checkBounds() { + if (x < MIN_X) x = MIN_X; + if (x > MAX_X) x = MAX_X; + if (y < MIN_Y) y = MIN_Y; + if (y > MAX_Y) y = MAX_Y; +} diff --git a/labb4/Unit.h b/labb4/Unit.h new file mode 100644 index 0000000..1de5687 --- /dev/null +++ b/labb4/Unit.h @@ -0,0 +1,57 @@ +/** + * Copyright (C) David Wolfe, 1999. All rights reserved. + * Ported to Qt and adapted for TDDD86, 2015. + */ + +#ifndef UNIT_H +#define UNIT_H + +#include "utilities.h" + +/* Root class for all pieces on the board. + * Subclasses are Robot, Hero and Junk. + */ +class Unit { +public: + Unit(); + Unit(const Unit& u); + Unit(const Point& p); + + /* + * Return Point representation of Unit + */ + Point asPoint() const; + + /* + * Am I in the same square as u? + */ + bool at(const Unit& u) const; + + /* + * Can I catch u in one move? + */ + bool attacks(const Unit& u) const; + + /* + * Take one step closer to u + */ + void moveTowards(const Unit& u); + + /* + * Teleport. Does not check for collision + */ + void teleport(); + + /* + * Euclidean distance to u + */ + double distanceTo(const Unit& u) const; +private: + int x; // x position of this unit + int y; // y position of this unit + + // private helpers + void checkBounds(); +}; + +#endif // UNIT_H diff --git a/labb4/constants.h b/labb4/constants.h new file mode 100644 index 0000000..b6e4f8c --- /dev/null +++ b/labb4/constants.h @@ -0,0 +1,32 @@ +/** + * Copyright (C) David Wolfe, 1999. All rights reserved. + * Ported to Qt and adapted for TDDD86, 2015. + */ + +#ifndef CONSTANTS_H +#define CONSTANTS_H + +#include <QColor> + +// GUI constants +const int MIN_X = 0; +const int MIN_Y = 0; +const int MAX_X = 39; +const int MAX_Y = 39; +const int UNIT_WIDTH = 10; +const int UNIT_HEIGHT = 10; +static const int SCENE_WIDTH = 400; +static const int SCENE_HEIGHT = 400; +const int JUNK_RADIUS = 9; +const QColor JUNK_COLOR = QColor(0, 0, 0); +const QColor ROBOT_COLOR = QColor(245, 95, 60); +const QColor HERO_COLOR = QColor(237, 207, 114); + +// game constants +const int MIN_ROBOTS = 10; +const int MAX_ROBOTS = 80; +const int ROBOTS_INC = 5; // Additional robots per round +const int POINTS_PER_ROBOT = 10; +const int WAIT_BONUS = 20; // Per robot + +#endif // CONSTANTS_H diff --git a/labb4/mainwindow.cpp b/labb4/mainwindow.cpp new file mode 100644 index 0000000..99d288f --- /dev/null +++ b/labb4/mainwindow.cpp @@ -0,0 +1,199 @@ +/** + * TDDD86 Robots + * This file contains the implementation of the mainwindow class. + * See mainwindow.h for comments about each member. + */ + +#include <algorithm> +#include "mainwindow.h" +#include "Hero.h" +#include "Robot.h" +#include "Junk.h" + +MainWindow::MainWindow(QWidget *parent) : + QWidget(parent) { + // set default size and color + scene = new QGraphicsScene(0, 0, SCENE_WIDTH, SCENE_HEIGHT); + scene->setBackgroundBrush(QBrush(QColor(204, 192, 179))); + view = new QGraphicsView(scene); + + // create the main layout + mainLayout = new QVBoxLayout(); + setLayout(mainLayout); + + // add game board to layout + mainLayout->insertWidget(0, view); + + // start new game and draw state + gameState.draw(scene); + + // create label layout + labelLayout = new QHBoxLayout(); + + // create the score widget + scoreLabel = new QLabel(QString("SCORE: %1").arg(score)); + scoreLabel->setStyleSheet("QLabel { color: rgb(235,224,214); font: 16pt; }"); + scoreLabel->setFixedHeight(35); + + // create the level widget + levelLabel = new QLabel(QString("LEVEL: %1").arg(level)); + levelLabel->setStyleSheet("QLabel { color: rgb(235,224,214); font: 16pt; }"); + levelLabel->setFixedHeight(35); + + // add score and level widgets to board + labelLayout->insertWidget(0, levelLabel, 0, Qt::AlignLeft); + labelLayout->insertWidget(1, scoreLabel, 0, Qt::AlignRight); + mainLayout->insertLayout(1, labelLayout); + + // style sheet of the game board + setStyleSheet("MainWindow { background-color: rgb(187,173,160) }"); + + // no resizing of game window + setFixedSize(sizeHint()); + + connect(gameOverWindow.getResetBtn(), SIGNAL(clicked()), this, SLOT(resetGame())); +} + +MainWindow::~MainWindow() { + delete scoreLabel; + delete levelLabel; + delete labelLayout; + delete scene; + delete view; + delete mainLayout; +} + +/* + * Listens to key press events from the graphical subsystem, + * and handles the events appropriately: + * - '[n]' moves hero in direction [n], + * where n = 1, 2, 3, 4, 6, 7, 8, 9 on the numeric keypad + * - '5' on the numeric keypad makes the hero wait one turn + * - 'T' teleports hero + */ +void MainWindow::keyPressEvent(QKeyEvent *e) +{ + if (!gameOver) { // only process key presses while game is not over + Hero hero = gameState.getHero(); + Point pt = hero.asPoint(); + bool actionTaken = false; + bool waiting = false; + + if (e->modifiers() == Qt::KeypadModifier) { + switch (e->key()) { + case Qt::Key_1: + actionTaken = tryMove(hero, Point{pt.x - 1, pt.y + 1}); + break; + case Qt::Key_2: + actionTaken = tryMove(hero, Point{pt.x, pt.y + 1}); + break; + case Qt::Key_3: + actionTaken = tryMove(hero, Point{pt.x + 1, pt.y + 1}); + break; + case Qt::Key_4: + actionTaken = tryMove(hero, Point{pt.x - 1, pt.y}); + break; + case Qt::Key_6: + actionTaken = tryMove(hero, Point{pt.x + 1, pt.y}); + break; + case Qt::Key_7: + actionTaken = tryMove(hero, Point{pt.x - 1, pt.y - 1}); + break; + case Qt::Key_8: + actionTaken = tryMove(hero, Point{pt.x, pt.y - 1}); + break; + case Qt::Key_9: + actionTaken = tryMove(hero, Point{pt.x + 1, pt.y - 1}); + break; + case Qt::Key_5: + actionTaken = true; + waiting = true; + break; + default: + QWidget::keyPressEvent(e); + } + } else { + switch (e->key()) { + case Qt::Key_T: + gameState.teleportHero(); + actionTaken = true; + break; + default: + QWidget::keyPressEvent(e); + } + } + + if (actionTaken) { // process results of viable move + processMove(waiting); + } + } else { // game is over - do not process key press + QWidget::keyPressEvent(e); + } +} + +/* + * Try to move hero to unit + */ +bool MainWindow::tryMove(Hero hero, const Point& point) { + if (!outsideBorder(point)) { + hero.moveTowards(point); + if (gameState.isSafe(hero)) { + gameState.moveHeroTowards(point); + return true; + } + } + return false; +} + +/* + * Process results of viable move + */ +void MainWindow::processMove(bool waiting) { + gameState.moveRobots(); + score += gameState.countCollisions() * (POINTS_PER_ROBOT + + (waiting ? WAIT_BONUS : 0)); + gameState.draw(scene); + displayScore(); + + if (!gameState.anyRobotsLeft()) { // won level + numberOfRobots = std::min(MAX_ROBOTS, numberOfRobots + ROBOTS_INC); + gameState = GameState(numberOfRobots); + gameState.draw(scene); + ++level; + displayLevel(); + } else if (gameState.heroDead()) { // game over + gameOver = true; + gameOverWindow.show(); + } +} + +/* + * Is point outside of playing field? + */ +bool MainWindow::outsideBorder(Point const& point) const { + return point.x > MAX_X + || point.y > MAX_Y + || point.x < MIN_X + || point.y < MIN_Y; +} + + +void MainWindow::displayScore() const { + scoreLabel->setText(QString("SCORE: %1").arg(score)); +} + +void MainWindow::displayLevel() const { + levelLabel->setText(QString("LEVEL: %1").arg(level)); +} + +void MainWindow::resetGame() { + score = 0; + level = 1; + numberOfRobots = MIN_ROBOTS; + gameState = GameState(numberOfRobots); + gameState.draw(scene); + displayScore(); + displayLevel(); + gameOver = false; + gameOverWindow.hide(); +} diff --git a/labb4/mainwindow.h b/labb4/mainwindow.h new file mode 100644 index 0000000..1ebbb7d --- /dev/null +++ b/labb4/mainwindow.h @@ -0,0 +1,58 @@ +/** + * TDDD86 Robots + * This file contains the declaration of the mainwindow class. + * See mainwindow.cpp for implementation of each member. + */ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QWidget> +#include <QVBoxLayout> +#include <QHBoxLayout> +#include <QGraphicsView> +#include <QGraphicsScene> +#include <QLabel> +#include <QKeyEvent> +#include "qgameoverwindow.h" +#include "constants.h" +#include "GameState.h" + +class MainWindow : public QWidget { + Q_OBJECT +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +signals: + +protected: + void keyPressEvent(QKeyEvent *k); + +public slots: + void resetGame(); + +private: + QVBoxLayout* mainLayout = nullptr; // main layout + QHBoxLayout* labelLayout = nullptr; // label layout + QGraphicsView* view = nullptr; // playing field widget + QGraphicsScene* scene = nullptr; // scene for playing field + QLabel* scoreLabel = nullptr; // score widget + QLabel* levelLabel = nullptr; // level widget + QGameOverWindow gameOverWindow; // game over widget + + int score = 0; // current score + int level = 1; // current level + int numberOfRobots = MIN_ROBOTS; // current no. of robots + GameState gameState = GameState(numberOfRobots); // current state of game + bool gameOver = false; + + // private helpers + bool tryMove(Hero hero, const Point &point); + void processMove(bool waiting); + bool outsideBorder(const Point &point) const; + void displayScore() const; + void displayLevel() const; +}; + +#endif // MAINWINDOW_H diff --git a/labb4/qgameoverwindow.cpp b/labb4/qgameoverwindow.cpp new file mode 100644 index 0000000..5a3b359 --- /dev/null +++ b/labb4/qgameoverwindow.cpp @@ -0,0 +1,42 @@ +/** + * TDDD86 Robots + * This file contains the implementation of the QGameOverWindow class. + */ + +#include "qgameoverwindow.h" +#include "qresetbutton.h" + +#include <QVBoxLayout> +#include <QLabel> + +QGameOverWindow::QGameOverWindow(QWidget *parent) : + QWidget(parent) { + setStyleSheet("QGameOverWindow { background: rgb(237,224,200); }"); + setFixedSize(425, 205); + QVBoxLayout *layout = new QVBoxLayout(this); + + // game over label + QLabel* gameover = new QLabel("Game Over!", this); + gameover->setStyleSheet("QLabel { color: rgb(119,110,101); font: 40pt; font: bold;} "); + + // reset button + reset = new QResetButton(this); + reset->setFixedHeight(50); + reset->setFixedWidth(100); + + // add game over label to window + layout->insertWidget(0, gameover, 0, Qt::AlignCenter); + + // add reset button to window + layout->insertWidget(1, reset, 0, Qt::AlignCenter); +} + +QGameOverWindow::~QGameOverWindow() { + delete reset; + delete gameover; + delete layout; +} + +QResetButton* QGameOverWindow::getResetBtn() const { + return reset; +} diff --git a/labb4/qgameoverwindow.h b/labb4/qgameoverwindow.h new file mode 100644 index 0000000..2185d34 --- /dev/null +++ b/labb4/qgameoverwindow.h @@ -0,0 +1,35 @@ +/** + * TDDD86 Robots + * This file contains the declaration of the QGameOverWindow class. + * See qgameoverwindow.cpp for implementation of each member. + */ + +#ifndef QGAMEOVERWINDOW_H +#define QGAMEOVERWINDOW_H + +#include <QWidget> +#include <QVBoxLayout> +#include <QLabel> +#include "qresetbutton.h" + +class QGameOverWindow : public QWidget +{ + Q_OBJECT +public: + explicit QGameOverWindow(QWidget *parent = 0); + ~QGameOverWindow(); + + QResetButton* getResetBtn() const; + +signals: + +public slots: + +private: + + QVBoxLayout *layout = nullptr; + QLabel* gameover = nullptr; + QResetButton* reset = nullptr; +}; + +#endif // QGAMEOVERWINDOW_H diff --git a/labb4/qresetbutton.cpp b/labb4/qresetbutton.cpp new file mode 100644 index 0000000..8eef506 --- /dev/null +++ b/labb4/qresetbutton.cpp @@ -0,0 +1,18 @@ +/** + * TDDD86 Robots + * This file contains the implementation of the QResetButton class. + */ + +#include "qresetbutton.h" + +QResetButton::QResetButton( QWidget* parent) : QLabel(parent) { + setText("Try again!"); + setAlignment(Qt::AlignCenter); + setStyleSheet("QResetButton { background-color: rgb(143,122,102); border-radius: 10px; font: bold; color: white; }"); +} + +void QResetButton::mousePressEvent(QMouseEvent* event) { + if (event) { + emit clicked(); + } +} diff --git a/labb4/qresetbutton.h b/labb4/qresetbutton.h new file mode 100644 index 0000000..4b15e6f --- /dev/null +++ b/labb4/qresetbutton.h @@ -0,0 +1,28 @@ +/** + * TDDD86 Robots + * This file contains the declaration of the QResetButton class. + * See qresetbutton.cpp for implementation of each member. + */ + +#ifndef QRESETBUTTON_H +#define QRESETBUTTON_H + +#include <QLabel> + +class QResetButton : public QLabel +{ + Q_OBJECT +public: + QResetButton( QWidget* parent = 0); + +signals: + void clicked(); + +public slots: + +protected: + void mousePressEvent(QMouseEvent* event); + +}; + +#endif // QRESETBUTTON_H diff --git a/labb4/robotsmain.cpp b/labb4/robotsmain.cpp new file mode 100644 index 0000000..64eb176 --- /dev/null +++ b/labb4/robotsmain.cpp @@ -0,0 +1,21 @@ +/** + * TDDD86 Robots + * This client program contains the 'main' function to set up the overall + * program's graphical user interface. + * Your code should work properly with an unmodified version of this file. + */ + +#include <QApplication> +#include "mainwindow.h" +#include "utilities.h" + +int main(int argc, char *argv[]) { + QApplication a(argc, argv); + + rand_seed(); // seed random number generator + + MainWindow view; // create main window + view.show(); // display main window + + return a.exec(); // start Qt event loop +} diff --git a/labb4/utilities.cpp b/labb4/utilities.cpp new file mode 100644 index 0000000..a894a3c --- /dev/null +++ b/labb4/utilities.cpp @@ -0,0 +1,18 @@ +/** + * Copyright (C) David Wolfe, 1999. All rights reserved. + * Ported to Qt and adapted for TDDD86, 2015. + */ + +#include "utilities.h" +#include "constants.h" +#include <cstdlib> +#include <ctime> + +void rand_seed() { + int seed = static_cast<int>(time(0)); + srand(seed); +} + +int rand_int(int a, int b) { + return a + rand() % (b - a + 1); +} diff --git a/labb4/utilities.h b/labb4/utilities.h new file mode 100644 index 0000000..c180129 --- /dev/null +++ b/labb4/utilities.h @@ -0,0 +1,27 @@ +/** + * Copyright (C) David Wolfe, 1999. All rights reserved. + * Ported to Qt and adapted for TDDD86, 2015. + */ + +#ifndef UTILITIES_H +#define UTILITIES_H + +struct Point { + int x; + int y; +}; + +/** + * Sets the seed of the random number generator. + */ +void rand_seed(); + +/** + * Returns a random integer in a range. + * @param a the bottom of the range + * @param b the top of the range + * &return a random number x having a <= x and x <= b + */ +int rand_int(int a, int b); + +#endif // UTILITIES_H |
