c# - Number Guessing Game: difficulty setting -
this how generating random number, able have user select difficulty: easy = 1-10, intermediate = 1-50, hard = 1-100. wondering how done. appreciated.
{ class hilow { private int number; public int number { { return number; } } public hilow(int taker) { number = new random().next(taker); } } } { class hilowui { public void welcome() { console.writeline("\nwelcome highlow game!" + "\n\ninstructions: \ntry guess number between selected difficulty range." + "\nif guess right number win!"); console.writeline("\ndifficulty: \n[1] (1-10) easy \n[2] (1-50) intermediate \n[3] (1-100) hard"); } public void play() { welcome(); while (true) { hilow hi = null; int number = hi.number; int guess; int attempts = 0; int difficulty = promptforint("select difficulty"); if (difficulty == 1) { hi = new hilow(10); } else if (difficulty == 2) { hi = new hilow(50); } else if (difficulty == 3) { hi = new hilow(100); } (guess = promptforint("\nenter guess! "); guess != number; guess = promptforint("\nenter guess! ")) { if (guess < number) { console.writeline("higher"); attempts++; } else if (guess > number) { console.writeline("lower"); attempts++; } } console.writeline("\nit took {0} attempts, you've won!\n{1} correct number!\n", attempts, number); console.writeline("would play again? (y/n)"); if (console.readline() != "y") break; } } private int promptforint(string prompt) { int value; console.write(prompt); int.tryparse(console.readline(), out value); return value; } } }
when have user's setting, add option constructor of hilow maximum number. use number generating random number.
Comments
Post a Comment