# Thread: Does anyone have issues with codeblocks ide

1. Originally Posted by stahta01 So, you still have a lot to learn because that statement means you have no idea of the correct state.

The compiler shipped with Code::Blocks or the compiler being used by Code::Blocks are more accurate.

There is no compiler within Code::Blocks!

Tim S. 2. the revised function so far. I cant think of a way to shorten it any further. the brief for the function is to make sure the piece is moving in the right direction (up for white down for black) so the y values have to reflect this to make sure its a valid move.

Code:
```int calculate_moves(int old_x, int old_y, int new_x, int new_y)
{
int x, value_to_return = -1;

if (whos_turn == false)
{
x = 1;
}
else
{
x = -1;
}
switch (old_x - new_x)
{
case 0:// taking 2 pieces // if ((old_x - new_x == 0) && (new_y - old_y == 4 * x))
if (new_y - old_y == 4 * x)
{
//printf("take 2 pieces");
if(take_piece(old_x, old_y, new_x, new_y) == 0)
{
value_to_return = 0;
break;
}
}
case 1: case -1: // if(((old_x - new_x == 1) || (old_x - new_x == -1)) && (new_y - old_y == 1 * x))
if (new_y - old_y == 1 * x)
{
//printf("move piece\n");
if(move_piece(old_x, old_y, new_x, new_y) == 0)
{
value_to_return = 0;
break;
}
}
case 2: case -2: //if(((old_x - new_x == 2) || (old_x - new_x == -2)) && ((new_y - old_y == 2 * x) || (new_y - old_y == 2 * x)))
if (new_y - old_y == 2 * x) // taking 1 piece
{
//printf("1 piece taken");
if (take_piece(old_x, old_y, new_x, new_y) == 0)
{
value_to_return = 0;
break;
}
}
else if (new_y - old_y == 6 * x) // taking three pieces
{
//printf("three pieces taken\n");
if (take_piece(old_x, old_y, new_x, new_y) == 0)
{
value_to_return = 0;
break;
}
}
case 4: case -4://take 2 pieces in a straight line // if(((old_x - new_x == 4) || (old_x - new_x == -4)) && (new_y - old_y == 4 * x))
if (new_y - old_y == 4 * x)
{
//printf("2 pieces taken in a straight line");
if (take_piece(old_x, old_y, new_x, new_y) == 0)
{
value_to_return = 0;
break;
}
}
case 6: case -6: // take 3 pieces in a straight line //if(((old_x - new_x == 6) || (old_x - new_x == -6)) && (new_y - old_y == 6 * x))
if (new_y - old_y == 6 * x)
{
//printf("3 pieces taken in a straight line");
if (take_piece(old_x, old_y, new_x, new_y) == 0)
{
value_to_return = 0;
break;
}
}
default:
//printf("Illegal move");
value_to_return = -1;
}

return value_to_return;
}```
any feedback greatly appreciated
coop 3. Originally Posted by cooper1200 You do not know enough to know what compiler you are using.
Once you do you should understand which is correct.
Note: In some cases they both are correct.

Tim S. 4. If you find yourself making a giant switch statement of all possible moves like this, stop and think. In any sort of game or simulation, I don't think I've ever found this to be the best or even an acceptable solution. There are too many possible moves in a checkers game to represent in a switch statement. I can't tell if this supports zig-zagging or becoming a king in the middle of a chain of jumps. There's likely no way to solve the problem in a giant switch statement like this, you'll have to break the problem down into smaller chunks.

The first thing I would start with is a function that determines whether a move (a single move, moving one piece once, not an entire turn) is valid. Trying to determine whether a move is valid at the same time as trying to implement that move is going to make for a very convoluted program. This function should return a bool and check for things like whether the move is even on the board (valid x,y coordinates for from and to), whether they're on the correct color squares, whether the move is a diagonal of either 1 or 2 spaces, if it's in the correct direction, if it is 2 spaces, whether it jumps a piece belonging to the other player, etc. If the move the player input is invalid, it should return false. The main loop outside of this function should tell the player it's an invalid move and keep asking for moves until they enter a valid one.

Code:
`bool is_move_valid(int from_x, int from_y, int to_x int to_y, int player)`
Next, write the function that actually performs the move. This function will be pretty short. You can make all the assumptions in the world in this function because you've validated it with the move validation function. So you can just move the piece from the from x,y to the to x,y and if it jumped a piece, remove that piece. It should return a boolean telling whether or not it jumped a piece.

Code:
`bool move_piece(int from_x, int from_y, int to_x, int to_y)`
Now, back in your main loop comes the chained jumps. If the player jumped a piece, take the x,y they just moved to and check if the 4 diagonals in two spaces is a valid move (in other words, if the piece that just moved can take another piece). If it can, give that player another turn, but make sure that the from x,y is always the x,y of the piece that just moved.

Here's how I would approach the main loop to give you an idea of how these functions are used.

Code:
```int player = 0;
bool chain = false;
int chain_x, chain_y;
while(1) {
int from_x, from_y, to_x, to_y;

// If chain is true, from_x,from_y is always the piece that last moved
if(chain) {
from_x = chain_x;
from_y = chain_y;
printf("%d,%d ", from_x, from_y);
scanf("%d,%d", &to_x, &to_y);
} else {
scanf("%d,%d %d,%d", &from_x, &from_y, &to_x, &to_y);
}

// Try again if the move is invalid
if(!is_move_valid(from_x, from_y, to_x, to_y, player)) {
printf("Invalid move.\n");
continue;
}

// Move the piece, chain if it captured a piece and can move again
if(move_piece(from_x, from_y, to_x, to_y)) {
chain_x = to_x;
chain_y = to_y;

// Check the 4 diagonals to see if we can capture another piece
// Chain if any of these 4 moves are valid
chain =
is_valid_move(to_x, to_y, to_x-2, to_y-2, player) ||
is_valid_move(to_x, to_y, to_x+2, to_y-2, player) ||
is_valid_move(to_x, to_y, to_x-2, to_y+2, player) ||
is_valid_move(to_x, to_y, to_x-2, to_y+2, player);
} else {
// We didn't capture a piece, don't chain
chain = false;
}

// If it's not a chain, it's the next player's turn
if(!chain) player = (player+1)%2;
}```

The problem with your function is it's trying to do too much. It's trying to make sure the move is valid, capture multiple pieces and do the moves all at once. Break the problem down into more manageable pieces. 5. here is my program flow in comments rather than code as i have hundreds of lines of code.
Code:
```main
{
// initalize the game ie set up the piece struct arrays for white and black and the piece array and set whos turn to white
//clear screen
//draw board
//play game
}

play game
{
//get co-ordinates of piece (set surrender code here)
//check the co-ordinates are valid ie on the board
//check a piece exists
//get new co-ordinates
//check co-ordinates are on the board
//check its a legal square ie black
//check new square isnt occupied by another piece
//check if king if so call calculate king moves function if not call call calculate piece moves function (what i posed above)
//check the appropriate function returns a valid response ie it worked
// if any check fails start back at getting co-ordinates of the piece
//update piece array and appropriate element of the white or black struct array
//check if a piece has landed on the othr side of the board and make king (making a king ends turn)
//clear screen
//draw board
}
all the above is done by calling different functions and using if else statements apart from the one i posted above that uses the switch.

move piece
{
//update the piece array by marking the old element ' ' (a space) and the new one with either a w or b (W or B if king)
// update the pieces struct array element with the new co-ordinates
}

take piece
{
// create a temp array with in the function and load in the piece array
// check to see if the piece up and left 1 square is black (down and left if blacks turn)
//check to see if the piece up and right 1 square is black (down and right if blacks turn)
// check to see if the "landing square" is free and legal
//if so mark the "taken piece" as i the the temp array and move piece to new position ie 2 away from where it started
//if new co-ordinates match ones supplied by the player break out of loop and return 1 else keep going
if no move is possible ie all the above checks are false break loop and return -1
}```
thats as far as i have got
hope this explains my thought process a bit more. I should add that most of the above statements are a separate function with whos_turn the struct arrays and piece array as global variables. this will be changed in future once i have finished the main bulk of the program (and learnt to deal with pointers etc!!)
coop 6. lines 37 and 38 are meant to say if whites turn not if blacks turn sorry Popular pages Recent additions code, compile, line, missing, wrong 