0% found this document useful (0 votes)
9 views

Game Project Code

Uploaded by

yejed87634
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Game Project Code

Uploaded by

yejed87634
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

#include <string>
#include <cstdlib>
#include <ctime>

enum class GameMode {


HumanVsHuman,
HumanVsComputer,
ComputerVsComputer
};
-----------------------------------------------------------------------------------
---------------------------------------------------------------------
class Player {
protected:
int x;
int y;

public:
Player(int startX, int startY) : x(startX), y(start) {}

virtual void autoMoveDown(int(&nextStage)[20][13], int(&currStage)[20][13],


bool& isBomb) = 0;
virtual void moveBlock(int newX, int newY, int(&currStage)[20][13], bool&
isBomb) = 0;
virtual void bombtheboard(int(&currStage)[20][13], int(&nextStage)[20][13],
int& x, int& y) = 0;
};
-----------------------------------------------------------------------------------
---------------------------------------------------------------------
class HumanPlayer : public Player {
public:
HumanPlayer(int startX, int startY) : Player(startX, startY) {}

void autoMoveDown(int(&nextStage)[20][13], int(&currStage)[20][13], bool&


isBomb) override {}

void moveBlock(int newX, int newY, int(&currStage)[20][13], bool& isBomb)


override {
std::cout << "Enter your move (left, right, down, rotate, bomb): ";
std::string move;
std::cin >> move;

if (move == "left") {
if (newX > 0 && currStage[newY][newX - 1] == 0) {
x--;
}
} else if (move == "right") {
if (newX < 12 && currStage[newY][newX + 1] == 0) {
x++;
}
} else if (move == "down") {
if (newY < 19 && currStage[newY + 1][newX] == 0) {
y++;
}
} else if (move == "rotate") {
// Implement block rotation
} else if (move == "bomb") {
// Implement bomb drop
}
}

void bombtheboard(int(&currStage)[20][13], int(&nextStage)[20][13], int& x,


int& y) override {}
};
-----------------------------------------------------------------------------------
---------------------------------------------------------------------
class ComputerPlayer : public Player {
public:
ComputerPlayer(int startX, int startY) : Player(startX, startY) {}

void autoMoveDown(int(&nextStage)[20][13], int(&currStage)[20][13], bool&


isBomb) override {
if (rand() % 2 == 0) {
if (y < 19 && currStage[y + 1][x] == 0) {
y++;
}
} else {
// Do nothing or pause
}
}

void moveBlock(int newX, int newY, int(&currStage)[20][13], bool& isBomb)


override {
int direction = rand() % 2 == 0 ? -1 : 1;
if (newX + direction >= 0 && newX + direction <= 12 && currStage[newY][newX
+ direction] == 0) {
x += direction;
}
}

void bombtheboard(int(&currStage)[20][13], int(&nextStage)[20][13], int& x,


int& y) override {}
};

bool gameOver(int(&currStage)[20][13]) {
for (int i = 0; i < 13; i++) {
if (currStage[0][i] != 0) {
return true;
}
}
return false;
}
-----------------------------------------------------------------------------------
---------------------------------------------------------------------
class Game {
private:
Player* player1;
Player* player2;

public:
Game(GameMode mode) {
if (mode == GameMode::HumanVsHuman) {
player1 = new HumanPlayer(4, 0);
player2 = new HumanPlayer(4, 0);
} else if (mode == GameMode::HumanVsComputer) {
player1 = new HumanPlayer(4, 0);
player2 = new ComputerPlayer(4, 0);
} else if (mode == GameMode::ComputerVsComputer) {
player1 = new ComputerPlayer(4, 0);
player2 = new ComputerPlayer(4, 0);
}
}

~Game() {
delete player1;
delete player2;
}

void start() {
int nextStage[20][13];
int currStage[20][13];
bool isBomb1 = false;
bool isBomb2 = false;

while (!gameOver(currStage)) {
player1->moveBlock(player1->x, player1->y, currStage, isBomb1);
player2->moveBlock(player2->x, player2->y, currStage, isBomb2);
}
}
};

int main() {
srand(time(NULL)); // Seed the random number generator

Game humanVsHuman(GameMode::HumanVsHuman);
humanVsHuman.start();

Game humanVsComputer(GameMode::HumanVsComputer);
humanVsComputer.start();

Game computerVsComputer(GameMode::ComputerVsComputer);
computerVsComputer.start();

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy