Question: I am stuck with how to code this in C Starting Point: Lab8_1.c Lab8_2.c Compiling the lab this week requires the use -lncurses flag Ex.
I am stuck with how to code this in C
Starting Point:
Lab8_1.c
Lab8_2.c
Compiling the lab this week requires the use -lncurses flag
Ex. gcc -o lab8 lab8.c -lncurses
Process:
Problem
In this and the following lab, you will develop a simple, real-time game controlled by the DualShock 4. The game will be a maze navigation game where you control an avatar using the gyroscope of the DS4. The DS4 will be used to move the avatar left and right on the maze. The avatar will start at the top of the maze, the screen, and at predetermined time intervals, the avatar will move down the maze. Walls in the maze will stop the avatar from moving. The game is won if the avatar reaches the bottom of the maze.
Criteria:
Start your avatar (a single character) at the top center of the screen. A function that lets you write a character to any character location in the window is included in the starting source code. DO NOT CHANGE THE FUNCTION.
At a certain time increment, the avatar will fall one line down the screen. Finding a reasonable interval of time is part of your task. Ensure that the avatar is erased when it moves to a new spot. Your avatar should not leave a trail of old avatar characters as it moves around.
Character movement
If the DualShock 4 is tilted right, the avatar will move to right.
If the DualShock 4 is titled left, the avatar will move to the left.
This portion of the code will require using a moving average (explained below)
The avatar may not move into locations occupied by the maze or off of the screen.
The avatar wins if it makes it to the bottom of the screen without getting stuck. The program will terminate when this condition is met.
Design:
Part A Moving Averages:
When dealing with real-time data sources such as the DualShock 4, it is common to want to smooth out rough data. One way to do that is to apply a moving average to the data.We will develop a function that reads from ds4rd.exe the x, y, and z values from the gyroscope and computes moving averages on the data in real time.
A moving average of length n computes the average of the last n inputs. For instance, a moving average of length 2 of (1, 3 , 5, 6,3) is (2, 4, 5.5, 4.5). A moving average of length 3 of the same data would be (3, 4.666, 4.666). Note that the moving average has to accumulate n inputs before it can output something.
For this lab, start with the lab8_1.c source code linked above. This program will provide you will a skeleton for pulling in gyroscope values from the DS4. It will contain a m_avg() function that you have to implement. The function takes in an array of doubles, an integer that contains the number of values to average, and a double value to insert into the array. The function will return a double that is the computed average over the updated set of values.
To implement the function, you will have to shift the values in the array, insert the new value into the array, and perform the computation of the moving average.
The length of the moving average shall be given on the command line to your program (code is included in the sample to help you read this input). Once enough data has been read to output a moving average, a new output should be generated for each input line.
When you are convinced that you have implemented the moving average correctly, run the program with a circular movement of the controller, such as spinning the controller. Capture the output of the program into a CSV file and create three graphs in excel for each axis.
Part B: Character Movement
Now that you have the moving average completed, you can begin on printing the avatar and moving it around on the screen. Download and open the lab8_2.c source code. Copy and paste the function for the moving average into the sample code. In the sample code, we have included a function that lets you write a character to a specific x,y position on the screen. Be sure to use this function (draw_character). DO NOT MODIFY THIS FUNCTION. In a loop, you will make the character begin at the top center of the screen and move downward every so often. Note that if you do this without a delay, the program will complete so fast that you can barely see the avatar move.
To add delay, remember that if the -t option is given to ds4rd.exe, the first argument is the time in milliseconds since the executable starts. Use this data to wait some number of milliseconds (a delay for you to determine), and then implement the avatar moving down the screen with the delay. Keep track of a moving average of the gyroscope as well here so that you can use them to control the avatar.
For the left and right movement, you will need to use the moving averages function along with the gyroscope data to determine if the avatar moves left or right. No credit will be given if your solution does not make use of the moving average in a meaningful way. Please note that you need to fill the array with input data before you calculate moving average data, otherwise your moving average will be skewed.
The program should end when the avatar hits the bottom of the screen.
Here is the code included:
8.1
int main(int argc, char* argv[]) { /* DO NOT CHANGE THIS PART OF THE CODE */ double x[MAXPOINTS], y[MAXPOINTS], z[MAXPOINTS]; double new_x, new_y, new_z; double avg_x, avg_y, avg_z; int lengthofavg = 0; if (argc>1) { sscanf(argv[1], "%d", &lengthofavg ); printf("You entered a buffer length of %d ", lengthofavg); } else { printf("Enter a length on the command line "); return -1; } if (lengthofavg MAXPOINTS) { printf("Invalid length "); return -1; } for(i = 0; i 8.2
/* Mathmatical constants */ #define PI 3.14159 /* Screen geometry Use ROWS and COLUMNS for the screen height and width (set by system) MAXIMUMS */ #define COLUMNS 100 #define ROWS 80 /* Character definitions taken from the ASCII table */ #define AVATAR 'A' #define WALL '*' #define EMPTY_SPACE ' ' /* Number of samples taken to form an moving average for the gyroscope data Feel free to tweak this. */ #define NUM_SAMPLES 10 /*----------------------------------------------------------------------------- - Static Data -----------------------------------------------------------------------------*/ /* 2D character array which the maze is mapped into */ char MAZE[COLUMNS][ROWS]; /*----------------------------------------------------------------------------- - Prototypes -----------------------------------------------------------------------------*/ /* POST: Generates a random maze structure into MAZE[][] You will want to use the rand() function and maybe use the output %100. 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). */ void generate_maze(int difficulty); /* PRE: MAZE[][] has been initialized by generate_maze() POST: Draws the maze to the screen */ void draw_maze(void); /* PRE: 0 1.0 You may want to reuse the roll function written in previous labs. */ double calc_roll(double mag); /* Updates the buffer with the new_item and returns the computed moving average of the updated buffer */ double m_avg(double buffer[], int avg_size, double new_item); /*----------------------------------------------------------------------------- - Implementation -----------------------------------------------------------------------------*/ /* Main - Run with './ds4rd.exe -t -g -b' piped into STDIN */ void main(int argc, char* argv[]) { /* Setup screen for Ncurses The initscr functionis used to setup the Ncurses environment The refreash function needs to be called to refresh the outputs to the screen */ initscr(); refresh(); /* WEEK 2 Generate the Maze */ /* Read gyroscope data and fill the buffer before continuing */ /* Event loop */ do { /* Read data, update average */ /* Is it time to move? if so, then move avatar */ } while(1); // Change this to end game at right time /* Print the win message */ /* This function is used to cleanup the Ncurses environment. Without it, the characters printed to the screen will persist even after the progam terminates */ endwin(); printf("YOU WIN! "); } double m_avg(double buffer[], int avg_size, double new_item) { int k; for (k = 0; k Step 1 Moving AVG: 3 Step 2: Moving AVG: 5.2 Step 3 Moving AVG: 4.4 Input Data: 2 Input Data: 12 Input Data: 0 Moving Average Array Moving Average Array Moving Average Array Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
