C網(wǎng)絡(luò)五子棋源碼_第1頁
C網(wǎng)絡(luò)五子棋源碼_第2頁
C網(wǎng)絡(luò)五子棋源碼_第3頁
C網(wǎng)絡(luò)五子棋源碼_第4頁
C網(wǎng)絡(luò)五子棋源碼_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、using system;using system.windows.forms;using system.collections.generic;using system.componentmodel;using system.data;using system.drawing;using system.text;using system.io;using system.net.sockets;using system.threading;using system.diagnostics;using system.net;using microsoft.win32;namespace wuzi

2、qi public partial class form1 : form public form1() initializecomponent(); public button, buttons;/棋盤數(shù)組 public bool gamer1 = false, gamer2 = false,netcanplay=false ;/ 兩個玩家是否允下棋,連網(wǎng)狀態(tài)時是否允許下棋 public bool gamer1click = new bool 225;/玩家一所下的棋 public bool gamer2click = new bool225;/玩家二所下的棋 public static in

3、t port = 3332;/連接端口 public string myipadd = ""/本機ip public thread li = null;/創(chuàng)建監(jiān)聽進程 public thread se = null;/創(chuàng)建發(fā)送上線信息進程 public thread sen = null;/創(chuàng)建發(fā)送信息進程 public string sendmessage = ""/要發(fā)送的信息 public bool canplay = false;/是否可以開始游戲 private void form1_load(object sender, eventargs

4、e) cshqp(); /初始化棋盤 / / 初始化棋盤 / / 初始化是否成功 public bool cshqp() groupbox1.controls.clear(); buttons = new button15,15; int i = 0; for (int x = 0; x / 下棋 / / 要下棋子的索引 / public bool xiaqi(int index) /messagebox.show(index.tostring(); foreach (button b in buttons) if (b.tabindex = index ) bool check = fals

5、e; if (gamer1clickb.tabindex = true) check = true; if (gamer2clickb.tabindex = true) check = true; if (check != true) if (gamer1 = true) gamer1clickb.tabindex = true; b.image = global:wuziqi.properties.resources.black; canxiaqi("gamer2"); if (netpk.checked = false | asservice.checked = tru

6、e) if (checkwin("gamer1") label11.text = "贏家:玩家一" + player1.text; messagebox.show("恭喜玩家一:" + player1.text + ",你贏了!", "會飛的魚提醒", messageboxbuttons.ok, messageboxicon.information); canxiaqi(""); send("win恭喜玩家一:" + player1.text + &quo

7、t;,你贏了!"); else if (gamer2 = true) gamer2clickb.tabindex = true; b.image = global:wuziqi.properties.resources.write; canxiaqi("gamer1"); if (netpk.checked = false | asservice.checked = true) if (checkwin("gamer2") label11.text = "贏家:玩家二" + player2.text; messagebox.

8、show("恭喜玩家二:" + player2.text + ",你贏了!", "會飛的魚提醒", messageboxbuttons.ok, messageboxicon.information); canxiaqi(""); send("win恭喜玩家二:" + player2.text + ",你贏了!"); else check = false; messagebox.show("這個位置上不能下棋!", "會飛的魚提醒", m

9、essageboxbuttons.ok, messageboxicon.warning); /messagebox.show(b.tabindex.tostring(); return true; return false; /開始游戲 private void start_click(object sender, eventargs e) /if (player1.text = "" | player2.text = "") / / messagebox.show("請輸入你的名字!", "會飛的魚提醒", me

10、ssageboxbuttons.ok, messageboxicon.warning); / /else / if (canplay = true | netpk.checked = false) if (radiobutton1.checked = true) canxiaqi("gamer1"); else canxiaqi("gamer2"); groupbox2.enabled = false; start.enabled = false; else messagebox.show("還沒有建立連接或?qū)Ψ竭€沒有準備好!不能開始游戲!&q

11、uot;, "會飛的魚提醒", messageboxbuttons.ok, messageboxicon.warning); / /改變下棋狀態(tài) / / 改變下棋狀態(tài) / / 可以下棋的人的名字 public void canxiaqi(string s) if (s="gamer1") int sum = 0; for (int i = 0; i / 判斷輸贏 / / 要判斷的玩家的名字 / 是否贏 public bool checkwin(string s) if (s = "gamer1") for (int i = 0; i

12、/做為主機 private void asservice_checkedchanged_1(object sender, eventargs e) if (asservice.checked = true) waitconnect.enabled = true; asclinet.checked = false; asclinet.enabled = false; else waitconnect.enabled = false; groupbox3.enabled = true; asservice.enabled = true; asclinet.enabled = true; /做為客戶

13、機 private void asclinet_checkedchanged(object sender, eventargs e) if (asclinet.checked = true) connect.enabled = true; label12.enabled = true; ip.enabled = true; asservice.checked = false; asservice.enabled=false ; else connect.enabled = false ; label12.enabled = false; ip.enabled = false; groupbox

14、3.enabled = true; asservice.enabled = true; asclinet.enabled = true; /等待客戶機連接 private void waitconnect_click(object sender, eventargs e) iphostentry myentry = dns.gethostbyname(dns.gethostname(); ipaddress myaddress = new ipaddress(myentry.addresslist0.address); myipadd = myaddress.tostring(); threa

15、dstart s=new threadstart (listen ); li = new thread(s); li.start(); statue.text = "正在等待客戶機連接!現(xiàn)在還不能開始游戲!" waitconnect.enabled = false; /連接 private void connect_click(object sender, eventargs e) if (ip.text != "") iphostentry myentry = dns.gethostbyname(dns.gethostname(); ipaddress

16、 myaddress = new ipaddress(myentry.addresslist0.address); myipadd = myaddress.tostring(); threadstart s = new threadstart(co); se = new thread(s); se.start(); statue.text = "正在連接主機!現(xiàn)在還不能開始游戲!" threadstart sx = new threadstart(listen); li = new thread(sx); li.start(); connect.enabled = fals

17、e; else messagebox.show("請輸入一個正確的ip地址!", "會飛的魚提醒", messageboxbuttons.ok, messageboxicon.warning); /監(jiān)聽命令信息 / / 監(jiān)聽命令信息 / public void listen() try tcplistener li = new tcplistener(port); li.start(); while (true) tcpclient cl = li.accepttcpclient(); networkstream ns = cl.getstream();

18、 byte mbyte = new byte1024; int bytesread = ns.read(mbyte, 0, mbyte.length); string command = system.text.encoding.default.getstring(mbyte, 0, bytesread).trim(); execcommand(command); cl.close(); ns.close(); thread.sleep(500); catch /messagebox.show("建立連接出錯!", "會飛的魚提醒", messagebo

19、xbuttons.ok, messageboxicon.warning); /解釋命令 / / 解釋命令 / / 要解釋的命令 public void execcommand(string command) label13.text = command; /messagebox.show(command); if (command != "") if (command.substring(0, 7) = "connect") statue.text = "已經(jīng)建立連接!你可以開始游戲了!" ip.text = command.subs

20、tring(8, command.length - 8); ready.enabled = true; send("connect:" + myipadd); else if (command.substring(0, 5) = "start") int p =convert .toint32 (command.substring(6, 1); /player1 .text =command if (p = 1) netcanplay = true ; radiobutton1.checked = true; canxiaqi("gamer1&

21、quot;); groupbox2.enabled = false; else netcanplay = true; radiobutton2.checked = true; canxiaqi("gamer2"); groupbox2.enabled = false; if (radiobutton1.checked = true) messagebox.show("先手:玩家一!", "會飛的魚提醒", messageboxbuttons.ok, messageboxicon.information); else messagebo

22、x.show("先手:玩家二!", "會飛的魚提醒", messageboxbuttons.ok, messageboxicon.information); canplay = true; /ready.enabled = false; statue.text = "對方已經(jīng)準備好,你可以開始游戲了!" else if (command.substring(0, 5) = "xiaqi") int x = convert.toint32(command.substring(5, 1); if (x=1) canxi

23、aqi("gamer1"); else canxiaqi("gamer2"); /messagebox.show(command.substring(6, command.length - 6); xiaqi(convert.toint32(command.substring(7, command.length - 7); netcanplay = true; else if (command.substring(0, 3) = "win") canxiaqi(""); netcanplay = false; me

24、ssagebox.show(command.substring(3, command.length - 3), "會飛的魚提醒", messageboxbuttons.ok, messageboxicon.information); /發(fā)送上線信息 / / 向主機發(fā)送上線信息 / public void co() while (true) send("connect:"+myipadd ); thread.sleep(3000); /發(fā)送信息 / / 發(fā)送信息 / public void sendt() try /根據(jù)目標計算機地址建立連接 tcpcli

25、ent mytcpclient = new tcpclient(ip.text, port); /獲得用于網(wǎng)絡(luò)訪問的數(shù)據(jù)流 networkstream mytcpstream = mytcpclient.getstream(); streamwriter mystream = new streamwriter(mytcpstream, system.text.encoding.getencoding("gb2312"); /將字符串寫入流 mystream.write(sendmessage ); /將緩沖數(shù)據(jù)寫入基礎(chǔ)流 mystream.flush(); /關(guān)閉網(wǎng)絡(luò)流 mystream.close(); mytcpclient.close(); label14.

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論