C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old June 3rd, 2004, 05:58 AM
Kaiser_Sosae Kaiser_Sosae is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 1 Kaiser_Sosae User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Make using class

I need to make this using class, please help, i need it by tomorrow. I will greatfully, hook whoever helps me, up with a 50mb email account.

Code:
 /* 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;
}
 


Thanks Mike

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Make using class


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway