棋盘
using System;
using System.Collections.Generic;

using System.Text;

using System.Drawing;

namespace ChessChallenge
{
    
class Chessboard
    {

        
public const  int EMPTY = 0;
        
public const int BLACK = 1;
        
public const int WHITE = 2;     // 表示空 ,黑棋,白棋


        
public const int Row=15;       //  棋盘 长

        
public const int Col=15;       //  棋盘 宽

        
public int[,] board = new int[Row, Col]; // 棋盘


        
public const int ChessSize = 40;

        Image[] images 
= new Image[3];                      //界面显示要素


        
public Point move = new Point();



        
public Chessboard()
        {
            images[
0= Image.FromFile("background.gif");    //棋盘图片 

            images[
1= Image.FromFile("black.gif");        //黑子图片
            images[2= Image.FromFile("white.gif");         //白子图片

            board.Initialize();
        }





        
public bool PlaceNewChess(int x, int y, int color)
        {
            
if (board[x, y] != EMPTY)
            {
                System.Windows.Forms.MessageBox.Show(
"当前位置不能落子");
                
return false;
            }

            board[x, y] 
= color;

            
return true;
        }



        
int[] colors = new int[] { 021 };

        
int nowColor = Chessboard.WHITE ;

      
        
public bool PlaceNewChess(int x, int y)
        {                       

            
if (PlaceNewChess(x, y, nowColor))
            {
                nowColor 
= colors[nowColor];

                
return true;
            }

            
return false;
        }




        
public void Show(Graphics g)
        {
            g.DrawImage(images[
0], 00);

            
for (int x = 0; x < Row; x++)
            {
                
for (int y = 0; y < Col; y++)
                {
                    
if (board[x, y] > 0)
                    {
                        g.DrawImage(images[board[x, y]], x 
* ChessSize, y * ChessSize);
                    }

                }
            }
        }

    }
}
是否5子连
using System;
using System.Collections.Generic;
using System.Text;

namespace ChessChallenge
{
    
class CalculateBoard
    {

        
private static int SameColorChessCount(int[,] board, int chessX, int chessY, int directionX, int directionY)
        {
            
int color = board[chessX, chessY];

            
int x = chessX;
            
int y = chessY;

            
for (int step = 0; step < 5; step++)
            {
                x 
+= directionX;
                y 
+= directionY;

                
if ((x >= Chessboard.Row) || (x < 0)) return step;
                
if ((y >= Chessboard.Col) || (y < 0)) return step;


                
if (board[x, y] != color) return step;
            }

            
return 5;
        }


        
private static int Calculate(int[,] board, int chessX, int chessY)
        {
            
int[] count = new int[4];

            count[
0= SameColorChessCount(board, chessX, chessY, 01+ SameColorChessCount(board, chessX, chessY, 0-1+ 1;
            count[
1= SameColorChessCount(board, chessX, chessY, 10+ SameColorChessCount(board, chessX, chessY, -10+ 1;
            count[
2= SameColorChessCount(board, chessX, chessY, 11+ SameColorChessCount(board, chessX, chessY, -1-1+ 1;
            count[
3= SameColorChessCount(board, chessX, chessY, 1-1+ SameColorChessCount(board, chessX, chessY, -11+ 1;

            
for (int i = 0; i < 4; i++)
            {
                
if (count[i] >= 5return 5;
            }

            
return 1;
        }




        
public static int Win(int[,] board, int x, int y) //return 1 表示出现了五子连
        {

            
if (Calculate(board, x, y) == 5return board[x, y];
            

            
return 0;
        }


        
public static int Win(int[,] board)               //return 1 表示出现了5子连
        {
            
for (int x = 0; x < Chessboard.Row; x++)
            {
                
for (int y = 0; y < Chessboard.Col; y++)
                {
                    
if (board[x, y] > 0)
                    {
                        
if (Calculate(board, x, y) == 5return board[x, y];
                    }

                }
            }

            
return 0;
        }
    }
}
棋盘的高级模式计算
using System;
using System.Collections.Generic;
using System.Text;

namespace ChessChallenge
{
    
class FCalculateBoard
    {



        
int[][] patterns = new int[500][];   // 500 is enough for 20*20 board



        
string[] blackPattern = new string[] { "11111""011110""01111""10111""11011""11101""11110""01110""010110""011010" };

        
string[] whitePattern = new string[] { "22222""022220""02222""20222""22022""22202""22220""02220""020220""022020" };

        
public int[] FindPattern(string line)
        {


            
int i = 0;
            
for (; i < blackPattern.Length; i++)
            {
                
if (line.IndexOf(blackPattern[i]) >= 0)
                {
                    
break;
                }
            }

            
int j = 0;
            
for (; j < whitePattern.Length; i++)
            {
                
if (line.IndexOf(whitePattern[i]) >= 0)
                {
                    
break;
                }
            }

            
return new int[] { i, j };

        }

        
const int MaxLine = 20;

        
public string GenerateLine(int[,] board, int startX, int startY,int directionX,int directionY)
        {
           
            
int x=startX;
            
int y=startY;

            
string line = board[x,y].ToString();

            
for (int step = 0; step < MaxLine; step++)
            {
                x
+=directionX;
                y
+=directionY;

                
if((x <0|| (x >= Chessboard.Row )) break;
                
if ((y < 0|| (y >= Chessboard.Col)) break;


                line 
+= board[x, y].ToString();
            }

            
return line;
        }


        
public int Calculate(Chessboard board)
        {


            
int patternIndex = 0;

            
//计算每一列的模式 保存在patterns里

            
for (int x = 0; x < Chessboard.Row; x++)
            {
                
string line = "";

                
for (int y = 0; y < Chessboard.Col; y++)
                {
                    line 
+= board.board[x, y].ToString();
                }


                patterns[patternIndex
++= FindPattern(line);

            }



            
//计算每一列的模式 保存在patterns里
            for (int y = 0; y < Chessboard.Col; y++)
            
            {
                
string line = "";

                
for (int x = 0; x < Chessboard.Row; x++)
                {
                    line 
+= board.board[x, y].ToString();
                }

                patterns[patternIndex
++= FindPattern(line);
            }


            
//计算每一主对角线方向的模式 保存在patterns里
            for (int y = 0; y < Chessboard.Col-5; y++)
            {
                
string line =GenerateLine(board.board,0,y,1,1);


                patterns[patternIndex
++= FindPattern(line);
            }

            
for (int x = 0; x < Chessboard.Row - 5; x++)
            {
                
string line = GenerateLine(board.board, x, 011);


                patterns[patternIndex
++= FindPattern(line);
            }


            
//计算每一副对角线方向的模式 保存在patterns里
            for (int y = 5; y < Chessboard.Col ; y++)
            {
                
string line = GenerateLine(board.board, 0, y, -11);


                patterns[patternIndex
++= FindPattern(line);
            }

            
for (int x = 5; x < Chessboard.Row; x++)
            {
                
string line = GenerateLine(board.board, x, 0-11);


                patterns[patternIndex
++= FindPattern(line);
            }





            
return 1;



            
//return count;

        }
    }
}


 

加载玩家DLL

加载玩家DLL
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using System.IO;


namespace ChessChallenge
{
    
class Player
    {

        
object instance = null;

        MethodInfo CommunicateFunc
=null;

        MethodInfo WhereToPlaceFunc 
= null;

        
public bool Load(string dll)
        {
            
try
            {
                Assembly asm 
= System.Reflection.Assembly.LoadFile(dll);

                Type t 
= asm.GetType("ChessPlayer.Player");

                instance 
= asm.CreateInstance("ChessPlayer.Player");
           

                CommunicateFunc 
= t.GetMethod("Communicate");

                WhereToPlaceFunc 
= t.GetMethod("WhereToPlace");

                
return true;
                
            }
            
catch(Exception e)
            {
                
return false;
            }
        }


        
public string Communicate(string sentence)
        {
            
return (string)CommunicateFunc.Invoke(instance, new object[] { sentence} );
        }

        


       
public int[] WhereToPlace(int[,] board, int color)      //轮到你走棋,传入 棋盘 以及你的棋子颜色,  传出 你要走哪里        
        {
            
return (int[])WhereToPlaceFunc.Invoke(instance, new object[] { board,color });
        }
    }
}


玩家DLL管理

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using System.IO;


namespace ChessChallenge
{


    
class PlayerManager
    {

        
public List<Player> playerList = new List<Player>();

       
        
public string[] ParseFilename(string filename)
        {

            
string[] info = filename.Split('\\');

            
string[] fileInfo = info[info.Length - 1].Split('.');

            
return fileInfo;
        }


        
public PlayerManager()
        {
            
            
string[] fileEntries = Directory.GetFiles(Application.StartupPath+"\\player");  //载入player目录下所有动态库

            
foreach (string fileName in fileEntries)
            {
                
string[] info=ParseFilename(fileName);

                Player player 
= new Player();

                
if (player.Load(fileName) == true)
                {
                    playerList.Add( player);
                }
            }
        
        }

    }
}


主界面
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 ChessChallenge
{
    
public partial class Form1 : Form
    {

        PlayerManager playerManger 
= new PlayerManager();


        
public Form1()
        {
            InitializeComponent();

            
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);



            listBox1.Items.Add(
"本人自己点鼠标");
            listBox2.Items.Add(
"本人自己点鼠标");

            
for (int i = 0; i < playerManger.playerList.Count; i++)
            {

                listBox1.Items.Add(playerManger.playerList[i].Communicate(
"name"));

                listBox2.Items.Add(playerManger.playerList[i].Communicate(
"name"));
            }
        }



        Chessboard board 
= new Chessboard();

        Player[] player 
= new Player[2];


        
int turn = 0;

        
private void Restart()
        {
            board 
= new Chessboard();

            player[
0= null;
            player[
1= null;

            
if (listBox1.SelectedIndex > 0) player[0= playerManger.playerList[listBox1.SelectedIndex - 1];
            
if (listBox2.SelectedIndex > 0) player[1= playerManger.playerList[listBox2.SelectedIndex - 1];

            turn 
= 0;

            board.Show(
this.CreateGraphics());

        }

        
private void button1_Click(object sender, EventArgs e)
        {

            Restart();
            
//board.Show(this.CreateGraphics());

            PlayGame();
        }


        
private void PlayGame()
        {

            
if (CalculateBoard.Win(board.board) >0)
            {
                
int index = CalculateBoard.Win(board.board);

                
string[] info = new string[] { "Black Win""White Win" };
                
                MessageBox.Show(info[index
-1]);

                Restart();               

                
return;
            }

            board.Show(
this.CreateGraphics());


            
if (player[turn] == nullreturn;



            
int[] place = player[turn].WhereToPlace(board.board, Chessboard.BLACK);

            board.PlaceNewChess(place[
0], place[1]);

            turn 
= (turn + 1% 2;

            System.Threading.Thread.Sleep(
100);
            PlayGame();


            board.Show(
this.CreateGraphics());
            


        }


        
private void Form1_MouseClick(object sender, MouseEventArgs e)
        {

            
if (player[turn] != nullreturn;


            
int size = Chessboard.ChessSize;


            
int x = e.X / size;
            
int y = e.Y / size;

            
if (!board.PlaceNewChess(x, y)) return;   //落子不成功


            board.Show(
this.CreateGraphics());

            turn 
= (turn + 1% 2;

            PlayGame();


        }

        
private void button2_Click(object sender, EventArgs e)
        {

        }
    }
}


 


 

 

玩家DLL编写例子
using System;
using System.Collections.Generic;

using System.Text;


//4个地方不可修改,其他地方可随意修改, 产生出的ChessPlayer.dll 可随意改名, 放入player目录下,即可载入五子棋对战平台


namespace ChessPlayer       //命名空间不可更改
{
    
public class Player     //类名不可更改
    {


        
//此函数声明不可更改
        public string Communicate(string sentence)   
        {
            
if(sentence.Equals("name",StringComparison.OrdinalIgnoreCase) )   return "Computer001";

            
return "Nothing";
        }




        
//此函数声明不可更改
        public int[] WhereToPlace(int[,] board, int color)      //轮到你走棋,传入 棋盘 以及你的棋子颜色,  传出 你要走哪里    0=empty, 1=black, 2= white
        {
            

            
int Row = board.GetLength(0);
            
int Col = board.GetLength(1);


            
//随机产生一个空位
            Random rand = new Random();
           

            
for (int i = 0; i < 500; i++)
            {
                
int x = rand.Next(Row);

                
int y = rand.Next(Col);

                
if (board[x, y] == 0return new int[] { x, y };
            }

            
return new int[] { 00 };
        }
    }
}


 

 

 

 

posted on 2010-04-22 19:13  feathersky  阅读(635)  评论(6编辑  收藏  举报