Assignment 9 CG
Assignment 9 CG
Assignment 9
Student Name:
Branch: Be-CSE Section/Group: 22BCS_
Semester: 6th Date of Performance:03/04/25
Subject Name: Computer Graphics Subject Code: 22CSH-352
with Lab
1. Aim:
To generate and navigate complex 3D environments.
2. Objective:
Implement maze generation algorithms and explore pathfinding techniques in 3D space.
3. Implementation/Code:
#include <graphics.h>
#include <conio.h>
#include <iostream>
#include <stack>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#define ROWS 10
#define COLS 10
#define CELL_SIZE 40
using namespace std;
struct Cell {
bool visited;
bool walls[4]; // top, right, bottom, left
Cell() {
visited = false;
for (int i = 0; i < 4; i++) walls[i] = true;
}
DEPARTMENT OF
COMPUTER SCIENCE &
};
Cell maze[ROWS][COLS];
int dx[4] = {0, 1, 0, -1}; // Directions: top, right, bottom, left
int dy[4] = {-1, 0, 1, 0};
bool isValid(int x, int y) {
return (x >= 0 && x < COLS && y >= 0 && y < ROWS && !maze[y][x].visited);
}
void generateMaze(int x, int y) {
maze[y][x].visited = true;
vector<int> dirs;
dirs.push_back(0);
dirs.push_back(1);
dirs.push_back(2);
dirs.push_back(3);
random_shuffle(dirs.begin(), dirs.end());
for (int i = 0; i < dirs.size(); i++) {
int dir = dirs[i];
int nx = x + dx[dir];
int ny = y + dy[dir];
if (isValid(nx, ny)) {
maze[y][x].walls[dir] = false;
maze[ny][nx].walls[(dir + 2) % 4] = false;
generateMaze(nx, ny);
}
}
}
void drawMaze() {
setcolor(WHITE);
for (int y = 0; y < ROWS; y++) {
DEPARTMENT OF
COMPUTER SCIENCE &
4. Output:
Fig 2. Red dot overlaps with green dot win the game
DEPARTMENT OF
COMPUTER SCIENCE &
5. Learning Outcome:
Understand and implement recursive backtracking algorithms for maze generation and
pathfinding.
Learn the use of 2D arrays and structures to represent complex data like grids and cells in a
maze.
Gain proficiency in using graphics.h and drawing functions like line(), fillellipse(), and
floodfill() for visual representation.
Improve logical thinking and problem-solving skills through player movement mechanics and
wall-check conditions.
Learn to handle user input and keyboard events using getch() and extended key handling in
console applications.
Develop debugging and code correction skills by identifying and resolving errors related to C+
+98 compatibility and memory usage.