Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends, I have a question about the code below that I have. This code is for the king piece in chess game and I want to have valid moves of king and the image of the king on the board in this codes, but it doesnt work. Can you please help me about this? I have two questions.

1.Should I define anything for validmoves to work because the compiler gives error on validmoves:inaccessible due to its protection level?

2.what should I do for showing the image of king(siyahsah1 is the name of the kingimage) or what is my mistake that it doesnt show the image of it?

If the code below below doesnt help have do I permission to upload and link my project?

Thanks
class PiecePosition
{

  public enum ChessColor
  {
    White,
    Black,
  }
  public class ChessPiece
  {
    private Image DisplayedImage;
    private ChessColor DisplayedColor;
    private Point CurrentSquare;
    private Point[] ValidMoves;
    public ChessPiece(Image image, ChessColor color)
    {
      DisplayedImage = image;
      DisplayedColor = color;
    }
  } 

  public class KingPiece : ChessPiece
  {
    public KingPiece(Image image, ChessColor color)
                : base(image, color)
    {
      ValidMoves[0] = new Point(0, -1);    //  Up 1
      ValidMoves[1] = new Point(1, -1);    //  Up 1, Right 1
      ValidMoves[2] = new Point(1, 0);     //  Right 1

      ValidMoves[7] = new Point(-1, -1);   //  Left 1, Up 1

      System.Drawing.Bitmap siyahsah1 = chess6.Properties.Resources.siyahsah1;
      KingPiece kingPiece = new KingPiece(siyahsah1, ChessColor.White);
  }
Posted
Updated 7-Nov-10 22:38pm
v2
Comments
OriginalGriff 8-Nov-10 7:11am    
"how can i handle the paint event to display image?thanks"
See modified answer
OriginalGriff 13-Nov-10 5:40am    
"sorry that it took long time to comment on your answer..."

See modified answer.

1 solution

1) Change the definition of ValidMoves from privateto protected - this allows it to be visible to derived classes.
See here for details[^]

2) You will have to derive ChessPiece from a control, and handle the paint event to display the image, or modify your form code to display the appropriate pieces.
I would go with the first option, since the chess piece is something that will be interacted with by the user.

"how can i handle the paint event to display image?thanks - arashmobileboy 4 mins ago"

Add an event handler for your Form (or panel - what ever you want to display the piece on) and then use Graphics.DrawImage on the PaintEventArgs Graphics parameter:
C#
private void panel1_Paint(object sender, PaintEventArgs e)
    {
    e.Graphics.DrawImage(myChessPeiceImage, myChessPeiceLocation);
    }


"sorry that it took long time to comment on your answer. should i put siaysah1(the name of kingimage) instead of myChessPeiceImage in your code?because i put the siayhsah1 but it says siaysah1 doesnt exist in the current context(i added the siyahsah1 pic to resources):

private void ChessForm_Paint(object sender, PaintEventArgs e)
   {
    e.Graphics.DrawImage(siyahsah1,chess6.Properties.Resources.siyahsah1);
   }
- arashmobileboy 5 mins ago"

Assuming you have set an image file as an embedded resource called "myPicture", use:
Image myImage = Properties.Resources.myPicture;
e.Graphics.DrawImage(myImage, new Point(0, 0));
 
Share this answer
 
v3
Comments
arashmobileboy 8-Nov-10 7:00am    
how can i handle the paint event to display image?thanks
arashmobileboy 13-Nov-10 5:12am    
sorry that it took long time to comment on your answer. should i put siaysah1(the name of kingimage) instead of myChessPeiceImage in your code?because i put the siayhsah1 but it says siaysah1 doesnt exist in the current context(i added the siyahsah1 pic to resources):

private void ChessForm_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(siyahsah1,chess6.Properties.Resources.siyahsah1);
}
arashmobileboy 13-Nov-10 8:58am    
thanks,i did it,it doesnt gives any error but it doesnt show anything in the board,may it is for the problem in valid moves because compiler gives error for validmoves:ChessPiece.ValidMoves' is never assigned to, and will always have its default value null,how should i solve this problem?thanks
arashmobileboy 14-Nov-10 14:16pm    
anyone cant answer my question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900