Question

I have a class project which was given to me today and is due in 2 weeks. I have to make a knight from a chess board move around (just the knight, no other pieces) and I have started with this:

import math  
print("This is the program for the knight's movement in chess, Press Y when you are ready to move on.") 
input ("Would you like to move on?") 
position=int(input("What position is your knight in?")) 
print("If your knight is in the position",position,"you may move either 2 forward and 1 left, or 2 forward and 1 right, or the other way so 2 backward 1 left, or 2 backward and 1 right.")

...but I don't know how to move on. Does anybody have any advice?

I am only looking for advice not asking for answers so please don't tell me to si on it I was just stuck with one problem and couldn't find a way around it thanks for reading my problem Regards Roscomadrid

Was it helpful?

Solution

Step back from the code for a moment and spend some time thinking about how to actually approach the problem. There are a few separate steps involved in a problem like this:

1) What data structure will you use to represent the chessboard?

If you asked ten people this question you'd probably get ten different answers. Think of a few different possibilities, and select the one you like the most. One simple example might be an 8x8 array, but what advantages and disadvantages does that have? Might there be anything better?

2) How can I represent a move?

Again, many possible choices. You could have a Move class, a (fromSquare, toSquare) tuple, etc. The answer to this question will depend to some extent on your choice of data structure in part one.

3) How can I generate the set of legal knight moves?

Probably the trickiest aspect. You'll need to devise an algorithm that computes every legal (from, to) pair, using whatever move representation you selected. You will have to pay attention to what happens near the edge of the board to ensure that the knight cannot fall off, or wrap around to the opposite side of the board.

Once you have an answer to all of these, sit back down and work your way through the code slowly. Get one thing working at a time; perhaps the first milestone could be a little printout of the current board position?

Good luck with your assignment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top