#include <stdio.h>
#include <stdlib.h>
#include "useCurses.h"
#include "robotModel.h"
#include "maze.h"
/* function prototype for initializing maze and robot from command line arguments */
int initMazeAndRobotFromCLArgs(int argc, char *argv[], Maze *maze, RobotModel *robot);
int main( int argc, char *argv[] )
{
Maze maze; // strucutre to store maze
RobotModel robot; // structure to model robot (current position + direction)
int station; // Initializes the variable station
char direction[100]; // creates the array for direction
int move; // initializes the variable move for the array direction
int endpoint; // initalizes the variable endpoint
int i;
// initialize curses mode
initscr();
// if successfully have initialized robot and maze, solve maze
if (initMazeAndRobotFromCLArgs(argc, argv, &maze, &robot)) // while loop that runs until maze hits an endpoint
{
printMazePlusCurrentPos(maze,robot);
printw("\nWhich station would you like your robot to go to? 1 (Food Station), 2 (Drink Station), 3 (Pay Station) "); //Prints the question prompting what station to go to
scanw("%d", &station); // scans in the 1, 2, or 3 for the station
endpoint = 0; // initializes the endpoint for to be 0 to get into the while loop
direction[move] = 0; // initializes the array to start at 0
move = 0;
i=0;
while (endpoint<station) // while loop the only runs when the endpoint is less then the station number
{
if (blackToLeft(maze,robot) && blackInFront(maze, robot) && blackToRight(maze,robot) && onBlack(maze,robot)) //if statement to discover the endpoint
{
moveStraight(&robot); //check to make sure it is in the endpoint
if (blackToLeft(maze,robot) && blackToRight(maze,robot)) //if statement to check again if it is in the end point
{
endpoint++;
if (endpoint<station) // if statement to turn around if the robot is not in the right if statement
{
uTurn(&robot); // makes the robot do a u-turn
direction[move] = 4; // set first element of array to 4
move = move + 1; // increment index
moveStraight(&robot); // makes the robot move straight to get out of the endpoint
moveStraight(&robot);
}
}
else if (!blackToLeft(maze, robot) && !blackInFront(maze, robot) && !blackToRight(maze, robot)) //else if statement to make the robot reverse if it isn't in an endpoint
{
reverse(&robot); //makes the robot go backwords
turnLeft(&robot); //tells the robot to turn left
direction[move] = 1; // set first element of array to 4
move = move + 1; // increment index
}
}
else if (blackToLeft(maze, robot)) //applies the left hand on wall rule to start out forcing the robot to turn left if it can
{
turnLeft(&robot); //makes the robot go left
direction[move] = 1; // set first element of array to 1
move = move + 1; // increment index
printMazePlusCurrentPos(maze, robot); // printing the current position of the "robot"
}
else if (blackInFront(maze, robot)) //runs if the robot can't go left so it wants it to go straight
{
moveStraight(&robot); //makes the robot move straight
if (blackToRight(maze, robot)) //if statement to only count the straights that the robot makes at an intersection
{
direction[move] = 2; // set first element of array to 2
move = move + 1; // increment index
printMazePlusCurrentPos(maze, robot); // printing the current position of the "robot"
}
}
else if (blackToRight(maze, robot)) // runs if the robot can't go left or straight
{
turnRight(&robot); //makes the robot turn right
direction[move] = 3; // set first element of array to 3
move = move + 1; // increment index
printMazePlusCurrentPos(maze, robot); // printing the current position of the "robot"
}
else if (!blackToLeft(maze, robot) && !blackInFront(maze, robot) && !blackToRight(maze, robot)) //runs if the robot can't go left, straight, or right
{
uTurn(&robot); //makes the robot do a U-turn
direction[move] = 4; // set first element of array to 4
move = move + 1; // increment index
printMazePlusCurrentPos(maze, robot); // printing the current position of the "robot"
}
}
for (i=0; i<=move; i++) //for loop to print out the directions
{
if (direction[move] == 1)
{
printw("Left ");
}
else if (direction[move] == 2)
{
printw("Straight ");
}
else if (direction[move] == 3)
{
printw("Right ");
}
else if (direction[move] == 4)
{
printw("UTurn ");
}
}
for (i=move; i>0; i--) //for loop to print out the directions to go back to start
{
if (direction[move] == 1)
{
printw("Right ");
}
else if (direction[move] == 2)
{
printw("Straight ");
}
else if (direction[move] == 3)
{
printw("Left ");
}
else if (direction[move] == 4)
{
printw("UTurn ");
}
}
}
printw("\nDONE!");
getch();
endwin();
return 0;
}
]MOD EDIT: USE CODE TAGS WHEN POSTING CODE LIKE THIS:
HINT: Highlight your code and then press the [CODE] Icon at the top of the edit window.
This post has been edited by jimblumberg: 11 October 2012 - 08:30 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.

New Topic/Question
Reply


MultiQuote



|