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(" works "
}
board_f2[x_f] = 8;
return board_f2[x_f];
}
void display_board(int board_f[])
{
printf("%d%d%d\n", board_f[0], board_f[1], board_f[2] );
printf("%d%d%d\n", board_f[3], board_f[4], board_f[5] );
printf("%d%d%d\n", board_f[6], board_f[7], board_f[8] );
}
int place_it( int place_f)
{
puts("enter 4 or 8 or 6 or 2"
scanf("%d", &place_f);
return place_f;
}
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(" works "
}
board_f2[x_f] = 8;
return board_f2[x_f];
}
void display_board(int board_f[])
{
printf("%d%d%d\n", board_f[0], board_f[1], board_f[2] );
printf("%d%d%d\n", board_f[3], board_f[4], board_f[5] );
printf("%d%d%d\n", board_f[6], board_f[7], board_f[8] );
}
int place_it( int place_f)
{
puts("enter 4 or 8 or 6 or 2"
scanf("%d", &place_f);
return place_f;
}