private void ClickEvent(object sender, EventArgs e) { if (IsMyTurn) { Button btn = sender as Button; if (btn.BackgroundImage != null) { return; } TurnInfo nextTurn = new TurnInfo() { CurrentPlayer = CurrentPlayer, Coordinate = GetChessPoint(btn) }; turnStack.Push(nextTurn); SocketData positonData = new SocketData(); positonData.Command = (int)SocketCommand.SEND_COORDINATE; positonData.Point = GetChessPoint(btn); gameSocket.Send(positonData); MarkASquare(btn); ChangePlayer(); if (playerMarkEvent != null) { playerMarkEvent(this, new EventArgs()); } if (IsEndGame(btn)) { EndGame(); if (GAME_MODE == CaroConstant.GAME_MODE_LAN) { SocketData gameOverData = new SocketData(); gameOverData.Command = (int)SocketCommand.END_GAME; SendData(gameOverData); } } } else { MessageBox.Show("Chưa tới lượt của bạn"); } }
private void Listen() { Thread listenThread = new Thread(() => { while (true) { try { SocketData data = (SocketData)chessBoardManager.ReceiveData(); ProcessData(data); } catch (Exception e) { MessageBox.Show("Đối thủ đã thoát"); } } }); listenThread.IsBackground = true; listenThread.Start(); }
void Listen() //Thực hiện Receive { Thread listenThread = new Thread(() => { try { SocketData data = (SocketData)socket.Receive(); ProcessData(data); } catch (Exception) { } }); listenThread.IsBackground = true; listenThread.Start(); }
void Listen() { Thread listenThread = new Thread(() => { try { SocketData data = (SocketData)socket.Receive(); ProcessData(data); } catch { } }); listenThread.IsBackground = true; listenThread.Start(); //MessageBox.Show(data); }
private void ProcessData(SocketData data) { switch (data.Command) { case (int)SocketCommand.NOTIFY: MessageBox.Show(data.Message); break; case (int)SocketCommand.NEW_GAME: this.Invoke((MethodInvoker)(() => { NewGame(); pnlChessBoard.Enabled = false; })); break; case (int)SocketCommand.QUIT: tmCoolDown.Stop(); MessageBox.Show("Người chơi đã thoát!"); caroChess.Ready = false; break; case (int)SocketCommand.SEND_POINT: this.Invoke((MethodInvoker)(() => { prcbCoolDown.Value = 0; tmCoolDown.Start(); OtherPlayerMark(data.Point); })); break; case (int)SocketCommand.END_GAME: break; default: break; } Listen(); }
public void SendData(SocketData data) { gameSocket.Send(data); }
private void ProcessData(SocketData data) { PlayerName = board.ListPlayers[board.CurrentPlayer == 1 ? 0 : 1].Name; switch (data.Command) { case (int)SocketCommand.SEND_POINT: // Có thay đổi giao diện muốn chạy ngọt phải để trong đây this.Invoke((MethodInvoker)(() => { board.OtherPlayerClicked(data.Point); pn_GameBoard.Enabled = true; pgb_CountDown.Value = 0; tm_CountDown.Start(); undoToolStripMenuItem.Enabled = true; redoToolStripMenuItem.Enabled = true; btn_Undo.Enabled = true; btn_Redo.Enabled = true; })); break; case (int)SocketCommand.SEND_MESSAGE: txt_Chat.Text = data.Message; break; case (int)SocketCommand.NEW_GAME: this.Invoke((MethodInvoker)(() => { NewGame(); pn_GameBoard.Enabled = false; })); break; case (int)SocketCommand.UNDO: this.Invoke((MethodInvoker)(() => { pgb_CountDown.Value = 0; board.Undo(); })); break; case (int)SocketCommand.REDO: this.Invoke((MethodInvoker)(() => { // pgb_CountDown.Value = 0; board.Redo(); })); break; case (int)SocketCommand.END_GAME: this.Invoke((MethodInvoker)(() => { EndGame(); MessageBox.Show(PlayerName + " đã chiến thắng ♥ !!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); })); break; case (int)SocketCommand.TIME_OUT: this.Invoke((MethodInvoker)(() => { EndGame(); MessageBox.Show("Hết giờ rồi !!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); })); break; case (int)SocketCommand.QUIT: this.Invoke((MethodInvoker)(() => { tm_CountDown.Stop(); EndGame(); board.PlayMode = 2; socket.CloseConnect(); MessageBox.Show("Đối thủ đã chạy mất dép", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); })); break; default: break; } Listen(); }