GameMaker: Studio

GameMaker: Studio

View Stats:
supware May 26, 2017 @ 10:47am
Chess Bishops!!
Hey guys, I'm building a rudimentary chess engine and for some reason I just can't get the bishop movements to work ): I've managed everything else, including castling and en passants, but I just can't get the diagonal movement down so the bishops/queens don't work.

I thought it would be just like the rook movement, with both x and y instead of just one, but that doesn't seem to be the case. I'll include my rook code below; hopefully someone can find the tweak I need because I'm certainly struggling :L

if pc[msqx,msqy] > 6 or pc[msqx,msqy] = 0 {
if msqx = from_x and msqy != from_y
{for(k = min(msqy,from_y)+1;k < max(msqy,from_y);k ++)
{if pc[msqx,k] != 0 {pc[from_x,from_y] = pc_selected; pc_selected = 0; exit}};
{pc[msqx,msqy] = pc_selected; pc_selected = 0; placed = 1; epx = 99; epy = 99; script_execute(scr_name_move);
if from_x = 0 castle_bq = 0; if from_x = 7 castle_bk = 0; move = 0};}
if msqy = from_y and msqx != from_x
{for(k = min(msqx,from_x)+1;k < max(msqx,from_x);k ++)
{if pc[k,msqy] != 0 {pc[from_x,from_y] = pc_selected; pc_selected = 0; exit}};
{pc[msqx,msqy] = pc_selected; pc_selected = 0; placed = 1; epx = 99; epy = 99; script_execute(scr_name_move);
if from_x = 0 castle_bq = 0; if from_x = 7 castle_bk = 0; move = 0};};}
if placed = 0 {pc[from_x,from_y] = pc_selected; pc_selected = 0}

^code for moving a black rook.

Any ideas are very much appreciated :) thanks for reading
Last edited by supware; May 26, 2017 @ 10:48am
< >
Showing 1-4 of 4 comments
MinorThreat May 27, 2017 @ 9:34am 
Couldn't you spawn an invisible object where you want your bishop to go and then just use a grid or move_towards_point in the bishops code? Or you could use point_direction to find the angle you want to move at.
Last edited by MinorThreat; May 27, 2017 @ 9:48am
supware May 27, 2017 @ 11:24am 
It's on an array already, and there's only one object that draws the board and pieces and deals with all the code - so far, it looks at the destination square and determines two things: 1. the destination square (msqx,msqy) is on the correct vector relative to the 'from' square (from_x,from_y), and 2. the destination square doesn't contain a piece of the same colour as the piece to be placed.

I need to somehow make it check all squares between the 'from' square and the destination square, on the same diagonal (for example if the bishop is moving from c8 to h3, it needs to ckeck that the squares d7, e6, f5 and g4 are empty).

This was easy enough for the rooks - essentially "for(k = from_x+1;k < msqx;k ++) if pc[from_x+k,msqy] != 0 exit". With the bishops I've been trying things like "if pc[max(from_x,msqx)-min(from_x,msqx)+k,max(from_y,msqy)-min(from_y,msqy)] != 0 exit" but they always end up able to make illegal moves or unable to make legal ones ):
Last edited by supware; May 27, 2017 @ 11:27am
MinorThreat May 27, 2017 @ 2:00pm 
You can use point_direction to get the angle and lengthdir_x / lengthdir_y to move. Like this
x += lengthdir_x( dis, dir ); dis is the distance and dir is the direction which you get using point_direction.

By the way you would want to use both x += lengthdir_x and y += lengthdir_y to move diagonal. You can use the the same dir you get from point_direction in both the x and y.
Last edited by MinorThreat; May 27, 2017 @ 7:01pm
supware Jun 1, 2017 @ 1:52am 
I found a nice way to do it using nested for() statements. Thank you for your help!!
< >
Showing 1-4 of 4 comments
Per page: 1530 50

Date Posted: May 26, 2017 @ 10:47am
Posts: 4