鱼C论坛

 找回密码
 立即注册
查看: 2164|回复: 0

求大神解答五子棋的题目

[复制链接]
发表于 2017-6-1 20:47:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这个是C#的五子棋,怎么在里面再加一个东西,这个东西的作用是:随机时间在随机的位置,让这个位置上的棋子闪动三下,如果这个位置没有棋子,则自动产生一个棋子闪动三下然后消失。



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

namespace WindowsFormsApplicationgobang
{
    public partial class Form1 : Form
    {
        private static bool[,] isDraw = new bool[9, 9];//二维数组用于检验当前坐标是否放置了棋子
        private MouseEventArgs e;

        private static int ncount=1;
        //private static bool isBlackStoneWin=false;
        //private static int N = 1;
        private static bool isBlackStoneWin = false;
        private static bool isWhiteStoneWin = false;
        private static bool isBlueStoneWin = false;
        //黑棋
        private const int n = 32;
        private bool isPlayBlackStone;
        private static int nblackstone = 0;
        private static int bcount = 0;
        private BlackStone[] black=new BlackStone[n];
        private static bool[,] isBlackStone = new bool[9, 9];
        
        //白棋
        private bool isPlayWhiteStone;
        private static int nwhitestone = 0;
        private static int wcount = 0;
        private WhiteStone[] white = new WhiteStone[n];
        private static bool[,] isWhiteStone = new bool[9, 9];




        public Form1()
        {
            InitializeComponent();
            InitBlackStone();
            InitWhiteStone();
            InitRedStone();
            labelBlack.BackColor = Color.Red;
            labelBlack.Text = "黑方下";
        }

        public void InitBlackStone()
        {
            isPlayBlackStone = false;
            for (int i = 0; i < n; i++)
            {
                black[i] = new BlackStone();
                black[i].X = -10;
                black[i].Y = -10;
            }
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    isDraw[i, j] = false;
                    isBlackStone[i, j] = false;
                }
            }
        }
        public void InitWhiteStone()
        {
            isPlayWhiteStone = false;
            for (int i = 0; i < n; i++)
            {
                white[i] = new WhiteStone();
                white[i].X = -10;
                white[i].Y = -10;
            }
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    isWhiteStone[i, j] = false;
                }
            }
        }

        public void PlayBlackStone(MouseEventArgs e)      
        {
            int x = panelGobang.ClientRectangle.X;
            int y = panelGobang.ClientRectangle.Y;
            int[] gobangX = new int[9];
            int[] gobangY = new int[9];
           
            for (int i = 0; i < 9; i++)
            {
                gobangX[i] = x + (i + 1) * 40;
            }
            for (int i = 0; i < 9; i++)
            {
                gobangY[i] = y + (i + 1) * 40;
            }
            for (int i = 0; i < 9; i++)
            {
                if ((e.X >= gobangX[i] - 10) && (e.X <= gobangX[i] + 10))
                {
                    for (int j = 0; j < 9; j++)
                    {
                        if ((e.Y >= gobangY[j] - 10) && (e.Y <= gobangY[j] + 10))
                        {
                            black[bcount].X = gobangX[i];
                            black[bcount].Y = gobangY[j];
                            isPlayBlackStone = true;
                            break;
                        }
                    }
                    break;
                }
            }
        }
        public void PlayWhiteStone(MouseEventArgs e)
        {
            int x = panelGobang.ClientRectangle.X;
            int y = panelGobang.ClientRectangle.Y;
            int[] gobangX = new int[9];
            int[] gobangY = new int[9];

            for (int i = 0; i < 9; i++)
            {
                gobangX[i] = x + (i + 1) * 40;
            }
            for (int i = 0; i < 9; i++)
            {
                gobangY[i] = y + (i + 1) * 40;
            }
            for (int i = 0; i < 9; i++)
            {
                if ((e.X >= gobangX[i] - 10) && (e.X <= gobangX[i] + 10))
                {
                    for (int j = 0; j < 9; j++)
                    {
                        if ((e.Y >= gobangY[j] - 10) && (e.Y <= gobangY[j] + 10))
                        {
                            white[wcount].X = gobangX[i];
                            white[wcount].Y = gobangY[j];
                            isPlayWhiteStone = true;
                            break;
                        }
                    }
                    break;
                }
            }
        }
        public bool IsBlackStoneWinSR()
        {
            int N = 1;
            int x1 = black[bcount].X - 40;
            int x2 = black[bcount].X + 40;
            int y1 = black[bcount].Y + 40;
            int y2 = black[bcount].Y - 40;
            while (x1 >= 40 && y1 <= 360)
            {
                if (N == 5)
                {
                    break;
                }
                if (isBlackStone[x1 / 40 - 1, y1 / 40 - 1] != false)
                {
                    N++;
                    x1 = x1 - 40;
                    y1 = y1 + 40;
                }
                else
                {
                    break;
                }
            }
            if (N < 5)
            {
                while (x2 <= 360 && y2 >= 40)
                {
                    if (N == 5)
                    {
                        break;
                    }
                    if (isBlackStone[x2 / 40 - 1, y2 / 40 - 1])
                    {
                        N++;
                        x2 = x2 + 40;
                        y2 = y2 - 40;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (N == 5)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool IsBlackStoneWinSL()
        {
            int N = 1;
            int x1 = black[bcount].X - 40;
            int x2 = black[bcount].X + 40;
            int y1 = black[bcount].Y - 40;
            int y2 = black[bcount].Y + 40;
            while (x1>=40&&y1 >= 40)
            {
                if (N == 5)
                {
                    break;
                }
                if (isBlackStone[x1 / 40 - 1, y1 / 40 - 1] != false)
                {
                    N++;
                    x1 = x1 - 40;
                    y1 = y1 - 40;
                }
                else
                {
                    break;
                }
            }
            if (N < 5)
            {
                while (x2<=360&&y2 <= 360)
                {
                    if (N == 5)
                    {
                        break;
                    }
                    if (isBlackStone[x2 / 40 - 1, y2 / 40 - 1])
                    {
                        N++;
                        x2 = x2 + 40;
                        y2 = y2 + 40;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (N == 5)
            {
                return true;
            }
            else
            {
                return IsBlackStoneWinSR();
            }
        }
        public bool IsBlackStoneWinY()
        {
            int N = 1;
            int y1 = black[bcount].Y - 40;
            int y2 = black[bcount].Y + 40;
            while (y1 >= 40)
            {
                if (N == 5)
                {
                    break;
                }
                if (isBlackStone[black[bcount].X / 40 - 1, y1 / 40 - 1] != false)
                {
                    N++;
                    y1 = y1 - 40;
                }
                else
                {
                    break;
                }
            }
            if (N < 5)
            {
                while (y2 <= 360)
                {
                    if (N == 5)
                    {
                        break;
                    }
                    if (isBlackStone[black[bcount].X / 40 - 1, y2 / 40 - 1])
                    {
                        N++;
                        y2 = y2 + 40;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (N == 5)
            {
                return true;
            }
            else
            {
                return IsBlackStoneWinSL();
            }
        }
        public bool IsBlackStoneWinX()
        {
            int N = 1;
            int x1 = black[bcount].X - 40;
            int x2 = black[bcount].X + 40;
            while (x1 >= 40)
            {
                if (N == 5)
                {
                    break;
                }
                if (isBlackStone[x1 / 40 - 1, black[bcount].Y / 40 - 1] != false)
                {
                    N++;
                    x1 = x1 - 40;
                }
                else
                {
                    break;
                }
            }
            if (N < 5)
            {
                while (x2 <= 360)
                {
                    if (N == 5)
                    {
                        break;
                    }
                    if (isBlackStone[x2 / 40 - 1, black[bcount].Y / 40 - 1])
                    {
                        N++;
                        x2 = x2 + 40;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (N == 5)
            {
                return true;
            }
            else
            {
                return IsBlackStoneWinY();
            }
        }

        public bool IsWhiteStoneWinSR()
        {
            int N = 1;
            int x1 =white[wcount].X - 40;
            int x2 = white[wcount].X + 40;
            int y1 = white[wcount].Y + 40;
            int y2 = white[wcount].Y - 40;
            while (x1 >= 40 && y1 <= 360)
            {
                if (N == 5)
                {
                    break;
                }
                if (isWhiteStone[x1 / 40 - 1, y1 / 40 - 1] != false)
                {
                    N++;
                    x1 = x1 - 40;
                    y1 = y1 + 40;
                }
                else
                {
                    break;
                }
            }
            if (N < 5)
            {
                while (x2 <= 360 && y2 >= 40)
                {
                    if (N == 5)
                    {
                        break;
                    }
                    if (isWhiteStone[x2 / 40 - 1, y2 / 40 - 1])
                    {
                        N++;
                        x2 = x2 + 40;
                        y2 = y2 - 40;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (N == 5)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool IsWhiteStoneWinSL()
        {
            int N = 1;
            int x1 = white[wcount].X - 40;
            int x2 = white[wcount].X + 40;
            int y1 = white[wcount].Y - 40;
            int y2 = white[wcount].Y + 40;
            while (x1 >= 40 && y1 >= 40)
            {
                if (N == 5)
                {
                    break;
                }
                if (isWhiteStone[x1 / 40 - 1, y1 / 40 - 1] != false)
                {
                    N++;
                    x1 = x1 - 40;
                    y1 = y1 - 40;
                }
                else
                {
                    break;
                }
            }
            if (N < 5)
            {
                while (x2 <= 360 && y2 <= 360)
                {
                    if (N == 5)
                    {
                        break;
                    }
                    if (isWhiteStone[x2 / 40 - 1, y2 / 40 - 1])
                    {
                        N++;
                        x2 = x2 + 40;
                        y2 = y2 + 40;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (N == 5)
            {
                return true;
            }
            else
            {
                return IsWhiteStoneWinSR();
            }
        }
        public bool IsWhiteStoneWinY()
        {
            int N = 1;
            int y1 = white[wcount].Y - 40;
            int y2 = white[wcount].Y + 40;
            while (y1 >= 40)
            {
                if (N == 5)
                {
                    break;
                }
                if (isWhiteStone[white[wcount].X / 40 - 1, y1 / 40 - 1] != false)
                {
                    N++;
                    y1 = y1 - 40;
                }
                else
                {
                    break;
                }
            }
            if (N < 5)
            {
                while (y2 <= 360)
                {
                    if (N == 5)
                    {
                        break;
                    }
                    if (isWhiteStone[white[wcount].X / 40 - 1, y2 / 40 - 1])
                    {
                        N++;
                        y2 = y2 + 40;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (N == 5)
            {
                return true;
            }
            else
            {
                return IsWhiteStoneWinSL();
            }
        }
        public bool IsWhiteStoneWinX()
        {
            int N = 1;
            int x1 = white[wcount].X - 40;
            int x2 = white[wcount].X + 40;
            while (x1 >= 40)
            {
                if (N == 5)
                {
                    break;
                }
                if (isWhiteStone[x1 / 40 - 1, white[wcount].Y / 40 - 1] != false)
                {
                    N++;
                    x1 = x1 - 40;
                }
                else
                {
                    break;
                }
            }
            if (N < 5)
            {
                while (x2 <= 360)
                {
                    if (N == 5)
                    {
                        break;
                    }
                    if (isWhiteStone[x2 / 40 - 1, white[wcount].Y / 40 - 1])
                    {
                        N++;
                        x2 = x2 + 40;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (N == 5)
            {
                return true;
            }
            else
            {
                return IsWhiteStoneWinY();
            }
        }

        public void RenewStone()
        {
            bcount = 0;
            nblackstone = 0;
            isPlayBlackStone = false;
            for (int i = 0; i < n; i++)
            {
                //black[i] = new BlackStone();
                black[i].X = -10;
                black[i].Y = -10;
                //white[i] = new WhiteStone();
                white[i].X = -10;
                white[i].Y = -10;
            }
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    isDraw[i, j] = false;
                    isBlackStone[i, j] = false;
                }
            }
            ncount = 1;
            wcount = 0;
            nwhitestone = 0;
            isPlayWhiteStone = false;
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    isWhiteStone[i, j] = false;
                }
            }
        }

        private void panelGobang_Paint(object sender, PaintEventArgs e)
        {
            int x = panelGobang.ClientRectangle.X;
            int y = panelGobang.ClientRectangle.Y;
            for (int i = 0; i < 11; i++)
            {
               e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), new Point(x, y + 40 * i), new Point(x + 400, y + 40 * i));
            }
            for (int i = 0; i < 11; i++)
            {
               e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), new Point(x + 40 * i, y), new Point(x + 40 * i, y + 400));
            }
            for (int i = 0; i < nblackstone; i++)
            {
               black[i].DrawBlackStone(e.Graphics);
            }
            for (int i = 0; i < nwhitestone; i++)
            {
                white[i].DrawWhiteStone(e.Graphics);
            }
            if (ncount % 2 != 0)
            {
                if (isBlackStoneWin == true)
                {
                    isBlackStoneWin = false;
                    labelBlack.Text = "黑方下";
                }
                if (isWhiteStoneWin == true)
                {
                    isWhiteStoneWin = false;
                    labelBlack.Text="黑方下";
                    labelWhite.Text = "";
                }
                if (isPlayBlackStone)
                {
                    if (!isDraw[(black[bcount].X) / 40 - 1, (black[bcount].Y) / 40 - 1])
                    {
                        black[bcount].DrawBlackStone(e.Graphics);
                        nblackstone = bcount + 1;
                        isPlayBlackStone = false;
                        isDraw[(black[bcount].X) / 40 - 1, (black[bcount].Y) / 40 - 1] = true;
                        isBlackStone[(black[bcount].X) / 40 - 1, (black[bcount].Y) / 40 - 1] = true;      
                        if (IsBlackStoneWinX() == true)
                        {
                            MessageBox.Show("黑方获胜");
                            RenewStone();
                            isBlackStoneWin = true;
                            //labelBlack.Text = "";
                            //labelWhite.Text = "白方下";
                            panelGobang.Invalidate();
                            return;
                        }
                        bcount++;
                        ncount++;
                        labelWhite.BackColor = Color.Red;
                        labelWhite.Text = "白方下";
                        labelBlack.Text = "";
                    }
                }
            }
            else
            {
                if (isPlayWhiteStone)
                {
                    if (!isDraw[(white[wcount].X) / 40 - 1, (white[wcount].Y) / 40 - 1])
                    {
                        white[wcount].DrawWhiteStone(e.Graphics);
                        nwhitestone = wcount + 1;
                        isPlayWhiteStone = false;
                        isDraw[(white[wcount].X) / 40 - 1, (white[wcount].Y) / 40 - 1] = true;
                        isWhiteStone[(white[wcount].X) / 40 - 1, (white[wcount].Y) / 40 - 1] = true;
                        if (IsWhiteStoneWinX() == true)
                        {
                            MessageBox.Show("白方获胜");
                            RenewStone();
                            isWhiteStoneWin = true;
                            //labelBlack.Text = "";
                            //labelWhite.Text = "白方下";
                            panelGobang.Invalidate();
                            labelWhite.Text = "白方下";
                            return;
                        }
                        wcount++;
                        ncount++;
                        labelBlack.BackColor = Color.Red;
                        labelBlack.Text = "黑方下";
                        labelWhite.Text = "";
                    }
                }
            }
        }

        private void panelGobang_MouseClick(object sender, MouseEventArgs e)
        {
            if (ncount % 2 != 0)
            {
                if (bcount >= n)
                {
                    if (wcount == n)
                    {
                        MessageBox.Show("您的棋子己用完!");
                        isPlayBlackStone = false;
                        return;
                    }
                }
                this.e = e;
                PlayBlackStone(e);
                panelGobang.Invalidate();
            }
            else
            {
                if (wcount >= n)
                {
                    MessageBox.Show("您的棋子己用完!");
                    isPlayWhiteStone = false;
                    return;
                }
                this.e = e;
                PlayWhiteStone(e);
                panelGobang.Invalidate();
            }
        }
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 21:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表