Object-Oriented Programming Problem 4

အရင်ဆုံး သိရမဲ့ ကြောင်းအရာလေးတွေကို ပြန်ပြီးပြောမယ်နော်... ပထမဆုံး for loop, while loop, arrays တွေဘယ်လို လုပ်ရမလဲ? စတာတွေကိုအရင် ပြန်ပြီးကြည့်လိုက်ပါ... ပြီးရင်တော့ နောက်တစ်ခန်းဖြစ်တဲ့ Problem 4 ကိုဆက်ပါမယ်။

ဒီအခန်းမှာတော့ အသစ်ဆိုလို့ Random Number လုပ်တာ၊ Exception ဆိုတာဘာလဲ? try and catch ကိုဘယ်လိုနေရာတွေမှာ သုံးလည်းဆိုတာတွေ ပါတယ်။ ကျန်တာတွေကတော့ အရင်အခန်းတုန်းက အကြောင်းအရာတွေကိုပဲ ပြန်သုံးတယ်...
Problem Statement  ကတော့



Let’s build a game using Java!

Game Play
The computer generates 5 random numbers, each number within 1 to 50 (inclusive). The numbers are hidden from the player.

§       The player has three tries to guess three numbers out of the list of five numbers generated by the computer.
§       One (1) point is given for every correct number guessed.


Validation
The game should check that only valid numbers are entered. That is:
§       The player cannot enter characters, e.g. “twelve”.
§       Only numbers between 1 and 50 (inclusive) are acceptable.


Enhancement

The game should reject numbers which has already been guessed whether correct or not.  For example, if the user guesses three (3), he should not be allowed to guess three (3) again in the second and third tries.


Game Extension

Extend your program to give a player N rounds of playing the game, where N is a number entered when first starting the game. (Note: save this version of the game under a different name)

ကြည့်လိုက်တာနဲ့ ဘာလုပ်ရမလဲ ဆိုတာ ရှင်းနေပါတယ်။ မရှင်းဘူးဆိုရင်လည်း ပြောပြပါ့မယ်... အရင်ဆုံးကတော့ Random number ကိုလုပ်မယ်.. ပြီးရင် Arrays တစ်ခုကိုလုပ်မယ်၊ Arrays မှာ ၅ခုထားမယ်၊ ဆိုလိုတာက random number  ၅ခုလုပ်မယ်ပေါ့။ ကုဒ်ကတော့ အောက်မှာပါ...

private int[] arrayRandomNumber = new int[5];

private void generateNumbers() {
Random generator = new Random();
for (int i = 0; i < arrayRandomNumber.length; i++) {
arrayRandomNumber[i] = generator.nextInt(50) + 1;
System.out.println(arrayRandomNumber[i]);
}
}
ဒါက method တစ်ခုလုပ်တာပါ။ ပြီးတော့မှ ပြန်ပြီးယူသုံးမှာပါ။ ဘာလို့လုပ်လဲဆိုတာရယ် လုပ်ရင်ဘာတွေပိုကောင်းလည်းဆိုတာ နောက်အခန်းမှာ အသေးစိတ်ပြောပါမယ်။

မှတ်ရန်။ 
import java.util.Random;
ကို အရင်ဆုံး ရေးရမယ်။ random လုပ်ဖို့ import လုပ်တာပါ။

အပေါ်ကကုဒ်ကို ပြန်သုံးချင်ရင် generateNumbers(); ဆိုပြီးသုံးရင်ရပါပြီ။ နောက်တစ်ခုကတော့ ကစားမဲ့လူက ၃ ခါစမ်းခွင့် ရှိတဲ့အတွက် for loop နဲ့ ပြီးတော့ while loop နဲ့ ရေးမယ်...

for (int num = 0; num < 3; num++) {
boolean valid = true;
while (valid) {

int guessNumber = Keyboard.readInt("GUESS SECRET NUMBER "
+ (num + 1) + " > ");

ပြီးရင် if  နဲ့ conditions  တွေလုပ်မယ်၊ for loop နဲ့ numbers တွေတူလား အတူလား စစ်မယ်။ တူတယ်ဆိုရင် score ကို ၁ ပေါင်းပေးမယ်။ ဒီမှာ if ,else ၂ခုနဲ့လုပ်ထားတယ်၊ ပထမတစ်ခုက number ကို ၅၀ နဲ့ ၁ အတွင်းရှိမရှိစစ်တာ။ နောက်တစ်ခုကတော့ number  က random number နဲ့ တူ မတူ စစ်တာ။ တူရင် ၁ပေါင်းမယ်ပေါ့။ else ကတော့ ၁ ကနေ ၅၀ အတွင်း မဖြစ်ရင် ပြန်ရေးခိုင်းဖို့ပါ။ အောက် ကကုဒ်ကို ကြည့်လိုက်ရင် ရှင်းသွားမယ် ထင်တယ်။

if (guessNumber >= 1 && guessNumber <= 50) {

for (int i = 0; i < arrayRandomNumber.length; i++) {
if (guessNumber == arrayRandomNumber[i]) {
score++;
}
valid = false;
}

} else {
System.out.println("  Please enter number (1 to 50)  ");
}

နောက်ဆုံးအပိုင်းကတော့ ရှုံးလား နိုင်လား စစ်တာ ပါ။ if and else နဲ့ပါပဲ။ score ၁ ရ ရင် နိုင်တယ်။ မရရင် ရှုံးတယ်ပေါ့။

if (score <= 0) {
System.out.println("You Lose! Try Again...");
} else {
System.out.println(" You are **Winner** ");
System.out.println(" Your Score is " + score);
}

ပြီးရင်အားလုံးပြီးသွားပါပြီ။ ပေါ်မှာပြောထားတဲ့ Exception, try and catch အကြောင်းလေး မပြောလိုက်ရဘူး။ Exceptions တွေကတော့ integer  နေရာမှာ String ရေးလို့ မရတာ၊ zero နဲ့ စားလို့ မရတာ စတဲ့ errors တွေ ကို ပြောတာပါ။ အဲ့လို error တွေမဖြစ်ချင်ရင် try and catch ကို သုံးပေါ့။ ကျောင်းကပေးထားတဲ့ keyboard.java မှာ အများကြီးသုံးထားတယ်။ လေ့လာချင်ရင် keyboard.java ကို ဖွင့်ပြီး ကုဒ်တွေကို လေ့လာ ကြည့်ပါ... နောက် အခန်းမှာ class, methods, field, main စတာတွေ ပါလိမ့်မယ် ကြိုပြီးလေ့လာလို့ ရအောင် class diagram လေး ထည့်ပေးလိုက်တယ်။


နောက် အခန်းမှပဲ အသေးစိတ် ပြောတော့မယ်။ ဆက်ပါအုန်းမယ်.....



0 comments:

Post a Comment