Movement In a Grid (Array)

Hello everyone.

I have a project that requires me to build a gameboard, with two units in the board, and be able to move and fire at each other. The grid is 10 x 10, with each player occupying a space. Also, at the beginning of each execution, the game board must place the player and the computer (other unit) randomly in the grid. There are other factors as well, but for right now im concerned with the movement.


I already have the grid setup, as well as the random localization. What i need to do is how do i go about moving the actual ships. The player must command the player ship "Y" to move either N, S, E, or W. The computer's movements will all be dependant on how many moves he has available, must move towards the player. I would like to be able to just display the grid once for every move, but im sure it wont be possible, so redisplaying the grid is fine. How do i go about moving hte ships?

Here isthe code i have for the grid. I have more data, but that all is for the statistics of the ship (ie, armor, weapons, etc), and are not needed for this. Thank you in advance!
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
int main(int argc, char *argv[])
{
    const int ROW = 10;
    const int COL = 10;
    
    int player;
    int computer;
    int field[ROW][COL];
    
        
    cout << "Welcome to the Frigate Battle Game!!!" << endl;
    cout << "\nPlease enter the stats for your ship.\n" << endl;
    cout << endl;
    
    srand(time(0));
    player = rand()%99+1;
    computer = rand()%99+1;
    
    for (int rows = 0; rows < ROW; rows++)
    {
        for (int columns = 0; columns < COL; columns++)
        {
			if ((player / 10) == rows && (player % 10) == columns)
			{
				cout << " Y ";
			}
			else if ((computer / 10) == rows && (computer % 10) == columns)
			{
				cout << " E ";
			}
			
			else 
			{
				cout << "|_|";
			}
        }
        cout << endl; 
     }
    system("PAUSE");
    
    user.Move(); //this is simply the questioning of the moves. The functions for the actual movement i dont have.
     
    return EXIT_SUCCESS;
}
I see how you're storing the location. Just do this:
1
2
3
4
player++;//move right
player--;//move left
player+=10; //move down
player-=10;//move up 

You may need checks for the edges of the grid.
My problem is that i have the function for the movement inside the structure, so if i call for player, inside that function, it gives me an error, because player wasnt called in the struct.

Should i post the whole code up?
I would post the structure too, since it seems to be related to the problem.
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using namespace std;

struct FRIGATE
    {
           int ARMOR;
           int MOVE;
           int originalMOVE;
           int WEAPON;
           int RANGE;
           string DIRECTION;
           string _turn;
           string _fire;
           string VALUE;
                 
           
           void inputStats()
           {
               
                   cout << "\nPlease enter amount of armor: ";
                   cin >> ARMOR;
                   validateStat(ARMOR, "ARMOR");
                   cout << "Please enter the speed of your ship: ";
                   cin >> MOVE;
                   validateStat(MOVE, "MOVE");
                   originalMOVE = MOVE;
                   cout << "Please enter how many weapons on your ship: ";
                   cin >> WEAPON;
                   validateStat(WEAPON, "WEAPON");
                   cout << "Please enter the range of your guns: ";
                   cin >> RANGE;
                   validateStat(RANGE, "RANGE");
           }
           
           bool validateSum(int paramArmor, int paramMove, int paramWeapon, int paramRange)
           {
                if ((paramArmor + paramMove + paramWeapon + paramRange) <= 15)
                   return true;
                else   
                {
                       cout << "You have entered invalid values.\n Please ensure your ARMOR, MOVE, WEAPON, and RANGE values do not exceed 15.";
                       inputStats();
                       return false;       
                }
           }  
           
           void validateStat(int value, string stat)
           {
             if (stat == "ARMOR" || stat == "MOVE" || stat == "RANGE")
              {
                  if (value > 6 || value < 1)
                  {
                      cout << "You have entered invalid values. Please enter a/an " + stat + " value between 1 and 6.\n";
                      cout << "Please enter amount of " + stat + ": ";
                      cin >> value;
                      validateStat(value, stat);
                  }
              }
                
              if (stat ==  "WEAPON")
               {   
                  if (value > 4 || value < 1)
                  {
                      cout << "You have entered invalid values. Please enter a WEAPON value between 1 and 4.";
                      cout << "Please enter how many weapons on your ship: ";
                      cin >> WEAPON;
                      validateStat(WEAPON, "WEAPON");
                  }
                         
               }
            
           }  
           
           void Move()
           {
                if(MOVE >=1)
                {
                    if(DIRECTION == "")
                      DIRECTION = "N";
                    //this is where to tell the user where they are facing, and ask what they'd like to do
                    cout << "You are currently facing " + DIRECTION+ ". Would you like to move? (Y or N)";
                    cin >> _turn;
                    MOVEMENT();     
                }
           }  
           void MOVEMENT()           
           {
                    if (_turn == "Y" || _turn == "y")
                    {
                            cout << "Which direction would you like to move? (N, S, E or W)";
                            cin >> VALUE; 
                            if (VALUE == "N" || VALUE == "S" || VALUE == "E" || VALUE == "W")
                                {
                                      Turn(VALUE); 
                                }
                            else
                                {
                                      cout << "You have entered an invalid entry. Please enter a N, S, E, W." << endl;
                                      MOVEMENT();
                                }      
                    } 
                    if (_turn == "N" || _turn == "n")
                    {
                        cout << "Would you like to fire at your opponent?(Y or N)";
                        cin >> _fire; 
                        if (_fire == "Y" || _fire == "y")
                        { 
                          Fire(); 
                        }
                        
                    }
                
                    if (_turn != "N" || _turn != "n" || _turn != "Y" || _turn != "y")
                    {
                              cout << "You have entered an invalid entry. Please enter a Y or N." << endl;
                              Move();
                    }
                    else
                    {
                              cout << "You have no more action points this turn, it is now your opponent's turn.";
                    }
                
           }
           void Turn(string direction)
           {
                //this is where to determine how many actions (MOVE points) the user or computer has and subtract from that value..
                if( VALUE=="N" )
                {
                    //player+=10;
                }
                
                if(MOVE>=1)
                {
                          DIRECTION = direction;
                          MOVE = MOVE-1; //since the user turned, this subtracts 1 point from their move total
                          cout << "Please perform your next action. You have " << MOVE << " action points left this turn." << endl;
                          Move();
                }
                else
                {   
                    cout << "You don't have enough action points to allow this action." << endl;
                }
           }
           
           void Fire()
           {
              //this is where to determine where the opponent is, if they're in range and next to 
              //either side of the boat  and within range (RANGE) as well as how many times they fire (WEAPONS), 
              
              if(MOVE>=1)
                {
                          MOVE = MOVE-1; //since the user turned, this subtracts 1 point from their move total
                          cout << "Please perform your next action. You have " << MOVE << " action points left this turn.";
                          Move();
                }
                else
                {   
                    cout << "You don't have enough action points to allow this action.";
                }
              
              
           }
    };
    
The above is ONLY the structure, right before the first post i made. The structure goes first, then the rest. Thank you all for your quick responses.
Put an int position in there, then do what I said earlier.
Ok. That solved the problem of it being in there, how set it up so that each time the player moves, the screen clears and the new grid is placed with the new coordinates?


i placed this in the Turn function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void Turn(string direction)
           {
                if( VALUE=="N" )
                {
                    player+=10;
                 }
                
                if(MOVE>=1)
                {
                          DIRECTION = direction;
                          MOVE = MOVE-1; //since the user turned, this subtracts 1 point from their move total
                          cout << "Please perform your next action. You have " << MOVE << " action points left this turn." << endl;
                          Move();
                }
                else
                {   
                    cout << "You don't have enough action points to allow this action." << endl;
                }
           }


How do i either redisplay the grid with the new coordinates or wipe the screen clean, leaving only the new directions and hte grid?
On second thought, i figured out how to clear the screen, using system(cls), however i think the way i set up my grid, i cant refresh it.

How do i set up my array so that i cna repost it?
Can any help?
Ok well i fixed my array. I can now display it again. However, when it displays, it removes both ships, with out moving them. having the whole grid out of "|_|".


Any help would be great. TY
Topic archived. No new replies allowed.