Drunk Walker Program

So - Just a warning - this is a big question and will require me to post a lot of code. I'm working on a program called drunk walker. It starts as an array of periods. The user enters a row and column to start the "drunk walker." Then he moves around randomly, leaving the alphabet in his trail until he blocks himself in or comes to the end of the alphabet. My code will compile right now, but I cannot get the switch section of it to run at all. I'm wondering what I'm missing in order for it to at least run the switch and print out the second array? Any support is appreciated.

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
 /**********************************
*   libraries
**********************************/
#include <iostream>                // needed for cin and cout
using namespace std;

/**********************************
*   proto types
***********************************/
void initializeArray(); //enters a period in all entries
void initializeAlphabet(); //enters the alphabet
void printArray(); //prints array containing the drunken walk
void randDirection(); //generates a random direction


/***********************************
*      global variables
************************************/
char Array[10][10];
int  direction;
char Alphabet[26];

int main ()
{ 
    /*****************************
    *    local variables
    ******************************/
	int row;
	int col;
	int alphaIndex = 0; //letters of the alphabet
	int stopCode=0;
	int changeAlpha = 0;
	
	initializeArray();
	initializeAlphabet();
	printArray();	
	
	cout << "Please Enter a Row: ";
	cin >> row;
	cout << "Please Enter a Column: ";
	cin >> col;
	
	
	Array[row][col] = Alphabet[changeAlpha];
 
    while(stopCode != 1)
    	{
    	    randDirection();
    	    switch(direction)
    	    {       
    	        case 0:         									 //CASE 0
                     if (Array[row-1][col] == '.')  //CHECK NORTH
                     {
                        Array[row-1][col] = Alphabet[changeAlpha];
                        changeAlpha++;
                        row--;
                              break;
                     }
                     else if (Array[row][col+1] == '.')   //CHECK EAST
                     {
                        Array[row][col+1] = Alphabet[changeAlpha];
                        changeAlpha++;
                        col++;
                              break;
                     }
                     else if (Array[row+1][col] == '.')  //CHECK SOUTH
                     {
                        Array[row+1][col] = Alphabet[changeAlpha];
                        changeAlpha++;
                        row++;
                              break;
                     }  
                     else if (Array[row][col-1] == '.')   //CHECK WEST
                     {
                        Array[row][col+1] = Alphabet[changeAlpha];
                        changeAlpha++;
                        col--;
                              break;
                     }     
                     else              
    	                 break;
    	        case 1:													//CASE 1
                     if (Array[row][col+1] == '.')    //CHECK EAST
                     {
                        Array[row][col+1] = Alphabet[changeAlpha];
                        changeAlpha++;
                        col++;
                              break;
                     }
                     else if (Array[row+1][col] == '.')  //CHECK SOUTH
                     {
                        Array[row+1][col] = Alphabet[changeAlpha];
                        changeAlpha++;
                        row++;
                              break;
                     }  
                     else if (Array[row][col-1] == '.')  //CHECK WEST
                     {
                        Array[row][col+1] = Alphabet[changeAlpha];
                        changeAlpha++;
                        col--;
                              break;
                     }
                     else if (Array[row-1][col] == '.')  //CHECK NORTH
                     {
                        Array[row-1][col] = Alphabet[changeAlpha];
                        changeAlpha++;
                        row--;
                              break;
                     }
                     else
    	                 break;
    	        case 2:													//CASE 2
                     if (Array[row+1][col] == '.')  //CHECK SOUTH
                     {
                        Array[row+1][col] = Alphabet[changeAlpha];
                        changeAlpha++;
                        row++;
                              break;
                     }  
                     else if (Array[row][col-1] == '.')  //CHECK WEST
                     {
                        Array[row][col+1] = Alphabet[changeAlpha];
                        changeAlpha++;
                        col--;
                              break;
                     }
                     else if (Array[row-1][col] == '.')  //CHECK NORTH
                     {
                        Array[row-1][col] = Alphabet[changeAlpha];
                        changeAlpha++;
                        row--;
                              break;
                     }
                     else if (Array[row][col+1] == '.')    //CHECK EAST
                     {
                        Array[row][col+1] = Alphabet[changeAlpha];
                        changeAlpha++;
                        col++;
                              break;
                     }
                     else
    	                 break;
    	        case 3:                                                //CASE 3
                     if (Array[row][col-1] == '.')  //CHECK WEST
                     {
                        Array[row][col+1] = Alphabet[changeAlpha];
                        changeAlpha++;
                        col--;
                              break;
                     }
                     else if (Array[row-1][col] == '.')  //CHECK NORTH
                     {
                        Array[row-1][col] = Alphabet[changeAlpha];
                        changeAlpha++;
                        row--;
                              break;
                     }
                     else if (Array[row][col+1] == '.')    //CHECK EAST
                     {
                        Array[row][col+1] = Alphabet[changeAlpha];
                        changeAlpha++;
                        col++;
                              break;
                     }
                     else if (Array[row+1][col] == '.')  //CHECK SOUTH
                     {
                        Array[row+1][col] = Alphabet[changeAlpha];
                        changeAlpha++;
                        row++;
                              break;
                     }  
                     else
    	                 break;
    	        default: break;
    	    } // end switch

    	    if(alphaIndex == 26)
    	    {
    	       stopCode = 1;
    	    }
    	} // end while loop
  
	printArray(); //print final array		
	
	system("pause");

    return 0;
} //end of main()

/********************************************
*    set all elements in the array to a '.'
********************************************/
void initializeArray()
{
     int row;
     int col;
     for(row=0; row<10; row++)
     {
         for(col=0; col<10; col++)
         {
             Array[row][col] = '.';
         }
     }
}// end of initializeArray()

/**************************************************
*   initialize the alphabet array from 'A' to 'Z'
**************************************************/
void initializeAlphabet()
{
	int row=0;
	
	for(int n = 65; n<91; n++)
	{
		Alphabet[row]=n;
		row++;
	}
	/*for(int x=0; x<27; x++)
	{
	cout << Alphabet[x];
	}*/
	
}//end of initializeAlphabet()
 
/********************************************
*    print out the 10X10 array
********************************************/
void printArray()
{
     int row;
     int col;
     for(row=0; row<10; row++)
     {
         cout << "\n";
         for(col=0; col<10; col++)
         {
             cout << Array[row][col];
         }
     }
     cout << "\n\n";
}// end  of printArray()

/********************************************
*    creata a random number from 0~4 and
*    save the number in a GLOBAL variable
*    named  direction
********************************************/
void randDirection()
{ 
     int n = rand() % 100;
     direction = n % 4;
}//end of randDirection(); 
Your program does enter the switch statement but then gets stuck there forever, because the only way it can exit (the while loop) is if alphaIndex equals 26. You initialize this to 0 at the beginning of your program but then never do anything else with it so that condition is never met.
Last edited on
Topic archived. No new replies allowed.