New dev team/java learning thread.
Forum rules
- All off-topic posts will be removed without notice.
- Poorly written topics/posts will be edited without warning
- Unnecessary topics will be deleted or locked without warning.
- Posts may be moved without warning.
- All off-topic posts will be removed without notice.
- Poorly written topics/posts will be edited without warning
- Unnecessary topics will be deleted or locked without warning.
- Posts may be moved without warning.
No I was complaining about how the code tags work here purely, the alignment is just a suggestion to make comments more readable.
They're watching ...
"I am forbidden tag" -CvN
"I am forbidden tag" -CvN
Re: New dev team/java learning thread.
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.
"Being a christian democrat is like being a christian satanist" - Adam Berces
Re: New dev team/java learning thread.
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.
I'd also look at FTB and such, see how they do it and what they do right and wrong.
- I can be reached as ducky215 on minecraft forums -
Re: New dev team/java learning thread.
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.
"Being a christian democrat is like being a christian satanist" - Adam Berces
Re: New dev team/java learning thread.
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.
They're watching ...
"I am forbidden tag" -CvN
"I am forbidden tag" -CvN
Re: New dev team/java learning thread.
They let you make stuff like this for adventure maps:
Yeah, it's a sphere. Whatcha gonna do about it?
- I can be reached as ducky215 on minecraft forums -
Re: New dev team/java learning thread.
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))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.
Program
Source: (github doesn't display it properly)
Spoiler:
edit2:source open it in eclipse; it should work for now. I'll try fixing the spoiler
Code: Select all
╔═══ ║ ║ ══╦══ ║ ║ ╔══╗ ╔═══ ╔═══╗ ╔══╗ ╔═══╗ ╔═══ ══╦══
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
╠══ ║ ║ ║ ║ ║ ╠══╩╗ ╠══ ║ ╠══╩╗ ╠═══╣ ╠══ ║
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
║ ╚═══╝ ║ ╚═══╝ ║ ║ ╚═══ ╚═══╝ ║ ║ ║ ║ ║ ║
- fr0stbyte124
- Developer
- Posts:727
- Joined:Fri Dec 07, 2012 3:39 am
- Affiliation:Aye-Aye
Re: New dev team/java learning thread.
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.
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.
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.
They're watching ...
"I am forbidden tag" -CvN
"I am forbidden tag" -CvN
- fr0stbyte124
- Developer
- Posts:727
- Joined:Fri Dec 07, 2012 3:39 am
- Affiliation:Aye-Aye
Re: New dev team/java learning thread.
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:
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.
Speaking of which, you can test for repeat characters in the input like this:
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.
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;
}
*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;
}
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;
}
}
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.
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.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.
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.
;.'.;'::.;:".":;",,;':",;
(Kzinti script, as best as can be displayed in Human characters, translated roughly as "For the Patriarchy!")
(Kzinti script, as best as can be displayed in Human characters, translated roughly as "For the Patriarchy!")
- Tau
- Admin
- Posts:750
- Joined:Mon Dec 10, 2012 9:58 am
- Affiliation:Futureville Mafia
- IGN:TehPwnzor7306
- Location:Ancapistan
Re: New dev team/java learning thread.
I honestly have no desire to play a game where a desert and a tundra can spawn right fucking next to each other.
Vinyl wrote:"RP" and gaming and homosexuality is what's keeping [the forum] afloat.
- fr0stbyte124
- Developer
- Posts:727
- Joined:Fri Dec 07, 2012 3:39 am
- Affiliation:Aye-Aye
Re: New dev team/java learning thread.
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.
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.Mistake Not... wrote:if you want to see what MC should have been like. 1.3_01 was a good version.
"Being a christian democrat is like being a christian satanist" - Adam Berces
Re: New dev team/java learning thread.
I am actually resiging from the dev team now.