I have a problem programming the way a knight moves!

So I am coding a game called “Knights Out!” The main objective is to have the knight reach every tile on the board. If the knight steps on a tile that has already been touched, he dies. This is my first major project so I am excited!

1 Like

@Goyga can you share a link to your code?

1 Like

https://makecode.com/_0KhajcWwjL4A

1 Like

so, i’m assuming you want to move 1 tile at a time instead of all over the place like you currently have.

well, using a tilemap is the right idea! inside the Scene category of the toolbox, you’ll find 3 nifty blocks:

image

image

image

a tilemap location is a single tile on the tilemap (e.g. a black square or a white square). to move a sprite one tile at a time, we want to place our sprite on a location next to the sprite’s current location. for example:

this will move the sprite to whatever location is to the left of the sprite whenever the left button is pressed!

now, let’s turn the tile red when we move! to make that happen, we can use the “set tile at location” block from the scene category. you’ll also need to open up the tilemap editor and create a red tile in the “my tiles” section

finally, we want to make sure that we can’t move into any walls. we’ll use three new blocks for that: an if statement, a not block, and an “tile at location is wall” block:

multiply this code by 4 (once for each controller direction) and you’ll have something!

1 Like

Would it be possible to get it into the way on how a usual knight moves on a chess board?

@Goyga sure, but that’s a bit tougher…

knights can move 8 different ways which is too many to assign to the direction buttons. you’re going to need some sort of ui to let the player choose where to move. for the purposes of this example let’s do the following:

  1. highlight each of the potential move spots
  2. let the player use the B button to cycle through the spots
  3. move the player to the selected spot when the A button is pressed

first off, we’re going to create a function called “showMoveChoices” which will highlight the legal tiles;
let’s start by creating an array of all the possible moves:

next we loop over those locations and check to see if they are walls. if they aren’t, create a sprite with the kind “PossibleMove” and place it on the location.

image

we’re also going to use a variable to store which move is currently selected. set it to 0 at the bottom of the function:

image

next, we want to write the code to highlight whichever move is currently selected. let’s put this in a function called “highlightSelectedMove”:

image

let’s loop over all of the sprites with kind “PossibleMove” and set their images to be the unselected color. then, we’ll set the image on the currently selected move to be a different color:

make sure to call this function at the bottom of “showMoveChoices”!

now we’re going to cycle through the choices when the B button is pressed. we can use the “remainder of ___ / ___” to make sure we never let our index go out of range:

and finally, inside the A button pressed event we can do the move, clear the sprites, and start the whole process over again:

3 Likes

So now what happens is the array keeps on glitching out. https://makecode.com/_gyk1Cyf6fdh7