Tightening up code/tips/constructive criticism/ about my BASIC game

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 2437 Views - Last Post: 09 September 2012 - 04:52 PM Rate Topic: -----

#1 Java Hardstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 04-September 12

Tightening up code/tips/constructive criticism/ about my BASIC game

Posted 04 September 2012 - 07:36 PM

I've recently just started to learn Java to pursue my passion for computer games. Although I know creating games in Java isn't an easy task, I'm extremely determined and am willing to take the steps to get where I want.

I was searching the web looking for easy projects to do in order to gain a little bit of experience with the language and I stumbled upon a thread on this website that prompted a "Rock, Paper, Scissors" application. This looked like fun so I decided to try to make one on my own using the knowledge of Java I had up to this point.

As I was writing the code and running the application to test/debug it, I noticed that it put a huge lag on my system to type in code or run the program, so I figured I was doing things the complicated way, giving the computer more to read than it needed.

Any help, tips, advice, suggestions, criticism or comments will be appreciated! (Particularly help with decreasing the amount of lag the program puts on my system, given the problem isn't my hardware.)

Here's my code:
import java.util.Scanner;
import java.util.Random;
public class stuff {
	public static void main(String args[]){
	Random dice = new Random();	
	Scanner input = new Scanner(System.in);
	Scanner input2 = new Scanner(System.in);
	String uchoice;
	int compchoice;
	String continueRunning= ("Yes");
	do{
	System.out.println("Welcome to Ace's first java game!");
	System.out.println("Type in Rock, Paper, or Scissors to make your selection: ");
		uchoice = input.nextLine();
	compchoice=dice.nextInt(3)+1;
				if(uchoice.equals("Rock")&& compchoice == 1){
			System.out.println("You and the computer both selected Rock.");
			System.out.println("Tie game, sir.");
		}
		else if(uchoice.equals("Rock")&& compchoice == 2){
			System.out.println("You selected Rock and the computer selected Paper.");
			System.out.println("You lose. Sucker.");
		}
		else if (uchoice.equals("Rock")&& compchoice == 3){
			System.out.println("You selected Rock and the computer selected Scissors.");
			System.out.println("You win!");
		}
		else if (uchoice.equals("Paper")&& compchoice == 1){
			System.out.println("You selected Paper and the computer selected Rock.");
			System.out.println("You win!");
		}
		else if (uchoice.equals("Paper")&& compchoice == 2){
			System.out.println("You selected Paper and the computer selected Paper.");
			System.out.println("Tie game, sir.");
		}
		else if (uchoice.equals("Paper")&& compchoice == 3){
			System.out.println("You selected Paper and the computer selected Scissors.");
			System.out.println("You lose. Sucker.");			
		}
		else if (uchoice.equals("Scissors")&& compchoice == 1){
			System.out.println("You selected Scissors and the computer selected Rock.");
			System.out.println("You lose. Sucker.");
		}
		else if (uchoice.equals("Scissors")&& compchoice == 2){
			System.out.println("You selected Scissors and the computer selected Paper.");
			System.out.println("You win!");
		}
		else if (uchoice.equals("Scissors")&& compchoice == 3){
			System.out.println("You selected Scissors and the computer selected Scissors.");
			System.out.println("Tie game, sir.");
		}
		else{System.out.println("Try using a captial letter, dork.");}
		System.out.println("Would you like to play again? Type Yes or No.");
		continueRunning=input2.nextLine();
				
	}
		while(continueRunning.equals("Yes"));
	
	
	}
	}





Thanks again!

-Java Hardstyle

I just noticed that BASIC is another programming language and that I have no idea why I capitalized that, as I mean it as an adjective referring to my game. This game was definitely built with Java. /Facepalm

Is This A Good Question/Topic? 0
  • +

Replies To: Tightening up code/tips/constructive criticism/ about my BASIC game

#2 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Tightening up code/tips/constructive criticism/ about my BASIC game

Posted 04 September 2012 - 08:03 PM

Quote

I noticed that it put a huge lag on my system to type in code or run the program

Your computer is lagging when you are writing the Java code? Maybe that's your IDE. If an IDE is trying to suggest options to you it can be a little unresponsive while its being "intelligent".

Your code looks fine for a beginner. I don't see anything that would lag the computer. A couple suggestions, though:

  • You only need one Scanner object for System.in. Having more than one is pointless.
  • Your code might be easier to understand if you declared some constants for your computer numbers. static final int ROCK = 3; Like that.
  • Next see if you can make your own methods to shorten your code and make it easier to understand.
  • And also, typically class names start with a capital letter. It's the Java convention and most the Java programmers are used to that. It makes it easier to differentiate between methods/objects and classes/constructors.

This post has been edited by Sheph: 04 September 2012 - 08:07 PM

Was This Post Helpful? 2
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Tightening up code/tips/constructive criticism/ about my BASIC game

Posted 04 September 2012 - 08:38 PM

Why 2 Scanner objects ?
When you establich a telephone conversation with a correspondant do you hang up the phone an redial after each sentence ?

You main() class is far too long. You should create a stuff object and the rest should happen into the stuff object methods ?

Don't use if &&
isolate your tests in block
if(compChoice == 1) {
   .. all the tests for uChoice here
}
else if(compChoice == 2) {
   ...


Was This Post Helpful? 2
  • +
  • -

#4 Java Hardstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 04-September 12

Re: Tightening up code/tips/constructive criticism/ about my BASIC game

Posted 04 September 2012 - 09:01 PM

Sheph,
I'm using Eclipse and I have a feeling you're right about that.

Yeah I debated leaving the second Scanner in there and didn't know if I needed it or not, thanks for the confirmation.

I'm not sure what "static final" means in the context of an int variable, but how would I implement that so that the comp choice was still random?

Okay, I'll start working on making methods. I'm not really sure how to use them or their syntax yet, but I understand that you make a method and then call it in your main class, and can call that same method in different parts of the code (correct me if im wrong.)
Alright, from now on I'll start my classes with a capital, thanks.

Again, thanks for all the help. I appreciate it!

-Java Hardstyle
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Tightening up code/tips/constructive criticism/ about my BASIC game

Posted 04 September 2012 - 09:04 PM

View PostJava Hardstyle, on 05 September 2012 - 12:01 AM, said:

I'm not sure what "static final" means in the context of an int variable,


It defines it as a constant so you can't change it's value
Was This Post Helpful? 1
  • +
  • -

#6 Java Hardstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 04-September 12

Re: Tightening up code/tips/constructive criticism/ about my BASIC game

Posted 04 September 2012 - 09:11 PM

pbl,

If you have time, in reference to creating objects and object methods, would you mind demonstrating what you mean in the context of my code?
I thought that {whatever is in here} were statements. Would you mind explaining a little more?

Sorry for my lack of understanding it mainly comes from a lack of experience with the language.
Any help is appreciated.

Thanks!

-Java Hardstyle
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Tightening up code/tips/constructive criticism/ about my BASIC game

Posted 05 September 2012 - 03:55 AM

Sorry, don't really understand your question. What do you mean by "what you mean in the context of my code" which remark ?
Was This Post Helpful? 0
  • +
  • -

#8 Java Hardstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 04-September 12

Re: Tightening up code/tips/constructive criticism/ about my BASIC game

Posted 05 September 2012 - 09:44 AM

Pbl,

Would you mind showing me how to use objects and object methods in my code?

Thanks

-Java Hardstyle
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7644
  • View blog
  • Posts: 12,896
  • Joined: 19-March 11

Re: Tightening up code/tips/constructive criticism/ about my BASIC game

Posted 05 September 2012 - 09:52 AM

if(compChoice == 1) {
   .. all the tests for uChoice here
}
else if(compChoice == 2) {
   ...



This seems pretty clear to me, in the context of your code.
A little more explicit:

if(compChoice == 1) {
   if (uChoice.equals("Rock") 
     { 
        // code here
     }
   if (uChoice.equals("Paper") 
     { 
        // code here
     }
    // etc




This is a start towards clarity - just a start, but a good one. Go ahead and make that change and see what it looks like.
Was This Post Helpful? 1
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Tightening up code/tips/constructive criticism/ about my BASIC game

Posted 05 September 2012 - 11:15 AM

You are not OO
You need to create an Object and play with it
main() is just a driver

import java.util.Scanner;
import java.util.Random;
public class Stuff {

	private Random dice;	
    private static String[] str = {"Rock", "Paper", "Scissors"};
    
	Stuff() {
		dice = new Random();	
	}
	
	void play(String uChoice) {
		int userChoice;
		for(userChoice = 0; userChoice < str.length; ++userChoice) {
			if(uChoice.equalsIgnoreCase(str[userChoice]))
				break;
		}
		if(userChoice == str.length) {
			System.out.print("Invalid choice, the valid choice are: ");
			for(String choice : str) 
				System.out.print(" " + choice);
			System.out.println();
			return;
		}
		int compChoice = dice.nextInt(3);
		System.out.print("You selected: " + str[userChoice] + " computer selected: " + str[compChoice]);
		if(compChoice == userChoice) {
			System.out.println(" Even");
			return;
		}
		switch(compChoice) {
		case 0:
			if(userChoice == 1)
				System.out.println("You win");
			else
				System.out.println("Computer win");
			break;
		case 1:
			if(userChoice == 2)
				System.out.println("You win");
			else
				System.out.println("Computer win");
			break;
		case 2:
			if(userChoice == 0)
				System.out.println("You win");
			else
				System.out.println("Computer win");
			break;
		}
	}
	public static void main(String args[]){
		Scanner input = new Scanner(System.in);
		String uchoice;

		Stuff stuff = new Stuff();
		
		String continueRunning= ("Yes");
		do{
			System.out.println("Welcome to Ace's first java game!");
			System.out.println("Type in Rock, Paper, or Scissors to make your selection: ");
			uchoice = input.nextLine();
			stuff.play(uchoice);

			System.out.println("Would you like to play again? Type Yes or No.");
			continueRunning=input.nextLine();

		}
		while(continueRunning.equalsIgnoreCase("Yes"));


	}
}


Was This Post Helpful? 1
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • Joined: 16-October 07

Re: Tightening up code/tips/constructive criticism/ about my BASIC game

Posted 05 September 2012 - 01:16 PM

Well, since pbl choked up complete code...

Honestly, computer picked a number, you pick a number. And why start at 1?

e.g.
import java.util.Scanner;
import java.util.Random;

// don't lower case a class name, tis confusing
class Stuff {
	private static int C_ROCK = 0;
	private static int C_PAPER = 1;
	private static int C_SCISSORS = 2;
	private static final String [] choices = {"Rock", "Paper", "Scissors"};
	private final Random dice;
	private final Scanner input;

	
	public Stuff() {
		this.dice = new Random();
		this.input = new Scanner(System.in);
	}
	
	private int getComputerChoice() { return dice.nextInt(3); }
	
	private int getUserChoice() {
		int choice = -1;
		System.out.println("Type in Rock, Paper, or Scissors to make your selection: ");
		while(choice == -1) {
			String uchoice = input.nextLine();
			for(int i=0; i<choices.length; i++) {
				if (choices[i].equals(uchoice)) {
					choice = i;
					break;
				}
			}
			System.out.println("Try using a captial letter, dork.");
		}
		return choice;
	}
	
	private boolean isUserWin(int compChoice, int userChoice) {
		if(userChoice==C_ROCK) { return compChoice==C_SCISSORS; }
		if(userChoice==C_PAPER) { return compChoice==C_ROCK; }
		return compChoice==C_PAPER;
	}
	
	private void play() {
		int userChoice = getUserChoice();
		int compChoice = getComputerChoice();
		if (compChoice==userChoice) {
			System.out.println("You and the computer both selected " + choices[compChoice] + ".");
			System.out.println("Tie game, sir.");
		} else {
			System.out.println("You selected " + choices[userChoice] + " and the computer selected " + choices[compChoice] + ".");
			if (isUserWin(compChoice, userChoice)) {
				System.out.println("You win!");
			} else {
				System.out.println("You lose. Sucker.");
			}
		}
	}

	public void run() {
		String continueRunning="Yes";
		while(continueRunning.equals("Yes")) {
			play();
			System.out.println("Would you like to play again? Type Yes or No.");
			continueRunning=input.nextLine();
		}
	}
	
	public static void main(String args[]){
		new Stuff().run();
	}
}


Was This Post Helpful? 1
  • +
  • -

#12 Java Hardstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 04-September 12

Re: Tightening up code/tips/constructive criticism/ about my BASIC game

Posted 05 September 2012 - 11:16 PM

Woah Thanks for all the input guys! I'll spend the next day or so trying to decipher exactly what is going on in the suggestions you guys made.




-Java Hardstyle

Baavgai,

Haha thanks for the code! I'll take a look at it and try to figure out what's going on.

Oh, I just started at 1 to help myself understand what was going on, although it did actually make things more complicated.



-Java Hardstyle
Was This Post Helpful? 0
  • +
  • -

#13 Java Hardstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 04-September 12

Re: Tightening up code/tips/constructive criticism/ about my BASIC game

Posted 08 September 2012 - 04:54 PM

View Postpbl, on 05 September 2012 - 11:15 AM, said:

You are not OO
You need to create an Object and play with it
main() is just a driver

import java.util.Scanner;
import java.util.Random;
public class Stuff {

	private Random dice;	
    private static String[] str = {"Rock", "Paper", "Scissors"};
    
	Stuff() {
		dice = new Random();	
	}
	
	void play(String uChoice) {
		int userChoice;
		for(userChoice = 0; userChoice < str.length; ++userChoice) {
			if(uChoice.equalsIgnoreCase(str[userChoice]))
				break;
		}
		if(userChoice == str.length) {
			System.out.print("Invalid choice, the valid choice are: ");
			for(String choice : str) 
				System.out.print(" " + choice);
			System.out.println();
			return;
		}
		int compChoice = dice.nextInt(3);
		System.out.print("You selected: " + str[userChoice] + " computer selected: " + str[compChoice]);
		if(compChoice == userChoice) {
			System.out.println(" Even");
			return;
		}
		switch(compChoice) {
		case 0:
			if(userChoice == 1)
				System.out.println("You win");
			else
				System.out.println("Computer win");
			break;
		case 1:
			if(userChoice == 2)
				System.out.println("You win");
			else
				System.out.println("Computer win");
			break;
		case 2:
			if(userChoice == 0)
				System.out.println("You win");
			else
				System.out.println("Computer win");
			break;
		}
	}
	public static void main(String args[]){
		Scanner input = new Scanner(System.in);
		String uchoice;

		Stuff stuff = new Stuff();
		
		String continueRunning= ("Yes");
		do{
			System.out.println("Welcome to Ace's first java game!");
			System.out.println("Type in Rock, Paper, or Scissors to make your selection: ");
			uchoice = input.nextLine();
			stuff.play(uchoice);

			System.out.println("Would you like to play again? Type Yes or No.");
			continueRunning=input.nextLine();

		}
		while(continueRunning.equalsIgnoreCase("Yes"));


	}
}



So in this version "play" is an object?

Thanks,

-Java Hardstyle
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7644
  • View blog
  • Posts: 12,896
  • Joined: 19-March 11

Re: Tightening up code/tips/constructive criticism/ about my BASIC game

Posted 08 September 2012 - 05:00 PM

View PostJava Hardstyle, on 08 September 2012 - 06:54 PM, said:

So in this version "play" is an object?


play is a method.
Was This Post Helpful? 1
  • +
  • -

#15 Java Hardstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 04-September 12

Re: Tightening up code/tips/constructive criticism/ about my BASIC game

Posted 08 September 2012 - 06:32 PM

Jon,
Then would "Stuff" be the object? I also realize the Class is called stuff. Help me out here haha.


Thanks,

-Java Hardstyle
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2