Hello again. Thank you for the replies!
I have managed to get this working anyway so here's the revised code
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 60 61 62
|
#include <cstdlib> //don't know if this is needed (Dev-c++ default entry)
#include <iostream>
#define WIDTH 20 //defenition for dimensions
#define HEIGHT 20 //
using namespace std;
char print(char board[WIDTH][HEIGHT]); //print function prototype
int main(int argc, char *argv[])
{
char board[WIDTH][HEIGHT]; //declare board and it's dimensions
for(int a=0;a<HEIGHT;a++){ //
for(int b=0;b<WIDTH;b++){ //
board[a][b]='*';} //loop to fill board with astrix's
} //
char *p; //Declare pointer p
p = &board[WIDTH/2][HEIGHT/2]; //place p at board centre
*p = '?'; //mark centre using pointer p
print(board); //print board
for(int x=0;/*x<100*/;x++){
print(board); //loop to keep program running (infinite).
char direct; //declare wariable to hold user input
cin >> direct; //get use input
if(direct=='w'){*p = '*'; //evaluate input against predefined direction
p = p - WIDTH; //replace marker with board unit
*p = '?';} //place a new marker on destination square
else if (direct == 's'){*p = '*';
p = p + WIDTH;
*p = '?';}
else if (direct == 'a'){*p = '*';
p = p - 1;
*p = '?';}
else if (direct == 'd'){*p = '*';
p = p + 1;
*p = '?';}
else {cout << "please enter the correct controls \n"; // prompt user to enter corect control
system("pause"); //pause to allow messege to be read
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
char print(char board[WIDTH][HEIGHT]){
system("cls"); //clear screan to avoid clutter
for(int a=0;a<HEIGHT;a++){ // for loop to print board
for(int b=0;b<WIDTH;b++){
cout << board[a][b];
}
cout << "\n"; //newline to keep visual dimensions
}
}
|
@Slambofett: thank you for your suggestion but I don't think I explained the symptoms of my problem in enough detail. the pointer p was marking the square correctly but the pointer itself was returning to the centre at the start of every go.
I was ending up with something like this at the end of 4 trys
******
**?***
*?*?**
**?***
******
you got me thinking though, about where the pointer move was being implemented so I decided to scrap the entire "move" function and I placed it's code in main, inside the infinite loop.
now the changes are persistent and the '?' appears to move around the board like an object.
@firedraco: I understand that I can't change the value of the pointer but I was attempting to change the position of it. I was only changing the value of the pointer's address so I'd have a visual representation of where the pointer is.
@jRaskell: I've seen this in a book somewhere but I have no Idea how to implement it correctly :(