Assignment4 OOP
Assignment4 OOP
H-11 Campus
Assignment 4
COURSE: Object Oriented Programming
NAME: Muhammad Asheer Ali
ENROLMENT: 09-134242-007
Dice Duel Game
Objective
To design and implement a simple console-based game that demonstrates the core
principles of OOP along with robust exception handling using custom and standard
exceptions.
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class InvalidRollException {
public:
};
class CheatingException {
public:
};
class Player {
protected:
string name;
int score;
public:
Player(string n) {
name = n;
score = 0;
return name;
int getScore() {
return score;
void addScore(int s) {
score += s;
virtual ~Player() {}
};
public:
NormalPlayer(string n) : Player(n) {}
int rollDice() {
return roll;
};
public:
LuckyPlayer(string n) : Player(n) {}
int rollDice() {
int roll = rand() % 4 + 3;
return roll;
};
public:
CheaterPlayer(string n) : Player(n) {}
int rollDice() {
return roll;
};
return roll * 2;
try {
int r1 = p1->rollDice();
int r2 = p2->rollDice();
cout << p1->getName() << " rolled: " << r1 << endl;
cout << p2->getName() << " rolled: " << r2 << endl;
p1->addScore(calculatePoints(r1));
p2->addScore(calculatePoints(r2));
catch (InvalidRollException& e) {
catch (CheatingException& e) {
cout << p1->getName() << ": " << p1->getScore() << endl;
cout << p2->getName() << ": " << p2->getScore() << endl;
else if (p2->getScore() > p1->getScore()) cout << p2->getName() << " wins!\n";
int main() {
srand((unsigned int)time(0));
playGame(player1, player2);
delete player1;
delete player2;
return 0;