Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Really simple game in java

Hello to everybody, I have an assignment and I need help. I want to create a simple game in java that allows a user to play with computer. First the user has to choose who will start the game. First choice the user, second choice the computer, third choice randomly the program decides who will start. Then if the user choose the first he have to decide how many coins he want to take, one or two. Then the computer decides how many coins wants to take, one or two. The game will finish the last one picks the last coin. And whoever picks the last coin is loosing. Finally the program will print who won.

So I started like this import javax.swing.JOptionPane; public class game{ public static void main(String args []){ // i declare the variables for the coins int coin1 ,,,, coin27; String userChoice; int choice1 = 1; int choice2 = 2; int choice3 = 3;

       userChoice = JOptionPane.showInputDialog("choose " + choice1 + " for the user " + " choose " + choice2 + " for the computer " + " choose " + choice3 + " randomly");
       int num = Integer.parseInt(userChoice);

       if (num =  choice1){
       // gives the choice to pick one or two coins.

      }

}

}

i tried to write code with different ways but i am stack. please help me!!! i am a beginner in Java!!!

6 Answers

Seems like this is for school or something. If you have any questions on this stuff I would be happy to help you better understand the pieces, but I don't wanna right it out for you lol. Not with some money lol.

So, here is the key to your code. On the third try, the computer at random picks. So you will need a counter, that counts to 3 and on the third one resets back to zero and calls this static math.random() method. It's static because it has no instance variables and does the same thing no matter what you give it and thus no reason to instantiate it.

Ok, so you will need 1 method that picks a random number between a range. this range happens to be 0 or 1. Since 2 we know is three. So you will need to look up the docs cause I wont tell it directly to you, but you can pass in args for a range into the math.random. So it will only randomly pick a number between a certain range. In this case 0,1. BUT wait, 0, 1 can also be used to represent the coins that were bet. 0 being 1 coin and 1 being 2 coins. Which means you can accomplish having the computer randomly pick as well as the random pick of coins to bet by the computer, and hey, even randomly pick what the computer is going to do with just 1 math.random. Now you don't have to put it into a method since it's static you can just call it with . notation. but I would put it into a methodto keep from typing math.random everywhere.

You will then need what I use at least is the scanner class. create an instance of scanner and you can prompt the user for input. use if else or if else if statements to based on what is happening to decide what to do next. I like scanner because I can open it and keep the running dialog going throughout the do while loop and just close it at the end. Saves some lines of programming.

Wrap all of this up in a DO WHILE loop. Do something while something else is true. Then you can incorporate and EXIT button in your game by matching the string exit.. but remember the difference between == and x.equals() since x.equals will make it so you don't need to create objects to compare. Anyway, if exit is typed you leave the game. So you set a boolean to true, then on exit you set it to false, not just on exit, but you also set it to false when the game is over whatever makes the game end. So it's probably best to make this a member variable in the class. So you don't have to worry about scope. So you are doing this.

Let me know if you have any specific questions I can address. Do not over think this either lol. It's really not very much code. Just sounds like a lot lol.

Thank you very match but this is not helpfull. i want parts of the code in order to guide me how to complete this assignment

If you want to know about parts, you can ask about parts. But assuming I give you all the parts, than I have to actually develop said parts as well as give them to you. Which voids the assignment. If you want to type out pseudo code and submit that, I can give direction on what operators and libraries to use for which sections. But that's all I can offer.

Perhaps someone else will do more? Thanks

how would you start this kind of game? what variable you would declare from the beginning? Its my first game and i trying to think with the simple way but now i am confused. i cant use the scanner. i prefer to use JOptionPane for the user input.

if it is so simple. could you please write me the code? i would like to examine it

JOptionPane is not what you want to use.

I will give myself 2 minutes free hand right now. We'll see how far I get. Aside form that. You are on your own without specific questions that at least show you explored or tried something.

public class MainClass {
           int mturn = 1;
           int mTurnCount = 0;

           public static void main(String[] args) {

                System.out.print("let's play a game! The play er is User1 and the computer is User2");  
    do {
                Scanner scanner = new Scanner(System.in);
        if (mTurnCount == 0) {
                        System.out.println("It's User1's turn to put a wager on this turn");
                        System.out.print("How many coins would you like to wager, 1 or 2?");
                        value = scanner.nextInt();
                        if (value == 1) {
                               System.out.print("User1 is wagering 1 coin.?");
                        }else if (type == 2 & type < 3){
                               System.out.print("User1 is wagering 2 coin.?");
                               System.out.print("It's the computers turn to choose a wager.");
                           }else   {
                                System.out.print("Please choose a valid wager. 1 or 2.");

             /**     create a new class called random. Then use that class to choose a random number between 1 and 2. Copy the if else if else statement and use that in the random class. So if the random number is 1 do x if 2 do y. Then return a string. Print that string. 

Don't forget to increment the mturncount so that the loop remembers who's turn it is. When it's turn 3 use the random method you already created to have the computer go first, then you go second with the code already generated. 

of course you can abstract this and there might be more variables or whatever I havn't thought about it but I believe I am approaching like 8 minutes So way over my time limit.
**/

Good luck!