You know this thing was giving me half a box, but me did lazy to use the debugger, I felt I could spot the error. Eventually I debugged it to find an unusual typo for me, a break statement by itself . Its unfinished still.
Code:
#include <iostream.h>
#include <fstream.h>
//enum dir {up, right, down, left};
#define BoardSize 50
class TurtleAndBoard
{
//Attribute:
int TurtleDirection;
int TurtleXPosition;
int TurtleYPosition;
int *****Down;
int BoardState[BoardSize][BoardSize];
//Behaviour:
public:
void InitState();
void ReadNextCommand(ifstream & CommandFile);
bool PerformCommand(int Command, int CommandParameter, ostream & DisplayHandle);
};
void TurtleAndBoard::InitState()
{
this->TurtleDirection = 0;
this->TurtleXPosition = BoardSize/2;
this->TurtleYPosition = BoardSize/2;
this->*****Down = 0;
for(int y = BoardSize - 1; y >= 0; y--)
{
for(int x = 0; x < BoardSize; x++)
{
this->BoardState[x][y] = 0;
}
}
}
bool TurtleAndBoard::PerformCommand(int Command, int CommandParameter, ostream & DisplayHandle)
{
switch(Command)
{
//1 Pen up
case 1:
this->*****Down = 0;
break;
//2 Pen down
case 2:
this->*****Down = 1;
break;
//3 Turn right
case 3:
this->TurtleDirection++;
if(this->TurtleDirection > 3)
{
this->TurtleDirection = 0;
}
break;
//4 Turn left
case 4:
this->TurtleDirection--;
if(this->TurtleDirection < 0)
{
this->TurtleDirection = 3;
}
break;
//5, 10 Move forward 10 spaces or a number other than ten
case 5:
int i;
switch(this->TurtleDirection)
{
//move up by n spaces
case 0:
for(i = 0; i < CommandParameter; i++)
{
//move up
this->TurtleYPosition++;
if(this->TurtleYPosition > (BoardSize - 1))
{
this->TurtleYPosition = (BoardSize - 1);
}
if(this->*****Down)
{
this->BoardState[TurtleXPosition][TurtleYPosition] = 1;
}
}
break;
//move right by n spaces
case 1:
for(i = 0; i < CommandParameter; i++)
{
//move right
this->TurtleXPosition++;
if(this->TurtleXPosition > (BoardSize - 1))
{
this->TurtleXPosition = (BoardSize - 1);
}
if(this->*****Down)
{
this->BoardState[TurtleXPosition][TurtleYPosition] = 1;
}
}
break;
//move down by n spaces
case 2:
for(i = 0; i < CommandParameter; i++)
{
//move down
this->TurtleYPosition--;
if(this->TurtleYPosition < 0)
{
this->TurtleYPosition = 0;
}
if(this->*****Down)
{
this->BoardState[TurtleXPosition][TurtleYPosition] = 1;
}
}
break;
//move left by n spaces
case 3:
for(i = 0; i < CommandParameter; i++)
{
//move left
this->TurtleXPosition--;
if(this->TurtleXPosition < 0)
{
this->TurtleXPosition = 0;
}
if(this->*****Down)
{
this->BoardState[TurtleXPosition][TurtleYPosition] = 1;
}
}
break;
}//end moving n spaces in specified direction
break;
//6 Print the 20 by 20 array
case 6:
//Start print the highest y row first
{
for(int y = BoardSize - 1; y >= 0; y--)
{
for(int x = 0; x < BoardSize; x++)
{
if(this->BoardState[x][y])
{
DisplayHandle << "*";
}
else
{
DisplayHandle << " ";
}
}
DisplayHandle << "\n";
}
}
break;
//9 End of data (sentinel)
case 9:
//WantAend
DisplayHandle << "Yow\n";
break;
}
return true;
}
int main()
{
TurtleAndBoard tb;
//init state
tb.InitState();
//execute lines 1, then 2, ....
/*
2
5, 12
3
5, 12
3
5, 12
3
5, 12
1
6
9
*/
tb.PerformCommand(2, 0, cout);
tb.PerformCommand(5, 12, cout);
tb.PerformCommand(3, 0, cout);
tb.PerformCommand(5, 12, cout);
tb.PerformCommand(3, 0, cout);
tb.PerformCommand(5, 12, cout);
tb.PerformCommand(3, 0, cout);
tb.PerformCommand(5, 12, cout);
tb.PerformCommand(1, 0, cout);
tb.PerformCommand(6, 0, cout);
tb.PerformCommand(9, 0, cout);
return 0;
}