Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7974083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:16:21+00:00 2026-06-04T08:16:21+00:00

Ok, so this is a kind of general program. I am trying to write

  • 0

Ok, so this is a kind of general program. I am trying to write a poker program where the user can interact against computer players. However, in order to simplify it I am testing with a much simpler program where two players are trying to guess a number from the computer. One is a human player and the other is a computer player.

In the game, the players take turn to guess the number beeing set by the program. The computer player just randomizes numbers, where as the user should input a number and press a guess button (or have the option to quit).

My problem is that when it is the humans players turn to act, how can I pause the excecution of the game in order for the human to respond by pressing a button or enter text?

I have tried with using Timer and starting it and waiting for the thread to end but it seems as if that still freezes the gui.

I have also tried to send messages from a background worker thread to update the gui but that gives me an error about not beeing able to access the gui elements from another thread.

Does anyone have any suggestions on how to implement this?

Note that this is not a complete program, since I just made this to understand how to interact with the gui during execution.

The number guesser class:

public class NumberGuesser {

    public Player HumanPlayer { get; private set; }

    public Player ComputerPlayer { get; private set;}

    private Player[] _players = new Player[2];

    private int _maxNumber = 20;

    private bool _done = false;

    private Random random;

    public NumberGuesser() {

        HumanPlayer = new HumanPlayer("Me");

        ComputerPlayer = new ComputerPlayer("HAL9000");

        _players[0] = HumanPlayer;
        _players[1] = ComputerPlayer;

        random = new Random((int) DateTime.Now.Ticks);

    }

    public void Play() {

        var myNumber = random.Next(_maxNumber);
        var index = 0;
        while (!_done) {

            var currentPlayer = _players[index];

            var guess = currentPlayer.Act(_maxNumber);
            if (guess == myNumber) {
                currentPlayer.Score++;
            }

            index = (1 + index)%2;
        }

    }
}

The computer player:

public class ComputerPlayer : Player {

    private readonly Random _random = new Random((int)DateTime.Now.Ticks);

    public ComputerPlayer(string name) : base(name) {
    }

    public override int Act(int max) {

        return _random.Next(max);

    }
}

The human player:

public class HumanPlayer : Player {

    public HumanPlayer(string name) : base(name) {

    }

    public override int Act(int max) {

        return UserGuess();

    }

    private int UserGuess() {


        return 0;

    }
}

The player class:

public class Player {
    public string Name { get; private set; }
    public int Score { get; set; }

    public Player(string name) {
        Name = name;
    }


    public virtual int Act(int max) {
        // How should the human enter the guess here???
        return 0;
    }
}

My main window:

<Window x:Class="GuessMyNumber.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="395" Width="728">
<Grid>
    <Label Content="Guess my number!" Height="28" HorizontalAlignment="Left" Margin="122,58,0,0" Name="label1" VerticalAlignment="Top" />
    <Grid Height="174" HorizontalAlignment="Left" Margin="31,170,0,0" Name="grid1" VerticalAlignment="Top" Width="272" DataContext="{Binding Path=HumanPlayer}">
        <Label Content="Human player" Height="28" HorizontalAlignment="Left" Margin="6,6,0,0" Name="label2" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="55,92,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" 
                 Text="{Binding Path=HumanGuess, UpdateSourceTrigger=PropertyChanged}"/>
        <Button Content="Guess" Height="23" HorizontalAlignment="Left" Margin="79,130,0,0" Name="button1" VerticalAlignment="Top" Width="75"
                Command="{Binding Path=GuessCommand}"/>
        <Label Content="{Binding Path=Name}" Height="28" HorizontalAlignment="Left" Margin="96,6,0,0" Name="label3" VerticalAlignment="Top" />
        <Label Content="Score" Height="28" HorizontalAlignment="Left" Margin="6,40,0,0" Name="label4" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="55,42,0,0" Name="textBox2" VerticalAlignment="Top" Width="75" Text="{Binding Path=Score, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" />
        <Button Content="Quit" Height="23" HorizontalAlignment="Left" Margin="182,130,0,0" Name="buttonQuit" VerticalAlignment="Top" Width="75" Command="{Binding Path=QuitCommand}" />
    </Grid>
    <Grid Height="174" HorizontalAlignment="Left" Margin="328,170,0,0" Name="grid2" VerticalAlignment="Top" Width="270" DataContext="{Binding Path=ComputerPlayer}" >

        <Label Content="Computer player" Height="28" HorizontalAlignment="Left" Margin="6,6,0,0"  VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="55,92,0,0"  VerticalAlignment="Top" Width="120" Text="{Binding Path=HumanGuess, UpdateSourceTrigger=PropertyChanged}"/>

        <Label Content="{Binding Path=Name}" Height="28" HorizontalAlignment="Left" Margin="111,6,0,0" VerticalAlignment="Top" />
        <Label Content="Score" Height="28" HorizontalAlignment="Left" Margin="6,40,0,0" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="55,42,0,0"  VerticalAlignment="Top" Width="75" Text="{Binding Path=Score, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" />
    </Grid>
    <Button Content="Start Playing" Height="23" HorizontalAlignment="Left" Margin="254,59,0,0" Name="buttonStartPlay" VerticalAlignment="Top" Width="75" Click="buttonStartPlay_Click" />
</Grid>

And the code behind:

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {

    private readonly NumberGuesser _numberGuesser;

    public MainWindow() {
        InitializeComponent();
        _numberGuesser = new NumberGuesser();
        DataContext = _numberGuesser;

    }

    private void buttonStartPlay_Click(object sender, RoutedEventArgs e) {

        _numberGuesser.Play();

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:16:23+00:00Added an answer on June 4, 2026 at 8:16 am

    You have to approach the problem from a different perspective. Instead of having a method that runs synchronously which asks the user to act, like you might interact with your local computer, you need to request it, and then wait asynchronously for something (e.g. an event) to tell you the user has finished. (more similar to requesting something of a remote computer)

    Here’s something to get you started:

    public abstract class Player
    {
        public string Name { get; private set; }
        public int Score { get; set; }
    
        public Player(string name)
        {
            Name = name;
        }
    
        public abstract void ActAsync(int max);
    
        public virtual event EventHandler<ActEventArgs> ActComplete;
    }
    public class ActEventArgs : EventArgs
    {
        public int Result { get; private set; }
        public ActEventArgs(int result)
        {
            this.Result = result;
        }
    }
    public class HumanPlayer : Player
    {
        public HumanPlayer(string name)
            : base(name)
        {
        }
    
        public override void ActAsync(int max)
        {
            // button1 should be the user's guess button; anything else you need to do to allow them to interact should be done here
            button1.IsEnabled = true;
            // this will result in a handler being added each time the user is asked to act; you'll actually want to attach just once
            button1.Click += (s, e) =>
                {
                    button1.IsEnabled = false;
                    if (ActComplete != null)
                        ActComplete(this, new ActEventArgs(int.Parse(data.HumanGuess))); // data.HumanGuess is their input, as a string
                };
        }
    
        public override event EventHandler<ActEventArgs> ActComplete;
    }
    public class ComputerPlayer : Player
    {
        private readonly Random _random = new Random((int)DateTime.Now.Ticks);
    
        public ComputerPlayer(string name)
            : base(name)
        {
        }
    
        public override void ActAsync(int max)
        {
            if (ActComplete != null)
                ActComplete(this, new ActEventArgs(_random.Next(max)));
        }
        public override event EventHandler<ActEventArgs> ActComplete;
    }
    

    Edit: Compared to Jmyster’s solution (IMHO): his is better for simple interactions, i.e. local-user-to-local-computer, but if you’re planning on expanding this into multi-user scenarios, events will be better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to figure out a MySQL statement, and this is kind of a general
this kind of emergency, so please, can someone help me... I'm using movingboxes plugin
OK so this is kind of a general question here. We run an ASP/C#
This is just kind of general question, steming from something else. Say you want
I know this kind of question has been asked before, but the general solution
This is kind of a general DB design question. If one has an associative
This kind of code would normally work in PHP, but since the scope is
This was the first time I encountered this kind of error after dealing with
I know this kind of questions have been asked already many times before. The
I am sure that this kind of questions must have been asked before, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.