gpt4 book ai didi

c# - 如何在棋局中实现移动?

转载 作者:行者123 更新时间:2023-12-01 15:02:08 25 4
gpt4 key购买 nike

我正在学习使用 Windows 窗体 C# 制作一个小型国际象棋游戏变体,该游戏仅包括双方的棋子,我已经画了棋盘并将棋子组织在那里,但老实说我不知道​​如何通过在棋子上单击鼠标,然后在我要移动它的位置开始实现移动。

作为引用,黑棋命名为piece,白棋命名为pieceW

这是我的板代码

    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;
}
}

最佳答案

这是一个非常简单的实现,我希望你不会介意我是从头开始的。

显然它非常简单,没有拖放和动画,但它满足您的要求。

我会逐一介绍并解释它们

初始化游戏

  • 您可以在此处设置图像尺寸(它们显然应该相同)
  • 您在字典中添加作品类型/颜色与您的位图之间的关系

注意:网格会被缩放,所以你可以扔出你喜欢的任何大小的位图

CreateBoard、DrawGame、DrawPieces

那里没有什么特别之处,请注意,为了简单起见,每次用户点击时我都会这样做,但这应该不是什么大问题,毕竟不是孤岛危机:D

PickOrDropPiece

这就是pick/drop发生的逻辑,真的很琐碎,我让你自己看看。

您的代码之间的差异

我创建了一个 Board 类型,它可以容纳各个部分并且您可以轻松更新。

注意:不要删除 Piece 中的相等成员,它们是为了帮助字典。

确保使用带有透明边框的 32 位位图

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
}
}

关于c# - 如何在棋局中实现移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22882110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com