help with this function

hi can someone please please assist me
i am unsure of what to do for function movePen()

function movePen moves one space in the direction that is currently being pointed in. You already know what your current direction is, you know whether the pen is up or down, and you have your floor. So, based on these pieces of information, you should move one space and also update your xPos and yPos. Note: do not allow the user to move off the screen.

i have filled out most of my functions but im unclear of how to go about doing movePen();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "board.h"
#include<iostream>
#include <stdlib.h>
using namespace std;

Board::Board()
{   for(int j = 0;j<ARRAY_SIZE;j++){
    for(int i = 0;i<ARRAY_SIZE;i++){
    int floor[xPos][yPos]={{0},{0}};
    direction=East;
    floor[j][i]=clearSymbol;
    penDown=false;
    }
 }



}
 void Board::movePen(){
    

}
 void Board ::penUp(){
    penDown=false;

 }
 void Board:: setPenDown(){
     penDown=true;

 }
 void Board ::turnRight(){
    direction++;
 }
 void Board ::turnLeft(){
   direction--;
 }

 void Board::printBoard(){
     system("cls");//clears the screen;
     // loop for array's rows
     if (direction == North)
         cout<<"N\n^\n|\n|\n"; //should produce something like
for(int i = 0;i<=xPos;i++)
    for(int j = 0;j<=yPos;j++)
        cout<<" "<< floor[i][0] << " | "<< floor [i][1] << " | "
                << floor [i][2];
        if(j!=2)
        cout <<"\n---|---|---\n"; //you should not hardcode this line as you see here




  }


 bool Board::getPenStatus(){
return penDown;
 }
Last edited on
Maybe something like this. Note that your constructor can be simplified.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Board::Board()
{
    direction = East;
    penDown = false;
    xPos = 0;
    yPos = 0;
    for(int x = 0; x < ARRAY_SIZE; x++)
        for(int y = 0; y < ARRAY_SIZE; y++)
            floor[x][y] = clearSymbol;
}

void Board::movePen()
{
    if (penDown) floor[xPos][yPos] = Mark;

    switch (direction)
    {
    case East:  if (xPos < ARRAY_SIZE-1) ++xPos; break;
    case West:  if (xPos > 0           ) --xPos; break;
    case South: if (yPos < ARRAY_SIZE-1) ++yPos; break;
    case North: if (yPos > 0           ) --yPos; break;
    }
}

You'll also want to wrap direction in the turn methods:

1
2
void Board::turnRight() { if (++direction >= 4) direction = 0; }
void Board::turnLeft () { if (--direction <  0) direction = 3; }

Last edited on
Topic archived. No new replies allowed.