RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 2065 | Replies: 1 | Thread Tools  Display Modes
Reply
Join Date: Jun 2004
Posts: 2
Reputation: Kaiser_Sosae is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Kaiser_Sosae Kaiser_Sosae is offline Offline
Newbie Poster

Urgent! Change using class

  #1  
Jun 3rd, 2004
I need to change this code i have written into class

I need it done urgently, i have tried, and failed numerous times.

/* Assignment 2.cpp
Michael Ainsworth
15737944	
15.05.2004 
Assignment 2.cpp
Computerized Sketchpad
*/ 

//Preprocessor Section
#include <iostream>
#include <stdlib.h>

using namespace std;

const int MAXCOMMANDS = 100;
const int SIZE = 20;	// grid is SIZE * SIZE
const int RIGHT = 0;	// facing right
const int DOWN = 1;		// facing down
const int LEFT = 2;		// facing left
const int UP = 3;		// facing up

//Main Program Section
void ClearFloor(int floor[SIZE][SIZE])
{ // set the floor array to all clear
	for (int x = 0; x < SIZE; x++)
	{
		for (int y = 0; y < SIZE; y++)
		{
			floor[y][x] = 0;
		}
	}
}

void ClearCommands(int commandArray[MAXCOMMANDS][2])
{ // empty the commands array
	for (int i = 0; i < MAXCOMMANDS; i++)
	{
		commandArray[i][0] = 0;
		commandArray[i][1] = 0;
	}
}

void GetCommands(int commandArray[MAXCOMMANDS][2])
{
	int		count = 0;	// count of commands so far received
	char	buff[255];	// temp buffer for storing info in
	int		command;	// command request
	int		distance;	// distance to move if request is of type 5
	int		i;			// temp value
	char	loadfile;	// should we load from file
	bool	bLoadFile;	// are we loading from file
	FILE	*fp = NULL;	// file pointer we are reading from

	cout << "Computerized Sketchpad" << endl;

	
	cout << "Would you like to load your commands from file (y/n) ? ";
	cin >> loadfile;

	if (loadfile == 'y' || loadfile == 'Y')
		bLoadFile = true;
	else
		bLoadFile = false;

	if (!bLoadFile)
	{ // not loading from file so request manual command entry
		cout << endl;
		cout << "Please enter the commands you wish to execute, 1 per line." << endl << endl << "Enter 9 to finish" << endl << endl 
			<< "Command Meanings" << endl 
			<< "1:Pen up" << endl
			<< "2:Pen down" << endl
			<< "3:Turn Right" << endl
			<< "4:Turn Left" << endl
			<< "5,10: Move Forward 10 spaces" << endl 
			<< "6:Print the 20-by-20 array" << endl << endl
			<<  "(in this example)(eg. 5,5 would move the turtle forward 5 spaces)" << endl << endl;
		
	}
 
	else
	{ // load commands from file
		do
		{ // loop until we get a valid file
			cout << endl << "Type name of file with type of file example (test.txt)!" << endl << endl
			<< "Filename?" ;
			cin >> buff;
			fp = fopen(buff, "rt"); // open the file as text
			if (fp == NULL)
			{ // failed to open specified file, let them know
				cout << "Unable to open file '" << buff << "' Please try again" << endl;
			}
		}
		while (fp == NULL);
	}

	do
	{ // loop through until we get a command 9, end of file or reach MAXCOMMANDS
		command = 0;
		distance = 0;
		cout << "Command " << count + 1 << " : ";
		if (bLoadFile)
		{ // load from file so get next line
			if (fgets(buff, 255, fp) == NULL)
			{ // no line read, set buffer to 9 to trigger exit
				strcpy(buff, "9\n");
			}
			cout << buff; // output to screen what command we got from the file
		}
		else
		{ // get command from user
			cin >> buff;
		}
		i = sscanf(buff, "%i, %i", &command, &distance);
		switch (i)
		{ // how many items did we parse from the command
			case 1: // must be a 1,2,3,4,6 or 9 command
				if ((command > 0 && command < 5) || (command == 6) || (command == 9))
				{ // is the commands we know
					commandArray[count][0] = command;
					commandArray[count][1] = 0;
					count++;
				}
				else if (command == 5)
				{ // command 5 with no distance, complain
					cout << "Move command must have a distance component (eg 5,10), please re-enter" << endl;
				}
				else
				{ // invalid command, complain
					cout << "unable to parse line '" << buff << "', please re-enter" << endl;
				}
				break;
			case 2: // must be a 5 command
				if (command == 5)
				{ // it is a 5 command
					commandArray[count][0] = command;
					commandArray[count][1] = distance;
					count++;
				}
				else
				{ // complain
					cout << "unable to parse line '" << buff << "', please re-enter" << endl;
				}
				break;
			default: // unknown, complain
				cout << "unable to parse line '" << buff << "', please re-enter" << endl;
				break;
		}
	}
	while ((command != 9) && (count < MAXCOMMANDS - 1));

	system("pause"); // pause so they can see all commands before processing

	commandArray[MAXCOMMANDS - 1][0] = 9; // just in case set the last entry in array to end command

	if (fp != NULL) // close the file if it is open
		fclose(fp);

	return;
}

// Direction the pen is moving
//            3 (up)
//            ^
//            |
// (left) 2 <-+-> 0 (right)
//            |
//            v
//            1 (down)

int turnRight(int direction)
{
	// right -> down -> left -> up -> right ....
	direction++;

	if (direction > UP) // rotated past up so reset to facing right
		direction = RIGHT;

	return direction;
}

int turnLeft(int direction)
{
	// up -> left -> down -> right -> up ...
	direction--;

	if (direction < RIGHT) // rotated past right so reset to facing up
		direction = UP;

	return direction;
}

void printArray(int floor[SIZE][SIZE])
{ // draw the sketch pad
	for (int y = 0; y < SIZE; y++)
	{
		for (int x = 0; x < SIZE; x++)
		{
			if (floor[y][x] == 0)
				cout << " "; // empty grid spot
			else
				cout << "*"; // active grid spot
		}
		cout << endl;
	}
}

void movePen(bool penDown, int floor[SIZE][SIZE], int direction, int distance, int &x, int &y)
{
	int xmov = 0;
	int ymov = 0;
	int value;

	if (penDown == false)
	{ // pen up so set floor values changed to 0 (unset)
		value = 0;
	}
	else
	{ // pen down so set floor values to 1 (set)
		value = 1;
	}

	switch (direction)
	{ // assuming top,left is 0,0
		case LEFT: // xpos will decrease when moving left
			xmov = -1;
			break;
		case RIGHT: // xpos will increase when moving right
			xmov = 1;
			break;
		case UP: // ypos will descrease if moving up
			ymov = -1;
			break;
		case DOWN: // ypos will increase if moving down
			ymov = 1;
			break;
	}

	// change the set/unset value of the current pen position
	floor[y][x] = value;

	for(int i = 0; i < distance; i++)
	{
		x += xmov; // move our x position
		y += ymov; // move our y position

		if (x < 0)
			x = 0;
		if (x >= SIZE)
			x = SIZE - 1;
		if (y < 0)
			y = 0;
		if (y >= SIZE)
			y = SIZE - 1;

		// set/unset the value of the spot we have moved to.
		floor[y][x] = value;
	}

	return;
}

int main(int argc, char* argv[])
{
	int		floor[SIZE][SIZE] = {0};			// size of the drawing space on screen
	int		command = 0;						// records current command
	int		distance = 0;						// distance to be moved on the screen
	int		direction = 0;						// current direction
	int		count = 0;							// loop counter
	int		x, y;								// x & y pos on sketchpad
	int		commandArray[MAXCOMMANDS][2] = {0};	// array to record a set of commands
	bool	penDown = false;					// pen position
	char	again = 'n';						// program flag


	do
	{
		// initialize variables each time
		command = 0;
		distance = 0;
		direction = 0;
		count = 0;
		x = -1;
		y = -1;
		penDown = false;
		ClearFloor(floor);
		ClearCommands(commandArray);
		system("cls");

		// get the commands
		GetCommands(commandArray);
		system("cls");

		// retrieve first command
		command = commandArray[count][0];

		while ( (command != 9))
		{ // loop through commands until we hit marker
			switch ( command )
			{
				case 1: // penDown
					penDown = false;
					break;
				case 2: // penUp
					penDown = true;
					break;
				case 3: // turn right
					direction = turnRight(direction);
					break;
				case 4: // turn left
					direction = turnLeft(direction);
					break;
				case 5: // move the pen
					distance = commandArray[count][1];
					movePen(penDown, floor, direction, distance, x, y);
					break;
				case 6: // draw the sketch pad
					cout << endl << "The drawing is:" << endl << endl;
					printArray(floor);
					break;
			}
			command = commandArray[++count][0];
		}

		cout << endl << "Do you want to draw again (y/n)? ";
		cin >> again;
	}
	while ( (again == 'y') || (again == 'Y') );

	return 0;
}

Thankyou, Mike
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2004
Posts: 2
Reputation: Kaiser_Sosae is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Kaiser_Sosae Kaiser_Sosae is offline Offline
Newbie Poster

Re: Urgent! Change using class

  #2  
Jun 3rd, 2004
If anyone can help, i will greatfully give them a 50mb email account.

Thanks again
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 6:41 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC