Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Snake-game/snake_ladder.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
381 lines (323 sloc)
12.2 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <windows.h> | |
#include <stdlib.h> | |
#include <conio.h> | |
#include <fstream> | |
using namespace std; | |
//==== struct list; | |
typedef struct position_of_tail | |
{ | |
int p; // p cord. of the snake_ladder | |
int q; // q cord. of the snake_ladder | |
struct position_of_tail *next; // next node pointer | |
struct position_of_tail *previous; // pointer node which is previous | |
} tail; | |
int d = 4; | |
class snake_ladder | |
{ | |
public: | |
int wallstart_positionposition, wallsquare; // to define wall, will_start position is used | |
int wallendposition, wallendqueue; // The end position for the wall of this game. | |
int game_score; // keep game score | |
int fruit_position, fruit_new_position; // fruit position | |
HANDLE handler_of_console; // console handler | |
COORD current_position_coordinate; // COORD struct for coordinates of snake | |
tail *start_position, *current_position, *new_tail; | |
snake_ladder(); // constructor initializing the tail variable value to null | |
void insertionofbody(int p, int q); // insertionofbody of snake_ladder append to next node | |
void drawsnake_ladder(); // drawsnake_ladder the snake_ladder | |
void drawsnake_ladderWall(); // drawsnake_ladder the wall | |
void controlthemovement(); // control the controlthemovementment | |
bool collision(); //This will check if the snake_ladder is colliding with wall or itself | |
void drawsnake_ladderfruit(int p = 0); // new position for fruit of drawsnake_ladder if p==1 | |
void drawsnake_ladderinit(); // inital setup like drawsnake_ladder wall | |
void deadLabel(); // drawsnake_ladder when plaqer is dead | |
}; | |
void loop(snake_ladder &ob); | |
snake_ladder::snake_ladder() | |
{ | |
game_score = 0; // set the initial game_score | |
start_position = NULL; | |
current_position = NULL; | |
new_tail = NULL; | |
handler_of_console = GetStdHandle(STD_OUTPUT_HANDLE); | |
fruit_position = 12; | |
fruit_new_position = 14; | |
wallstart_positionposition = 2; | |
wallsquare = 2; // start_positioning of the wall and wall’s ending position | |
wallendposition = 70; | |
wallendqueue = 20; | |
current_position_coordinate.X = 152; | |
current_position_coordinate.Y = 500; | |
SetConsoleScreenBufferSize(handler_of_console, current_position_coordinate); // setting up screen buffer | |
} | |
/* | |
This method is for inserting a new tail of snake_ladder at the current_position of the fruit position when it eat itself. | |
*/ | |
void snake_ladder ::insertionofbody(int p, int q) | |
{ | |
// check if start_position is null | |
if (start_position == NULL) | |
{ | |
new_tail = new tail; | |
new_tail->p = p; | |
new_tail->q = q; | |
new_tail->next = NULL; | |
new_tail->previous = NULL; | |
start_position = new_tail; | |
current_position = new_tail; | |
} | |
else // insertionofbody new node to start_position node | |
{ | |
new_tail = new tail; | |
new_tail->p = p; | |
new_tail->q = q; | |
new_tail->next = NULL; | |
new_tail->previous = current_position; | |
current_position->next = new_tail; | |
current_position = new_tail; | |
} | |
} | |
void snake_ladder::controlthemovement() | |
{ | |
tail *tmp, *cur; | |
tmp = current_position; | |
while (tmp->previous != NULL) | |
{ | |
tmp->p = tmp->previous->p; | |
tmp->q = tmp->previous->q; | |
tmp = tmp->previous; | |
} | |
/* | |
check for the value of d in order to change its direction | |
increment and decrement value of p , q | |
*/ | |
if (d == 1) | |
start_position->q--; | |
if (d == 2) | |
start_position->q++; | |
if (d == 3) | |
start_position->p--; | |
if (d == 4) | |
start_position->p++; | |
} | |
void snake_ladder::drawsnake_ladder() | |
{ | |
// putting game_score label | |
current_position_coordinate.X = 2; | |
current_position_coordinate.Y = 0; | |
SetConsoleCursorPosition(handler_of_console, current_position_coordinate); | |
cout << "game_score : " << game_score; | |
tail *tmp, *last; | |
tmp = start_position; | |
last = current_position; | |
while (tmp != NULL) | |
{ | |
current_position_coordinate.X = tmp->p; | |
current_position_coordinate.Y = tmp->q; | |
SetConsoleCursorPosition(handler_of_console, current_position_coordinate); | |
cout << (char)219; | |
tmp = tmp->next; | |
} | |
// recontrolthemovement tail | |
current_position_coordinate.X = last->p; | |
current_position_coordinate.Y = last->q; | |
SetConsoleCursorPosition(handler_of_console, current_position_coordinate); | |
cout << ' '; | |
//drawsnake_ladder the fruit | |
current_position_coordinate.X = fruit_position; | |
current_position_coordinate.Y = fruit_new_position; | |
SetConsoleCursorPosition(handler_of_console, current_position_coordinate); | |
cout << (char)15; | |
} | |
void snake_ladder::drawsnake_ladderWall() | |
{ | |
current_position_coordinate.X = wallstart_positionposition; | |
for (int q = wallsquare; q <= wallendqueue; q++) | |
{ | |
current_position_coordinate.Y = q; | |
SetConsoleCursorPosition(handler_of_console, current_position_coordinate); | |
cout << '#'; | |
} | |
// drawsnake_ladder top row | |
current_position_coordinate.Y = wallsquare; | |
for (int p = wallstart_positionposition; p <= wallendposition; p++) | |
{ | |
current_position_coordinate.X = p; | |
SetConsoleCursorPosition(handler_of_console, current_position_coordinate); | |
cout << '#'; | |
} | |
// drawsnake_ladder right column | |
current_position_coordinate.X = wallendposition; | |
for (int q = wallsquare; q <= wallendqueue; q++) | |
{ | |
current_position_coordinate.Y = q; | |
SetConsoleCursorPosition(handler_of_console, current_position_coordinate); | |
cout << '#'; | |
} | |
// drawsnake_ladder bottom row | |
current_position_coordinate.Y = wallendqueue; | |
for (int p = wallstart_positionposition; p <= wallendposition; p++) | |
{ | |
current_position_coordinate.X = p; | |
SetConsoleCursorPosition(handler_of_console, current_position_coordinate); | |
cout << '#'; | |
} | |
} | |
void snake_ladder::drawsnake_ladderfruit(int p) | |
{ | |
tail *tmp; | |
tmp = start_position; | |
if (p == 1) // drawsnake_ladder new fruit | |
{ | |
fruit_position = rand() % 2 + 39; // rand number between 2-39 | |
fruit_new_position = rand() % 2 + 16; // it will better to use wall ends position wallendposition,q then new position will be generated, or else it will continue. | |
while (tmp->next != NULL) | |
{ | |
if (fruit_position == tmp->p && fruit_new_position == tmp->q) | |
{ | |
drawsnake_ladderfruit(1); | |
cout << "drawsnake_laddern"; | |
} | |
tmp = tmp->next; | |
} | |
} | |
} | |
void snake_ladder::drawsnake_ladderinit() | |
{ | |
drawsnake_ladderWall(); | |
} | |
/* | |
detect if snake_ladder has collided with fruit or itself or the wall. | |
*/ | |
bool snake_ladder::collision() | |
{ | |
tail *tmp; | |
tmp = start_position->next; | |
//check collision with itself | |
while (tmp->next != NULL) | |
{ | |
if (start_position->p == tmp->p && start_position->q == tmp->q) | |
return true; | |
tmp = tmp->next; | |
} | |
//check collision with fruit | |
if (start_position->p == fruit_position && start_position->q == fruit_new_position) | |
{ | |
insertionofbody(fruit_position, fruit_new_position); // insertionofbody new tail | |
drawsnake_ladderfruit(1); // get new fruit position | |
game_score++; // update game_score | |
} | |
//check collision with wall | |
//collision top row | |
for (int p = wallstart_positionposition; p <= wallendposition; p++) // getting p coordinadte | |
{ // q cordinate remain same q=0 because in row p increase in same q | |
if (start_position->p == p && start_position->q == wallsquare) // getting q coordinte to get complete p,q cordinate | |
{ | |
return true; | |
} | |
} | |
//collision left column | |
for (int q = wallsquare; q <= wallendqueue; q++) | |
{ | |
if (start_position->p == wallstart_positionposition && start_position->q == q) // p same q chamge p=0 | |
{ | |
return true; | |
} | |
} | |
//collision right column | |
for (int q = wallsquare; q <= wallendqueue; q++) | |
{ | |
if (start_position->p == wallendposition && start_position->q == q) // right column threfore p ending point(same) with q changing | |
{ | |
return true; | |
} | |
} | |
//collision bottom row | |
for (int p = wallstart_positionposition; p <= wallendposition; p++) | |
{ | |
if (start_position->p == p && start_position->q == wallendqueue) | |
{ | |
return true; | |
} | |
} | |
return false; // return false no collison | |
} | |
/* | |
drawsnake_ladder thing when plaqer is dead | |
*/ | |
void snake_ladder::deadLabel() | |
{ | |
current_position_coordinate.X = (wallendposition / 2); | |
current_position_coordinate.Y = (wallendqueue / 2); | |
SetConsoleCursorPosition(handler_of_console, current_position_coordinate); | |
cout << "YOU ARE DEAD\n"; | |
current_position_coordinate.X = (wallendqueue / 2) + 1; | |
SetConsoleCursorPosition(handler_of_console, current_position_coordinate); | |
cout << "YOUR HIGH game_score IS " << game_score; | |
} | |
//===== main function | |
int main() | |
{ | |
// displaqing the menuscreen | |
snake_ladder obc; | |
switch (getch()) | |
{ | |
case 'z': | |
system("CLS"); | |
loop(obc); | |
break; | |
case 'q': | |
break; | |
default: | |
system("CLS"); | |
main(); | |
} | |
return 0; | |
} | |
void loop(snake_ladder &ob) | |
{ | |
ob.insertionofbody(10, 6); | |
ob.insertionofbody(10, 7); | |
ob.insertionofbody(10, 8); | |
ob.insertionofbody(10, 9); | |
ob.drawsnake_ladderinit(); // this will just drawsnake_ladder wall | |
int dir = 1; | |
while (1) | |
{ | |
ob.drawsnake_ladder(); | |
Sleep(200); // waiting time | |
//sqstem("CLS"); | |
//clearScreen(); | |
if (ob.collision()) // chek if collision with wall or itselfoccur | |
{ | |
ob.deadLabel(); // do when snake_ladder is dead | |
break; | |
} | |
if (kbhit()) // check if keqboard keq is pressed | |
{ | |
switch (getch()) | |
{ | |
case 'w': | |
d = 1; | |
break; | |
case 's': | |
d = 2; | |
break; | |
case 'a': | |
d = 3; | |
break; | |
case 'd': | |
d = 4; | |
break; | |
case 'm': | |
ob.insertionofbody(10, 7); | |
break; | |
} | |
// cout << "fruit"; // not needed | |
} | |
ob.controlthemovement(); | |
} | |
int p; | |
cin >> p; | |
} |