2014-04-05 3 views
1

나는 체스 게임의 작은 변종을 창문 형태 C#을 사용하여 만들 것을 배우고있다. 게임은 양면의 졸 뿐이며, 나는 보드를 뽑아 거기에 조각들을 조직했다. 그러나 나는 정직하게 시작하는 법을 모른다. 조각을 마우스로 클릭 한 다음 이동하려는 위치를 클릭하여 이동을 구현합니다.체스 게임에서 동작을 구현하는 방법은 무엇입니까?

검은 전당포가 조각 이름이 참조로

, 흰색 폰은 보드 여기

using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Windows.Forms; 

    namespace AIchess 
    { 
     public partial class Form1 : Form 
     { 
      static System.Drawing.Bitmap piece = AIchess.Properties.Resources.piece; 
      ChessPiece Piece = new ChessPiece(piece, ChessColor.Black); 

      static System.Drawing.Bitmap pieceW = AIchess.Properties.Resources.pieceW; 
      ChessPiece PieceW = new ChessPiece(pieceW, ChessColor.White); 

      Square[,] square = new Square[8, 8]; 
      public Form1() 
      { 
       InitializeComponent(); 
       int i, j; 

       for (i = 0; i < 8; i++) 
       { 
        for (j = 0; j < 8; j++) 
        { 
         this.square[i, j] = new Square(); 
         this.square[i, j].BackColor = System.Drawing.SystemColors.ActiveCaption; 
         this.square[i, j].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; 
         this.square[i, j].Location = new System.Drawing.Point(57 + i * 60, 109 + j * 60); 
         this.square[i, j].Name = i.ToString()+j.ToString(); 
         this.square[i, j].Size = new System.Drawing.Size(60, 60); 
         this.square[i, j].TabIndex = 2; 
         this.square[i, j].TabStop = false; 
         this.Controls.Add(this.square[i, j]); 

         if (j == 1) 
         { 
          this.square[i, j].Image = piece; 
          this.square[i, j].AllocatedBy = "black"; 
         } 
         if (j == 6) 
         { 
          this.square[i, j].Image = pieceW; 
          this.square[i, j].AllocatedBy = "white"; 
         } 

         if (((i+j) % 2) ==0) 
          this.square[i, j].BackColor = Color.RoyalBlue; 
         else 
          this.square[i, j].BackColor = Color.LightBlue; 
        } 
       } 
      } 
     } 



public enum ChessColor 
    { 
     White, 
     Black, 

    }; 

    class ChessPiece 
    { 
     private Image DisplayedImage; 
     private ChessColor DisplayedColor; 
     private Point CurrentSquare; 
     public ChessPiece(Image image, ChessColor color) 
     { 
      DisplayedImage = image; 
      DisplayedColor = color; 
     } 
    } 
     class Square:PictureBox 
     { 
      private bool color; 
      public string AllocatedBy; 
     } 
    } 
+0

32 비트 비트 맵을 사용하세요 무엇합니까처럼'ChessPiece' 클래스보기? –

+0

공개 열거 형 ChessColor { 흰색, 검정, }}; 클래스 체스 조각 { 개인 이미지 DisplayedImage; 비공개 ChessColor DisplayedColor; 개인 포인트 CurrentSquare; 공개 체스 조각 (이미지 이미지, 체스 컬러) { DisplayedImage = image; DisplayedColor = color; } } –

+0

아마 이걸 [gamedev.stackexchange.com] (http://gamedev.stackexchange.com) – James

답변

6

내 코드는 정말 간단한 구현, 나는 희망 당신은 상관하지 않습니다 것입니다 pieceW

여기

라는 나는 처음부터 그것을했다.

분명히 매우 간단합니다. 드래그 앤 드롭과 애니메이션이 없지만 요구 사항을 충족시킵니다.

나는

InitializeGame을 각 부분을 통과하고 설명하겠습니다

  • 당신이 (그들은 분명 동일해야합니다)
  • 당신은에 추가 할 이미지의 크기를 설정합니까 사전 조각 유형/색상 및 비트 맵 간의 관계

참고 : 간단한 일을 유지하기 위해 내가 모든 것을 할 수 있습니다, 당신은

CreateBoard, DrawGame을 좋아하는 비트 맵의 ​​크기를 던져 DrawPieces 거기에 뛰어난

아무것도 할 수 있도록 눈금 크기가 조정됩니다 시간이 사용자가 클릭하지만이 문제의 많은해서는 안됩니다, 그것은 결국 크라이시스 아니다 : D

PickOrDropPiece

이것은 picking/dropping이 일어나는 논리입니다. 정말 사소한 것이고, 저는 여러분이 직접 살펴 보도록하겠습니다. 코드 사이

차이는

나는 조각을 보유하고 Board 유형을 만든 당신은 쉽게 업데이트 할 수 있습니다.

참고 : Piece에서 평등 회원을 삭제하지 마십시오. 사전을 돕기위한 것입니다.

확인 투명 국경

enter image description here

using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      pictureBox1.MouseDown += pictureBox1_MouseDown; 
     } 

     #region Properties 

     private Board Board { get; set; } 
     private Piece CurrentPiece { get; set; } 
     private Dictionary<Piece, Bitmap> PieceBitmaps { get; set; } 
     private int TileWidth { get; set; } 
     private int TileHeight { get; set; } 

     #endregion 

     #region Events 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      InitializeGame(); 
      DrawGame(); 
     } 

     private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      PickOrDropPiece(e); 
      DrawGame(); 
     } 

     #endregion 

     #region Methods 

     private void InitializeGame() 
     { 
      TileWidth = 64; 
      TileHeight = 64; 

      Board = new Board(); 

      PieceBitmaps = new Dictionary<Piece, Bitmap>(); 
      PieceBitmaps.Add(new Piece(PieceType.Pawn, PieceColor.Black), new Bitmap("pawnblack.png")); 
      PieceBitmaps.Add(new Piece(PieceType.Pawn, PieceColor.White), new Bitmap("pawnwhite.png")); 
     } 

     private void DrawGame() 
     { 
      var tileSize = new Size(TileWidth, TileHeight); 
      Bitmap bitmap = CreateBoard(tileSize); 
      DrawPieces(bitmap); 
      pictureBox1.Image = bitmap; 
     } 

     private Bitmap CreateBoard(Size tileSize) 
     { 
      int tileWidth = tileSize.Width; 
      int tileHeight = tileSize.Height; 
      var bitmap = new Bitmap(tileWidth*8, tileHeight*8); 
      using (Graphics graphics = Graphics.FromImage(bitmap)) 
      { 
       for (int x = 0; x < 8; x++) 
       { 
        for (int y = 0; y < 8; y++) 
        { 
         Brush brush = (x%2 == 0 && y%2 == 0) || (x%2 != 0 && y%2 != 0) ? Brushes.Black : Brushes.White; 
         graphics.FillRectangle(brush, new Rectangle(x*tileWidth, y*tileHeight, tileWidth, tileHeight)); 
        } 
       } 
      } 
      return bitmap; 
     } 

     private void DrawPieces(Bitmap bitmap) 
     { 
      using (Graphics graphics = Graphics.FromImage(bitmap)) 
      { 
       Board board = Board; 
       for (int x = 0; x < 8; x++) 
       { 
        for (int y = 0; y < 8; y++) 
        { 
         Piece piece = board.GetPiece(x, y); 
         if (piece != null) 
         { 
          Bitmap bitmap1 = PieceBitmaps[piece]; 

          graphics.DrawImageUnscaled(bitmap1, new Point(x*TileWidth, y*TileHeight)); 
         } 
        } 
       } 
      } 
     } 

     private void PickOrDropPiece(MouseEventArgs e) 
     { 
      Point location = e.Location; 
      int x = location.X/TileWidth; 
      int y = location.Y/TileHeight; 
      bool pickOrDrop = CurrentPiece == null; 
      if (pickOrDrop) 
      { 
       // Pick a piece 
       Piece piece = Board.GetPiece(x, y); 
       Board.SetPiece(x, y, null); 
       if (piece != null) 
       { 
        label1.Text = string.Format("You picked a {0} {1} at location {2},{3}", piece.Color, piece.Type, x, 
         y); 
       } 
       else 
       { 
        label1.Text = "Nothing there !"; 
       } 
       CurrentPiece = piece; 
      } 
      else 
      { 
       // Drop picked piece 
       Board.SetPiece(x, y, CurrentPiece); 
       label1.Text = string.Format("You dropped a {0} {1} at location {2},{3}", CurrentPiece.Color, 
        CurrentPiece.Type, x, 
        y); 
       CurrentPiece = null; 
      } 
     } 

     #endregion 
    } 

    public class Board 
    { 
     private readonly Piece[] _pieces; 

     public Board() 
     { 
      _pieces = new Piece[8*8]; 
      PopulatePieces(); 
     } 

     public Piece GetPiece(int x, int y) 
     { 
      int i = y*8 + x; 
      return _pieces[i]; 
     } 

     public void SetPiece(int x, int y, Piece piece) 
     { 
      int i = y*8 + x; 
      _pieces[i] = piece; 
     } 

     private void PopulatePieces() 
     { 
      for (int i = 0; i < 8; i++) 
      { 
       SetPiece(i, 1, new Piece(PieceType.Pawn, PieceColor.Black)); 
       SetPiece(i, 7, new Piece(PieceType.Pawn, PieceColor.White)); 
      } 
     } 
    } 

    public class Piece 
    { 
     private readonly PieceColor _color; 
     private readonly PieceType _type; 

     public Piece(PieceType type, PieceColor color) 
     { 
      _type = type; 
      _color = color; 
     } 

     public PieceType Type 
     { 
      get { return _type; } 
     } 

     public PieceColor Color 
     { 
      get { return _color; } 
     } 

     protected bool Equals(Piece other) 
     { 
      return _color == other._color && _type == other._type; 
     } 

     public override bool Equals(object obj) 
     { 
      if (ReferenceEquals(null, obj)) return false; 
      if (ReferenceEquals(this, obj)) return true; 
      if (obj.GetType() != GetType()) return false; 
      return Equals((Piece) obj); 
     } 

     public override int GetHashCode() 
     { 
      unchecked 
      { 
       return ((int) _color*397)^(int) _type; 
      } 
     } 

     public static bool operator ==(Piece left, Piece right) 
     { 
      return Equals(left, right); 
     } 

     public static bool operator !=(Piece left, Piece right) 
     { 
      return !Equals(left, right); 
     } 
    } 

    public enum PieceType 
    { 
     Pawn 
    } 

    public enum PieceColor 
    { 
     Black, 
     White 
    } 
} 
관련 문제