Итак, я создаю игровую программу-лабиринт, но для того, чтобы я смог начать писать код, я должен создать make-файл, который компилирует и связывает файлы mazes.c и mazeDisplay.c в исполняемый файл с именемлабиринты. Makefile должен позволять командам make all и make clean работать правильно, и я также должен включить что-то, называемое библиотекой -lX11, чтобы код правильно связывался.
Я пытался скомпилироватьмоя программа и создать мой make-файл за последние 2 дня, и я ничего не могу заставить работать. Мой компилятор, похоже, не распознает указатель. Я продолжаю получать сообщение об ошибке «ошибка: ожидается»; «,« или ») до« * »токена Graph * computeGraph (char maze [HEIGHT] [WIDTH]) {", и я начинаю очень беспокоиться какмое задание скоро должно быть, и я не могу даже скомпилировать исходные программные файлы, которые мне нужны для запуска моей задачи из-за этой проблемы с make-файлом. Пожалуйста, любая помощь будет оценена. Я схожу с ума, пытаясь выполнить простую задачу, и я просто не могу понять, в чем проблема. Что-то не так с моими инструкциями для make-файла?
Makefile:
GCC = gcc
all: mazes.o mazeDisplay.o
$(GCC) -o mazes mazes.o mazeDisplay.o -lX11
mazes.o: mazes.c mazeDisplay.h
$(GCC) -c mazes.c
mazeDisplay.o: mazeDisplay.c mazeDisplay.h
$(GCC) -c mazeDisplay.c
clean:
rm -f *.o mazes
CODE:
mazes.c:
#include <stdio.h>
#include <stdlib.h>
#include "graphSet.h"
#include "mazeDisplay.h"
// Compute the graph for the given maze and add it to the given graph set.
Graph *computeGraph(char maze[HEIGHT][WIDTH]) {
// Create the initially-empty graph
// Find a starting node, then trace out the maze recursively. A starting node can be
// found by searching from top to bottom, left to right, for a non-wall maze location.
// You MUST NOT hard-code this start location ... it must be determined by your code.
// To trace out the maze recursively, you will likely want to create a recursive
// procedure that is called by this one. It should take parameters to indicate
// the location in the maze to start tracing from, the maze itself and also the node
// that led to this node (i.e., the previous one in the tree that led here). If you
// start with the root node, then the previous node should be NULL.
//
// As you go through the maze, make sure to mark off maze locations that you have
// visited (perhaps by putting a '2' character at that location) so that you do not
// go back to that location again and end up with infinite recursion. So you can
// stop the recursion when you reach a wall (i.e., a '0' character in the maze) or a
// '2' character. A '1' character means that it is a free space that you just arrived
// at for the first time. Make sure to check recursively in all directions. In my
// solutions (shown on the assignment), I used an ordering of up/down/left/right. So
// if you want solutions to look like mine, you should follow that ordering as well.
//
// As you traverse the maze, make sure to connect the previous node to the current one.
// You'll have to check which direction you can from (i.e., x and y values) so that
// you know whether it is the up/down/left or right pointer to set.
// You need not do this recursively, but it will lkely be a lot harder to do it non-
// recursively.
return NULL; // Remove this line when you write your code
}
// This procedure must clean up graph by removing all nodes in which the previous and
// next nodes have the same x or y value as it.
void cleanUpGraph(Node *n, Node *previousNode) {
}
// This is where it all begins
int main() {
char mazes[5][HEIGHT][WIDTH] = {
{"111111111111111111111",
"100000001000100000001",
"101111111010111011111",
"100000000010000010001",
"101110111111101110111",
"100010001000000000001",
"111011111111111110111",
"101010001000100000001",
"101110111011101011101",
"100010000000001010001",
"101010111011111111111",
"101000001000100000001",
"101111111110101111101",
"100010100000100000101",
"111110101110101111101",
"100010001000000010101",
"101010111111111010111",
"101010001000000010001",
"101111111010111011101",
"100000000010001000001",
"111111111111111111111"},
{"111111111111111111111",
"100000000000000000001",
"101111111111111111111",
"100000000000000000001",
"101111111111111111111",
"100000000000000000001",
"111111111111111111101",
"100000000000000000001",
"101111111111111111111",
"100000000000000000001",
"111111111111111111101",
"100000000000000000001",
"101111111111111111111",
"101111111111111111101",
"101111111111111111101",
"101000100010001000101",
"101010101010101010101",
"101010101010101010101",
"101010101010101010101",
"100010001000100010001",
"111111111111111111111"},
{"111111111111111111111",
"100000000000000000001",
"101010101010101010101",
"100000000000000000001",
"101110111011101110111",
"100000000000000000001",
"101111101111101111101",
"100000000000000000001",
"101111111001111111101",
"100000000000000000001",
"101111111111111111101",
"100111111111111111001",
"100011111111111110001",
"100001111111111100001",
"100000111111111000001",
"100000011111110000001",
"100000001111100000001",
"100000000111000000001",
"100000000010000000001",
"100000000000000000001",
"111111111111111111111"},
{"111111111111111111111",
"111111111111111111111",
"111111111111111111111",
"111111111111111111111",
"111111111111111111111",
"111111111111111111111",
"111111111111111111111",
"111111111111111111111",
"111111111111111111111",
"111111111111111111111",
"111111111110111111111",
"111111111111111111111",
"111111111111111111111",
"111111111111111111111",
"111111111111111111111",
"111111111111111111111",
"111111111111111111111",
"111111111111111111111",
"111111111111111111111",
"111111111111111111111",
"111111111111111111111"},
{"111111111111111111111",
"111100000000000000001",
"111000000000000000001",
"100000000000000000001",
"100000000000000000001",
"100000000000000000001",
"100000000000000000001",
"100000000000000000001",
"100000000000000000001",
"100000000000000000001",
"100000000000000000001",
"100000000000000000001",
"100000000000000000001",
"100000000000000000001",
"100000000000000000001",
"100000000000000000001",
"100000000000000000001",
"100000000000000000001",
"100000000000000000001",
"100000000000000000001",
"111111111111111111111"}};
// Open a display window
openDisplayWindow();
// Allocate a GraphSet to store the graphs for each maze
GraphSet *gSet;
// Compute the graphs for each maze and add them to a Graph Set
for (int i=0; i<5; i++) {
Graph *g = computeGraph(mazes[i]);
// Add g to gSet properly
// ...
}
// Show the graphs
Graph *g; // ... set this to the first graph in gSet ...
for (int i=0; i<5; i++) {
drawMaze(mazes[i]); // Draw the maze
// drawGraph(g->rootNode); // Draw the graph
getchar(); // Wait for user to press enter
// cleanUpGraph(g->rootNode, NULL); // Clean up the graph
// drawMaze(mazes[i]);
// drawGraph(g->rootNode);
// ... get the next graph in the set ...
// ... INSERT A LINE OF CODE HERE ...
getchar(); // Wait again for the user to press ENTER before going on to the next maze
}
// Free up all allocated memory
// ...
// Close the display window
closeDisplayWindow();
}
mazeDisplay.c:
#include <stdio.h>
#include <X11/Xlib.h>
#include <unistd.h>
#include "graphSet.h"
#include "mazeDisplay.h"
#define SCALE 25
// These are display-related variables
Display *display;
Window win;
GC gc;
// Draw the Maze on the window.
void drawMaze(char grid[WIDTH][HEIGHT]) {
// First erase background
XSetForeground(display, gc, 0xFFFFFF);
XFillRectangle(display, win, gc, 0, 0, 750, 750);
XFlush(display);
// Draw the grid maze
for (int y=0; y<WIDTH; y++) {
for (int x=0; x<HEIGHT; x++) {
if (grid[y][x] == '1')
XSetForeground(display, gc, 0x333333);
else
XSetForeground(display, gc, 0xFFFFFF);
XFillRectangle(display, win, gc, x*SCALE, y*SCALE, SCALE, SCALE);
}
}
XFlush(display);
}
// Draws an edge with the given color (e.g., 0x0000FF is blue)
// from cell (c1, r1) to cell (c2, r2) of the maze
void drawEdgeWithColor(int c1, int r1, int c2, int r2, int color) {
XSetForeground(display, gc, color);
XDrawLine(display, win, gc, c1*SCALE + SCALE/2, r1*SCALE + SCALE/2, c2*SCALE + SCALE/2, r2*SCALE + SCALE/2);
XFlush(display);
}
// Draws a node with the given color (e.g., 0x0000FF is blue)
// centered at the given cell (c1, r1) of the maze.
void drawNodeWithColor(int c1, int r1, int color) {
XSetForeground(display, gc, color);
XFillArc(display, win, gc,
c1*SCALE-SCALE/4 + SCALE/2,
r1*SCALE-SCALE/4 + SCALE/2,
11, 11, 0, 360*64);
XFlush(display);
}
// Draw a single graph starting at the given root node n.
void drawGraph(Node *n) {
// Quit recursion if there are no Nodes
if (n == NULL)
return;
// Recursively draw in each direction. Draw the edges before the recursive call so that
// vertices are drawn on top of the edges
if (n->up != NULL) {
drawEdgeWithColor(n->x, n->y, n->up->x, n->up->y, 0x0000FF);
drawGraph(n->up);
}
if (n->down != NULL) {
drawEdgeWithColor(n->x, n->y, n->down->x, n->down->y, 0x0000FF);
drawGraph(n->down);
}
if (n->left != NULL) {
drawEdgeWithColor(n->x, n->y, n->left->x, n->left->y, 0x0000FF);
drawGraph(n->left);
}
if (n->right != NULL) {
drawEdgeWithColor(n->x, n->y, n->right->x, n->right->y, 0x0000FF);
drawGraph(n->right);
}
drawNodeWithColor(n->x, n->y, 0xFF0000);
}
// Open a display window
int openDisplayWindow() {
// Opens connection to X server
display = XOpenDisplay(NULL);
// Create a simple window
win = XCreateSimpleWindow(display, // our connection to server
RootWindow(display, 0), // parent window (none in this example)
0, 0, // x,y (w.r.t. parent ... ignored here)
WIDTH*25,HEIGHT*25, // width, height
0, // border width
0x000000, // border color (ignored in this example)
0xFFFFFF); // background color = WHITE
// Set the name of the window
XStoreName(display, win, "Maze Displayer");
// Get the graphics context
gc = XCreateGC(display, win, 0, NULL);
// Make it visible
XMapWindow(display, win);
XFlush(display);
usleep(20000); // sleep for 20 milliseconds.
}
// Close the display window
int closeDisplayWindow() {
// Clean up and close the window
XFreeGC(display, gc);
XUnmapWindow(display, win);
XDestroyWindow(display, win);
XCloseDisplay(display);
}
mazeDisplay.h:
#include <X11/Xlib.h>
#define WIDTH 21
#define HEIGHT 21
// Draw the maze on the window.
extern void drawMaze(char maze[WIDTH][HEIGHT]);
// Draw the graph on the window.
extern void drawGraph(Node *firstNodeOfGraph);
// Open a display window
extern int openDisplayWindow();
// Close a display window
extern int closeDisplayWindow();
// Draws a node with the given color (e.g., 0x0000FF is blue)
// centered at the given cell (c1, r1) of the maze.
extern void drawNodeWithColor(int c1, int r1, int color);
// Draws an edge with the given color (e.g., 0x0000FF is blue)
// from cell (c1, r1) to cell (c2, r2) of the maze
extern void drawEdgeWithColor(int c1, int r1, int c2, int r2, int color);
graphSet.h:
// This struct represents a single intersection/Node in a maze. It keeps track
// of the x(i.e., column) and y (i.e. row) of the intersection in the maze
// as well as the Nodes in all 4 directions around it). NULL is used to
// indicate that no Node is beside it in a particular direction.
typedef struct nd {
int x;
int y;
struct nd *up;
struct nd *down;
struct nd *left;
struct nd *right;
} Node;
// This struct represents a single maze graph
typedef struct gr {
Node *rootNode;
struct gr *nextGraph;
} Graph;
// This struct represents a set of maze graphs
typedef struct {
Graph *firstGraph;
Graph *lastGraph;
} GraphSet;
Журнал для марки:
student@COMP2401-F19:~/Desktop/Mazes$ make
gcc -c mazes.c
mazes.c:11:7: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token
Graph *computeGraph(char maze[HEIGHT][WIDTH]) {
makefile:7: recipe for target 'mazes' failed
make: *** [mazes.o] Error 1