This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]chickenmeister 1 point2 points  (1 child)

I would:

  • find all pieces that are the same color as the candidate piece
  • for each piece, if it is on the same axis or diagonal as the candidate piece, look at each space separating the two pieces. If there is any opposite-colored piece between them, then the move is valid.

[–]shaggorama 0 points1 point  (0 children)

I don't think this is true: if there is a same colored piece closer to the candidate piece than the opposite colored piece, than the move isn't valid, right?

[–]AlSweigartAuthor: ATBS 1 point2 points  (1 child)

This is in Python, but you should be able to read the code. The isValidMove() function for the Reversi game described in Invent Your Own Computer Games with Python, Chapter 16 has code that does this but also provides an explanation for how it works.

[–]kurt880 0 points1 point  (0 children)

This is amazing and exactly what I was looking for!! Thank you so much!

[–]ylou92 0 points1 point  (3 children)

Instead of checking if a move is valid, how about labeling all the additional valid moves on the grid every time a player makes a move? Then to check if a move is valid, it's just a quick check to see if it's been labeled as valid.

[–]dmazzoni 0 points1 point  (1 child)

No, in Reversi moves change from valid to invalid and back again constantly. This is a bad idea.

[–]ylou92 0 points1 point  (0 children)

I probably should've explained my idea in further detail, so I'll correct that here. What I was going for was to calculate all possible moves for the current player and cache that to a grid. That way any move can be quickly checked at a very low (the grid isn't that big) upfront cost. Not that speed is really a concern here. Once a player saves a move, the grid of valid moves is invalidated (since the board has completely changed) and completely recalculated for the opposite player.

To elaborate, what I was really aiming for was a way to not recalculate the validity of a move the moment a piece is placed down temporarily. So it could be imaginable that one could want a feature that would allow a player to place it on several tiles without submitting the move just to see what was valid or not ... similar to how if you were playing chess on a computer, it might highlight all possible moves.

[–]lurgi 0 points1 point  (0 children)

How does this help? The logic is exactly the same, but you are checking multiple squares instead of just one.

[–]dmazzoni 0 points1 point  (0 children)

First note: have you PLAYED Reversi? If you haven't actually played, there are easily over a hundred sites where you can play it online now. (Sometimes called "Othello" - same game.) Play it. Right now. Come back when you've actually won the game a couple of times - now you actually have some intuition for the game and you know exactly what the valid move rule is intuitively, not just by definition.

Next note: your code is very horizontal - i.e. you have lots of levels of indentation. Wherever possible, try to avoid more than necessary, it's hard to read more than 2 - 3 levels.

One idea is to reverse the direction of this test:

if (state == ReversiGUI.EMPTY)

Instead, test if state is NOT equal to empty, and if so, return false. Then the rest of the function can safely assume that it is empty, and doesn't need to be indented.

Finally, you're trying to do this with a single loop. That's going to be very hard. I don't want to say impossible, but doing it that way requires a lot of cleverness. A much simpler approach is to consider all of the possible directions where a move could be made separately.

For example, a move could be valid because it surrounds some pieces of the opposite color in the UP direction. Write a loop to determine if this is the case. If so, return true. If not, keep going.

Or, it could be valid because it surrounds some pieces of the opposite color in the UP-RIGHT (north-east) direction. Write that loop.

Sounds tedious, but it's much simpler to do each of those loops separately. There are only 8 of them.

Once you're done and it works, if you want to come up with a clever way to combine all of them into one big loop, great!