Results 1 to 7 of 7
  1. #1
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Wink Tips for doing this better?

    As my first java program I decided to write a game where words are generated randomly and you have to type them correctly. Since i pretty much finished the main part of the game, I wanted to know some ways that this could have been done better/faster..Thanks in advance:
    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    // Typing Game by theunlockeRRR, 14. aug. 2016
    public class Game {
    	static String word = "";
    	static int ranswer;
    	static int wanswer;
    	static int lives = 3;
    
    	private static Scanner reader;
    	public static void main(String[] args)
    	{
    		while (lives > 0) {
    	
    		Games();
    		CheckAnswer();
    	}
    	
    	GameOver(); 
    	}
    
    private static void GameOver() {
    	System.out.println("GAME OVER! \n If you want to start again type restart");
    
    }
    private static void CheckAnswer() {
    	@SuppressWarnings("resource")
    	Scanner reader = new Scanner(System.in);
    	String answer = reader.nextLine();
    	if (answer.equals(word)) {
    		ranswer++;
    		System.out.println("CORRECT, you have " + lives + " lives left." + ranswer + " words typed correctly.\n"
    				+ wanswer + " words typed incorrectly");
    	}
    
    	else {
    		wanswer++;
    		lives--;
    		System.out.println("NOT CORRECT, you have " + lives + " lives left." + ranswer + " words typed correctly.\n"
    				+ wanswer + " words typed incorrectly.");
    	}
    	word = "";
    
    }
    public static void Games() {
    	int maxlength = 5;
    	final String chars = "abcdefghijklmnopqrstuvwxyz";
    	word = "";
    	while (word.length() < maxlength) {
    		Random rand = new Random();
    		int x = rand.nextInt(26);
    		word = word + chars.charAt(x);
    	}
    
    	System.out.println("TYPE: " + word);
    
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Tips for doing this better?

    One suggestion is to get rid of all the usages of static except for with the main() method.
    Another is to properly indent the code. Code within each level of {}s should be indented 2-3 spaces.
    Another is to follow java naming conventions: method names should start with lowercase characters.
    Don't give the same name to instance variables and local variables: reader on line 10 and 28
    The variable word used locally in the games() method should be defined local to that method.
    games() should be named getWord() and return the value of word.
    Last edited by Norm; 08-14-2016 at 11:29 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Tips for doing this better?

    Also, don't keep instantiating a new Scanner for user input. Just create one at the beginning of the program and use it
    throughout. It looks like you intended to do that with your static Scanner declaration. But your actual instantiation of
    Scanner in CheckAnswer hides it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Re: Tips for doing this better?

    Quote Originally Posted by Norm View Post
    One suggestion is to get rid of all the usages of static except for with the main() method.
    Another is to properly indent the code. Code within each level of {}s should be indented 2-3 spaces.
    Another is to follow java naming conventions: method names should start with lowercase characters.
    Don't give the same name to instance variables and local variables: reader on line 10 and 28
    The variable word used locally in the games() method should be defined local to that method.
    games() should be named getWord() and return the value of word.
    Statics: to be completely honest, I just make the variables, and if Eclipse gives me an error/warning I just do what I need to fix it.
    Indent: I'm pretty sure I used Eclipse's auto format thing(ctrl+shift+f)
    Naming: I usually try to do it but I always capitalize the first letter of the word, not sure why, will try to improve on this.
    reader: not sure why I did that, i deleted that from line 10.
    word: I had to define it on top because I was trying something out but I removed it afterward.
    games: Yeah, I agree on that, but that Games was like first that came to my mind, did not even know what am I going to even program.

  5. #5
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Re: Tips for doing this better?

    Quote Originally Posted by jim829 View Post
    Also, don't keep instantiating a new Scanner for user input. Just create one at the beginning of the program and use it
    throughout. It looks like you intended to do that with your static Scanner declaration. But your actual instantiation of
    Scanner in CheckAnswer hides it.

    Regards,
    Jim
    Hmm, I thought I needed to do it every time.

    Thanks to both of you!

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Tips for doing this better?

    Quote Originally Posted by theunlockeRRR View Post
    games: Yeah, I agree on that, but that Games was like first that came to my mind, did not even know what am I going to even program.
    This page on another forum may help: https://coderanch.com/wiki/659832/Wiki/Stop-Coding
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Re: Tips for doing this better?

    Quote Originally Posted by DarrylBurke View Post
    This page on another forum may help: https://coderanch.com/wiki/659832/Wiki/Stop-Coding
    I read that and I did not start to code like that, I've watched dozens of tutorials and finished some fast courses when I did not even have eclipse installed :). And for the problem solving... when I encounter a problem with my code I usually just read every line of code again, think of what could be wrong and try to solve it, if I can't solve it I either take a brake or look online.

Similar Threads

  1. Any tips on building online 2d game? (Just tips)
    By Lionlev in forum Networking
    Replies: 0
    Last Post: 10-16-2012, 12:56 AM
  2. Get me some TIPS to start with please !
    By lght306 in forum New To Java
    Replies: 6
    Last Post: 12-26-2011, 12:08 PM
  3. i need some tips on this question
    By reeveliew in forum New To Java
    Replies: 13
    Last Post: 06-15-2010, 05:29 PM
  4. Need some tips.
    By PVL268 in forum New To Java
    Replies: 4
    Last Post: 02-27-2009, 02:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •