3 Replies - 636 Views - Last Post: 02 May 2014 - 05:48 AM Rate Topic: -----

#1 iNoobCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-April 14

Need help with creating a chess game!

Posted 28 April 2014 - 07:45 PM

Hi all,

I'm pretty new to C# and I've made a project for myself to help further my learning.
Essentially, I want to create a simple chess game. By simple, I mean that I just want basic functionality (i.e: pieces move, pieces can only do valid moves, and etc).
For now, I'd like to leave out anything difficult like AI, castling, and etc.

That being said, I started attempting this project and I've hit a block in the road.
I have several ideas of how to go about this project but I need help understanding how to fully make this project work.

I created an 8x8 White/Black Chess Board using:

        private void Form1_Load_1(object sender, EventArgs e)
        {
            // Variable creation for board 

            Bitmap board = new Bitmap(8 * 80, 8 * 80);
            Graphics Graphic = Graphics.FromImage(board);
            Color color1, color2;

            // For loop that alternates color for tiles on the x-axis

            for (int x = 0; x < 8; x++)
            {
                if (x % 2 == 0)
                {
                    color1 = Color.Black;
                    color2 = Color.White;
                }

                else
                {
                    color1 = Color.White;
                    color2 = Color.Black;
                }

                // Solid Brush Class used to fill shape colors

                SolidBrush black = new SolidBrush(color1);
                SolidBrush white = new SolidBrush(color2);

                // For loop to alternate colors for tiles on y-axis
                for (int y = 0; y < 8; y++)
                {
                    if (y % 2 == 0)
                        Graphic.FillRectangle(black, x * 80, y * 80, 80, 80);
                    else
                        Graphic.FillRectangle(white, x * 80, y * 80, 80, 80);
                }

            }



This works out pretty nicely. Now what I'm confused with is how to put pieces on to each square and how to move and save positions of each piece. For the new and current positions, I figure that an array would be needed to represent the 8x8 board but I'm not sure if this right or if I could just use the above code to represent piece positions.

If I could use the array idea, I created a ChessPiece class and a class for each piece to help define what the piece looks like, what color it is, and possible moves. This is what my code looks like:

        // Piece Creation
        // http://stackoverflow.com/questions/4112769/inserting-image-into-codes-in-chess-game-c-sharp

        // Enumerate to Define What Color a Piece
        public enum PieceColor
        {
            White,
            Black,
        }

        // Attributes for each Chess Piece

        public class ChessPiece
        {
            public Image ChessSprite;
            public PieceColor ColorShown;
            public Point CurrentPosition;
            public Point[] possibleMoves;
        }

        public class King : ChessPiece
        {

            public King(Image ChessSprite, PieceColor ColorShown)
            {
                
                

                // Possible Moves for King Piece

                possibleMoves[0] = new Point(0, 1);    // Up 1
                possibleMoves[1] = new Point(-1, 1);   // Up 1, Left 1
                possibleMoves[2] = new Point(1, 1);    // Up 1, Right 1
                possibleMoves[3] = new Point(1, 0);    // Right 1 
                possibleMoves[4] = new Point(-1, 0);   // Left 1
                possibleMoves[5] = new Point(1, -1);   // Down 1, Right 1
                possibleMoves[6] = new Point(-1, -1);  // Down 1, Left 1
                possibleMoves[7] = new Point(0, -1);   // Down 1         
            }
        } // End King Class




I apologize for the long post but this is what I have so far and I'd like some help with where to go next.
TL;DR: I'm unsure of how to display pieces on each tile and link each piece to my class to define the possible moves that piece can make.
Thank you for your time!

- iNoobCoder

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with creating a chess game!

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Need help with creating a chess game!

Posted 28 April 2014 - 08:18 PM

Separating the game logic (by using an array to represent the board) from the UI is a good start.

Now that you have something to represent the pieces you now need to model the board. Think about what a chess board would need (something to actually represent the board, something to hold piece collections, something that allows you to move the pieces in the representation, something to check validity of moving the piece, etc.). Once you have that, then you can tie the UI to the board (I'd use an Event in the board class to signal the UI that the board has changed).

Now I'd build the UI, which you seem to have already done :)
Was This Post Helpful? 1
  • +
  • -

#3 iNoobCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-April 14

Re: Need help with creating a chess game!

Posted 01 May 2014 - 07:52 PM

View PostMomerath, on 28 April 2014 - 08:18 PM, said:

Separating the game logic (by using an array to represent the board) from the UI is a good start.

Now that you have something to represent the pieces you now need to model the board. Think about what a chess board would need (something to actually represent the board, something to hold piece collections, something that allows you to move the pieces in the representation, something to check validity of moving the piece, etc.). Once you have that, then you can tie the UI to the board (I'd use an Event in the board class to signal the UI that the board has changed).

Now I'd build the UI, which you seem to have already done :)/>



Okay! Thanks for the help I'm starting to understand how to work this game a little better.
I created an 8 x 8 array in order to represent my board. I inputted what pieces would be there initially like so:

// Board Representation, Model 

            // Create an 8x8 Array 

            int[,] chessBoard = new int[8, 8] {

                     {'R', 'N', 'B', 'K', 'Q', 'B', 'N', 'R'}, // 0     Black 
                     {'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'}, // 1     Pieces
                     { 0 ,  0,   0,   0,   0,   0,   0,   0 }, // 2
                     { 0 ,  0,   0,   0,   0,   0,   0,   0 }, // 3
                     { 0 ,  0,   0,   0,   0,   0,   0,   0 }, // 4
                     { 0 ,  0,   0,   0,   0,   0,   0,   0 }, // 5
                     {'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'}, // 6     White
                     {'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'}, // 7     Pieces
                 //   |0|  |1|  |2|  |3|  |4|  |5|  |6|  |7|
                 
            };

            // Array - Piece Assignment (Initial Board State)

            // Black Pieces

            //Royal Row
            chessBoard[0, 0] = 'R'; // Rook
            chessBoard[1, 0] = 'N'; // Knight
            chessBoard[2, 0] = 'B'; // Bishop
            chessBoard[3, 0] = 'Q'; // Queen
            chessBoard[4, 0] = 'K'; // King
            chessBoard[5, 0] = 'B'; // Bishop
            chessBoard[6, 0] = 'N'; // Knight
            chessBoard[7, 0] = 'R'; // Rook

            // Pawn Row
            chessBoard[0, 1] = 'P'; 
            chessBoard[1, 1] = 'P';
            chessBoard[2, 1] = 'P';
            chessBoard[3, 1] = 'P';
            chessBoard[4, 1] = 'P';
            chessBoard[5, 1] = 'P';
            chessBoard[6, 1] = 'P';
            chessBoard[7, 1] = 'P';




But one thing I'm not quite sure is how to get individual squares so I can start assigning pictures to each square keep track of positions. Since I created a bit map, I thought about a for loop and conditionals that would see if the pixels are at a certain position and that could determine the square. Would you mind helping me start this or understand the concept better?
Thank you much for your help already!
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,117
  • Joined: 05-May 12

Re: Need help with creating a chess game!

Posted 02 May 2014 - 05:48 AM

Look over this tutorial: Separating data from GUI - PLUS - serializing the data to XML

Basically don't use the UI to store your data. Use a different data structure to store it. In your case, you have an array. Peek at the array, not at the UI to see what your game state is.

This post has been edited by tlhIn`toq: 11 May 2014 - 07:42 AM
Reason for edit:: Fixed the link

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1