8皇后C#语言非地归实现(带界面)

这个是那时候想的算法,用C#写的程序,共享一下:)

运行效果抓图

 

不知道为什么上传文件不好使,那只有粘上来代码了,建立一个WinForm工程把下面代码粘进去。可以找一个自己喜欢的图片作为棋子与exe放在一起,重名为棋子.bmp

 

using  System;
using  System.Drawing;
using  System.Collections;
using  System.ComponentModel;
using  System.Windows.Forms;
using  System.Data;

namespace  bahuanghou
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
    
{
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;

        
public Form1()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();

            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows 窗体设计器生成的代码

        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }



        
private static int QMax=8;
        
private static Point [] places;
        
private static int [,]QiPan=new int[8,8];
        
private System.Windows.Forms.Label label1;
        
private System.Windows.Forms.Button button1;
        
private System.Windows.Forms.Button button2;
        
private System.Windows.Forms.ComboBox comboBox1;
        
private static ArrayList total=new ArrayList();
        
private static Image image;

        
private void Form1_Load(object sender, System.EventArgs e)
        
{
            calculateQueen();
            
for(int i=0;i<total.Count;i++)
                comboBox1.Items.Add(i.ToString());
            image
=Image.FromFile("棋子.bmp");
        }


        
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        
{
            Graphics g
=e.Graphics;
            drawQiPan(g,
40,new Point(5,10));
            
//    drawWangHou(g,new Point(5,10),40,new Point(2,5));
        
//    initForCal();
            Point [] points=(Point [])total[Int32.Parse(label1.Text)];
            
for(int i=0;i<QMax;i++)
            
{
                drawWangHou(g,
new Point(5,10),40,points[i]);
            }

        }

        
private void drawQiPan(Graphics g,int everySize,Point startPos)
        
{
            Brush bb
=Brushes.Black;
            Brush bw
=Brushes.White;
            
for(int j=0;j<8;j++)
            
{
                
for(int i=0;i<4;i++)
                
{
                    g.FillRectangle(bb,
new Rectangle(
                        
new Point(startPos.X+i*everySize*2,
                        startPos.Y
+j*everySize),new Size(everySize,everySize)));
                    g.FillRectangle(bw,
new Rectangle(
                        
new Point(startPos.X+everySize+i*everySize*2,
                        startPos.Y
+j*everySize),new Size(everySize,everySize)));
                }

                Brush t
=bb;
                bb
=bw;
                bw
=t;
            }

        }


        
private void drawWangHou(Graphics g,Point startPos,int size,Point pos)
        
{
            
int x=startPos.X+(pos.X*size);
            
int y=startPos.Y+(pos.Y*size);
        
//    g.FillEllipse(Brushes.Red,new Rectangle(new Point(x,y),new Size(size,size)));
            g.DrawImage(image,new Rectangle(new Point(x,y),new Size(size,size)));
        }


        
public static void initForCal()
        
{
            places
=new Point[QMax];
            
for(int i=0;i<8;i++)
                
for(int j=0;j<8;j++)
                
{
                    QiPan[i,j]
=1;//1 表示棋盘当前可以放棋子
                    
//9 表示棋盘当前已经放子了
                    
//-1 表示棋盘当前位置不可以放子
                    
//-n 表示这是由第n行导致的不能放旗子
                }

        }


        
public static void calculateQueen()
        
{
            
/*逐行扫描
                当push成功时,stack.push(当前坐标)
                当扫描到某行却一次也push不成功时
            
*/

            
int startX=0;//每次开始的x坐标
        
//    int num=0;
            int y=0;
            Stack stack
=new Stack();

            initForCal();
            
while(true)
            
{
                
bool bPush=Push(startX,y);
                
if(bPush)
                
{
                    stack.Push(
new Point(startX,y));
                    y
++;
                    startX
=0;
                    
if(stack.Count==8)
                    
{
                        Stack copyStack
=(Stack)stack.Clone();
                        
for(int i=0;i<places.Length;i++)
                        
{
                            places[i]
=(Point)copyStack.Pop();
                        }

                    
//    if(total.Contains(places))
                    
//    {//如果已经存在
                    
//        break;//跳出循环
                    
//    }
                        string sss=null;
                        
foreach(Point ps in places)
                        
{
                            sss
+=(ps.ToString()+" ");
                        }

                    
//    Console.WriteLine(sss);
                        total.Add(places);
                        places
=new Point[8];
                        Point p
=(Point)stack.Pop();
                        startX
=p.X;
                        y
=p.Y;
                        cancelEffect(startX,y,
1);
                        QiPan[startX,y]
=1;
                        startX
++;
                        
                    }

                }

                
else if(startX>=8)
                
{
                    
if(stack.Count==0)
                    
{
                        
break;
                    }

                    Point p
=(Point)stack.Pop();
                    y
=p.Y;
                    startX
=p.X;
                    
//
                    cancelEffect(startX,y,1);
                    QiPan[startX,y]
=1;
                    startX
++;
                
//    coutQiPan();
                }

                
else if(!bPush && startX<8)
                
{
                    startX
++;
                }

            
//    coutQiPan();
            }

        }


        
private static void coutQiPan()
        
{////this funcation is used to debug
            Console.WriteLine("棋盘:");
            
for(int i=0;i<8;i++)
            
{
                
for(int j=0;j<8;j++)
                
{
                    Console.Write(QiPan[j,i].ToString(
" 0;-#;")+" ");
                }

                Console.Write(
" ");
            }

        }


        
private static void forcancel(int [,] qp,int x,int y,int row,int setValue)
        
{
            
if(qp[x,y]==(0-row))
                qp[x,y]
=setValue;
        }


        
public static void cancelEffect(int x,int y,int flag)//flag=1
        {
            
for(int i=0;i<8;i++)
            
{///将横和竖标记为不可以放棋子了
            //    if(QiPan[x,i]==(0-y))
            
//        QiPan[x,i]=flag;
                forcancel(QiPan,x,i,y,flag);
                forcancel(QiPan,i,y,y,flag);
                
//if(QiPan[y,i]=flag;
            }

            
//下面将斜位置标记为不可以放棋子
            int n=0;
            
while(x-n>=0 && y-n>=0//将左斜上方
            {
                forcancel(QiPan,x
-n,y-n,y,flag);
                
//QiPan[x-n,y-n]=flag;
                n++;
            }

            n
=0;
            
while(x+n<8 && y+n<8//右下
            {
                forcancel(QiPan,x
+n,y+n,y,flag);
                
//QiPan[x+n,y+n]=flag;
                n++;
            }

            n
=0;
            
int k=0;
            
while(x+n<8 && y-k>=0)//右上
            {
                forcancel(QiPan,x
+n,y-k,y,flag);
                
//QiPan[x+n,y-k]=flag;
                n++;k++;
            }

            n
=0;k=0;
            
while(x-n>=0 && y+k<8)//左下
            {
                forcancel(QiPan,x
-n,y+k,y,flag);
            
//    QiPan[x-n,y+k]=flag;
                n++;k++;
            }

            
//            
        }


        
public static bool canPush(int x,int y)
        
{
            
if(x<0 || x>=8 || y<0 || y>=8)return false;
            
return (QiPan[x,y]<=0 || QiPan[x,y]==9 )?false:true;
        }


        
private static void fortag(int[,] qp,int x,int y,int flag)
        
{
            
if(qp[x,y]==1)
                qp[x,y]
=flag;
        }

        
public static void tag(int x,int y,int flag)
        
{
            flag
=-y;
            
for(int i=0;i<8;i++)
            
{///将横和竖标记为不可以放棋子了
            //    QiPan[x,i]=flag;
            
//    QiPan[i,y]=flag;
                fortag(QiPan,x,i,flag);
                fortag(QiPan,i,y,flag);
            }

            
//下面将斜位置标记为不可以放棋子
            int n=0;
            
while(x-n>=0 && y-n>=0//将左斜上方
            {
            
//    QiPan[x-n,y-n]=flag;
                fortag(QiPan,x-n,y-n,flag);
                n
++;
            }

            n
=0;
            
while(x+n<8 && y+n<8//右下
            {
            
//    QiPan[x+n,y+n]=flag;
                fortag(QiPan,x+n,y+n,flag);
                n
++;
            }

            n
=0;
            
int k=0;
            
while(x+n<8 && y-k>=0)//右上
            {
            
//    QiPan[x+n,y-k]=flag;
                fortag(QiPan,x+n,y-k,flag);
                n
++;k++;
            }

            n
=0;k=0;
            
while(x-n>=0 && y+k<8)//左下
            {
            
//    QiPan[x-n,y+k]=flag;
                fortag(QiPan,x-n,y+k,flag);
                n
++;k++;
            }

            
//
        }


        
public static bool Push(int x,int y)
        
{
            
if(!canPush(x,y))
            
{
                
//    MessageBox.Show("shit");
                return false;
            }

            tag(x,y,
-1);//标记出棋盘上不能放棋子的位置
            QiPan[x,y]=9;//将当前位置标记为已经放入棋子
            return true;
        }


        
private void button1_Click(object sender, System.EventArgs e)
        
{
            
int current=Int32.Parse(label1.Text);
            
if(current>=total.Count-1)
                
return;
            
int now=current+1;
            label1.Text
=now.ToString();
            
this.Refresh();
        }


        
private void button2_Click(object sender, System.EventArgs e)
        
{
            
int current=Int32.Parse(label1.Text);
            
if(current<=0)
                
return;
            
int now=current-1;
            label1.Text
=now.ToString();        
            
this.Refresh();
        }


        
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        
{
            
int current=Int32.Parse(comboBox1.Text);
            
if(current>=total.Count || current<0)
                
return;
            
int now=current;
            label1.Text
=now.ToString();
            
this.Refresh();
        }


    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值