The A-Mazing Race
The A-Mazing Race
LAB # 7
SECTION # B
FULL NAME
Clark Reimers
SUBMISSION DATE:
04/25/18
DATE: 04/25/18
Problem
We are tasked with creating a randomly generated maze that can be more difficult with higher
set levels and we must be able to navigate an avatar through said maze whether it can be completed or
not. We will need dualshocks for this as well as Ncurses
Analysis
We will need to analyze the data from the dualshocks to decide what inputs we are given and
will need to use to interact with the maze. We will also need to decide how to use this data to create a
moving average in order to move the avatar. You must not be able to move through walls and you
should lose if you get into an entrapment you cannot get out of.
Design
We will need several functions and a do while loop to complete this lap. One function will gather
gyroscopic data from the dualshock to be used as a control for the avatar. We will also need a function
to generate the maze and draw the avatar as well as drop it down every few seconds. We will also need
a function to define how the avatar should interact with the maze walls and to define what it takes to
complete the maze.
Testing
Recursion is something that would be nice to use here. Break the big problem up into little
problems and piece them together, I started with drawing the maze and then the avatar. After that I
needed to figure out how to move the avatar through the maze. Once this was all figured out I could at
least see what was happening graphically if I was to run my code so this might it easier to see what
would need to be changed each time I encountered a problem, such as moving through walls and
moving through walls diagonally as well as not being able to lose if the avatar got trapped and seeing
how hard the maze got when the argument vector was changed (this can be noted where figure 1 is
difficulty 3 and figure 2 is difficulty 5) with testing along each new feature that was added.
Comments
See answers to questions at the very bottom
Source Code
/*-----------------------------------------------------------------------------
- SE 185 Lab 07
- Section: B
- NetID:Creimers
- Date:04/25/18
-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------
- Includes
-----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ncurses/ncurses.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
/*-----------------------------------------------------------------------------
- Defines
-----------------------------------------------------------------------------*/
/* Mathmatical constants */
#define PI 3.14159
/* Screen geometry
Use NUMROWS and NUMCOLS for the screen height and width (set by system)
MAXIMUMS */
#define NUMROWS 80
/*parameter definitions*/
#define TRUE 1
#define FALSE 0
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
#define NONE 4
/* Number of samples taken to form an moving average for the gyroscope data
#define NUM_SAMPLES 10
/*-----------------------------------------------------------------------------
- Static Data
-----------------------------------------------------------------------------*/
char MAZE[NUMCOLS][NUMROWS];
You will have to use the argument to the command line to determine how
difficult the maze is (how many maze characters are on the screen).
You will want to use the rand() function and maybe use the output %100. */
POST: Draws the maze to the screen. You must use the draw_character
void draw_maze(void);
/* PRE: 0 < x < NUMCOLS, 0 < y < NUMROWS, characters are defined above
POST: Draws character use to the screen and position y,x as in a graph
When using the i and j from the maze, you will want to remember that
and corresponds to x. */
if(argc > 1) {
difficulty = atoi(argv[1]);
else {
exit(-1);
// setup screen
initscr();
refresh();
generate_maze(difficulty);
draw_maze();
ay = 0;
ax = (NUMCOLS/2)-7;
int g[6];
int t;
// Event loop
char txt[25];
first = TRUE;
do
scanf("%d, %lf, %lf, %lf, %d, %d, %d, %d", &t, &x, &y, &z, &g[0], &g[1], &g[2], &g[3] );
if(first == TRUE) {
last_t = t;
first = FALSE;
moving = TRUE;
last_t = t;
else {
moving = FALSE;
//averages
move_dir = LEFT;
move_dir = RIGHT;
if(move_dir == LEFT) {
if(MAZE[ay][ax-1] == '*') {
move_dir = NONE;
move_dir = NONE;
if(moving == TRUE) {
alt_y = ay;
if(MAZE[ay+1][ax] != '*') {
ay++;
alt_x = ax;
if(move_dir == LEFT) {
ax--;
ax++;
else {
ax = ax;
}
//no diagonals
if ((y+1 != alt_y && x-1 != alt_x) && ((y-+1 != alt_y && x+1 != alt_x))){
y = alt_y;
next_char = MAZE[alt_y][alt_x];
break;
refresh();
if(ay == NUMROWS-8) {
endwin();
printf("%s", txt);
}
void generate_maze(int difficulty) {
srand(time(0));
int n, i, h;
for(i=0;i<NUMROWS;i++) {
for(h=0;h<NUMCOLS;h++) {
n = rand() % 100;
MAZE[i][h] = WALL;
else {
MAZE[i][h] = EMPTY_SPACE;
void draw_maze(void) {
int i, h, x, y;
for(i=0;i<NUMROWS;i++) {
for(h=0;h<NUMCOLS;h++)
draw_character(h, i, MAZE[i][h]);
refresh();
}
/* PRE: 0 < x < NUMCOLS, 0 < y < NUMROWS, characters are defined above
POST: Draws character use to the screen and position y,x as in a graph
When using the i and j to draw the maze, you will want to remember that
i (outer loop) is the NUMROWS and corresponds to y, while j (the inner loop) is the NUMCOLS
and corresponds to x.
This code places the Avatar and the maze on the screen.
mvaddch(y,x,use);
refresh();
double val;
val = asin(-PI/2);
val = asin(PI/2);
} else {
val = asin(x_mag);
}
return val;
double m_avg = 0;
int i;
return m_avg;
int close;
close = TRUE;
} else {
close = FALSE;
return close;
}
Screen Shots
Image 1
Image 2
Answers to Lab 7 questions:
1. Explain the differences between the raw data and the averaged data in your graph for
part A.
The differences between the raw data and the average data is a moving average, this
means that we take an ordered pair, add them together and then divide by number of
samples. This ensures for a smooth moving average. So for an example this means that if
you have a raw data of 1.0002 and 1.025, then your average would be 1.0126 and as
stated before this makes the movement of the avatar smoother.
2. Explain the delay you used to ensure character movement is not erratic.
The delay I used was a delay of 400 milliseconds. This way I can see the movement of the
avatar easily and would be able to react accordingly without the game dragging on forever
and being way too easy. This also helps to give a smoother moving average as the code
can collect more data points before returning an average making it an overall better
experience.