c計算機網絡編程24點游戲_第1頁
c計算機網絡編程24點游戲_第2頁
c計算機網絡編程24點游戲_第3頁
c計算機網絡編程24點游戲_第4頁
c計算機網絡編程24點游戲_第5頁
已閱讀5頁,還剩25頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、 軟 件 學 院課程設計報告書課程名稱 計算機網絡編程 設計題目 24分游戲 專業(yè)班級 網絡工程11-1班 學 號 1120020103 姓 名 韓生曌 指導教師 李建東 2014年 1月1 設計時間2014年1月6日-1月10日2 設計目的目的:(1)加深對計算機網絡的基本概念和原理,以及網絡編程接口Socket概念及編程原理的理解;(2)提高學生網絡應用與編程的能力。學生在四個設計題目中選擇一個(按照學號依次選擇),提高其分析與解決問題的能力,為大型網絡編程打下堅實基礎;(3)通過撰寫課程設計報告,鍛煉學生的邏輯組織和語言表達能力;(4)培養(yǎng)學生理論運用于實踐的綜合應用和設計創(chuàng)新能力。3設

2、計任務(1)設計完成與網絡應用相關題目的網絡應用軟件;(2)調試運行之后,要求邊演示邊解釋設計的思想、過程及采用的方法;(3)完成課程設計報告。4 設計內容 24分游戲軟件名:24分游戲(分服務器端和客戶端兩部分)功能:游戲后臺運行一個服務器軟件,游戲者通過客戶端程序登陸后,服務器隨機產生四個1-9之間的隨機數供游戲者使用,游戲者根據計算得出結果。若計算出結果,且在規(guī)定的時間內,則從客戶端輸入表達式,服務器端根據客戶端的表達式計算,若正確,軟件提示:“對了”,如果錯了,軟件提示“錯了”,若超時。軟件提示“超時”。服務器同時記錄并維護一個游戲玩家的TOP10排行榜,供玩家查詢。描述:服務器端:接

3、收客戶端的命令請求并提供相應的服務。客戶端:完成游戲人機界界面的設計,以命令的方式向服務器請求服務,并顯示相應的提示。4.1系統分析 要設計一個24分游戲,并且有服務器端和客戶端兩部分,首先需要實現服務器端和客戶端的連接,用TCP進行連接,BinaryReader和BinaryWriter進行存寫。首先編寫一個服務端,用TcpListener進行監(jiān)聽,監(jiān)聽客戶端的請求。并且用一個listbox顯示當前監(jiān)聽的狀態(tài),比如當前接入的是那個用戶,和當前連接的用戶數。其次要設計客戶端的界面,首先客戶端需要實現對服務器的請求。通過IP地址和端口號進行連接。然后要把客戶端的用戶名上傳到服務器上。等用戶登錄之

4、后,就可以開始游戲,由系統產生四個隨機數。然后由用戶輸入輸入答案,輸入之后點確定,由系統判斷用戶的答案是否正確。如果錯誤,系統提示用戶答案錯誤,之后用戶可以查看答案或者繼續(xù)游戲。4.2算法設計服務器端代碼如下首先要添加一個User類using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net ;using System.Net.Sockets ;using System.IO;namespace The24Server class User public Bi

5、naryReader br get; private set; public BinaryWriter bw get; private set; public TcpClient client get; private set; public string userName get; set; public User(TcpClient client) this.client = client; this.userName = "" NetworkStream networkStream = client.GetStream(); br = new BinaryReader

6、(networkStream); bw = new BinaryWriter(networkStream); MainForm的代碼如下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

7、 The24Server public partial class MainForm : Form private List<User> userList = new List<User>(); IPAddress localAddress; private const int port = 51000; private TcpListener myListener; bool isNormalExit = false; public MainForm() InitializeComponent(); listBoxStatus.HorizontalScrollbar

8、= true; IPAddress addrIP = Dns.GetHostAddresses(Dns.GetHostName(); localAddress = addrIP0; buttonStop.Enabled = false; private void buttonStart_Click(object sender, EventArgs e) myListener = new TcpListener(localAddress, port); myListener.Start(); listBoxStatus.Items.Add(string.Format("開始在0:1監(jiān)聽

9、客戶連接", localAddress, port); Thread myThread = new Thread(ListenClientConnect); myThread.Start(); buttonStart.Enabled = false; buttonStop.Enabled = true; private void ListenClientConnect() TcpClient newClient = null; while (true) try newClient = myListener.AcceptTcpClient(); catch break; User us

10、er = new User(newClient); Thread threadReceive = new Thread(ReceiveData); threadReceive.Start(user); userList.Add(user); listBoxStatus.Items.Add(string.Format("0進入", newClient.Client.RemoteEndPoint); listBoxStatus.Items.Add(string.Format("當前連接用戶數:0", userList.Count); private void

11、 ReceiveData(object userState) User user = (User)userState; TcpClient client = user.client; while (isNormalExit = false) string receiveString = null; try receiveString = user.br.ReadString(); catch if (isNormalExit = false) listBoxStatus.Items.Add(string.Format("與0失去聯系,已終止接收該用戶信息", client.

12、Client.RemoteEndPoint); userList.Remove(user); break; string splitString = receiveString.Split(','); switch (splitString0) case "CONNECT": listBoxStatus.Items.Add(string.Format("來自0", user.client.Client.RemoteEndPoint); SendToClient(user, "CONNECT," + userList0.

13、userName); break; case "LOGIN": user.userName = splitString1; listBoxStatus.Items.Add(string.Format("該用戶用戶名為0",splitString1); SendToClient(user, "lOGIN," + userList0.userName); break; case "EXIT": return; case "COUNT": break; private void SendToClien

14、t(User user, string message) try user.bw.Write(message); user.bw.Flush(); catch (Exception e) private void SendToAllClient(User user, string message) string command = message.Split(',')0.ToLower(); if (command = "lOGIN") for (int i = 0; i < userList.Count; i+) SendToClient(userL

15、isti, message); if (userListi.userName != user.userName) SendToClient(user, "lOGIN," + userListi.userName); else if (command = "EXIT") for (int i = 0; i < userList.Count; i+) if (userListi.userName != user.userName) SendToClient(userListi, message); private void RemoveUser(Use

16、r user) throw new NotImplementedException(); private void buttonStop_Click(object sender, EventArgs e) myListener.Stop(); buttonStart.Enabled = true; buttonStop.Enabled = false; private void MainForm_FormClosing(object sender, FormClosingEventArgs e) if (myListener != null) buttonStop.PerformClick()

17、; 客戶端部分代碼如下: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;using System.Net;using System.Net.Sockets;using System.IO;using System.Threading;using System.Collections ;names

18、pace The24Client public partial class MainForm : Form int count = 31;/如果設置為30的話,在登錄之后時間就會變?yōu)?9,暫時還沒發(fā)現是那塊錯了,就先設置為31; private int jifen=0; private System.DateTime StartTime; private TcpClient client; private BinaryReader br; private BinaryWriter bw; private bool isExit = false; IPAddress localAddress;

19、public MainForm() InitializeComponent(); buttonExit.Enabled = false; buttonGetNum.Enabled = false; buttonAnswer.Enabled = false;buttonEnter.Enabled = false; buttonLogin.Enabled = false; buttonGetTop10.Enabled = false; IPAddress addrIP = Dns.GetHostAddresses(Dns.GetHostName(); localAddress = addrIP0;

20、 textBoxIP.Text = localAddress.ToString(); textBoxDuank.Text = "51000" private void buttonConnect_Click(object sender, EventArgs e) buttonLogin.Enabled = true; buttonConnect.Enabled = false; buttonExit.Enabled = true; try client = new TcpClient(Dns.GetHostName(), 51000); listBoxInfo.Items.

21、Add("連接成功"); catch listBoxInfo.Items.Add("連接失敗"); buttonConnect.Enabled = true;return; NetworkStream networkStream = client.GetStream(); br = new BinaryReader(networkStream);bw = new BinaryWriter(networkStream); SendMessage("CONNECT" + textBoxIP.Text); Thread threadRece

22、ive = new Thread(new ThreadStart(ReceiveData); threadReceive.IsBackground = true; threadReceive.Start(); private void ReceiveData() string receiveString = null; while (isExit = false) try receiveString = br.ReadString(); catch if (isExit = false) MessageBox.Show("與服務器失去聯系。"); break; string

23、 splitString = receiveString.Split(','); string command = splitString0.ToLower(); switch (command) case "CONNECT": break; case "LOGIN": listBoxInfo.Items.Add("用戶名上傳成功"); break; case "EXIT": listBoxInfo.Items.Add("連接失敗或與服務器斷開連接"); break; case

24、"COUNT": listBoxInfo.Items.Add(string.Format("用戶0檢查結果并上傳耗時", splitString1); break; default: listBoxInfo.Items.Add(receiveString); break; Application.Exit(); private void SendMessage(string message) try bw.Write(message); bw.Flush(); catch listBoxInfo.Items.Add("發(fā)送失敗!");

25、 private void buttonLogin_Click(object sender, EventArgs e) buttonGetTop10.Enabled = true; buttonGetNum.Enabled = true; buttonLogin.Enabled = false; NetworkStream networkStream = client.GetStream(); br = new BinaryReader(networkStream); bw = new BinaryWriter(networkStream); SendMessage("LOGIN,&

26、quot; + textBoxUserName.Text); Thread threadReceive = new Thread(new ThreadStart(ReceiveData); threadReceive.IsBackground = true; threadReceive.Start(); label18.Text = textBoxUserName.Text; label19.Text = jifen.ToString(); count = 30; private void buttonGetNum_Click(object sender, EventArgs e) butto

27、nEnter.Enabled = true; buttonAnswer.Enabled = true; Random random = new Random(DateTime.Now.Millisecond); int numb1 = random.Next(1, 9); int numb2 = random.Next(1, 9); int numb3 = random.Next(1, 9); int numb4 = random.Next(1, 9); label11.Text = numb1.ToString(); label12.Text = numb2.ToString(); labe

28、l13.Text = numb3.ToString(); label14.Text = numb4.ToString(); StartTime = DateTime.Now; listBoxInfo.Items.Clear(); count = 30; label15.Text = count.ToString(); timer1.Start(); private void buttonAnswer_Click(object sender, EventArgs e) timer1.Stop(); this.listBoxInfo.Items.Clear(); runter(); private

29、 bool isSame(int a, int b, int c, int d) if (a = b | a = c | a = d | b = c | b = d | c = d) return false; return true; public int counter(int i, int sum, int num) if (i = 0) return sum + num; else if (i = 1) return sum - num; else if (i = 2) return sum * num; else float temp = (float)sum / num; if (

30、num != 0) if (temp > (sum / num) return -1000; else return sum / num; else return -1000; private string operators(int i) if (i = 0) return "+" else if (i = 1) return "-" else if (i = 2) return "×" else return "÷" private void runter() bool isResul

31、t = false; int numb = 0, 0, 0, 0 ; numb0 = Int16.Parse(label11.Text); numb1 = Int16.Parse(label12.Text); numb2 = Int16.Parse(label13.Text); numb3 = Int16.Parse(label14.Text); int count = 0; for (int a = 0; a < 4; a+) for (int b = 0; b < 4; b+) for (int c = 0; c < 4; c+) for (int d = 0; d &l

32、t; 4; d+) if (this.isSame(a, b, c, d) /+,-,*,/,循環(huán)* for (int a1 = 0; a1 < 4; a1+) for (int b1 = 0; b1 < 4; b1+) for (int c1 = 0; c1 < 4; c1+) count = this.counter(c1, this.counter(b1, this.counter(a1, numba, numbb), numbc), numbd); if (count = 24) listBoxInfo.Items.Add("(" + numba

33、+ operators(a1) + numbb + ")" + operators(b1) + numbc + ")" + operators(c1) + numbd + "=" + count); isResult = true; count = this.counter(c1, this.counter(a1, numba, this.counter(b1, numbb, numbc), numbd); if (count = 24) listBoxInfo.Items.Add("(" + numba + op

34、erators(a1) + "(" + numbb + operators(b1) + numbc + ")" + operators(c1) + numbd + "=" + count); isResult = true; count = this.counter(b1, this.counter(a1, numba, numbb), this.counter(c1, numbc, numbd); if (count = 24) listBoxInfo.Items.Add("(" + numba + operat

35、ors(a1) + numbb + ")" + operators(b1) + "(" + numbc + operators(c1) + numbd + ")" + "=" + count); isResult = true; count = this.counter(a1, numba, this.counter(b1, numbb, this.counter(c1, numbc, numbd); if (count = 24) listBoxInfo.Items.Add("" + numb

36、a + operators(a1) + "(" + numbb + operators(b1) + "(" + numbc + operators(c1) + numbd + ")" + "=" + count); isResult = true; count = this.counter(a1, numba, this.counter(c1, this.counter(b1, numbb, numbc), numbd); if (count = 24) listBoxInfo.Items.Add("&q

37、uot; + numba + operators(a1) + "(" + numbb + operators(b1) + numbc + ")" + operators(c1) + numbd + ")" + "=" + count); isResult = true; if (!isResult) MessageBox.Show("此副牌無解"); private void buttonEnter_Click(object sender, EventArgs e) timer1.Stop();

38、 runter(); string str = new stringlistBoxInfo.Items.Count; for (int i = 0; i < listBoxInfo.Items.Count; i+) stri = listBoxInfo.Itemsi.ToString(); if (textBox1.Text = stri) MessageBox.Show("恭喜您,您贏了,您的積分+1!"); jifen += 1; label19.Text = jifen.ToString(); break; MessageBox.Show("對不起,您

39、輸了,您的積分會減-1!"); jifen -= 1; label19.Text = jifen.ToString(); break; private void listBox1_SelectedIndexChanged(object sender, EventArgs e) private void groupBox4_Enter(object sender, EventArgs e) private void buttonGetTop10_Click(object sender, EventArgs e) MessageBox.Show("對不起,此功能尚未完善。&qu

40、ot;,"error"); private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) private void timer1_Tick(object sender, EventArgs e) if (count !=0) count = count - 1; label15.Text = count.ToString(); else timer1.Stop(); MessageBox.Show("已經超過30秒了!", "很遺憾!"); privat

41、e void MainForm_Load(object sender, EventArgs e) private void label4_Click(object sender, EventArgs e) 4.3界面設計服務器端的設計界面圖1客戶端的設計界面圖24.4運行與測試4.4.1運行與測試首先啟動服務器端進行監(jiān)聽圖3啟動客戶端,連接服務器之后輸入用戶名進行登錄。圖4此時服務器端的狀態(tài)為圖5產生四個隨機數,計時區(qū)開始計時,倒計時30秒。當輸入的答案正確的時候圖6圖7會提示積分+1;點擊確定之后可以看到當前積分變?yōu)?.圖8如果答案錯誤或者空答案的話,就會提示答案錯誤,并且積分會減1圖9當產生的四個隨機數構不成24時圖10游戲規(guī)定倒計時30

溫馨提示

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

評論

0/150

提交評論