Friday 15 April 2011

c# - Chess Logic in XNA -


So I have created a 2D Chess Game Board with all the pieces using the XNA Library and all. Although I do not know how to run pieces, if I click them. This is one of my knighthood arguments.

  if pressed (mouse.LeftButton == buttonstate and mouse.x == wknight1.position.X & amp;; Amp; mouse.W == wknight1.position.Y) { }  

How do I select the piece to allow it to move?

I'm not particularly familiar with XNA but I will give some suggestions. For one thing, you probably want to place the piece in the 2D array or instead of being in its own variables (like wknight1). Otherwise, you have to check each variable the user clicks on.

I'm assuming that you want a system, where the user clicks on a piece to select it, then clicks on an empty square to clear it. Here are some pseudo-codes, such as (I mean an empty square here means an empty square):

  if pressed (mouse.LeftButton == buttonstate & amp; Board [mouse.x] [mouse]!! = Null & amp; fragment selected == tap) {piece selection = board [mouse.x] [mouse];; SelectX = mouse.x; SelectY = mouse.y} and if (mouse leftbutton == buttonstate.Pressing & amp; board [mouse.x] [mouse] == blank and select pseudo! = Null) {board [selected x] [ Selected w] == faucet; Board [mouse.x] [mouse.] = Fragment selected; Piece selection = null; }  

You can add in such situations as isAvalidMove (piece type, starks, starkey, finish, finish) where you check that the player is trying to move his piece and It is a legal chess take etc. You can probably make it even more elegant (and / or event-driven) (and add-click-and-drag etc.), but I keep it easy to explain basic logic.


No comments:

Post a Comment