Hi Guys,
I am learning Java right now and I'm currently on the topic of OOP basics. I wrote this small program, just wondering what you guys think.
How this program works is that it creates 3 players, generates 3 mystery numbers and generates 3 number guesses for the players. It then compares the numbers and check if the players guessed right. The console shows which player(s) won.
I have not learned how use the input stream yet so no hate :). But I'm sure it's not that difficult since I have programmed in C before.
Right now I'm trying to focus on learning OOP as this is the first time I'm tackling it.
I made 3 classes. First is the game launcher, where my objects are created and methods are called. Second consists of a method that generate a random number between 1-9 for each of the three players and another method to compare the mystery number and the player guess number. Lastly, a class for generating user guesses randomly.
I am learning Java right now and I'm currently on the topic of OOP basics. I wrote this small program, just wondering what you guys think.
How this program works is that it creates 3 players, generates 3 mystery numbers and generates 3 number guesses for the players. It then compares the numbers and check if the players guessed right. The console shows which player(s) won.
I have not learned how use the input stream yet so no hate :). But I'm sure it's not that difficult since I have programmed in C before.
Right now I'm trying to focus on learning OOP as this is the first time I'm tackling it.
I made 3 classes. First is the game launcher, where my objects are created and methods are called. Second consists of a method that generate a random number between 1-9 for each of the three players and another method to compare the mystery number and the player guess number. Lastly, a class for generating user guesses randomly.
PHP Code
<?php
public class GameLauncher{
public static void main(String args[]){
GuessGame p1 = new GuessGame();
GuessGame p2 = new GuessGame();
GuessGame p3 = new GuessGame();
p1.mysteryNum = p1.genRandomNum();
p2.mysteryNum = p2.genRandomNum();
p3.mysteryNum = p2.genRandomNum();
Player g1 = new Player();
Player g2 = new Player();
Player g3 = new Player();
g1.playerGuess = g1.genPlayerNum();
g2.playerGuess = g2.genPlayerNum();
g3.playerGuess = g3.genPlayerNum();
p1.compareNums(p1.mysteryNum, g1.playerGuess, 1);
p2.compareNums(p2.mysteryNum, g2.playerGuess, 2);
p3.compareNums(p3.mysteryNum, g3.playerGuess, 3);
}
}
PHP Code
<?php
class GuessGame{
int mysteryNum;
int genRandomNum(){
return((int)Math.floor(Math.random() * 10));
}
void compareNums(int num1, int num2, int playerNum){
System.out.print("P"+playerNum+": Mystery #="+num1+" Guess="+num2);
if(num1 == num2){
System.out.println(" ---> WIN");
}
else{
System.out.println(" ---> LOST");
}
}
}
PHP Code
<?php
class Player{
int playerGuess;
int genPlayerNum(){
return((int)Math.floor(Math.random() * 10));
}
}



