Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <cstdlib>
- typedef struct SnakePiece {
- int x;
- int y;
- SnakePiece *next;
- } SnakePiece;
- void initialize_board (char **board, int width, int height) {
- for (int i = 0; i < width; i++) {
- board[0][i] = '#';
- board[width-1][i] = '#';
- }
- for (int i = 0; i < height; i++) {
- board[i][0] = '#';
- board[i][height-1] = '#';
- }
- for (int i = 1; i < height-1; i++) {
- for (int j = 1; j < width-1; j++) {
- board[i][j] = ' ';
- }
- }
- }
- void show_board (char **board, int width, int height) {
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- printf ("%c", board[i][j]);
- }
- printf ("\n");
- }
- }
- void initialize_snake (SnakePiece *head, int width, int height) {
- head->x = (width/2)-1;
- head->y = (height/2)-1;
- for (int i = 1; i <= 4; i++) {
- SnakePiece *piece = (SnakePiece *) malloc (sizeof(SnakePiece));
- piece->x = head->x;
- piece->y = (head->y)+i;
- piece->next = head;
- head = piece;
- }
- }
- void show_snake (SnakePiece *head, char **board) {
- SnakePiece *ptr;
- ptr = head;
- while (ptr != NULL) {
- printf ("o", board[ptr->y][ptr->x]);
- ptr = ptr->next;
- }
- }
- void generate_fruit (char **board, int width, int height) {
- int fruitx, fruity;
- fruitx = rand() % width;
- fruity = rand() % height;
- board[fruity][fruitx] = 'x';
- }
- int main()
- {
- char **board;
- int width, height;
- SnakePiece *head;
- scanf("%d", &width, &height);
- board = (char **) malloc (width * sizeof(char *));
- for (int i = 0; i < width; i++) {
- board[i] = (char *) malloc (height * sizeof(char));
- }
- initialize_board(board, width, height);
- bool fruit_eaten = 1;
- while (true) {
- if (fruit_eaten) {
- generate_fruit(board, width, height);
- fruit_eaten = 0;
- }
- //initialize_snake(head, width, height);
- //show_snake(head, board4);
- show_board(board, width, height);
- system ("cls");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement