コード例 #1
0
        private void UserMove(object sender, RoutedEventArgs e)
        {
            var bt = (Button)sender;
            var r  = Grid.GetRow(bt);
            var c  = Grid.GetColumn(bt);

            var moved = client.Move(r, c, out var serverRow, out var serverCol);

            if (moved)
            {
                bt.Content = "O";
            }
            else
            {
                status.Content = "Nie możesz wykonać takiego ruchu!";
                return;
            }

            var userResult = client.CheckWin(r, c);

            if (userResult != 0)
            {
                status.Content = "Wygrałeś!";
                Stop(r, c, userResult, true);
                return;
            }


            if (serverCol == -1 && serverRow == -1)
            {
                status.Content = "Remis!";
                Stop(r, c, 0, false);
                return;
            }


            var srv = (Button)board.Children.Cast <UIElement>()
                      .First(b => Grid.GetRow(b) == serverRow && Grid.GetColumn(b) == serverCol);

            srv.Content = "X";

            var serverResult = client.CheckWin(serverRow, serverCol);

            if (serverResult != 0)
            {
                status.Content = "Serwer wygrał!";
                Stop(serverRow, serverCol, serverResult, false);
                return;
            }

            status.Content = "Gra w toku...";
        }