Moving player object like chess or checkers.

Hi,
How do I restrict movement on an object so a player can only move it a certain distance in one move?, i.e. chess, checkers.

Thanks,

Greg

Presumably you’ve already worked out the part about getting your piece to move. You need to have an event that’s called when the player decides where they want to move the piece. If you’re moving pieces with the mouse, this’ll probably be OnMouseUp().

In this function, whatever it is, you’ll need to add some code that gets which space the player tried to move to and knows which space it came from and then apply a rule.

I realize this is all very abstract, but without a concrete model it’s the best I can do.

Yeah, this is a pretty open-ended question with any number of answers. It really depends on what you’re doing exactly…might be as simple as limiting a variable with Mathf.Clamp.

–Eric

Thanks guys, it’s taking a lot of trial and error, but I think I’m working it out. I appreciate your time.

-Greg

And also, in some instances … though I may be wildly wrong here and be beaten senseless with “Learn Javascript in 24 Hours” books by enraged programmers who actually know what they’re doing … can’t you also do this sort of thing with an array??? I seem to recall seeing some game porgramming with boards done that way.

I’ll try to answer in a very open way since the question is very open too:

  • Create an array on boolean or integer 8x8
  • Set everything to 0 (false)
  • Set to 1 (true or other flag) possible movements of the piece you take (activate it with the mouse, keyboard or whatever you want)
  • If mouse or analog movement, let the user drop it only on the valid positions
  • If keyboard or direct movement, let the user cycle between the valid positions
  • Get the next movement

any questions ?

Omar Rojo

Terrific! I will have to investigate this and learn more about using an array. Thanks!

Its very easy, the concept is to represent the grid of possible movements and to constraint the input of the player to that, its more easy to look at it that way than on 3D coordinates, areas, or anything else.

If you have any questions, ask them

Omar Rojo

You can also use this type of structure to store exacly what pieces are where by assigning each piece a number, and when you move to the square as above, you set the value of that square in your board matrix to your piece number. (horrible sentence :wink: )

//just an example, wont compile

myBoard[8][8];

whitePawn = 0;
WhiteBishop = 1;
//....
blackPawn = 7;
blackBisop = 8;
//etc...

//put a white pawn at the top left corner of the board
myBoard[0][0] = whitePawn; //put the number 0 here

//put a black bisop on the board
myBoard[2][5] = blackBisop;     //put the number 8 here

//and so on

That way your AI (if any) can “see” the board, and you can also use it to restrict legal and illegal moves.

This is not worded all that great tonight, but I hope it helps :wink:

-Jeremy