Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with some code

Status
Not open for further replies.

KPlateo

Programmer
Mar 14, 2001
6
US
the problem seems to be around line 25, the board prints
out nine 0's and then an eight in the square you move to, however it always leaves an eight in board[0] always, if you could please help me remove this, using djgpp on win98, thanks in advance.

#include <stdio.h>

int board[9] = {8} , place = 0; /* initialises first 0 to 8 in board[0] */

void display_board(int board_f[]); /* displays nine 0's on the screen */
int place_it( int place_f); /* gets a user input */
int move_pman( int place_f2, int board_f2[] ); /* changes board[x_f] to 0, then checks user input for direction */
/* to move and if it goes out of bounds it reverses the move,then*/
/* assigns value 8 to place in board[9]. */
int main()
{
display_board(board);
while (place != 9)
{
place = place_it(place);
board[0] = move_pman( place, board);
display_board(board);
}
return 0;

}

int move_pman( int place_f2, int board_f2[] )
{
static int x_f = 0;
board_f2[x_f] = 0;
switch (place_f2)
{
case 4:{ x_f = x_f - 1;
if (x_f < 0 )
x_f = x_f + 1;
break; }
case 2:{ x_f = x_f + 3;
if (x_f > 8 )
x_f = x_f - 3;
break; }
case 6:{ x_f = x_f + 1;
if (x_f > 8 )
x_f = x_f - 1;
break; }
case 8:{ x_f = x_f - 3;
if (x_f < 0 )
x_f = x_f + 3;
break; }
default: puts(&quot; works &quot;);
}

board_f2[x_f] = 8;
return board_f2[x_f];
}

void display_board(int board_f[])
{
printf(&quot;%d%d%d\n&quot;, board_f[0], board_f[1], board_f[2] );
printf(&quot;%d%d%d\n&quot;, board_f[3], board_f[4], board_f[5] );
printf(&quot;%d%d%d\n&quot;, board_f[6], board_f[7], board_f[8] );
}

int place_it( int place_f)
{
puts(&quot;enter 4 or 8 or 6 or 2&quot;);
scanf(&quot;%d&quot;, &place_f);
return place_f;
}
 
This first line sets the 1st element of the array to 8:

int board[9] = {8}

Then you set whatever element x_f evaluates to to 8:

board_f2[x_f] = 8;

Therefore, 8 is the only possible value for board[0] in your program. You initialize it to 8, then you set it to 8 if x_f evaluates to 0.

Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top