Page 5 of 6
Re: New dev team/java learning thread.
Posted: Sun Jan 26, 2014 1:58 pm
by Iv121
No I was complaining about how the code tags work here purely, the alignment is just a suggestion to make comments more readable.
Re: New dev team/java learning thread.
Posted: Tue Jan 28, 2014 7:59 pm
by CMA
Alright, sorry for being away, but it looks like I won't be able to get on for this week much. Also, everyone play vanilla minecraft if you can, I realized that we're trying to make a mod for a game most(?) of us rarely play.
Re: New dev team/java learning thread.
Posted: Tue Jan 28, 2014 8:52 pm
by Keon
I do a lot of minecraft invention stuff with command blocks, and I suppose I'm one of the few that plays. I haven't played survival in so long. I can't do it anymore. FTB and creative ruined me.
I'd also look at FTB and such, see how they do it and what they do right and wrong.
Re: New dev team/java learning thread.
Posted: Tue Jan 28, 2014 11:57 pm
by CMA
I don't even know what a command block does, is it like scripting, sort of? I'll look at FTB, if I have time.
Re: New dev team/java learning thread.
Posted: Wed Jan 29, 2014 1:33 am
by Iv121
Almost, a command block is like a redstone-triggered admin that runs almost any command apart from stuff like kicks or bans or whitelists and so on. It is mostly used by map makers to create complex scenarios.
Re: New dev team/java learning thread.
Posted: Wed Jan 29, 2014 9:14 am
by Keon
Re: New dev team/java learning thread.
Posted: Wed Jan 29, 2014 11:04 am
by craftqq
Iv121 wrote:The program contains errors of different sorts and does not meet the requirements of the program:
1. The random number contains 0's and repeating numbers (yea I forgot to add the 0's to the list but the repeating numbers was clearly specified)
2. The program responds incorrectly to my guesses, I didn’t have a single hit in my guess yet I was shown a hit.
3. The player's invalid input is not handled: You are able to input letters and repeating numbers.
4. The player is unable to press 'escape' and leave the game.
5. Try to use less global variables (Like 'hits' for example).
6. Improper usage of strings, a string is an array in it's own right, no need to create a separate string for each number , instead add the new number directly into the string.
Should be fixed now (apart from the "hit escape to exit the program" - I'll do that once I'm able to use swing since java doesn't really support key presses (current workaround is to write "exit" to exit))
Program
Source: (github doesn't display it properly)
edit: Eclipse has a different layout. great. I'll try to fix it
edit2:
source open it in eclipse; it should work for now. I'll try fixing the spoiler
Re: New dev team/java learning thread.
Posted: Wed Jan 29, 2014 2:34 pm
by fr0stbyte124
For future reference, if you include the
Code: Select all
tag, it will respect your whitespace. (*edit* nvm) Can we install a code coloring plugin for the forum? It's kind of tough to read the way it is.
On topic: I've never been a fan of the command block system. If you are going to go to all the work to support a scripting language in Minecraft, why would you go and stuff it in the redstone pipeline? If you need stuff that updates on a per-tick basis you are spamming the block rather than setting up events. We won't have lambda expressions until JDK 8, but even without that you should be able to register some standard triggers to subscribe scripts to. This will probably need to be addressed in the mod since we'll be dealing with partially loaded chunks. Anything that doesn't need to physically interract with the chunk should be moved to an abstract layer to avoid unpredictable conditions.
Re: New dev team/java learning thread.
Posted: Wed Jan 29, 2014 3:18 pm
by Iv121
Alright the program technically works but it is inefficient in some points.
1. generateNumber uses 3 loops and an unneeded buffer array merely to assign a number with different letters, redo it using only 2 loops and no additional arrays.
2. In compareNumber and getInput again you use unneeded arrays and strings, and the way you use them might hint you do not wholly understand how they work.
Other than that the program is fine.
I suggest reading the Java book (or any other place you might find better for this) about arrays and also strings specifically. Also as a practice you can add a scoreboard future to the game which stores the high scores even if the program was closed across many games.
Re: New dev team/java learning thread.
Posted: Wed Jan 29, 2014 5:37 pm
by fr0stbyte124
I looked over the code, and I have some recommendations for making it a bit cleaner. The main thing is that you do a lot of nested looping and have some really strange string manipulation going on, which makes the program difficult to follow.
Firstly, I'm going to assume that your desired digit domain is supposed to be [1,9] inclusive and not [1,8] like your random number generator currently produces ( [0,1) * 8 < 8).
I would write generateNumber() to use a permutation pool, like so:
Code: Select all
static String generateNumber() // generates a new number
{
ArrayList<Integer> digitPool = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9)); //There are many ways to do this. This was simply the most compact.
String strSecret = "";
//int[] secret = new int[4]; //alternate representation of strSecret;
Random rnd = new Random();
for(int i=0; i<4; i++)
{
int slot = rnd.nextInt(digitPool.size()); //pick some number in the range of digitPool indexes
//secret[i] = digitPool.get(slot);
strSecret += digitPool.get(slot).toString();
digitPool.remove(slot);
}
return strSecret;
}
I used java.util.Random.nextInt() for the prng instead of Math.random(), because it is cleaner and takes care of any modulo bias (unless your modulo is a power of two, some remainders will appear slightly more frequently than others unless you take steps to avoid it. nextInt() takes care of that for you).
*Edit* the modulo bias only comes into play for integer based random number generators, which is generally what you will see in other languages. Java's just got to be different.
You made compareNumber() way,
way more complicated than it needed to be.
I'm not sure why you are chose to use a String to represent the secret number instead of an int[4], but as long as you are, you might as well take advantage of the built-in String functions.
Code: Select all
static String compareNumber(String number1, String number2) //number1 = secret key, number2 = user guess
{
int countBullseye = 0;
int countHit = 0;
for(int i=0; i<4; i++)
{
char guess = number2.charAt(i); //charAt(i) acts like String[i] would if Java was less dumb. Use this instead of String.substring(i, (i + 1))
if((number1.charAt(i) == guess)
countBullseye++;
else
if(number1.indexOf(guess) != -1) //indexOf finds the first instance of a character in a String, or -1 if it is not present
countHit++;
}
String result = "";
if(countBullseye > 0)
result += new String(new char[countBullseye]).replace('\0', 'X'); //creates a string of n repeated characters. I didn't want to import StringUtils just for this. Java is a stupid language.
if(countHit > 0)
result += new String(new char[countHit]).replace('\0', 'O');
return result;
}
Speaking of which, you can test for repeat characters in the input like this:
Code: Select all
boolean inputOK = true;
for(int i = 0; i<3; i++)
{
char current = input.charAt(i);
if(input.indexOf(current, i+1) != -1) //same as indexOf(char), only it begins looking from a specific starting point
{
inputOK = false;
break;
}
}
Other things: I would recommend using breaks to exit the loop rather than while(!terminationFlag). It will make the program a lot easier to follow, even if you still use the flags for other things.
For the rounds loop, you already have a round counter, so just use a for() loop, and break out of it early if you win. Just remember that if you are counting from 1 to 20, the rounds counter will be 21 when the loop exits.
Re: New dev team/java learning thread.
Posted: Wed Jan 29, 2014 5:45 pm
by ҉
CMA wrote:Also, everyone play vanilla minecraft if you can, I realized that we're trying to make a mod for a game most(?) of us rarely play.
How relevant is that? Understanding the code is important, but game design for FC is going to be a very different thing from game design for Minecraft.
Also, vMC sucks. A lot. So many things have been botched. The new launcher lets you play whatever version you want; I strongly suggest that you drop back to early Beta if you want to see what MC should have been like. 1.3_01 was a good version.
Re: New dev team/java learning thread.
Posted: Wed Jan 29, 2014 5:50 pm
by Tau
I honestly have no desire to play a game where a desert and a tundra can spawn right fucking next to each other.
Re: New dev team/java learning thread.
Posted: Wed Jan 29, 2014 5:54 pm
by fr0stbyte124
Yeah, realistically everything should have the exact same climate, considering that the world is basically flat and evenly exposed to sunlight.
Re: New dev team/java learning thread.
Posted: Wed Jan 29, 2014 7:17 pm
by CMA
Mistake Not... wrote:if you want to see what MC should have been like. 1.3_01 was a good version.
Do that, then. I didn't know the new launcher did that. The climate thing bothers me as well, but I still think it should retain some of the feel of vMC.
Re: New dev team/java learning thread.
Posted: Sun Feb 02, 2014 4:02 pm
by Solar112
I am actually resiging from the dev team now.