Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
CAN YOU HELP ME WITH MAKING THE SNAKE MOVEMENT LOGIC MORE REFINED INSTEAD USING RANDOM DIRECTIONS I WANT IT TO USE A LOGIC TO CHASE THE PLAYER AND ALSO CAN YOU MAKE IT LOOK LIKE THE OUTPUT OF THIS PICTURE?
GAME.C:
#include "game.h"
#include
// Reads the map from the file and initializes the game state
void readMap(const char *filename, GameState *game) {
FILE*file=fopen(filename, "r");
if (!file) {
perror("Error opening map file");
exit(EXIT_FAILURE);
}
intnumRows, numCols, numWalls;
fscanf(file, "%d%d", &numRows, &numCols);
fscanf(file, "%d", &numWalls);
for (int i =0; i < MAX_ROWS; i++) {
for (int j =0; j < MAX_COLS; j++) {
int manhattanDistance =abs(game->playerPosition.x- i) +abs(game->playerPosition.y- j);
if (manhattanDistance <= visionRadius) {
if (i ==game->playerPosition.x&& j ==game->playerPosition.y) {
printf("P");
} else if (i == game->snakePosition.x && j == game->snakePosition.y) {
printf("~");
} else {
switch (game->map[i][j]) {
case0: printf("."); break; // Empty space
case1: printf("O"); break; // Wall
case2: printf("@"); break; // Lantern
case5: printf("$"); break; // Treasure
default: printf("?"); break;
}
}
} else {
printf(" "); // Print empty space for areas outside of vision
}
}
printf("\n");
}
#else
// Standard mode: print everything
for (inti=0; iplayerPosition.x&&j==game->playerPosition.y) {
printf("P");
} else if (i == game->snakePosition.x && j == game->snakePosition.y) {
printf("~");
} else {
switch (game->map[i][j]) {
case0: printf(" "); break;
case1: printf("O"); break;
case2: printf("@"); break;
case5: printf("$"); break;
default: printf("?"); break;
}
}
}
printf("\n");
}
#endif
}
// Handles player input and undo functionality
void handleInput(GameState *game, Node **undoStack, int *isGameOver) {
charinput=getchar();
intmoveMade=0;
switch (input) {
case'w': case'a': case's': case'd':
pushState(game, undoStack); // Save the current state for undo
movePlayer(input, game); // Move the player
moveSnake(game); // Move the snake after player moves
moveMade=1;
break;
case'u':
undoLastMove(game, undoStack);
moveMade=1;
break;
default:
printf("Invalid input! Use 'w', 'a', 's', 'd' for movement or 'u' for undo.\n");
break;
}
// Check for winning or losing conditions
if (moveMade) {
if (game->playerPosition.x==game->treasurePosition.x&&game->playerPosition.y==game->treasurePosition.y) {
system("clear");
printf("You win! You found the treasure!\n");
*isGameOver=1;
}
if (game->playerPosition.x==game->snakePosition.x&&game->playerPosition.y==game->snakePosition.y) {
system("clear");
printf("You lose! The snake caught you!\n");
*isGameOver=1;
}
}
}
// Moves the player according to the direction input ('w', 'a', 's', 'd')
void movePlayer(char direction, GameState *game) {
Position*player=&game->playerPosition;
intvalidMove=0;
switch (direction) {
case'w': if (player->x>0&&game->map[player->x-1][player->y] !=1) { player->x--; validMove=1; } break;
case's': if (player->xmap[player->x+1][player->y] !=1) { player->x++; validMove=1; } break;
case'a': if (player->y>0&&game->map[player->x][player->y-1] !=1) { player->y--; validMove=1; } break;
case'd': if (player->ymap[player->x][player->y+1] !=1) { player->y++; validMove=1; } break;
}
// Check if player has picked up the lantern
if (validMove&&player->x==game->lanternPosition.x&&player->y==game->lanternPosition.y) {
game->hasLantern=1;
game->map[player->x][player->y] =0; // Remove lantern from map
}
}
// Moves the snake randomly in 8 possible directions
void moveSnake(GameState *game) {
Position*snake=&game->snakePosition;
intdirection=randomDirection(); // Generates a random direction (0-7)
switch (direction) {
case0: if (snake->x>0&&game->map[snake->x-1][snake->y] !=1) snake->x--; break; // up
case1: if (snake->xmap[snake->x+1][snake->y] !=1) snake->x++; break; // down
case2: if (snake->y>0&&game->map[snake->x][snake->y-1] !=1) snake->y--; break; // left
case3: if (snake->ymap[snake->x][snake->y+1] !=1) snake->y++; break; // right
case4: if (snake->x>0&&snake->y>0&&game->map[snake->x-1][snake->y-1] !=1) { snake->x--; snake->y--; } break; // up-left
case5: if (snake->x>0&&snake->ymap[snake->x-1][snake->y+1] !=1) { snake->x--; snake->y++; } break; // up-right
case6: if (snake->xy>0&&game->map[snake->x+1][snake->y-1] !=1) { snake->x++; snake->y--; } break; // down-left
case7: if (snake->xymap[snake->x+1][snake->y+1] !=1) { snake->x++; snake->y++; } break; // down-right
}
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by stepSolved in 2 steps
Knowledge Booster
Similar questions
- Java : Create a .txt file with 3 rows of various movies data of your choice in the following table format: Movie ID number of viewers rating release year Movie name 0000012211 174 8.4 2017 "Star Wars: The Last Jedi" 0000122110 369 7.9 2017 "Thor: Ragnarok" Create a class Movie which instantiates variables corresponds to the table columns. Implement getters, setters, constructors and toString. Implement 2 readData() methods : the first one will return an array of Movies and the second one will return a List of movies . Test your methods.arrow_forwardcreateDatabaseOfProfiles(String filename) This method creates and populates the database array with the profiles from the input file (profile.txt) filename parameter. Each profile includes a persons' name and two DNA sequences. 1. Reads the number of profiles from the input file AND create the database array to hold that number profiles. 2. Reads the profiles from the input file. 3. For each person in the file 1. creates a Profile object with the information from file (see input file format below). 2. insert the newly created profile into the next position in the database array (instance variable).arrow_forwardyou have been asked by a computer gaming company to create a role-playing game, commonly known as an rpg. the theme of this game will be of the user trying to get a treasure that is being guarded the user will choose a character from a list you create create a character class, in a file named character.h, which will: have the following variables: name race (chosen from a list that you create, like knight, wizard, elf, etc.) weapon (chosen from a list that you create) spells (true meaning has the power to cast spells on others) anything else you want to add the treasure will be hidden in one of the rooms in a castle create a castle class, in a file named castle.h, that will: have these variables: at least four rooms named room1, room2, etc. moat (which is a lagoon surrounding a castle) which boolean (not all castles have them) anything else you want to add create a default constructor for each class that initializes each argument to a blank or false value create a second…arrow_forward
- 1. Create a file that contains 20 integers or download the attached file twenty-integers.txttwenty-integers.txt reads:12 20 13 45 67 90 100 34 56 89 33 44 66 77 88 99 20 69 45 20 Create a program that: 2. Declares a c-style string array that will store a filename. 3. Prompts the user to enter a filename. Store the file name declared in the c-string. 4. Opens the file. Write code to check the file state. If the file fails to open, display a message and exit the program. 5. Declare an array that holds 20 integers. Read the 20 integers from the file into the array. 6. Write a function that accepts the filled array as a parameter and determines the MAXIMUM value in the array. Return the maximum value from the function (the function will be of type int). 7. Print ALL the array values AND print the maximum value in the array using a range-based for loop. Use informational messages. Ensure the output is readable.arrow_forwardUsing C# in Microsoft Visual Studio create an application that grades the written portion of the driver’s license exam. The exam has 20 multiple-choice questions. (The correct answers are provided in the screenshot.) the program should store these correct answers in an array. The program should read the student’s answers for each of the 20 questions from a text file and store the answers in another array. (Create your own text file to test the application.) After the student’s answers have been read from the file, the program should display a message indicating whether the student passed or failed the exam. (A student must correctly answer 15 of the 20 questions to pass the exam.) It should then display the total number of correctly answered questions, the total number of incorrectly answered questions, and a list showing the question numbers of the incorrectly answered questions.arrow_forwardCreate a .txt file with 3 rows of various movies data of your choice in the following table format: Movie ID number of viewers rating release year Movie name 0000012211 174 8.4 2017 "Star Wars: The Last Jedi" 0000122110 369 7.9 2017 "Thor: Ragnarok" Create a class Movie which instantiates variables corresponds to the table columns. Implement getters, setters, constructors and toString. ----Important 1. Implement two search methods to search Movies by their rating from unsorted array and by year (first) and a rating (second) from unsorted List. Test your methods and explain their time complexity.arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education