首页

五子棋单机版(C#源码)

c#

2020-9-5

用于同一台电脑两个人操作,可悔棋。有兴趣的可自行添加联机版本(socket协议)


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 FiveConfucianists
{
    public enum Player
    {
        无,
        黑,
        白
    }
    public enum Piece
    {
        无,
        黑子,
        白子
    }
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            for (int row = 1; row <= 15; row  )
            {
                for (int col = 1; col <= 15; col  )
                {
                    PieceAll[row, col] = Piece.无;
                }
            }
            for (int i = 1; i <= 2; i  )
            {
                Bitmaps[i ] = new Bitmap(((Piece)i).ToString()   ".bmp");
            }
        }
        private Point LeftPoint = new Point(30, 30);
        private const int Wide = 60;
        private const int Lenth = 60;
        private Bitmap backGrand = new Bitmap("bck.bmp");
        private Piece[,] PieceAll = new Piece[16, 16];
        private Bitmap[] Bitmaps = new Bitmap[3];
        private int pieceR = 20;
        private Player Player = Player.无;
        List<Step> StepAll = new List<Step>();
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            PaintQP(e.Graphics);
            PaintQz(e.Graphics);
        }

        private void PaintQP(Graphics g)
        {
            Pen thinPen = new Pen(Color.White, 2);
            //Pen fatPen = new Pen(Color.Yellow, 3);
            //g.DrawRectangle(fatPen, LeftPoint.X - 10, LeftPoint.Y - 10, Wide * 14  LeftPoint.X , Lenth * 14-6  LeftPoint.Y-6);
            g.DrawImage(backGrand,new Point(0,0));
            for (int i = 0; i < 15; i  )
            {
                g.DrawLine(thinPen, LeftPoint.X , LeftPoint.Y i*Lenth, Wide * 14   LeftPoint.X , LeftPoint.Y    i * Lenth);
            }
            for (int i = 0; i < 15; i  )
            {
                g.DrawLine(thinPen, LeftPoint.X    i * Wide, LeftPoint.Y , LeftPoint.X    i * Wide, LeftPoint.Y  Lenth*14);
            }
        }

        private void PaintQz(Graphics g)
        { 
            for (int row = 1; row <= 15; row  )
            {
                for (int col = 1; col <=15; col  )
                {
                    if (PieceAll[row, col] != Piece.无)//(2,1)
                    {
                        g.DrawImage(Bitmaps[(int)PieceAll[row, col]], new Point(LeftPoint.X   (col -1)*Wide-pieceR, LeftPoint.Y   (row-1)*Lenth-pieceR));
                    }
                }
            }
        }
        bool IsStart = false;
        private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IsStart = true;
            lbl_player.Text = "白";
            StepAll.Clear();
            for (int row = 1; row <= 15; row  )
            {
                for (int col = 1; col <=15; col  )
                {
                    PieceAll[row, col] = Piece.无;
                }
            }
            Player = Player.白;
            Invalidate();
            //默认白子优先
        }
        bool ConvertPointToRowCol(Point p, out int row, out int col)
        {
            row = (p.Y - LeftPoint.Y) / Lenth   1;
            if (((p.Y - LeftPoint.Y) % Wide) >= Wide / 2)
                row = row   1;
            col = (p.X - LeftPoint.X) / Wide   1;
            if (((p.X - LeftPoint.X) % Wide) >= Wide / 2)
                col = col   1;
            Point chessP = new Point();
            chessP.X = LeftPoint.X   Wide * (col - 1);
            chessP.Y = LeftPoint.Y   Wide * (row - 1);
            double dist = Math.Sqrt(Math.Pow(p.X - chessP.X, 2)   Math.Pow(p.Y - chessP.Y, 2));
            if ((dist <= pieceR) && (row <= 15) && (row >= 1) && (col <= 15) && (col >= 1))
            {
                return true;
            }
            else
            {
                row = 0; col = 0;
                return false;
            }
        }
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (IsStart)
            {
                if (e.Button == MouseButtons.Left)
                {
                    int row, col;
                    bool valid = ConvertPointToRowCol(new Point(e.X, e.Y), out row, out col);
                    if (valid)
                    {
                        if (PieceAll[row, col] == Piece.无)
                        {
                            if (Player == Player.白)
                            {
                                PieceAll[row, col] = Piece.白子;
                            }
                            else
                            {
                                PieceAll[row, col] = Piece.黑子;
                            }
                            //存储步数
                            Step step = new Step();
                            step.Player = Player;
                            step.DropRow = row;
                            step.DropCol = col;
                            StepAll.Add(step);
                            Invalidate();
                            if (IfWin())
                            {
                                MessageBox.Show("玩家:"   Player   "   胜!");
                                lbl_player.Text = "";
                                IsStart = false;
                            }
                            else
                            {
                                if (Player == Player.白)
                                {
                                    Player = Player.黑;
                                    lbl_player.Text = "黑";
                                }
                                else
                                {
                                    Player = Player.白;
                                    lbl_player.Text = "白";
                                }
                            }
                        }
                    }
                }
            }
          
        }
        private bool IfWin()
        {
            bool IsWin = false;
            for(int row = 1; row <= 15; row  )
            {
                for(int col = 1; col <= 11; col  )
                {
                    if (PieceAll[row, col] != Piece.无)
                    {
                        if(PieceAll[row, col] == PieceAll[row , col 1] && PieceAll[row, col] == PieceAll[row , col 2] &&
                           PieceAll[row, col] == PieceAll[row , col 3] && PieceAll[row, col] == PieceAll[row , col 4])
                        {
                            IsWin = true;
                        }
                    }
                }
            }
            for (int col = 1; col <= 15; col  )
            {
                for (int row = 1; row <= 11; row  )
                {
                    if (PieceAll[row, col] != Piece.无)
                    {
                        if (PieceAll[row, col] == PieceAll[row   1, col]&& PieceAll[row, col] == PieceAll[row   2, col]&&
                           PieceAll[row, col] == PieceAll[row   3, col]&& PieceAll[row, col] == PieceAll[row   4, col])
                        {
                            IsWin = true;
                        }
                    }
                }
            }
            for (int col = 1; col <= 10; col  )
            {
                for (int row = 1; row <= 10; row  )
                {
                    if (PieceAll[row, col] != Piece.无)
                    {
                        if (PieceAll[row, col] == PieceAll[row   1, col   1] && PieceAll[row, col] == PieceAll[row   2, col   2] &&
                            PieceAll[row, col] == PieceAll[row   3, col   3] && PieceAll[row, col] == PieceAll[row   4, col   4])
                            IsWin = true;
                    }
                }
            }
            for (int row = 1; row <=11; row  )
            {
                for (int col = 15; col >=5; col--)
                {
                    if (PieceAll[row, col] != Piece.无)
                    {
                        if (PieceAll[row, col] == PieceAll[row   1, col - 1] && PieceAll[row, col] == PieceAll[row   2, col - 2] &&
                          PieceAll[row, col] == PieceAll[row   3, col - 3] && PieceAll[row, col] == PieceAll[row   4, col - 4])
                            IsWin = true;
                    }
                }
            }
            return IsWin;
        }

        private void 悔棋ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (IsStart)
            {
                if (StepAll.Count > 0)
                {
                    Step step = StepAll.Last();
                    if (step.Player == Player.白)
                    {
                        Player = Player.白;
                        PieceAll[step.DropRow, step.DropCol] = Piece.无;
                        lbl_player.Text = "白";
                    }
                    else
                    {
                        Player = Player.黑;
                        PieceAll[step.DropRow, step.DropCol] = Piece.无;
                        lbl_player.Text = "黑";
                    }
                    StepAll.RemoveAt(StepAll.Count - 1);
                    Invalidate();
                }
            }
        }
    }
}

资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
FiveConfucianists/App.config , 189
FiveConfucianists/FiveConfucianists.csproj , 3832
FiveConfucianists/Form1.Designer.cs , 5320
FiveConfucianists/Form1.cs , 9720
FiveConfucianists/Form1.resx , 12747
FiveConfucianists/Program.cs , 529
FiveConfucianists/Properties/AssemblyInfo.cs , 1332
FiveConfucianists/Properties/Resources.Designer.cs , 2847
FiveConfucianists/Properties/Resources.resx , 5612
FiveConfucianists/Properties/Settings.Designer.cs , 1104
FiveConfucianists/Properties/Settings.settings , 249
FiveConfucianists/Step.cs , 326
FiveConfucianists/bin/Debug/FiveConfucianists.exe , 23552
FiveConfucianists/bin/Debug/FiveConfucianists.exe.config , 189
FiveConfucianists/bin/Debug/FiveConfucianists.pdb , 38400
FiveConfucianists/bin/Debug/bck.bmp , 133112
FiveConfucianists/bin/Debug/bitbug_favicon.ico , 4286
FiveConfucianists/bin/Debug/░╫╫╙.bmp , 1748
FiveConfucianists/bin/Debug/║┌╫╙.bmp , 3740
FiveConfucianists/bin/╬σ╫╙╞σú¿╡Ñ╗·░µú⌐.zip , 155846
FiveConfucianists/bitbug_favicon.ico , 4286
FiveConfucianists/obj/Debug/DesignTimeResolveAssemblyReferences.cache , 1443
FiveConfucianists/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache , 7385
FiveConfucianists/obj/Debug/FiveConfucianists.Form1.resources , 4852
FiveConfucianists/obj/Debug/FiveConfucianists.Properties.Resources.resources , 180
FiveConfucianists/obj/Debug/FiveConfucianists.csproj.CoreCompileInputs.cache , 42
FiveConfucianists/obj/Debug/FiveConfucianists.csproj.FileListAbsolute.txt , 1185
FiveConfucianists/obj/Debug/FiveConfucianists.csproj.GenerateResource.cache , 1012
FiveConfucianists/obj/Debug/FiveConfucianists.csprojAssemblyReference.cache , 11490
FiveConfucianists/obj/Debug/FiveConfucianists.exe , 23552
FiveConfucianists/obj/Debug/FiveConfucianists.pdb , 38400
FiveConfucianists/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs , 0
FiveConfucianists/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs , 0
FiveConfucianists/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs , 0
没有账号? 忘记密码?

社交账号快速登录