コード例 #1
0
        void CellClicked(object sender, InputEventArgs e)
        {
            if (!currentTurn)
            {
                return;
            }
            var cell = sender as TTTCell;

            if (cell.Owner == PlayerID.None)
            {
                numMoves++;
                currentTurn = !currentTurn;
                cell.Owner  = player;
                writer.Write("move");
                writer.Write(cell.Row);
                writer.Write(cell.Column);
                if (CheckWinner(cell.Row, cell.Column, player))
                {
                    MessageBox.Show("Congratulations, You Have Won the Game.");
                    writer.Write("playagain");
                    var dialog = new PlayAgainDialog();
                    if (dialog.ShowDialog() == true)
                    {
                        writer.Write(dialog.ResponseText);
                    }
                    else
                    {
                        writer.Write("no");
                    }
                }
            }
        }
コード例 #2
0
        private void HandleRequest()
        {
            netStream = socket.GetStream();
            reader    = new BinaryReader(netStream);
            writer    = new BinaryWriter(netStream);

            while (socket.Connected)
            {
                var cmd = reader.ReadString();
                switch (cmd)
                {
                case "move":
                    numMoves++;
                    var r = reader.ReadInt32();
                    var c = reader.ReadInt32();
                    Application.Current.Dispatcher.Invoke(() => { board[r, c].Owner = opponent; });
                    currentTurn = !currentTurn;
                    if (CheckWinner(r, c, opponent))
                    {
                        Application.Current.Dispatcher.Invoke(() => {
                            MessageBox.Show("Sorry, You Have Lost the Game.");
                        });
                        writer.Write("playagain");
                        Application.Current.Dispatcher.Invoke(() => {
                            var dialog = new PlayAgainDialog();
                            if (dialog.ShowDialog() == true)
                            {
                                writer.Write(dialog.ResponseText);
                            }
                            else
                            {
                                writer.Write("no");
                            }
                        });
                    }
                    break;

                case "chat":
                    var msg = "Opponent> " + reader.ReadString() + "\n";
                    Application.Current.Dispatcher.Invoke(() => { ChatLog.Text += msg; });
                    break;

                case "playagain":
                    var playagain = reader.ReadString();
                    if (playagain == "yes")
                    {
                        Application.Current.Dispatcher.Invoke(() => { Reset(); });
                    }
                    else
                    {
                        socket.Close();
                    }
                    break;
                }
            }
        }