import std.stdio, std.cstream, std.c.time, std.c.stdlib;

void main()
{
	immutable ubyte limit = 20;
	
	srand(time(null));
	uint chosen = 1 + rand() % limit;
	
	writeln("This is a guessing game");
	writeln("I have chosen a number between 1 and 20"
	        " which you must guess");
	
	ubyte guess = 0;
	
	for(ubyte icount = 3; icount > 0; --icount)
	{
		writefln("You have %s tr%s left.", icount, icount == 1 ? "y" : "ies");
		write("enter a guess: "); // prompt for a guess
		scanf("%d", &guess); // Read a guess
		
		// check if guess correct
		if(guess == chosen)
		{
			writefln("\nYou guessed it!");
			return; 
		}
		
		// Check for an invalid guess
		if(guess < 1 || guess > 20)
		{
			writeln("I said between 1 and 20. ");
		}
		else
		{
			writefln("Sorry. %s is wrong.", guess);
		}
	}
	writefln("you have had three tries and failed. The number was %s", chosen);
	din.getc();
}