Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ID: 208957837 Name: Alon Doron */
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <time.h>
- #pragma region Structs
- typedef struct _Cell {
- int x;
- int y;
- struct _Cell *next;
- } Snake_Cell;
- typedef struct _List {
- Snake_Cell *head;
- Snake_Cell *tail;
- int count;
- } Snake;
- #pragma endregion
- #pragma region Consts
- #define ROWS 25
- #define COLS 75
- #define BORDER_SIGN '#'
- #define SNAKE_SIGN '@'
- #define SLEEP_TIMER 1
- #define CREATE_NEW_CELL_TIMER 0.5
- #pragma endregion
- #pragma region Declarations
- // This function clears the screen.
- void clearScreen(void);
- // This function starts the game.
- void playGame(void);
- // Moves the cursor to position (x,y) on screen.
- // Parameters:
- // x: the row of the posiiton.
- // y: the column of the posiiton.
- void gotoxy(int x, int y);
- // Delays the execution of the program.
- // Parameters:
- // secs: the length of the delay in seconds.
- void sleep(float secs);
- //This function goes to desired row and col, and prints a char.
- void gotoxyAndPrint(int row, int col, char sign);
- // This function prints the board's first state.
- void initBoard();
- //This function prints the board boundries.
- void printBoardBorders(void);
- // This function prints a full horizontal line of '#' at a given row.
- void printHorizontalBorder(int row);
- // This function prints a full vertical line of '#' at a given col.
- void printVerticalBorder(int col);
- //This function inits a snake object in a desired position and returns it.
- Snake* initSnakeObject(int x, int y);
- //This function creates a new cell.
- Snake_Cell* createNewSnakeCell(int x, int y);
- //This function inserts a cell into the snake.
- void insertCellToSnake(Snake* snake, Snake_Cell* newCellToAdd);
- // This function removes the tail from the snake.
- void removeTailFromSnake(Snake* snake);
- // This function checks if the snake's head collided with a border.
- bool isSnakeCollisionedWithBorder(Snake* snake);
- // This function checks if the snake collisoned with himself.
- bool isSnakeCollisionedWithItself(Snake* snake, Snake_Cell* futureCell);
- // This function prints the whole snake!
- void printSnake(Snake* snake);
- //This function gets the time since the snake's newest cell was created.
- double getTimeSinceLastCreatedCell(clock_t currentClock);
- // This function prints that the user has lost the game.
- void printGameOver(void);
- //This function gets a row and col and returns the updated data depending on the user's direction input.
- void updateRowAndCol(int* row, int* col, char keyPressed);
- #pragma endregion
- #pragma region Main
- int main()
- {
- initBoard();
- playGame();
- return 0;
- }
- #pragma endregion
- #pragma region Implementations
- void clearScreen(void) {
- system("cls");
- }
- void initBoard() {
- clearScreen();
- printBoardBorders();
- }
- void playGame(void) {
- char c = 'd', prevC = 'd';
- int row = ROWS / 2, col = COLS / 2;
- clock_t secondsTimer;
- double timeSpent;
- secondsTimer = clock();
- Snake* snake = initSnakeObject(row, col);
- Snake_Cell* NewSnakeCellToAdd;
- while (!isSnakeCollisionedWithBorder(snake)) {
- printSnake(snake);
- sleep(SLEEP_TIMER);
- if (_kbhit())
- c = _getch();
- if (c != 'd' && c != 'a' && c != 'w' && c != 's')
- c = prevC;
- updateRowAndCol(&row, &col, c);
- prevC = c;
- NewSnakeCellToAdd = createNewSnakeCell(row, col);
- if (isSnakeCollisionedWithItself(snake, NewSnakeCellToAdd)) {
- break;
- }
- insertCellToSnake(snake, NewSnakeCellToAdd);
- if (snake->count > 1) {
- removeTailFromSnake(snake);
- }
- timeSpent = getTimeSinceLastCreatedCell(secondsTimer);
- if (timeSpent >= CREATE_NEW_CELL_TIMER) {
- updateRowAndCol(&row, &col, c);
- NewSnakeCellToAdd = createNewSnakeCell(row, col);
- insertCellToSnake(snake, NewSnakeCellToAdd);
- timeSpent = 0.0;
- secondsTimer = clock();
- }
- }
- printGameOver();
- }
- void gotoxy(int x, int y) {
- printf("\x1b[%d;%df", x, y);
- }
- void sleep(float secs) {
- clock_t clocks_start = clock();
- while (clock() - clocks_start < secs*CLOCKS_PER_SEC);
- }
- void gotoxyAndPrint(int row, int col, char sign) {
- gotoxy(row, col);
- printf("%c", sign);
- }
- void printBoardBorders(void) {
- printHorizontalBorder(0);
- printVerticalBorder(0);
- printHorizontalBorder(ROWS);
- printVerticalBorder(COLS);
- }
- void printHorizontalBorder(int row) {
- int i;
- for (i = 0; i < COLS; i++) {
- gotoxyAndPrint(row, i, BORDER_SIGN);
- }
- }
- void printVerticalBorder(int col) {
- int i;
- for (i = 0; i < ROWS; i++) {
- gotoxyAndPrint(i, col, BORDER_SIGN);
- }
- }
- Snake* initSnakeObject(int x, int y)
- {
- Snake_Cell* snakeHead = createNewSnakeCell(x, y);
- Snake* snake = (Snake*)malloc(sizeof(Snake));
- snake->head = snakeHead;
- snake->tail = snakeHead;
- snake->count = 1;
- return snake;
- }
- Snake_Cell* createNewSnakeCell(int x, int y)
- {
- Snake_Cell* newCell = (Snake_Cell*)malloc(sizeof(Snake_Cell));
- newCell->x = x;
- newCell->y = y;
- newCell->next = NULL;
- return newCell;
- }
- void insertCellToSnake(Snake * snake, Snake_Cell * newCellToAdd) {
- Snake_Cell* currentHead = snake->head;
- snake->head = newCellToAdd;
- newCellToAdd->next = currentHead;
- snake->count++;
- }
- void removeTailFromSnake(Snake * snake) {
- Snake_Cell* currentCell = snake->head;
- Snake_Cell* snakeTail = snake->tail;
- while (currentCell->next != snake->tail)
- currentCell = currentCell->next;
- gotoxyAndPrint(snakeTail->x, snakeTail->y, ' ');
- free(snakeTail);
- currentCell->next = NULL;
- snake->tail = currentCell;
- snake->count--;
- }
- bool isSnakeCollisionedWithBorder(Snake * snake) {
- if (snake->head->x == ROWS || snake->head->x == 1 ||
- snake->head->y == COLS || snake->head->y == 1)
- return true;
- return false;
- }
- bool isSnakeCollisionedWithItself(Snake * snake, Snake_Cell* futureCell) {
- Snake_Cell* currentCell = snake->head;
- while (currentCell != NULL) {
- if (futureCell->x == currentCell->x && futureCell->y == currentCell->y)
- return true;
- currentCell = currentCell->next;
- }
- return false;
- }
- void printSnake(Snake* snake) {
- Snake_Cell* head = snake->head;
- while (head != NULL) {
- gotoxyAndPrint(head->x, head->y, SNAKE_SIGN);
- head = head->next;
- }
- }
- double getTimeSinceLastCreatedCell(clock_t curretTimer) {
- return (double)(clock() - curretTimer) / CLOCKS_PER_SEC;
- }
- void printGameOver(void) {
- clearScreen();
- printBoardBorders();
- gotoxy(ROWS / 2, COLS / 2 - 3);
- printf("GAME OVER!");
- gotoxy(ROWS + 1, 0);
- }
- void updateRowAndCol(int * row, int * col, char keyPressed) {
- switch (keyPressed)
- {
- case 'd':
- *col += 1;
- break;
- case 'a':
- *col -= 1;
- break;
- case 'w':
- *row -= 1;
- break;
- case 's':
- *row += 1;
- break;
- }
- }
- #pragma endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement