?? gobang.cs
字號:
?using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
namespace gobang
{
public partial class GoBang : Form
{
Socket m_socWorker;
chess_color color;
private bool myTurn;
int[][] chess_board;
int[][] chess_board_record;
byte[] mes1;
byte[] mes4;
byte[] chess_info;
bool isRequireStart = false, isRequireOrder = false;
int[][] posx;
int[][] posy;
private Bitmap map;
byte md;
int i = 0;
public GoBang()
{
InitializeComponent();
map = new Bitmap(455, 455);
this.pictureBox1.Image = (Image)map;
}
private void initial()
{
mes1 = new byte[1];
mes4 = new byte[4];
chess_info = new byte[8];
chess_board = new int[15][];
chess_board_record = new int[15][];
posx = new int[15][];
posy = new int[15][];
for (int i = 0; i < 15; i++)
{
chess_board[i] = new int[15];
chess_board_record[i] = new int[15];
posx[i] = new int[15];
posy[i] = new int[15];
}
int x = 17, y = 17;
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
posx[i][j] = x;
posy[i][j] = y;
x += 30;
}
x = 17;
y += 30;
}
try
{
//create a new client socket ...
m_socWorker = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
String szIPSelected = "127.0.0.1";
String szPort = "9527";
int alPort = System.Convert.ToInt16(szPort, 10);
System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse(szIPSelected);
System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, alPort);
m_socWorker.Connect(remoteEndPoint);
}
catch (System.Net.Sockets.SocketException se)
{
MessageBox.Show(se.Message);
}
}
private void sendData(byte[] require)
{
try
{
m_socWorker.Send(require);
}
catch (System.Net.Sockets.SocketException se)
{
MessageBox.Show(se.Message);
}
}
private void start_Click(object sender, EventArgs e)
{
md = MessageDefine.COMM_MSG_FIRST;
start.Enabled = false;
startse.Enabled = false;
initial();
gameUnstarted();
}
private void startse_Click(object sender, EventArgs e)
{
md = MessageDefine.COMM_MSG_SECOND;
start.Enabled = false;
startse.Enabled = false;
initial();
gameUnstarted();
}
private void gameUnstarted()
{
bool notBegin = true;
while (notBegin)
{
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
chess_board_record[i][j] = 0;
}
}
m_socWorker.Receive(mes1);
switch (mes1[0])
{
case MessageDefine.COMM_MSG_FIRST:
myTurn = true;
color = chess_color.Chess_Clr_Black;
pictureBox2.Image = global::gobang.Properties.Resources.dib4;
break;
case MessageDefine.COMM_MSG_SECOND:
myTurn = false;
color = chess_color.Chess_Clr_White;
pictureBox2.Image = global::gobang.Properties.Resources.dib2;
break;
case MessageDefine.COMM_MSG_REJECTED:
m_socWorker.Receive(mes4);
break;
case MessageDefine.COMM_MSG_GAME_START:
notBegin = false;
break;
}
if (!isRequireOrder && !isRequireStart)
{
byte[] mes = new byte[1];
mes[0] = md;
m_socWorker.Send(mes);
isRequireOrder = true;
}
else if (isRequireOrder && !isRequireStart)
{
byte[] mes = new byte[1];
mes[0] = MessageDefine.COMM_MSG_GAME_REQUIRE_START;
m_socWorker.Send(mes);
isRequireStart = true;
}
}
gameStarted();
}
private void gameStarted()
{
byte[] index = { 0, 0 };
bool isNotEnd = true;
while (isNotEnd)
{
if (myTurn == true)
{
if (i == 16)
i = 0;
repeat:
index = getIndex();
if (index == null)
return;
chess_info[1] = (byte)(color - 1);
chess_info[2] = index[0];
chess_info[3] = index[1];
mes1[0] = MessageDefine.COMM_MSG_CHESS;
m_socWorker.Send(mes1);
m_socWorker.Send(chess_info);
m_socWorker.Receive(mes1);
switch (mes1[0])
{
case MessageDefine.COMM_MSG_ACCEPT:
break;
case MessageDefine.COMM_MSG_REJECTED:
m_socWorker.Receive(mes4);
if (mes4[1] == MessageDefine.SOLUTION_ACTION_REPEAT)
{
chess_board_record[index[0]][index[1]] = -1;
goto repeat;
}
break;
case MessageDefine.COMM_MSG_LOSE:
label2.Text = "status: Lose";
isNotEnd = false;
break;
case MessageDefine.COMM_MSG_WIN:
label2.Text = "status: Win";
isNotEnd = false;
break;
case MessageDefine.COMM_MSG_DRAW:
label2.Text = "status: Draw";
isNotEnd = false;
break;
case MessageDefine.COMM_MSG_TIMEOUT:
label2.Text = "status:Time Out, Lose";
m_socWorker.Receive(mes1);
isNotEnd = false;
break;
default:
MessageBox.Show("1:" + mes1[0].ToString());
break;
}
i++;
}
if (isNotEnd == false)
continue;
m_socWorker.Receive(mes1);
switch (mes1[0])
{
case MessageDefine.COMM_MSG_REJECTED:
m_socWorker.Receive(mes4);
break;
case MessageDefine.COMM_MSG_LOSE:
label2.Text = "status: Lose";
isNotEnd = false;
break;
case MessageDefine.COMM_MSG_WIN:
label2.Text = "status: Win";
isNotEnd = false;
break;
case MessageDefine.COMM_MSG_DRAW:
label2.Text = "status: Draw";
isNotEnd = false;
break;
case MessageDefine.COMM_MSG_CHESS:
m_socWorker.Receive(chess_info);
chess_board[chess_info[2]][chess_info[3]] = chess_info[1] + 1;
Graphics g = Graphics.FromImage(this.pictureBox1.Image);
if (chess_info[1] == 1)
{
g.DrawImage(global::gobang.Properties.Resources.white, posx[chess_info[2]][chess_info[3]] - 15, posy[chess_info[2]][chess_info[3]] - 15);
}
else
{
g.DrawImage(global::gobang.Properties.Resources.black, posx[chess_info[2]][chess_info[3]] - 15, posy[chess_info[2]][chess_info[3]] - 15);
}
this.Refresh();
myTurn = (myTurn == true) ? false : true;
break;
default:
MessageBox.Show(mes1[0].ToString());
break;
}
}
}
// the most important part
private int va(int i, int j)
{
if ((i >= 0) && (j >= 0) && (i < 15) && (j < 15))
return 1;
else return 0;
}
private bool check(int[][] board, int i, int j)
{
int x, y;
for (x = -3; x < 4; x++)
for (y = -3; y < 4; y++)
{
if ((System.Math.Abs(x) + System.Math.Abs(y) <= 4) && (!(x == 0 && y == 0)) && (va(x + i, y + j) == 1))
{
if (board[x + i][y + j] != 0)
return true;
}
}
return false;
}
private byte[] getIndex()
{
int[] maxo;
byte[] res = new byte[2];
maxo = findEndmin();
res[0] = (byte)maxo[0];
res[1] = (byte)maxo[1];
return res;
}
private int[] findEndmin()
{
int minval = int.MaxValue;
int val;
bool pass = false;
Computer computer = new Computer();
int[] res = new int[3];
byte x = 7, y = 7;
CheckForbidden checkForbidden = new CheckForbidden();
for (byte i = 0; i < 15; i++)
{
for (byte j = 0; j < 15; j++)
{
if (chess_board[i][j] == 0)
{
if (chess_board_record[i][j] == -1)
continue;
if (!check(chess_board, i, j))
continue;
pass = true;
if (computer.checkWin(i, j, chess_board, color))
{
res[0] = i;
res[1] = j;
res[2] = int.MaxValue - 10;
return res;
}
chess_board[i][j] = (int)color;
val = findEndmax();
chess_board[i][j] = 0;
if (minval > val)
{
minval = val;
x = i;
y = j;
}
else if (minval == val)
{
if (System.Math.Abs(x - 7) + System.Math.Abs(y - 7) > System.Math.Abs(i - 7) + System.Math.Abs(j - 7))
{
x = i;
y = j;
}
}
}
}
}
if (!pass)
minval = int.MinValue + 10;
res[0] = x;
res[1] = y;
res[2] = minval;
return res;
}
private int findEndmax()
{
int maxval = int.MinValue;
int val, x = 7, y = 7;
bool pass = false;
Computer computer = new Computer();
int res;
CheckForbidden checkForbidden = new CheckForbidden();
for (byte i = 0; i < 15; i++)
{
for (byte j = 0; j < 15; j++)
{
if (chess_board[i][j] == 0)
{
if (!check(chess_board, i, j))
continue;
pass = true;
if (computer.checkWin(i, j, chess_board,(color == chess_color.Chess_Clr_White) ? chess_color.Chess_Clr_Black : chess_color.Chess_Clr_White))
return int.MaxValue - 10;
val = computer.evaluate(i, j, chess_board, (color == chess_color.Chess_Clr_White) ? chess_color.Chess_Clr_Black : chess_color.Chess_Clr_White);
if (maxval < val)
{
maxval = val;
x = i;
y = j;
}
else if( maxval == val)
{
if (System.Math.Abs(x - 7) + System.Math.Abs(y - 7) > System.Math.Abs(i - 7) + System.Math.Abs(j - 7))
{
x = i;
y = j;
}
}
}
}
}
if (!pass)
res = int.MinValue + 10;
res = maxval;
return res;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -