c#游戏------联机五子棋

发布时间:2009年06月04日      浏览次数:949 次
最近想学一些简单的网络编程
在网上找了很久没有找到联机五子棋的程序代码
就自己写了一个
原来是很简单的(嘿嘿)
代码写的很差, bug很大
请大家指教,优化
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;
using System.Net.Sockets;
using System.Threading;
namespace 五子棋
{
public partial class Form1 : Form
{
//联机参数
private IPAddress HostIP = IPAddress.Parse("127.0.0.1");
private IPEndPoint ChatServer;
private Socket ChatSocket;
private bool flag = true;
private Socket AcceptedSocket;
private bool sign = false;
private bool play;
public Form1()
{
InitializeComponent();
}
//五子棋参数
private enum Chess { Nune = 0, White, Black };
private Chess [,]Box = new Chess[15,15];
private Chess m_Player;
private Chess Player
{
get
{
return m_Player;
}
set
{
m_Player = value;
}
}
//重载onpaint函数
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
Pen pen1 = new Pen(Color.Black, 1);
for (int i = 40; i <= 460; i = i + 30)
for (int j = 40; j <= 460; j = j + 30)
{
g.DrawLine(pen1, i, 40, i, 460);
g.DrawLine(pen1, 40, j, 460, j);
}
base.OnPaint(e);
}
//鼠标单击事件
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);

if (e.Button == MouseButtons.Left)
{
if (sign)
{
Point p = new Point((e.X - 20) / 30, (e.Y - 20) / 30);
//出界判断
if ((p.X < 0 || p.X > 14) || (p.Y < 0 || p.Y > 14))
return;
if (AddChess(p))
{
sign = false;
if (play)
{
Resurt(p, Chess.Black);
try
{
Byte[] SentByte = new Byte[4];
string SentStr = p.X.ToString("X") + p.Y.ToString("X");
SentByte = System.Text.Encoding.BigEndianUnicode.GetBytes(SentStr.ToCharArray());
AcceptedSocket.Send(SentByte, SentByte.Length, 0);
}
catch
{ toolStripStatusLabel1.Text = "The connection has not existed!"; }
}
else
{
Resurt(p, Chess.White);
try
{
Byte[] SentByte = new Byte[4];
string SentStr = p.X.ToString("X") + p.Y.ToString("X");
SentByte = System.Text.Encoding.BigEndianUnicode.GetBytes(SentStr.ToCharArray());
ChatSocket.Send(SentByte, SentByte.Length, 0);
}
catch
{ toolStripStatusLabel1.Text = "The connection has not existed!"; }
}
}
}
}
}
//在棋盘上加一个棋子
private bool AddChess(Point p)
{
//已有棋子判断
if (Box[p.Y, p.X] != Chess.Nune) return false;
//画棋子
Graphics g = this.CreateGraphics();
Pen pen1 = new Pen(Color.Black, 3);
Pen pen2 = new Pen(Color.Red, 3);
if (Player == Chess.Black)
{
g.DrawEllipse(pen1, p.X * 30 + 30, p.Y * 30 + 30, 20, 20);
Box[p.Y, p.X] = Chess.Black;
}
else
{
g.DrawEllipse(pen2, p.X * 30 + 30, p.Y * 30 + 30, 20, 20);
Box[p.Y, p.X] = Chess.White;
}
return true;
}
//检查游戏规则
private void Resurt(Point p, Chess LastPlayer)
{
int x, y;
//Chess LastPlayer;
//if(play)
// LastPlayer = Chess.Black;
//else
// LastPlayer = Chess.White;
int n;
//检查同一行
n = 1;
y = p.Y;
for (x = p.X - 1; x >= 0; x--)
{
if (Box[y, x] == LastPlayer) n++;
else break;
}
for (x = p.X + 1; x < 15; x++)
{
if (Box[y, x] == LastPlayer) n++;
else break;
}
if (n >= 5)
{
Message(LastPlayer);
return;
}
//检查同一列
n = 1;
x = p.X;
for (y = p.Y - 1; y >= 0; y--)
{
if (Box[y, x] == LastPlayer) n++;
else break;
}
for (y = p.Y + 1; y < 15; y++)
{
if (Box[y, x] == LastPlayer) n++;
else break;
}
if (n >= 5)
{
Message(LastPlayer);
return;
}
//检查左斜线
n = 1;
for (x = p.X + 1, y = p.Y - 1; x < 15 && y >= 0; x++, y--)
{
if (Box[y, x] == LastPlayer) n++;
else break;
}
for (x = p.X - 1, y = p.Y + 1; x >= 0 && y < 15; x--, y++)
{
if (Box[y, x] == LastPlayer) n++;
else break;
}
if (n >= 5)
{
Message(LastPlayer);
return;
}
//检查右斜线
n = 1;
for (x = p.X - 1, y = p.Y - 1; x >= 0 && y >= 0; x--, y--)
{
if (Box[y, x] == LastPlayer) n++;
else break;
}
for (x = p.X + 1, y = p.Y + 1; x < 15 && y < 15; x++, y++)
{
if (Box[y, x] == LastPlayer) n++;
else break;
}
if (n >= 5)
{
Message(LastPlayer);
return;
}
//平局
if (Box[14, 14] != Chess.Nune)
{
Message(LastPlayer);
return;
}
}
private void Message(Chess LastPlayer)
{
if (LastPlayer == Chess.Black)
{
label1.Text = "winer is black!!!!";
}
else
{
if (LastPlayer == Chess.White)
{
label1.Text = "winer is white!!!!";
}
else
{
label1.Text = "none!!!!";
}
}
sign = false;
button4.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
Array.Clear(Box, 0, 15 * 15);
}
//创建新游戏
private void button1_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Pen pen1 = new Pen(Color.Black, 3);
g.DrawEllipse(pen1, 20, 20, 20, 20);

sign = true;
play = true;
Player = Chess.Black;
HostIP = IPAddress.Parse("127.0.0.1");
try
{

ChatServer = new IPEndPoint(HostIP, Int32.Parse("8081"));
ChatSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ChatSocket.Bind(ChatServer);
ChatSocket.Listen(50);
toolStripStatusLabel1.Text = "Now begin to listen...";
AcceptedSocket = ChatSocket.Accept();
Thread thread = new Thread(new ThreadStart(ChatProcesss));
thread.Start();
}
catch (Exception ee)
{ toolStripStatusLabel1.Text = ee.Message; }
}
private void paints(string qq)
{
int a = Convert.ToInt32(qq[0]) - 48, b = Convert.ToInt32(qq[1]) - 48;
Point pt;
if (a < 10 && b < 10)
{
pt = new Point(a, b);
}
else
if (a > 10 && b < 10)
{
pt = new Point(a - 7, b);
}
else
if (a < 10 && b > 10)
{
pt = new Point(a, b - 7);
}
else
{
pt = new Point(a - 7, b - 7);
}
Graphics h = this.CreateGraphics();
Pen pen3 = new Pen(Color.Red, 3);
h.DrawEllipse(pen3, pt.X * 30 + 30, pt.Y * 30 + 30, 20, 20);
Box[pt.Y, pt.X] = Chess.White;
sign = true;
Resurt(pt, Chess.White);
}
private void ChatProcesss()
{
try
{
if (AcceptedSocket.Connected)
{
toolStripStatusLabel1.Text = "Ready for the play!";
while (flag)
{
Byte[] ReceivedByte = new Byte[4];
AcceptedSocket.Receive(ReceivedByte, ReceivedByte.Length, 0);
string ReceivedStr = System.Text.Encoding.BigEndianUnicode.GetString(ReceivedByte);
paints(ReceivedStr);
}
}
}
catch
{
MessageBox.Show("111");
}
}
//加入游戏
private void button2_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Pen pen1 = new Pen(Color.Red, 3);
g.DrawEllipse(pen1, 20, 20, 20, 20);
if (button4.Enabled)
sign = false;
else
{
play = false;
Player = Chess.White;
HostIP = IPAddress.Parse("127.0.0.1");
try
{
ChatServer = new IPEndPoint(HostIP, Int32.Parse("8081"));
ChatSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ChatSocket.Connect(ChatServer);
toolStripStatusLabel1.Text = "Now begin to connect...";
Thread thread = new Thread(new ThreadStart(ChatProcessc));
thread.Start();
}
catch (Exception ee)
{ toolStripStatusLabel1.Text = ee.Message; }
}
}
private void paintc(string qq)
{
int a = Convert.ToInt32(qq[0]) - 48, b = Convert.ToInt32(qq[1]) - 48;
Point pt;
if (a < 10 && b < 10)
{
pt = new Point(a, b);
}
else
if (a > 10 && b < 10)
{
pt = new Point(a - 7, b);
}
else
if (a < 10 && b > 10)
{
pt = new Point(a, b - 7);
}
else
{
pt = new Point(a - 7, b - 7);
}
Graphics h = this.CreateGraphics();
Pen pen3 = new Pen(Color.Black, 3);
h.DrawEllipse(pen3, pt.X * 30 + 30, pt.Y * 30 + 30, 20, 20);
Box[pt.Y, pt.X] = Chess.Black;
sign = true;
Resurt(pt, Chess.Black);
}
private void ChatProcessc()
{
try
{
if (ChatSocket.Connected)
{
toolStripStatusLabel1.Text = "Ready for the play!";
while (flag)
{
Byte[] ReceivedByte = new Byte[4];
ChatSocket.Receive(ReceivedByte, ReceivedByte.Length, 0);
string ReceivedStr = System.Text.Encoding.BigEndianUnicode.GetString(ReceivedByte);
paintc(ReceivedStr);
}
}
}
catch
{
label1.Text = "对方已推出!!!!";
}
}
private void button4_Click(object sender, EventArgs e)
{
Array.Clear(Box, 0, 15*15);
this.BackColor = System.Drawing.SystemColors.Control;
this.Refresh();
if (play)
sign = true;
toolStripStatusLabel1.Text = "wait the other!!";
button4.Enabled = false;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void button3_Click(object sender, EventArgs e)
{
Thread.Sleep(1);
Application.Exit();
}
}
}
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!