1
What you need to answer the question is comments in the code:
using System;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
player1.Start();
t.Start();
while (t.IsAlive); //A intenção seria depois que o Thread t acabar se a Thread player1
//ainda estiver ativa o jogador perde a rodada.
//gostaria também de que se a Thread player1 já acabou
//a Thread t pode se encerrar.
if (player1.IsAlive) player1.Abort(); Console.Write("\nSeu Tempo Acabou");
}
static void player()
{
Console.Write("Player1 Escreva Algo (Nao Deixe Passar 10 Segundos): ");
str += Console.ReadLine() + " ";
Console.Write(" {0}", str);
}
static void tempo()
{
System.Threading.Thread.Sleep(10000); //10 sec = 10.000 milisec
}
static String str = "";
static System.Threading.Thread player1 = new System.Threading.Thread(player);
static System.Threading.Thread t = new System.Threading.Thread(tempo);
}
Ex: In chess games you have a time available to move your piece, if you do not move within for example 30 seconds you lose your move and it is the next player’s turn (Ai would show the message), now if you move your piece ahead of time, You just pass the time to another player without losing anything. the idea is +/- this.
I don’t know if this really serves is because it’s kind of confusing for me but for me it seems that always after passing the 10 seconds it will show " nseu Tempo Acabou" I want this only appears if the player does not type anything.
– axell-brendow
I hadn’t understood it that way. I’ll edit the answer. Whatever happens after the player types something?
– ramaral
Well just do the following if the user report something beauty put in str and close the program, if it does not report anything in 10 seconds say that your time is over and pause the program with a Readline() and then close.
– axell-brendow
How would I treat the case where for example the player keystrokes something and then deletes ? in this case should also be shown the message.
– axell-brendow
I discovered a bug mt loco kkkk if you type for example Asd and then go pressing Backspace until the cursor reaches the word 'player1' then just come pressing the arrow to the right and see it happen kkk
– axell-brendow
For that we would have to use
Console.ReadLine()
instead ofConsole.ReadKey();
. In this situation it will only be considered that the player entered something after this typing enter, that is, the time is counting until it is keyboard enter– ramaral