zoukankan      html  css  js  c++  java
  • c#五子棋源码,自己的练习作品

    用label生成棋谱速度好慢,有待改进,另外计算获胜时的四个方法应该可以合并为一个,没有优化整合
    代码
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;

    namespace wzq
    {
        
    public partial class Form1 : Form
        {
            
    public Form1()
            {
                InitializeComponent();
            }

            
    private int person = 0;
            
    private ArrayList blackal = new ArrayList();//黑色坐标记录
            private ArrayList whiteal = new ArrayList();//白色坐标记录
            int startx = 41, starty = 8, step = 26;

            
    private void Form1_Load(object sender, EventArgs e)
            {
                panel1.Visible 
    = false;
            }

            
    #region 生成棋谱
            
    private void loadLocal()
            {
                
    //ArrayList al = new ArrayList();
                int[] alx = new int[15];
                
    int[] aly = new int[15];
                
    for (int i = 0; i < 15; i++)
                {
                    alx[i] 
    = startx + (i * step);
                }

                
    for (int i = 0; i < 15; i++)
                {
                    aly[i] 
    = starty + (i * step);
                }

                
    for (int i = 0; i < 15; i++)
                {
                    
    for (int j = 0; j < 15; j++)
                    {
                        Label lbl 
    = new Label();
                        lbl.BackColor 
    = Color.Transparent;
                        lbl.Text 
    = " ";
                        lbl.Name 
    = "lbl" + i;
                        lbl.Location 
    = new Point(alx[i], aly[j]);
                        lbl.Width 
    = 24;
                        lbl.Height 
    = 23;
                        lbl.Cursor 
    = Cursors.Hand;
                        lbl.MouseClick 
    += new MouseEventHandler(lbl_click);
                        lbl.Font 
    = new Font("宋体"14);
                        panel1.Controls.Add(lbl);
                    }
                }
                panel1.Visible 
    = true;
            }
            
    #endregion

            
    #region 点击棋谱
            
    private void lbl_click(object sender, MouseEventArgs args)
            {
                Label lbl 
    = sender as Label;
                
    if (lbl.Image != null)//查看是否被点过
                {
                    
    return;
                }

                localclass lc 
    = new localclass();
                lc.X 
    = (lbl.Location.X - startx) / step;//获取x坐标
                lc.Y = (lbl.Location.Y - starty) / step;//获取y坐标
                lc.Xsuby = lc.X - lc.Y;
                lc.Xaddy 
    = lc.X + lc.Y;

                
    if (person % 2 == 0)
                {
                    lbl.Image 
    = Properties.Resources.black;
                    lc.Color 
    = "黑方";
                    blackal.Add(lc);
                    iswin(blackal);
                    
                }
                
    else
                {
                    lbl.Image 
    = Properties.Resources.white;
                    lc.Color 
    = "白方";
                    whiteal.Add(lc);
                    iswin(whiteal);
                }
                person
    ++;//黑白切换下棋
            }
            
    #endregion

            
    #region 判断是否胜利
            
    private void iswin(ArrayList al)
            {
                checkX(al);
                checkY(al);
                checkLeft(al);
                checkRight(al);
            }
            
    #endregion

            
    #region 查检是否有水平方向连续5个
            
    private void checkY(ArrayList tal)
            {
                Sortx lc 
    = new Sortx();
                tal.Sort(lc);
    //按x从大到小排序,以便后面做减法时可以判断连续

                Dictionary
    <int, ArrayList> di = new Dictionary<int, ArrayList>();//用于存放相同Y轴的分组
                foreach (object t in tal)//遍历整个数组
                {
                    localclass _t 
    = (localclass)t;
                    
    foreach (object t2 in tal)//嵌套遍历整个数组,用于数组自己与自己对比,得出相同的Y轴数据
                    {
                        localclass _t2 
    = (localclass)t2;
                        
    if (_t.Y == _t2.Y)//通过判断,找出所有Y轴相等的数据
                        {
                            ArrayList temp;
                            di.TryGetValue(_t2.Y, 
    out temp);//从字典中尝试取出当前Y轴的数据列表
                            if (temp == null)//如果以前没有存放过,则生成列表并存入字典中
                            {
                                temp 
    = new ArrayList();
                                temp.Add(_t2);
                                di.Add(_t2.Y, temp);
                            }
                            
    else//如果以前已经有过当前Y轴的列表,则更新字典中的数据
                            {
                                temp.Add(_t2);
                                di.Remove(_t2.Y);
                                di.Add(_t2.Y, temp);
                            }
                        }
                    }
                }

                
    foreach (KeyValuePair<int,ArrayList> d in di)//遍历字典,取出所有Y轴分组,以对比列表中的x是否连续
                {
                    
    int j = 0;//减数
                    int g = 0;//被减数
                    int num = 0;//有几次连续的连续,满5次说明胜利了
                    foreach (localclass cl in ((ArrayList)d.Value))//遍历列表进行对比
                    {
                        g 
    = cl.X;//这里的和上面排序的内容应一致(上面如果是以x轴排序的,这里就应该取X轴)
                        if (j - g == 1)//说明连续
                        {
                            num
    ++;
                            
    if (num >= 4)//第一个没算,所以这里是4,有待多测试
                            {
                                win(cl.Color);
                                
    return;
                            }
                        }
                        
    else
                        {
                            num 
    = 0;
                        }
                        j 
    = g;
                    }
                }
                Console.WriteLine(
    "-----");
            }
            
    #endregion

            
    #region 查检是否有垂直方向连续5个
            
    private void checkX(ArrayList tal)
            {
                Sorty lc 
    = new Sorty();
                tal.Sort(lc);
    //按y从大到小排序,以便后面做减法时可以判断连续

                Dictionary
    <int, ArrayList> di = new Dictionary<int, ArrayList>();//用于存放相同x轴的分组
                foreach (object t in tal)//遍历整个数组
                {
                    localclass _t 
    = (localclass)t;
                    
    foreach (object t2 in tal)//嵌套遍历整个数组,用于数组自己与自己对比,得出相同的Y轴数据
                    {
                        localclass _t2 
    = (localclass)t2;
                        
    if (_t.X == _t2.X)//通过判断,找出所有x轴相等的数据
                        {
                            ArrayList temp;
                            di.TryGetValue(_t2.X, 
    out temp);//从字典中尝试取出当前x轴的数据列表
                            if (temp == null)//如果以前没有存放过,则生成列表并存入字典中
                            {
                                temp 
    = new ArrayList();
                                temp.Add(_t2);
                                di.Add(_t2.X, temp);
                            }
                            
    else//如果以前已经有过当前x轴的列表,则更新字典中的数据
                            {
                                temp.Add(_t2);
                                di.Remove(_t2.X);
                                di.Add(_t2.X, temp);
                            }
                        }
                    }
                }

                
    foreach (KeyValuePair<int, ArrayList> d in di)//遍历字典,取出所有Y轴分组,以对比列表中的y是否连续
                {
                    
    int j = 0;//减数
                    int g = 0;//被减数
                    int num = 0;//有几次连续的连续,满5次说明胜利了
                    foreach (localclass cl in ((ArrayList)d.Value))//遍历列表进行对比
                    {
                        g 
    = cl.Y;//这里的和上面排序的内容应一致(上面如果是以y轴排序的,这里就应该取y轴)
                        if (j - g == 1)//说明连续
                        {
                            num
    ++;
                            
    if (num >= 4)//第一个没算,所以这里是4,有待多测试
                            {
                                win(cl.Color);
                                
    return;
                            }
                        }
                        
    else
                        {
                            num 
    = 0;
                        }
                        j 
    = g;
                    }
                }
                Console.WriteLine(
    "-----");
            }
            
    #endregion

            
    #region 从左往右斜线连续5个(特点是x轴减y轴相等)
            
    private void checkLeft(ArrayList tal)
            {
                Sortx lc 
    = new Sortx();
                tal.Sort(lc);
    //按x从大到小排序,以便后面做减法时可以判断连续

                Dictionary
    <int, ArrayList> di = new Dictionary<int, ArrayList>();//用于存放相同x轴的分组
                foreach (object t in tal)//遍历整个数组
                {
                    localclass _t 
    = (localclass)t;
                    
    foreach (object t2 in tal)//嵌套遍历整个数组,用于数组自己与自己对比,得出相同的Y轴数据
                    {
                        localclass _t2 
    = (localclass)t2;
                        
    if (_t.Xsuby == _t2.Xsuby)//通过判断,找出所有x轴相等的数据
                        {
                            ArrayList temp;
                            di.TryGetValue(_t2.Xsuby, 
    out temp);//从字典中尝试取出当前x轴的数据列表
                            if (temp == null)//如果以前没有存放过,则生成列表并存入字典中
                            {
                                temp 
    = new ArrayList();
                                temp.Add(_t2);
                                di.Add(_t2.Xsuby, temp);
                            }
                            
    else//如果以前已经有过当前x轴的列表,则更新字典中的数据
                            {
                                temp.Add(_t2);
                                di.Remove(_t2.Xsuby);
                                di.Add(_t2.Xsuby, temp);
                            }
                        }
                    }
                }

                
    foreach (KeyValuePair<int, ArrayList> d in di)//遍历字典,取出所有Y轴分组,以对比列表中的y是否连续
                {
                    
    int j = 0;//减数
                    int g = 0;//被减数
                    int num = 0;//有几次连续的连续,满5次说明胜利了
                    foreach (localclass cl in ((ArrayList)d.Value))//遍历列表进行对比
                    {
                        g 
    = cl.X;//这里的和上面排序的内容应一致(上面如果是以x轴排序的,这里就应该取X轴)
                        if (j - g == 1)//说明连续
                        {
                            num
    ++;
                            
    if (num >= 4)//第一个没算,所以这里是4,有待多测试
                            {
                                win(cl.Color);
                                
    return;
                            }
                        }
                        
    else
                        {
                            num 
    = 0;
                        }
                        j 
    = g;
                    }
                }
                Console.WriteLine(
    "-----");
            }
            
    #endregion

            
    #region 从右往左斜线连续5个(特点是x轴加y轴相等)
            
    private void checkRight(ArrayList tal)
            {
                Sortx lc 
    = new Sortx();
                tal.Sort(lc);
    //按x从大到小排序,以便后面做减法时可以判断连续

                Dictionary
    <int, ArrayList> di = new Dictionary<int, ArrayList>();//用于存放相同x轴的分组
                foreach (object t in tal)//遍历整个数组
                {
                    localclass _t 
    = (localclass)t;
                    
    foreach (object t2 in tal)//嵌套遍历整个数组,用于数组自己与自己对比,得出相同的Y轴数据
                    {
                        localclass _t2 
    = (localclass)t2;
                        
    if (_t.Xaddy == _t2.Xaddy)//通过判断,找出所有x轴相等的数据
                        {
                            ArrayList temp;
                            di.TryGetValue(_t2.Xaddy, 
    out temp);//从字典中尝试取出当前x轴的数据列表
                            if (temp == null)//如果以前没有存放过,则生成列表并存入字典中
                            {
                                temp 
    = new ArrayList();
                                temp.Add(_t2);
                                di.Add(_t2.Xaddy, temp);
                            }
                            
    else//如果以前已经有过当前x轴的列表,则更新字典中的数据
                            {
                                temp.Add(_t2);
                                di.Remove(_t2.Xaddy);
                                di.Add(_t2.Xaddy, temp);
                            }
                        }
                    }
                }

                
    foreach (KeyValuePair<int, ArrayList> d in di)//遍历字典,取出所有Y轴分组,以对比列表中的y是否连续
                {
                    
    int j = 0;//减数
                    int g = 0;//被减数
                    int num = 0;//有几次连续的连续,满5次说明胜利了
                    foreach (localclass cl in ((ArrayList)d.Value))//遍历列表进行对比
                    {
                        g 
    = cl.X;//这里的和上面排序的内容应一致(上面如果是以x轴排序的,这里就应该取X轴)
                        if (j - g == 1)//说明连续
                        {
                            num
    ++;
                            
    if (num >= 4)//第一个没算,所以这里是4,有待多测试
                            {
                                win(cl.Color);
                                
    return;
                            }
                        }
                        
    else
                        {
                            num 
    = 0;
                        }
                        j 
    = g;
                    }
                }
                Console.WriteLine(
    "-----");
            }
            
    #endregion

            
    #region 胜利的提示,并重新开始
            
    private void win(string color)
            {
                MessageBox.Show(color 
    + " 获胜!");

                restart();
            }
            
    #endregion

            
    #region 重新开始
            
    private void restart()
            {
                
    foreach (Control ct in panel1.Controls)//清空棋子
                {
                    
    if (ct.GetType().Name == "Label")
                    {
                        Label lbl 
    = ct as Label;
                        lbl.Image 
    = null;
                        lbl.Visible 
    = true;
                    }
                }

                blackal.Clear();
    //清空红方走棋记录
                whiteal.Clear();//清空兰方走棋记录
            }
            
    #endregion

            
    #region 按X排序
            
    private class Sortx : System.Collections.IComparer
            {

                
    public int Compare(object x, object y)
                {

                    
    return ((localclass)y).X - ((localclass)x).X;

                }
            }
            
    #endregion

            
    #region 按Y排序
            
    private class Sorty : System.Collections.IComparer
            {

                
    public int Compare(object x, object y)
                {

                    
    return ((localclass)y).Y - ((localclass)x).Y;

                }
            }
            
    #endregion
               
            
    #region 走棋类
            
    private class localclass
            {
                
    private int x, y, xsuby, xaddy;
                
    private string color;

                
    public string Color
                {
                    
    get { return color; }
                    
    set { color = value; }
                }

                
    public int Xaddy
                {
                    
    get { return xaddy; }
                    
    set { xaddy = value; }
                }

                
    public int Xsuby
                {
                    
    get { return xsuby; }
                    
    set { xsuby = value; }
                }

                
    public int Y
                {
                    
    get { return y; }
                    
    set { y = value; }
                }

                
    public int X
                {
                    
    get { return x; }
                    
    set { x = value; }
                }
            }
            
    #endregion

            
    #region 重新开始按钮
            
    private void button1_Click(object sender, EventArgs e)
            {
                restart();
            }
            
    #endregion

            
    private void Form1_Activated(object sender, EventArgs e)
            {
                loadLocal();
            }
        }
    }


    点击下载源代码
  • 相关阅读:
    php网页,想弹出对话框, 消息框 简单代码
    PHP搜索MYSQL数据库加分页浏览小结
    js验证表单大全
    百度 UEditor 简单安装调试和调用,网上其它的教程太官方了,不适合新手
    百度 迷你版 UMeditor富文本编辑器 使用方法
    mysql开启全文索引功能
    PHP 简单实现MySQL数据搜索、添加数据功能 以设备管理为例
    PHP 只有登陆后才能浏览的简单实现
    mongodb在windows下的安装
    ubuntu上mongodb的安装
  • 原文地址:https://www.cnblogs.com/yeagen/p/1799461.html
Copyright © 2011-2022 走看看