//游戏结束 public void gameover() { gameStatus = false; gameStatus_Own = false; gameStatus_Opponent = false; colorSelected = 0; colorSelected_Own = 0; colorSelected_Opponent = 0; myTurn = false; myWuZiQi.init(); listenerRun = false; thStop(); this.Close();//一局游戏结束过后直接关闭窗体 }
//准备关闭游戏界面窗口,所有变量赋为初始状态 private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (gameResult == 0) { send("2001"); } gameStatus = false; gameStatus_Own = false; gameStatus_Opponent = false; colorSelected = 0; colorSelected_Own = 0; colorSelected_Opponent = 0; myTurn = false; myWuZiQi.init(); listenerRun = false; thStop(); }
//确定哪一方执黑先走 public void gamestart() { se.Send("1001"); colorSelected_Own = StatusOfColor.Black; if (colorSelected_Opponent == StatusOfColor.Black) { colorSelected_Own = StatusOfColor.White; } while (colorSelected_Opponent == 0) { Thread.Sleep(100); } ; //等待接受 if (colorSelected_Own == StatusOfColor.Black && colorSelected_Opponent == StatusOfColor.White) { colorSelected = StatusOfColor.Black; } else if (colorSelected_Own == StatusOfColor.White && colorSelected_Opponent == StatusOfColor.Black) { colorSelected = StatusOfColor.White; } else { colorSelected = 0; } if (colorSelected == StatusOfColor.White) { //borw = StatusOfColor.White; myTurn = false; Game_lable_currentPlayer.Text = "•轮到对方•"; } else if (colorSelected == StatusOfColor.Black) { //borw = StatusOfColor.Black; myTurn = true; Game_lable_currentPlayer.Text = "•轮到你•"; } }
/// <summary> /// 参量x y 为在刚下完是鼠标所下的那个棋子的坐标 /// 坐标按照原点在左上角记录 下为y轴正方向 右为x轴正方向 /// </summary> /// <param name="x">指最近一次的棋子的横坐标</param> /// <param name="y">指最近一次的棋子的纵坐标</param> /// <returns></returns> public bool Judge(int x, int y) { StatusOfColor flag = statusOfPoint[x, y]; int count_heng = -1; int count_shu = -1; int count_xie_zuo = -1; int count_xie_rou = -1; //计算横排的数目 //先左方的计数 int X = x; int Y = y; while (statusOfPoint[X, Y] == flag) { count_heng++; if (X > 0) { X--; } else { break; } } X = x; while (statusOfPoint[X, Y] == flag) { count_heng++; if (X < 14) { X++; } else { break; } } //计算竖排的数目 X = x; Y = y; while (statusOfPoint[X, Y] == flag) { count_shu++; if (Y > 0) { Y--; } else { break; } } Y = y; while (statusOfPoint[X, Y] == flag) { count_shu++; if (Y < 14) { Y++; } else { break; } } //计算斜项的数目 \ 方向 X = x; Y = y; while (statusOfPoint[X, Y] == flag) { count_xie_zuo++; if (Y > 0 && X > 0) { Y--; X--; } else { break; } } X = x; Y = y; while (statusOfPoint[X, Y] == flag) { count_xie_zuo++; if (Y < 14 && X < 14) { Y++; X++; } else { break; } } //计算斜向数目 / 方向 X = x; Y = y; while (statusOfPoint[X, Y] == flag) { count_xie_rou++; if (Y > 0 && X < 14) { Y--; X++; } else { break; } } X = x; Y = y; while (statusOfPoint[X, Y] == flag) { count_xie_rou++; if (Y < 14 && X > 0) { Y++; X--; } else { break; } } if (count_heng >= 5 || count_shu >= 5 || count_xie_rou >= 5 || count_xie_zuo >= 5) { return(true); } else { return(false); } }
//开始监听 private void Listen() { try { string hostname = Dns.GetHostName(); IPHostEntry localhost = Dns.GetHostByName(hostname); IPAddress ipAddress = localhost.AddressList[0]; //IPHostEntry heserver = Dns.GetHostEntry("localhost"); //IPAddress ipAddress = heserver.AddressList[1]; //Console.WriteLine(ipAddress.ToString()); try { tcpl = new TcpListener(ipAddress, 5656); } catch (Exception e) {} tcpl.Start(); while (listenerRun)//开始监听 { Socket s = tcpl.AcceptSocket(); //string remote = s.RemoteEndPoint.ToString(); Byte[] stream = new Byte[80]; int i = s.Receive(stream);//接受连接请求的字节流 string msg = System.Text.Encoding.UTF8.GetString(stream); Int32 a0 = Convert.ToInt32(msg); //此处加判断 switch (a0) { case 1000: { gameStatus_Opponent = true; break; } case 1001: { if (colorSelected_Own == 0) { colorSelected_Opponent = StatusOfColor.Black; } else { colorSelected_Opponent = StatusOfColor.White; } break; } case 2000: { gameResult = 2; ChangeResult(2); break; } case 2001: { gameResult = 1; ChangeResult(1); break; } case 3000: { var gg = MessageBox.Show("是否同意和棋", "和棋", MessageBoxButtons.YesNo); if (gg == DialogResult.No) { send("3003"); } else if (gg == DialogResult.Yes) { send("3004"); gameResult = 3; ChangeResult(3); } break; } case 3004: { speakPeace = true; MessageBox.Show("对方同意和棋"); gameResult = 3; ChangeResult(3); break; } case 3003: { speakPeace = true; MessageBox.Show("对方不同意和棋"); break; } case 4000: { var gg = MessageBox.Show("是否同意悔棋", "悔棋", MessageBoxButtons.YesNo); if (gg == DialogResult.No) { send("4002"); } else if (gg == DialogResult.Yes) { send("4001"); if (myTurn == false) { myWuZiQi.statusOfPoint[myWuZiQi.recorder.Peek().X, myWuZiQi.recorder.Peek().Y] = StatusOfColor.Empty; myWuZiQi.recorder.Pop(); myWuZiQi.statusOfPoint[myWuZiQi.recorder.Peek().X, myWuZiQi.recorder.Peek().Y] = StatusOfColor.Empty; myWuZiQi.recorder.Pop(); Invalidate(); } else if (myTurn == true) { myWuZiQi.statusOfPoint[myWuZiQi.recorder.Peek().X, myWuZiQi.recorder.Peek().Y] = StatusOfColor.Empty; myWuZiQi.recorder.Pop(); Invalidate(); Changesituation(); } } break; } case 4002: { undo = true; MessageBox.Show("对方不同意悔棋"); break; } case 4001: { undo = true; MessageBox.Show("对方同意悔棋"); if (myTurn == false) { myWuZiQi.statusOfPoint[myWuZiQi.recorder.Peek().X, myWuZiQi.recorder.Peek().Y] = StatusOfColor.Empty; myWuZiQi.recorder.Pop(); Invalidate(); Changesituation(); } else if (myTurn == true) { myWuZiQi.statusOfPoint[myWuZiQi.recorder.Peek().X, myWuZiQi.recorder.Peek().Y] = StatusOfColor.Empty; myWuZiQi.recorder.Pop(); myWuZiQi.statusOfPoint[myWuZiQi.recorder.Peek().X, myWuZiQi.recorder.Peek().Y] = StatusOfColor.Empty; myWuZiQi.recorder.Pop(); Invalidate(); } break; } default: { if (Convert.ToInt32(msg) >= 0 && Convert.ToInt32(msg) < 300) { int c = Convert.ToInt32(msg) % 15; int d = Convert.ToInt32(msg) / 15; //myWuZiQi.statusOfPoint[c, d] = 3-borw; myWuZiQi.statusOfPoint[c, d] = 3 - colorSelected; myWuZiQi.recorder.Push(new Point(c, d)); Invalidate(); Changesituation(); } break; } } } } catch (System.Security.SecurityException) { } catch (Exception) { } }