C#-骑士周游世界

c# 骑士周游世界(人机交互)

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

namespace knight
{
    public partial class Form1 : Form
    {
        static int N = 8, cx = 4, cy = 4, step=1,gw, gh;
        static int[,] board = new int[N, N];
        static List<int[]>  loaction = new List<int[]>();
        public Form1()
        {
            InitializeComponent();
            pictureBox1.Width = (gw = pictureBox1.Width / N) * N;
            pictureBox1.Height = (gh = pictureBox1.Height / N) * N;
            board[4, 4] = step;
            loaction.Add(new int[] { 4, 4, step });
        }
        void DrawBoard(Graphics g)
        {
            g.Clear(Color.White);
            for (int row = 0; row < N; row++)
                for (int col = 0; col < N; col++)
                {
                    if ((row + col) % 2 == 1)
                        g.FillRectangle(Brushes.Black, row * gw, col * gh, gw, gh);
                    if (0 < board[row, col] && board[row, col] <= step)
                    {
                        g.DrawString(board[row, col].ToString(), Font, (row + col) % 2 == 1 ? Brushes.White : Brushes.Black, row*gw,col*gh);
                    }
                    else board[row, col] = 0;
                }
            g.DrawIcon(Icon, cx*gw + 2, cy*gh  + 2);
        }
        void PicBoardMouseUp(object sender, MouseEventArgs e)
        {
            int x = (int)(e.X / gw), y = (int)(e.Y / gh);
            if (board[x, y] == 0 && Math.Abs((x - cx) * (y - cy)) == 2)
                MoveTo(x, y, ++step);
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
        void PicboardPaint(object sender, PaintEventArgs e)
        {
            DrawBoard(e.Graphics);
        }
        private void MnuHelpClick(object sender, EventArgs e)
        {

            Form2 fm2 = new Form2();
            fm2.ShowDialog();
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {   bool flag=false;
            int x, y;
            Point point = pictureBox1.PointToClient(Control.MousePosition);
            x = point.X / gw;
            y = point.Y / gh;
            var current =loaction.Last();
            for(int i = 0; i < N; i++)
            {
                if ((x == current[0] + Knight.jump[i, 0] && y == current[1] + Knight.jump[i, 1]))//判断马是否走日
                {
                    flag = true;
                    break;
                }
            }
            if(Knight.check(x,y)&&flag)//当前棋盘未被踩过,且满足走日的规则
            {
                
                MoveTo(x, y,++step);
            }
        }
        void MoveTo(int x, int y, int s)
        {
            loaction.Add(new int[] { x, y, s });
            cx = x;
            cy = y;
            board[x, y] = s;
            Knight.board[x, y] = s;
            Refresh();
            if (Knight.Weights(x, y) == 0 && step == N * N && Math.Abs((x - Knight.sx) * (y - Knight.sy)) == 2)
            {
                Form4 fm2 = new Form4();
                fm2.ShowDialog();
            }
            if(Knight.Weights(x, y) == 0 && step != N * N)//无路可走
            {
                Form5 form5 = new Form5();
                form5.ShowDialog();
            }                                 
        }
        private void 开始游戏GToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int W = 0; W < N; W++)
                for (int H = 0; H < N; H++)
                {
                    board[W, H] = 0;
                    Knight.board[W, H] = 0;
                }
            step = 1;
            loaction.Clear();
            loaction.Add(new int[] { (cx = gw * 4) / gw, (cy = gh * 4) / gh, board[cx / gw, cy / gh] = Knight.board[Knight.sx = cx / gw, Knight.sy = cy / gh] = step });
            cx = 4;
            cy = 4;
            Refresh();
        }

        private void 后撤一步UToolStripMenuItem_Click(object sender, EventArgs e)
        {
            step--;
            var curretn = loaction.Last();
            board[curretn[0], curretn[1]] = 0;
            Knight.board[curretn[0], curretn[1]] = 0;
            loaction.Remove(curretn);
            var newcurretn = loaction.Last();
            cx = newcurretn [0];
            cy = newcurretn [1];
            Refresh();
        }
        private void exitclick(object sender, EventArgs e)
        {
            this.Close();
        }
        private void MnuHelpClick1(object sender, EventArgs e)
        {

            Form3 fm3 = new Form3();
            fm3.ShowDialog();
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void 菜单MToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 提示IToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var currentweight = new List<int[]>();
            var current = loaction.Last();
            bool flag = false;
            step++;
            for (int i = 0; i < N; i++)
            {
                if (Knight.check(current[0] + Knight.jump[i, 0], current[1] + Knight.jump[i, 1]))
                {
                    currentweight.Add(new int[] { current[0] + Knight.jump[i, 0], current[1] + Knight.jump[i, 1], Knight.Weights(current[0] + Knight.jump[i, 0], current[1] + Knight.jump[i, 1]) });
                }

            }
            currentweight.Sort((x, y) => x[2].CompareTo(y[2]));
            for (int i = 0; i < N; i++)
            {
                if (Knight.Run(currentweight[i][0], currentweight[i][1], step))
                {
                    MoveTo(currentweight[i][0], currentweight[i][1], step);
                    flag=true;
                    break;
                }
            }
            if (!flag)
            {
                step--;
            }
        }
    }
    public static class Knight
    {
        public static int N = 8, sx = 1, sy = 1;
        public static int[,] board = new int[N, N];
        public static int[,] jump = { { 1, 2 }, { 1, -2 }, { -1, 2 }, { -1, -2 }, { 2, 1 }, { -2, 1 }, { 2, -1 }, { -2, -1 } };
        public static bool check(int x, int y)
        {
            return 0 <= x && x < N && 0 <= y && y < N && board[x, y] == 0;
        }
        public static int Weights(int x0, int y0)
        {
            int sum = 0;
            for (int i = 0; i < N; i++)  sum += check(x0 + jump[i, 0], y0 + jump[i, 1]) ? 1 : 0;
            return sum;
        }
        public static bool Run(int x0, int y0, int step)
        {
            board[x0, y0] = step;
            if (step == N * N && Math.Abs((x0 - sx) * (y0 - sy)) == 2) return true;
            var ns = new List<int[]>();
            for (int i = 0, x1, y1; i < N; i++)
                if (check(x1 = x0 + jump[i, 0], y1 = y0 + jump[i, 1]))
                    ns.Add(new int[] { x1, y1, Weights(x1, y1) });
            ns.Sort((x, y) => x[2].CompareTo(y[2]));
            foreach (int[] i in ns) if (Run(i[0], i[1], step + 1)) return true;
            board[x0, y0] = 0;
            return false;
        }
    }
}

游戏规则简介
![游戏界面- 骑士周游世界]

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值