1
0
UAHCode/CPE455/gameproject/game.c

433 lines
22 KiB
C
Raw Normal View History

2022-08-28 21:12:16 +00:00
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
extern WINDOW* initscr(void);
extern int start_color(void);
extern int noecho(void);
/* LDRA_EXCLUDE RULE VIOLATION <justification start> FALSE POSITIVE -- why <justification end> */
int main(int argc, char *argv[])
{
FILE* mazeFile; /* Maze Input File */
unsigned int row = 0; /* Holds number of rows for input from file */
unsigned int col = 0; /* Holds number of cols for input from file */
unsigned int pcol = 0;
unsigned int prow = 0;
char shold[2000]; /* Holds lines for input from file */
unsigned int l = 0;
unsigned int k = 0;
char **maze; /* array for maze */
unsigned int c1 = 0;
unsigned int c2 = 0;
unsigned int i = 0;
unsigned int j = 0;
unsigned int x = 0;
unsigned int y = 0;
int deltaRow, deltaCol; /* Change in row-column position based on key input */
unsigned int score = 0;
WINDOW* W = 0; /* Curses Window handle */
unsigned int rowLimit,colLimit; /* Num of rows and columns in terminal window */
/* Initialize ncurses -- you will want to use the following settings */
/* check to see if argument */
if (argc != 2)
{
/*printw("An error has ocurred no input file was given.\n");*/
return 1;
}
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to open the file so must use bad one bc we cannot create one ourselves as we are not wizards <justification end> */
mazeFile = fopen(argv[1], "r"); /* open file */
if (mazeFile == NULL)
{
/*printw("Error in opening the file.\n");*/
return 1;
}
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to get first char from file and we must use bad one as we cannot make one ourselves <justification end> */
fgets(shold, 2, mazeFile);
while ( feof(mazeFile) == 0 )
{
/* parse maze file,
adding chars to array */
switch(shold[0])
{
case 'M':
/* read array values, dynamically allocate array */
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
fscanf(mazeFile, "%3u %3u", &row, &col);
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to dynamially allocate memory cannot create own as we lack experience <justification end> */
maze = (char**)malloc(row * 8); /* allocate memory for rows of array, 8 reperesents size of char* */
for(i = (unsigned) 0; i < row; i++)
{
if (maze==NULL)
{
break;
}
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to dynamially allocate memory cannot create own as we lack experience <justification end> */
maze[i] = malloc(col * 8); /* allocate memory for collumns of array, 8 reperesents size of char* */
}
for (x = (unsigned) 0; x < row; x++)
{
for (j = (unsigned) 0; j < col; j++)
{
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
fscanf(mazeFile, "%c", &maze[x][j]);
if (maze[x][j] == '\n')
{
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
fscanf(mazeFile, "%c", &maze[x][j]);
}
}
}
break;
case 'P':
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
fscanf(mazeFile, "%3u %3u", &k, &l);
/* check maze bounds */
if(k>= row || l>= col){
break;
}
maze[k][l] = 'P';
break;
case 'Z':
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
fscanf(mazeFile, "%3u %3u", &k, &l);
/* check maze bounds */
if(k>= row || l>= col){
break;
}
maze[k][l] = 'Z';
break;
case 'T':
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
fscanf(mazeFile, "%3u %3u", &k, &l);
/* check maze bounds */
if(k>= row || l>= col){
break;
}
maze[k][l] = 'T';
break;
default:
break;
};
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
fgets(shold, 2, mazeFile);
if (feof(mazeFile) != 0)
{
break;
}
}
rowLimit = row;
colLimit = col;
fclose(mazeFile);
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
W = initscr(); /* Determine terminal type and initialize curses data structures */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
start_color(); /* Enable use of color with ncurses */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
noecho(); /* Disable echo printing of chars type by user */
if (W == NULL)
{
return 0;
}
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
nodelay(W, true); /* Make getch( ) a non-blocking call */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
keypad(W, true); /* Enable keypad and arrow keys */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
curs_set(0); /* Set cursor to be invisible - 0 */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
/* Define color pallete foreground-background color pairs */
/* PairID#, Foreground Color, Background Color */
/* Foreground color == Background color ==> solid color block */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
/* start ncurses coloring of maze */
for (c1 =(unsigned)0; c1 < row; c1++)
{
for (c2=(unsigned)0; c2 < col; c2++)
{
if (maze==NULL)
{
break;
}
/* parse maze */
switch(maze[c1][c2])
{
/* color wall green */
case '#':
/* read array values, dynamically allocate array*/
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
attron(COLOR_PAIR(2));
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
wmove(W, c1,c2);
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
waddch(W,'#');
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
attroff(COLOR_PAIR(2));
break;
/* place space at position specified in array */
case ' ':
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
wmove(W, c1,c2);
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
waddch(W,' ');
break;
/* place player at position specified in file */
case 'P':
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
wmove(W, c1,c2);
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
waddch(W,'P');
prow=c1;
pcol=c2;
break;
/* place zombie at position specified in file */
case 'Z':
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
attron(COLOR_PAIR(4));
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
wmove(W, c1,c2);
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
waddch(W,'Z');
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
attroff(COLOR_PAIR(4));
break;
/* place treasure at position specified in file */
case 'T':
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
attron(COLOR_PAIR(5));
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
wmove(W, c1,c2);
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
waddch(W,'T');
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
attroff(COLOR_PAIR(5));
break;
default:
break;
};
}
}
deltaRow = 0;
deltaCol = 0;
do{
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
int kb = wgetch(W); /* Grab keypress */
/* Support player movement via WASD, IJKL, and arrow keys */
if (kb == (int)'a' || kb == (int)'j' || kb == KEY_LEFT)
{
/* Move Left */
deltaRow =0;
deltaCol =-1;
}
else if (kb == (int)'d' || kb == (int)'l' || kb == KEY_RIGHT)
{
/* Move Right */
deltaRow =0;
deltaCol =1;
}
else if (kb == (int)'w' || kb == (int)'i' || kb == KEY_UP)
{
/* Move Up */
deltaRow =-1;
deltaCol =0;
}
else if (kb == (int)'s' || kb == (int)'k' || kb == KEY_DOWN)
{
/* Move Down */
deltaRow =1;
deltaCol =0;
}
else if (kb == (int)'q')
{
/* q to exit player movement loop */
break;
};
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
attron(COLOR_PAIR(1));
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
wmove(W,prow,pcol);
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
waddch(W, 'P');
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
attroff(COLOR_PAIR(1));
/* check current player position against maze bounds,
if over the bounds, don't move */
if(((prow + (unsigned int)deltaRow) > row) || ((pcol + (unsigned int)deltaCol) > col)){
deltaRow = 0;
deltaCol = 0;
}
/* check future player position to see if wall;
if it is, stay in the same spot,
else move to new postion */
if(maze[((int)prow + deltaRow)][((int)pcol + deltaCol)] == '#'){
prow = prow;
pcol = pcol;
}
else{
prow = prow + (unsigned int)deltaRow;
pcol = pcol + (unsigned int)deltaCol;
}
/* check curremt position for treasure,
make treasure position a space,
and increment score */
if(maze[prow][pcol] == 'T')
{
maze[prow][pcol] = ' ';
score++;
}
/* check curremt position for zombie,
end game */
if(maze[prow][pcol] == 'Z')
{
break;
}
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
attron(COLOR_PAIR(3));
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
wmove(W,prow,pcol);
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
waddch(W, 'P');
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
attroff(COLOR_PAIR(3));
deltaRow =0;
deltaCol =0;
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
wrefresh(W); /* Refresh ncurses window */
} while (true); /* Repeat until user selects q to quit */
/* free the memory */
for(y = (unsigned)0; y < row; y++)
{
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to free allocated memory, cannot create one ourselves bc we are not wizards <justification end> */
free(maze[y]);
maze[y] = NULL;
}
if(maze != NULL){
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to free allocated memory, cannot create one ourselves bc we are not wizards <justification end> */
free(maze);
}
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
endwin();
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to print score for project, so even though not great have to use bad one <justification end> */
printf("Score = %u\n", score);
return 0;
/* LDRA_EXCLUDE 50 D <justification start> FALSE POSITIVE -- Y was statically allocated no need to free it <justification end> */
/* LDRA_EXCLUDE 50 D <justification start> FALSE POSITIVE -- Maze has been freed already valgrind shows no leaks error must have ocurred in analysis <justification end> */
}