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

MT Assignment1

X

Uploaded by

shekhar777229
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

MT Assignment1

X

Uploaded by

shekhar777229
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

Motilal Nehru National Institute of Technology

Allahabad
Department of Computer Science &
Engineering
Multimedia Technology Lab (CS17201)

MCA – 5th Semester

1. Design the following interactive games using Open GL.


a) Tic-Tac-Toe Game.

#include <GL/glut.h>

int grid[3][3] = {0}; // 0 = empty, 1 = X, 2 = O

int turn = 1; // 1 = X's turn, 2 = O's turn

void init() {

glClearColor(1.0, 1.0, 1.0, 1.0);

glMatrixMode(GL_PROJECTION);

gluOrtho2D(0.0, 3.0, 0.0, 3.0);

void drawX(float x, float y) {

glBegin(GL_LINES);

glVertex2f(x, y);

glVertex2f(x + 1, y + 1);

glVertex2f(x, y + 1);

glVertex2f(x + 1, y);

glEnd();

}
void drawO(float x, float y) {

float cx = x + 0.5, cy = y + 0.5, r = 0.5;

glBegin(GL_LINE_LOOP);

for (int i = 0; i < 360; i++) {

float theta = i * 3.14159f / 180;

glVertex2f(cx + r * cos(theta), cy + r * sin(theta));

glEnd();

void display() {

glClear(GL_COLOR_BUFFER_BIT);

// Draw grid

glBegin(GL_LINES);

for (int i = 1; i < 3; i++) {

glVertex2f(i, 0);

glVertex2f(i, 3);

glVertex2f(0, i);

glVertex2f(3, i);

glEnd();

// Draw Xs and Os

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

if (grid[i][j] == 1) drawX(j, 2 - i);

else if (grid[i][j] == 2) drawO(j, 2 - i);

}
glFlush();

int checkWin() {

for (int i = 0; i < 3; i++)

if (grid[i][0] == grid[i][1] && grid[i][1] == grid[i][2] && grid[i][0] != 0) return grid[i][0];

for (int i = 0; i < 3; i++)

if (grid[0][i] == grid[1][i] && grid[1][i] == grid[2][i] && grid[0][i] != 0) return grid[0][i];

if (grid[0][0] == grid[1][1] && grid[1][1] == grid[2][2] && grid[0][0] != 0) return grid[0][0];

if (grid[0][2] == grid[1][1] && grid[1][1] == grid[2][0] && grid[0][2] != 0) return grid[0][2];

return 0;

void mouse(int button, int state, int x, int y) {

if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {

int row = 2 - y / (500 / 3);

int col = x / (500 / 3);

if (grid[row][col] == 0) {

grid[row][col] = turn;

turn = (turn == 1) ? 2 : 1;

int winner = checkWin();

if (winner) {

printf("Player %d wins!\n", winner);

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

grid[i][j] = 0;

turn = 1;

glutPostRedisplay();

}
}

int main(int argc, char** argv) {

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(500, 500);

glutCreateWindow("Tic-Tac-Toe");

init();

glutDisplayFunc(display);

glutMouseFunc(mouse);

glutMainLoop();

return 0;

b) Snakes and Ladders Game.

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define BOARD_SIZE 100

int rollDice() {

return (rand() % 6) + 1;

int movePlayer(int position) {

int ladder[] = {3, 8, 28, 58, 75, 80}; // Ladders start positions

int ladderEnd[] = {22, 26, 84, 77, 86, 99}; // Ladders end positions
int snake[] = {17, 52, 57, 88, 62, 95}; // Snakes start positions

int snakeEnd[] = {7, 29, 40, 18, 22, 56}; // Snakes end positions

for (int i = 0; i < 6; i++) {

if (position == ladder[i]) {

printf("Yay! You found a ladder! Climb up to %d\n", ladderEnd[i]);

return ladderEnd[i];

} else if (position == snake[i]) {

printf("Oh no! A snake bit you! Slide down to %d\n", snakeEnd[i]);

return snakeEnd[i];

return position;

void playGame() {

int player1 = 1, player2 = 1;

int currentPlayer = 1;

int diceRoll;

while (player1 < BOARD_SIZE && player2 < BOARD_SIZE) {

printf("Player %d, press Enter to roll the dice...", currentPlayer);

getchar();

diceRoll = rollDice();

printf("You rolled a %d!\n", diceRoll);

if (currentPlayer == 1) {

player1 += diceRoll;

if (player1 > BOARD_SIZE) player1 -= diceRoll;

printf("Player 1 moves to %d\n", player1);


player1 = movePlayer(player1);

printf("Player 1 is now at %d\n", player1);

if (player1 == BOARD_SIZE) {

printf("Player 1 wins!\n");

break;

currentPlayer = 2;

} else {

player2 += diceRoll;

if (player2 > BOARD_SIZE) player2 -= diceRoll;

printf("Player 2 moves to %d\n", player2);

player2 = movePlayer(player2);

printf("Player 2 is now at %d\n", player2);

if (player2 == BOARD_SIZE) {

printf("Player 2 wins!\n");

break;

currentPlayer = 1;

}
}

int main() {

srand(time(0));

printf("Welcome to Snakes and Ladders!\n");

playGame();

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