Making my program pause between drawing cards - c#

I have made a little Blackjack game, and I'd like to make the computer wait between each card he pulls, however using System.Threading.Thread.Sleep(int x) does not make the program wait between cards, but makes it wait for x * amount of cards..
I also know that using Thread.Sleep is not a good way, so I'd rather learn a better way as I am creating this program entirely for educative purposes.
I'll add the code underneath which decides whether or not a card should be drawn, and the method that draws the card.
private void ComputerTurn()
{
drawCard.Enabled = false;
finishTurn.Enabled = false;
while (computerTotalScore <= 11)
{
ComputerDrawCard();
}
drawAgain = true;
while (drawAgain)
{
ComputerDrawCard();
if (totalScore <= 21)
{
if (computerTotalScore > totalScore)
{
drawAgain = false;
}
else
{
drawAgain = true;
}
}
else
{
if (computerTotalScore > 16)
{
drawAgain = false;
}
else
{
drawAgain = true;
}
}
}
DecideWinner();
}
public void ComputerDrawCard()
{
cardAlreadyPulled = true;
while (cardAlreadyPulled)
{
cardType = random.Next(0, 4);
cardNumber = random.Next(0, 13);
if (!pulledCards[cardType, cardNumber])
{
cardAlreadyPulled = false;
pulledCards[cardType, cardNumber] = true;
}
}
ComputerUpdateCardPictures();
computerScore = cardScores[cardNumber];
if (computerScore == 1)
{
if (computerTotalScore <= 10)
{
computerScore = 11;
}
else
{
computerScore = 1;
}
}
computerTotalScore += computerScore;
txtComputerCurrentScore.Text = computerScore.ToString();
txtComputerTotalScore.Text = computerTotalScore.ToString();
System.Threading.Thread.Sleep(random.Next(250, 750));
}

There are multiple ways to achieve something like this. I believe what you're attempting to do is simulate a human taking time to do things. I recommend using a combination of expected wait times and a timer to achieve what you want.
class Example
{
public Example()
{
// create a stalled timer
_pulse = new Timer(this.TakeAction);
}
TimeSpan _drawdelay = TimeSpan.FromSeconds(2);
DateTime _lastAction = DateTime.MinValue;
Timer _pulse;
public void Start()
{
// start the timer by asking it to call the worker method ever 0.5 seconds
_pulse.Change(0, 500);
}
public void Stop()
{
// stop the timer by setting the next pulse to infinitely in the future
_pulse.Change(Timeout.Infinite, Timeout.Infinite);
}
void TakeAction(object x)
{
lock (_pulse)
{
DateTime now = DateTime.Now;
if(now - _lastAction > _drawdelay)
{
// do work ...
_lastAction = now;
}
}
}
}
That said, the above code will run into issues if the work being done takes longer than 500 milliseconds to complete. Add thread safety as necessary.

I would add a last time drawn and time between draws members. Then before drawing a card, get the time between now and the last time pulled. If the time is greater than the allowed time between the draws its cool to draw.
private DateTime _lastWrite = DateTime.Now;
private TimeSpan _delay = TimeSpan.FromMilliseconds(100);
public void ComputerDrawCard() {
var now = DateTime.Now;
if (now - _lastWrite < _delay)
return;
_lastWrite = now;
draw card...
}
Here's a gist of an example working correctly.

Related

Need some help, running a console timer in C#, any ideas?

Since C# is being pretty cringe, I am asking for help on how to debug it based on the comments in the code. As a summary, I have a timer running in console (noob here, code copy-pasted or tutorial'd) and the numbers ONE and GO are running simultaneously, and I have not found a way to stop the timer after the intended result is reached, and help would be appreciated!
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public int startTime = 4;
void Start()
{
InvokeRepeating("timer", 1.0f, 1.0f);
}
void timer()
{
if (startTime > 0)
{
if (startTime == 3)
{
Debug.Log("THREE");
startTime -= 1;
}
else
{
if (startTime == 2)
{
Debug.Log("TWO");
startTime -= 1;
}
else
{
Debug.Log("ONE");
startTime -= 1;
//Precent "ONE" and "GO!" from running simultaneously
}
}
}
if (startTime == 0)
{
Debug.Log("GO!");
//Fix "GO!" playing to console indefinetly after timer is finished
}
void Update()
{
}
}
}
To do this, you can use the CancelInvoke method by passing the method name to quotes as a parameter you can call it when startTime goes to 0. You can safly remove the Update Method
public int startTime = 3;
void Start()
{
InvokeRepeating("timer", 1.0f, 1.0f);
}
void timer()
{
//go throw all the cases
switch (startTime)
{
case 0:
Debug.Log("GO!");
CancelInvoke("timer");
break;
case 1:
Debug.Log("ONE");
break;
case 2:
Debug.Log("TWO");
break;
case 3:
Debug.Log("THREE");
break;
}
//substract - from time
startTime -= 1;
}
Or use this Coroutine
void Start()
{
StartCoroutine("timer");
}
IEnumerator timer()
{
//debug the time
Debug.Log(startTime); //you can use the if statement do show it as text or use any humanizer **C# 101 series**
startTime--; //here we substract 1 from time
//we wait for 1s
yield return new WaitForSeconds(1);
//if the time still >= 0 repeat
if(startTime >=0)
StartCoroutine("timer");
}
i think you want
//if (startTime == 0)
else
{
Debug.Log("GO!");
//Fix "GO!" playing to console indefinetly after timer is finished
}

Xamarin StartTimer subtract seconds from TimeSpan don't work

I am programming a countdown timer with Xamarin Forms using the StartTimer method of Device class. The problem is that the selected time is not reduced by seconds but remains as originally selected. I don't know where the problem is. Thanks for the help.
public partial class MainPage : ContentPage
{
private int minutes = 0;
private int hours = 0;
private TimeSpan restTime = TimeSpan.Zero;
public MainPage()
{
InitializeComponent();
StartCountdown(hours, minutes);
}
public void StartCountdown(int hours, int min)
{
var M = TimeSpan.FromMinutes(min);
var H = TimeSpan.FromHours(hours);
restTime = H.Add(M);
Device.StartTimer(TimeSpan.FromSeconds(1), () =>
{
//Here should subtract from variable restTime one second every second
restTime.Add(new TimeSpan(0,0,-1));
if (restTime == TimeSpan.Zero)
{
//Do something
return false;
}
else
{
return true;
}
});
}
You're calling restTime.Add, but not doing anything with the result, here:
restTime.Add(new TimeSpan(0,0,-1));
The Add method doesn't change the existing TimeSpan - it returns a new one. Instead, you want:
restTime = restTime.Add(new TimeSpan(0, 0, -1));
Or more readably IMO:
restTime -= TimeSpan.FromSeconds(1);
(I'd also use restTime = new TimeSpan(hours, min, 0); to start with, probably. Or maybe restTime = TimeSpan.FromHours(hours) + TimeSpan.FromMinutes(min);.)
You might want to have a field for one second:
private static readonly TimeSpan OneSecond = TimeSpan.FromSeconds(1);
...
restTime -= OneSecond;

Run lerp over timer

First off, I am not using any kind of game engine, I am modding a game in C# and I am NOT using UnityEngine API so I do not have any Update() functions.
So I am trying to figure out how I could create a timer, some standard out of the box C# timer that would increase the lerp distance over a set speed.
model.rotationM = Vector3.Lerp(model.rotation, model.rotationM, (float)0.016);
NAPI.Entity.SetEntityRotation(model.handle, model.rotationM);
I would like to wrap this in a timer that every 100ms it will increase the float at the end of the lerp by some set amount over the duration of a time, so say I set float speed = 5f;
I want to increase that lerp distance every 100ms for 5 seconds until it reaches its goal.
Is this possible to do?
I've created an example timer class which will slowly increment a value by a given amount until it reaches 100% (1.0):
public class LerpTimer : IDisposable
{
private readonly Timer _timer;
private readonly float _incrementPercentage = 0;
public event EventHandler<float> DoLerp;
public event EventHandler Complete;
private bool _isDisposed = false;
private float _current;
public LerpTimer(double frequencyMs, float incrementPercentage)
{
if (frequencyMs <= 0)
{
throw new ArgumentOutOfRangeException(nameof(frequencyMs), "Frequency must be greater than 1ms.");
}
if (incrementPercentage < 0 || incrementPercentage > 1)
{
throw new ArgumentOutOfRangeException(nameof(incrementPercentage), "Increment percentage must be a value between 0 and 1");
}
_timer = new Timer(frequencyMs);
_timer.Elapsed += _timer_Elapsed;
_incrementPercentage = incrementPercentage;
}
private void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (_isDisposed)
{
return;
}
if (this.Current < 1)
{
this.Current = Math.Min(1, this.Current + _incrementPercentage);
this.DoLerp?.Invoke(this, this.Current);
}
if (this.Current >= 1)
{
this._timer.Stop();
this.Complete?.Invoke(this, EventArgs.Empty);
}
}
public float Current
{
get
{
if (_isDisposed)
{
throw new ObjectDisposedException(nameof(LerpTimer));
}
return _current;
}
set => _current = value;
}
public void Start()
{
if (_isDisposed)
{
throw new ObjectDisposedException(nameof(LerpTimer));
}
if (_timer.Enabled)
{
throw new InvalidOperationException("Timer already running.");
}
this.Current = 0;
_timer.Start();
}
public void Stop()
{
if (_isDisposed)
{
throw new ObjectDisposedException(nameof(LerpTimer));
}
if (!_timer.Enabled)
{
throw new InvalidOperationException("Timer not running.");
}
_timer.Stop();
}
public void Dispose()
{
_isDisposed = true;
_timer?.Dispose();
}
}
Sample usage:
var lerpTimer = new LerpTimer(100, 0.016f);
lerpTimer.DoLerp += (sender, value) => {
model.rotationM = Vector3.Lerp(startRotation, endRotation, value);
NAPI.Entity.SetEntityRotation(model.handle, model.rotationM);
};
lerpTimer.Start();
So you would call this once, and then it would keep going until it reaches 100% (endRotation).
It's not necessarily the code you should use, but it should illustrate how you can use a timer to increase the value over time.
Edit to add some clarity to what a lerp function does:
double lerp(double start, double end, double percentage)
{
return start + ((end - start) * percentage);
}
Imagine we call this every 10% from 4 to 125. We would get the following results:
0% 4
10% 16.1
20% 28.2
30% 40.3
40% 52.4
50% 64.5
60% 76.6
70% 88.7
80% 100.8
90% 112.9
100% 125
Try it online

ViewCell ForceUpdateSize not working sometimes

I am using a custom ViewCell in Xamarin Forms which looks like this:
public class NativeCell : ViewCell
{
public double Height
{
get
{
return base.Height;
}
set
{
base.Height = value;
ForceUpdateSize();
}
}
}
I have also created a method to animate the colapse of the ViewCell which works as expected. However, with the overlay that comes with await, each loop iteration lasts up to 100ms instead of 1ms:
async public void Colapse()
{
for (double i = Height; i >= 0; i--)
{
Height = i;
await Task.Delay(1);
}
}
So I made another method that used the Stopwatch to do the waiting:
async public void Colapse()
{
var Timer = new Stopwatch();
for (double i = Height; i >= 0; i--)
{
Height = i;
Timer.Start();
while(Timer.Elapsed.TotalMilliseconds < 1)
{
}
Debug.WriteLine("-->" + Timer.Elapsed.TotalMilliseconds);
Timer.Reset();
}
}
This last method reports that the wait time is now usually 1.1ms - 1.3ms. Which is great. However, the Height asignment is not doing anything. Or at least ForceUpdateSize is not triggering.
We need to solve few problems here.
You have to specify HasUnevenRows in list view
You cannot do "double i = Height" because Height will be "-1"
You last function setting Height using StopWatch is not really async, so you UI will not be updated till function exits.
Because Task.Delay is slow we can use Device timer with tick interval
Below is the solution that I tested on Android. The code is inside ViewCell in PCL
int currentHeight;
public void Colapse()
{
currentHeight = (int)this.View.Height;
Device.StartTimer(new TimeSpan(100), timerTick);
}
private bool timerTick()
{
if (currentHeight <= 0)
return false;
else
{
Height = currentHeight--;
ForceUpdateSize();
return true;
}
}

Game Object invisible for a given time

Good day everyone, currently I am developing a simple 2D game using SWINGAME. I have set a collision between 2 objects. So when they collide, I want to temporarily make one of them invisible for a certain time. I am stuck about the time component, let's say I want the object to be invisible for 3 seconds after that it will change back to the default object. Below are the two images, if the collision is true then it will display image2, or else display image1. BTW I use a different image to indicate the invisibility. Here's my code.
Player class:
public void Draw ()
{
if (invisible == true) {
if(elapsedTime <= 3.0){
elapsedTime += elapsedTime;
SwinGame.DrawBitmap ("image2.png", (float)X, (float)Y);
}
}else {
elapsedTime = 0;
SwinGame.DrawBitmap ("image1.png", (float)X, (float)Y);
}
}
public bool Invisible {
get { return invisible; }
set { invisible = value; }
}
Object Collision class :
{... //Some codes
for(int i = 0; i < _obstacles.Count; i++)
{
if (_obstacles [i] is Invisible) {
p.Invisible = true;
p.Draw ();
}
}
//Some codes ...}
This should help you to calculate the time accurately by using the StopWatch class:
//somewhere in your code
Stopwatch sw = new Stopwatch();
sw.Start();
public void Draw ()
{
if (invisible == true) {
if(sw.ElapsedMilliseconds <= 3000){
SwinGame.DrawBitmap ("image2.png", (float)X, (float)Y);
}
}else {
sw.Restart();
SwinGame.DrawBitmap ("image1.png", (float)X, (float)Y);
}
}

Categories

Resources