0% found this document useful (0 votes)
26 views6 pages

Assignment 9 CG

Computer graphics assignment 9 chandigarh university
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views6 pages

Assignment 9 CG

Computer graphics assignment 9 chandigarh university
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

DEPARTMENT OF

COMPUTER SCIENCE &

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 &

for (int x = 0; x < COLS; x++) {


int x0 = x * CELL_SIZE;
int y0 = y * CELL_SIZE;
int x1 = x0 + CELL_SIZE;
int y1 = y0 + CELL_SIZE;
if (maze[y][x].walls[0]) line(x0, y0, x1, y0); // Top
if (maze[y][x].walls[1]) line(x1, y0, x1, y1); // Right
if (maze[y][x].walls[2]) line(x1, y1, x0, y1); // Bottom
if (maze[y][x].walls[3]) line(x0, y1, x0, y0); // Left
setfillstyle(SOLID_FILL, DARKGRAY);
floodfill(x0 + 1, y0 + 1, WHITE);
}
}
int goalX = (COLS - 1) * CELL_SIZE + CELL_SIZE / 2;
int goalY = (ROWS - 1) * CELL_SIZE + CELL_SIZE / 2;
setcolor(GREEN);
setfillstyle(SOLID_FILL, GREEN);
fillellipse(goalX, goalY, 6, 6);
}
void drawPlayer(int x, int y) {
int cx = x * CELL_SIZE + CELL_SIZE / 2;
int cy = y * CELL_SIZE + CELL_SIZE / 2;
setcolor(RED);
setfillstyle(SOLID_FILL, RED);
fillellipse(cx, cy, 6, 6);
}
void showWinMessage() {
settextstyle(10, 0, 3);
setcolor(YELLOW);
DEPARTMENT OF
COMPUTER SCIENCE &

outtextxy(150, 200, "YOU WIN!");


getch();
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
srand(time(0));
generateMaze(0, 0); int playerX = 0, playerY = 0;
while (1) {
cleardevice();
drawMaze();
drawPlayer(playerX, playerY);
// Win condition
if (playerX == COLS - 1 && playerY == ROWS - 1) {
showWinMessage();
break;
}
char ch = getch();
if (ch == 27) break; // ESC
if (ch == 0) {
ch = getch();
if (ch == 72 && !maze[playerY][playerX].walls[0]) playerY--; // UP
if (ch == 80 && !maze[playerY][playerX].walls[2]) playerY++; // DOWN
if (ch == 75 && !maze[playerY][playerX].walls[3]) playerX--; // LEFT
if (ch == 77 && !maze[playerY][playerX].walls[1]) playerX++; // RIGHT
}}
closegraph();
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE &

4. Output:

Fig 1. Maze generated for playing the game

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.

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