您的位置:首页 > 编程语言

北大青鸟 中国象棋 实习项目 (项目源代码级说明十一)

2008-07-19 20:03 281 查看
棋子界面:(注释了的为网络版功能)

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;
using System.IO;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;

namespace chess
{
public partial class chessForm : Form
{
private IChessItem selectItem;
private IChessItem tempSelect;
private Label tempLabel;
private ChessTypes trunType;
private TcpClient client;
private TcpListener listener;
private Thread thread;
private bool isServer;
private byte[] buffer;
private String clientCommand;
private String serverCommand;
Socket socket;
public chessForm()
{

InitializeComponent();

}
/// <summary>
/// 接收数据
/// </summary>
//private void recevieData()
//{
// while (true)
// {
// if (isServer)
// {
// socket = listener.AcceptSocket();
// buffer = new byte[1024];
// socket.Receive(buffer);
// MemoryStream ms = new MemoryStream(buffer);
// BinaryFormatter bf = new BinaryFormatter();
// SendData data =(SendData) bf.Deserialize(ms);
// Console.WriteLine("收到客户端的代码(目标点)为:" + data.beginX+data.beginY+data.endX+data.endY);
// }
// else
// {
// NetworkStream netstream = client.GetStream();
// buffer = new byte[1024];
// netstream.Read(buffer, 0, buffer.Length);
// MemoryStream ms = new MemoryStream(buffer);
// BinaryFormatter bf = new BinaryFormatter();

// SendData data =(SendData) bf.Deserialize(ms);
// Console.WriteLine("收到服务器的代码(目标点)为:" + data.beginX + data.beginY+data.endX+data.endY);

// }
// }
//}

//private bool move()
//{
//}
//private String[] decodeCommand(String command)
//{
// String[] decomm;
// String mycommand = command;
// Char[] mysparator=new Char[':'];
// decomm = mycommand.Split(mysparator);
// return decomm;

//}
private void chessPanel1_MouseDown(object sender, MouseEventArgs e)
{
//Label label = (Label)selectItem;
//if (isServer)
//{

// SendData data = new SendData(label.Location.X, label.Location.Y, e.X, e.Y, true);
// BinaryFormatter bf = new BinaryFormatter();
// MemoryStream ms = new MemoryStream();
// bf.Serialize(ms, data);
// buffer = ms.ToArray();
// socket.Send(buffer);
//}
//else
//{
// SendData data = new SendData(label.Location.X, label.Location.Y, e.X, e.Y, true);
// BinaryFormatter bf = new BinaryFormatter();
// MemoryStream ms = new MemoryStream();
// bf.Serialize(ms, data);
// byte[] buffer = ms.ToArray();
// NetworkStream netstream = client.GetStream();
// netstream.Write(buffer, 0, buffer.Length);
//}

if (selectItem.chessType == trunType)
{
if (selectItem.MoveItem(e.X, e.Y))
{
if (trunType == ChessTypes.black)
{
trunType = ChessTypes.blue;
label1.Text = "该蓝色走";
}
else if (trunType == ChessTypes.blue)
{
trunType = ChessTypes.black;
label1.Text = "该黑色走";
}

}
}
}

private void chessItemBing1_Click(object sender, EventArgs e)
{
tempSelect = (IChessItem)sender;
tempLabel = (Label)sender;
if (selectItem == null)
{
selectItem = tempSelect;
trunType = selectItem.chessType;
}
if (tempSelect.chessType == trunType)
{
//并且轮次转换成功!
selectItem = tempSelect;
}

else if ((tempSelect.chessType != trunType) && (selectItem.chessType != tempSelect.chessType) && (!tempLabel.Text.Equals("炮")))
{
//吃棋
//tempLabel = (Label)sender;
if (selectItem.eatChess(tempLabel.Location.X, tempLabel.Location.Y, tempLabel, selectItem))
{
if (trunType == ChessTypes.black)
{
trunType = ChessTypes.blue;
label1.Text = "该蓝色走";
}
else if (trunType == ChessTypes.blue)
{
trunType = ChessTypes.black;
label1.Text = "该黑色走";
}
}

}

}

private void 主机ToolStripMenuItem_Click(object sender, EventArgs e)
{

// listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 5000);
// listener.Start();
// isServer = true;
// thread = new Thread(new ThreadStart(recevieData));
// thread.Start();
// label2.Text = "主机";
}

private void 客户机ToolStripMenuItem_Click(object sender, EventArgs e)
{
// client = new TcpClient();
// client.Connect(IPAddress.Parse("127.0.0.1"), 5000);
// isServer = false;
// thread = new Thread(new ThreadStart(recevieData));
// thread.Start();
// label2.Text = "客户机";
}

private void chessForm_FormClosed(object sender, FormClosedEventArgs e)
{
// thread.Abort();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: