简体   繁体   中英

Algorithm C# unity chess game

I'm making a 2D chess game using Unity.

I'm writing the code that tells if the piece can move to this position or not.

Here is the explanations about how I did it:

  • There is a "wallpaper" representing the board.
  • On each square of the "wallpaper", I put an other square with desactivated mesh renderer.
  • When the player select a square, if there is a piece on this position, I activate the mesh renderers of all squares where the piece can go.
  • All squares are stored in a simple array I use to determine possible positions.

With pawns, no problem, it works perfectly, but with rooks or knights, the mouvement is more complicated and I have issues doing this.

here I will work with knights.

When it is in the center of the board, no problem: http://prntscr.com/crcogo

But when I'm on a side of the board, well, check by yourself: http://prntscr.com/crcozu

Here is the code:

case "White_Knight(Clone)_0":
                int Index_Knight_1 = Array.IndexOf(Board, Square_Selected);
                // Les cases en +
                if (Index_Knight_1 + 6 <= 64 )
                {
                    Board[Index_Knight_1 + 6].GetComponent<MeshRenderer>().enabled = true;
                }
                if (Index_Knight_1 + 10 <= 64)
                {
                    Board[Index_Knight_1 + 10].GetComponent<MeshRenderer>().enabled = true;
                }
                if (Index_Knight_1 + 15 <= 64)
                {
                    Board[Index_Knight_1 + 15].GetComponent<MeshRenderer>().enabled = true;
                }
                if (Index_Knight_1 + 17 <= 64)
                {
                    Board[Index_Knight_1 + 17].GetComponent<MeshRenderer>().enabled = true;
                }
                // les cases en -
                if (Index_Knight_1 - 6 >= 0)
                {
                    Board[Index_Knight_1 - 6].GetComponent<MeshRenderer>().enabled = true;
                }
                if (Index_Knight_1 - 10 >= 0)
                {
                    Board[Index_Knight_1 - 10].GetComponent<MeshRenderer>().enabled = true;
                }
                if (Index_Knight_1 - 15 >= 0)
                {
                    Board[Index_Knight_1 - 15].GetComponent<MeshRenderer>().enabled = true;
                }
                if (Index_Knight_1 - 17 >= 0)
                {
                    Board[Index_Knight_1 - 17].GetComponent<MeshRenderer>().enabled = true;
                }

I thnik I don't use the correct way to do this, but I would like to script it myself without taking pieces of code there or there.

In this case, I thought about using multiples of eight, but I'm stuck here :D

Could someone give me a piece of an advice?

Thanks!

Ho, I know it should not be "64" in the conditions, but 63, just saw it.

I agree with Absinte - I am working on a similar game that uses a 7x7 board. It is much easier to work with a 2 dimension array. When one of my pieces is selected, I loop through a list of the moves it COULD make - these are +/- offsets assuming that the piece is in the center. For a pawn, the x,y offsets would be: 0,+1;0,+2(first move only),+1,+1,-1,+1 ( the last two are if capturing ).

I apply each of these to the current x,y location of the piece and if both the x and y are within 0 and 6, then I will display it as a possible location to which the piece could move.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM