Why can't my program get past a certain point?
Dec 5, 2010 at 7:23am UTC
I'm programing a game. In my program I enter coordinates and my program then converts this so that it refers to a specific place on an array. One of the two different ways to win is to capture 5 pairs of the enemy's pieces. You do this by placing one of your tokens on either side of two adjacent enemy pieces. i.e. xoox.
Everything in my program worked before I wrote the code for how to capture a piece, I would enter the x coordinate of my move and press enter, then enter the y coordinate and press enter, and then the program would move. Now, it will prompt me to enter the coordinates but it won't accept them, it won't get past entering the coordinates. I don't get any error messages nor do I get any warnings. I have turned the faulty code into a comment. It is about halfway down the program. I'm sorry that I can't better describe the problem, if you have any questions, PLEASE ask them. Thanks.
PS: how do I upload a cpp file so I don't have to upload the entire code? Or, is there something else I should do in the future in a situation like this? I am new to this website and I didn't see anything on this in the "how to ask questions" paper nor anywhere else on the website. Thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*if (board[move()]==board[move()+3]&&board[move()+2]==board[move()+1]&&board[move()+1]=='x')
{
board[move()+1]='+';
board[move()+2]='+';
p2p++;
}
if(board[move()]==board[move()-3]&&board[move()-2]==board[move()-1]&&board[move()-1]=='x')
{
board[move()-1]='+';
board[move()-2]='+';
p2p++;
}
//Program works unless this code is run
*/
Dec 5, 2010 at 7:24am UTC
The code to the entire program is:
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
// intro
#include<iostream>
#include<string>
using namespace std;
//global variables
string p1;
string p2;
int p1p=0;
int p2p=0;
int win=0;//indicates win. 0 = no win; 1 = p1 win; 2 = p2 win;
bool turn=false ;//turn determends if it is p1's turn
char board[400];
void showboard();
int move();//determends move
int main ()
{
// Choose user names
p1="p1" ;
p2="p2" ;
//initilize all the board variables to +
for (int i=1;i<362;i++)
{
board[i]='+' ;
board[181]='x' ;
}
while (win==0)
{
showboard();
if (turn==true )
{
cout<<p1<<" it's your turn" <<endl;
}
else
{
cout<<p2<<" it's your turn" <<endl;
}
switch (turn)
{
case (false ):
{
board[move()]='o' ;
/*if (board[move()]==board[move()+3]&&board[move()+2]==board[move()+1]&&board[move()+1]=='x')
{
board[move()+1]='+';
board[move()+2]='+';
p2p++;
}
if(board[move()]==board[move()-3]&&board[move()-2]==board[move()-1]&&board[move()-1]=='x')
{
board[move()-1]='+';
board[move()-2]='+';
p2p++;
}
//Program works unless this code is run
*/
turn=true ;
break ;
}
case (true ):
{
board[move()]='x' ;
turn=false ;
break ;
}
}
}
}
int move()
{
int movex;
int movey;
cin>>movex>>movey;
return ((movex+10)+((movey+9)*19));
}
void showboard()
{
cout<<" " <<9876543210123456789<<endl;
cout<<9<< board[ 343 ]<<board[ 344 ]<<board[ 345 ]<<board[ 346 ]<<board[ 347 ]<<board[ 348 ]<<board[ 349 ]<<board[ 350 ]<<board[ 351 ]<<board[ 352 ]<<board[ 353 ]<<board[ 354 ]<<board[ 355 ]<<board[ 356 ]<<board[ 357 ]<<board[ 358 ]<<board[ 359 ]<<board[ 360 ]<<board[ 361 ]<<9<<endl;
cout<<8<< board[ 324 ]<<board[ 325 ]<<board[ 326 ]<<board[ 327 ]<<board[ 328 ]<<board[ 329 ]<<board[ 330 ]<<board[ 331 ]<<board[ 332 ]<<board[ 333 ]<<board[ 334 ]<<board[ 335 ]<<board[ 336 ]<<board[ 337 ]<<board[ 338 ]<<board[ 339 ]<<board[ 340 ]<<board[ 341 ]<<board[ 342 ]<<8<<endl;
cout<<7<< board[ 305 ]<<board[ 306 ]<<board[ 307 ]<<board[ 308 ]<<board[ 309 ]<<board[ 310 ]<<board[ 311 ]<<board[ 312 ]<<board[ 313 ]<<board[ 314 ]<<board[ 315 ]<<board[ 316 ]<<board[ 317 ]<<board[ 318 ]<<board[ 319 ]<<board[ 320 ]<<board[ 321 ]<<board[ 322 ]<<board[ 323 ]<<7<<endl;
cout<<6<< board[ 286 ]<<board[ 287 ]<<board[ 288 ]<<board[ 289 ]<<board[ 290 ]<<board[ 291 ]<<board[ 292 ]<<board[ 293 ]<<board[ 294 ]<<board[ 295 ]<<board[ 296 ]<<board[ 297 ]<<board[ 298 ]<<board[ 299 ]<<board[ 300 ]<<board[ 301 ]<<board[ 302 ]<<board[ 303 ]<<board[ 304 ]<<6<<endl;
cout<<5<< board[ 267 ]<<board[ 268 ]<<board[ 269 ]<<board[ 270 ]<<board[ 271 ]<<board[ 272 ]<<board[ 273 ]<<board[ 274 ]<<board[ 275 ]<<board[ 276 ]<<board[ 277 ]<<board[ 278 ]<<board[ 279 ]<<board[ 280 ]<<board[ 281 ]<<board[ 282 ]<<board[ 283 ]<<board[ 284 ]<<board[ 285 ]<<5<<endl;
cout<<4<< board[ 248 ]<<board[ 249 ]<<board[ 250 ]<<board[ 251 ]<<board[ 252 ]<<board[ 253 ]<<board[ 254 ]<<board[ 255 ]<<board[ 256 ]<<board[ 257 ]<<board[ 258 ]<<board[ 259 ]<<board[ 260 ]<<board[ 261 ]<<board[ 262 ]<<board[ 263 ]<<board[ 264 ]<<board[ 265 ]<<board[ 266 ]<<4<<endl;
cout<<3<< board[ 229 ]<<board[ 230 ]<<board[ 231 ]<<board[ 232 ]<<board[ 233 ]<<board[ 234 ]<<board[ 235 ]<<board[ 236 ]<<board[ 237 ]<<board[ 238 ]<<board[ 239 ]<<board[ 240 ]<<board[ 241 ]<<board[ 242 ]<<board[ 243 ]<<board[ 244 ]<<board[ 245 ]<<board[ 246 ]<<board[ 247 ]<<3<<endl;
cout<<2<< board[ 210 ]<<board[ 211 ]<<board[ 212 ]<<board[ 213 ]<<board[ 214 ]<<board[ 215 ]<<board[ 216 ]<<board[ 217 ]<<board[ 218 ]<<board[ 219 ]<<board[ 220 ]<<board[ 221 ]<<board[ 222 ]<<board[ 223 ]<<board[ 224 ]<<board[ 225 ]<<board[ 226 ]<<board[ 227 ]<<board[ 228 ]<<2<<endl;
cout<<1<< board[ 191 ]<<board[ 192 ]<<board[ 193 ]<<board[ 194 ]<<board[ 195 ]<<board[ 196 ]<<board[ 197 ]<<board[ 198 ]<<board[ 199 ]<<board[ 200 ]<<board[ 201 ]<<board[ 202 ]<<board[ 203 ]<<board[ 204 ]<<board[ 205 ]<<board[ 206 ]<<board[ 207 ]<<board[ 208 ]<<board[ 209 ]<<1<<endl;
cout<<0<< board[ 172 ]<<board[ 173 ]<<board[ 174 ]<<board[ 175 ]<<board[ 176 ]<<board[ 177 ]<<board[ 178 ]<<board[ 179 ]<<board[ 180 ]<<board[ 181 ]<<board[ 182 ]<<board[ 183 ]<<board[ 184 ]<<board[ 185 ]<<board[ 186 ]<<board[ 187 ]<<board[ 188 ]<<board[ 189 ]<<board[ 190 ]<<0<<endl;
cout<<1<< board[ 153 ]<<board[ 154 ]<<board[ 155 ]<<board[ 156 ]<<board[ 157 ]<<board[ 158 ]<<board[ 159 ]<<board[ 160 ]<<board[ 161 ]<<board[ 162 ]<<board[ 163 ]<<board[ 164 ]<<board[ 165 ]<<board[ 166 ]<<board[ 167 ]<<board[ 168 ]<<board[ 169 ]<<board[ 170 ]<<board[ 171 ]<<1<<endl;
cout<<2<< board[ 134 ]<<board[ 135 ]<<board[ 136 ]<<board[ 137 ]<<board[ 138 ]<<board[ 139 ]<<board[ 140 ]<<board[ 141 ]<<board[ 142 ]<<board[ 143 ]<<board[ 144 ]<<board[ 145 ]<<board[ 146 ]<<board[ 147 ]<<board[ 148 ]<<board[ 149 ]<<board[ 150 ]<<board[ 151 ]<<board[ 152 ]<<2<<endl;
cout<<3<< board[ 115 ]<<board[ 116 ]<<board[ 117 ]<<board[ 118 ]<<board[ 119 ]<<board[ 120 ]<<board[ 121 ]<<board[ 122 ]<<board[ 123 ]<<board[ 124 ]<<board[ 125 ]<<board[ 126 ]<<board[ 127 ]<<board[ 128 ]<<board[ 129 ]<<board[ 130 ]<<board[ 131 ]<<board[ 132 ]<<board[ 133 ]<<3<<endl;
cout<<4<< board[ 96 ]<<board[ 97 ]<<board[ 98 ]<<board[ 99 ]<<board[ 100 ]<<board[ 101 ]<<board[ 102 ]<<board[ 103 ]<<board[ 104 ]<<board[ 105 ]<<board[ 106 ]<<board[ 107 ]<<board[ 108 ]<<board[ 109 ]<<board[ 110 ]<<board[ 111 ]<<board[ 112 ]<<board[ 113 ]<<board[ 114 ]<<4<<endl;
cout<<5<< board[ 77 ]<<board[ 78 ]<<board[ 79 ]<<board[ 80 ]<<board[ 81 ]<<board[ 82 ]<<board[ 83 ]<<board[ 84 ]<<board[ 85 ]<<board[ 86 ]<<board[ 87 ]<<board[ 88 ]<<board[ 89 ]<<board[ 90 ]<<board[ 91 ]<<board[ 92 ]<<board[ 93 ]<<board[ 94 ]<<board[ 95 ]<<5<<endl;
cout<<6<< board[ 58 ]<<board[ 59 ]<<board[ 60 ]<<board[ 61 ]<<board[ 62 ]<<board[ 63 ]<<board[ 64 ]<<board[ 65 ]<<board[ 66 ]<<board[ 67 ]<<board[ 68 ]<<board[ 69 ]<<board[ 70 ]<<board[ 71 ]<<board[ 72 ]<<board[ 73 ]<<board[ 74 ]<<board[ 75 ]<<board[ 76 ]<<6<<endl;
cout<<7<< board[ 39 ]<<board[ 40 ]<<board[ 41 ]<<board[ 42 ]<<board[ 43 ]<<board[ 44 ]<<board[ 45 ]<<board[ 46 ]<<board[ 47 ]<<board[ 48 ]<<board[ 49 ]<<board[ 50 ]<<board[ 51 ]<<board[ 52 ]<<board[ 53 ]<<board[ 54 ]<<board[ 55 ]<<board[ 56 ]<<board[ 57 ]<<7<<endl;
cout<<8<< board[ 20 ]<<board[ 21 ]<<board[ 22 ]<<board[ 23 ]<<board[ 24 ]<<board[ 25 ]<<board[ 26 ]<<board[ 27 ]<<board[ 28 ]<<board[ 29 ]<<board[ 30 ]<<board[ 31 ]<<board[ 32 ]<<board[ 33 ]<<board[ 34 ]<<board[ 35 ]<<board[ 36 ]<<board[ 37 ]<<board[ 38 ]<<8<<endl;
cout<<9<< board[ 1 ]<<board[ 2 ]<<board[ 3 ]<<board[ 4 ]<<board[ 5 ]<<board[ 6 ]<<board[ 7 ]<<board[ 8 ]<<board[ 9 ]<<board[ 10 ]<<board[ 11 ]<<board[ 12 ]<<board[ 13 ]<<board[ 14 ]<<board[ 15 ]<<board[ 16 ]<<board[ 17 ]<<board[ 18 ]<<board[ 19 ]<<9<<endl;
cout<<' ' <<9876543210123456789<<endl;
}
Dec 5, 2010 at 11:50am UTC
Hi
your problem is that you repeatedly call move() which waits for input
Ive changed part of the program so that it does not stop
by placing the result into a temp variable it then can be used in the if statements etc.
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
int tempMove = move();
switch (turn)
{
case (false ):
{
board[tempMove]='o' ;
if (board[tempMove]==board[tempMove+3]&&board[tempMove+2]==board[tempMove+1]&&board[tempMove+1]=='x' )
{
board[tempMove+1]='+' ;
board[tempMove+2]='+' ;
p2p++;
}
if (board[tempMove]==board[tempMove-3]&&board[tempMove-2]==board[tempMove-1]&&board[tempMove-1]=='x' )
{
board[tempMove-1]='+' ;
board[tempMove-2]='+' ;
p2p++;
}
//Program works unless this code is run
turn=true ;
break ;
}
case (true ):
{
board[tempMove]='x' ;
turn=false ;
break ;
}
}
Hope this helps
Shredded
Dec 6, 2010 at 8:36pm UTC
Thanks
Topic archived. No new replies allowed.