Java peg game
I took your program and refactored it so the main method looks like this:. Other stuff to fix: dice size, max score, etc. Repeated code scan input, check end-of-game conditions should be private methods.
Score, player name, etc. And generally, repeating code is the first indicator that you can improve things. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Pig, the Dice Game Ask Question. Asked 3 years, 9 months ago. Active 3 years, 9 months ago.
Viewed 6k times. I've finished the code, tried to make it as robust as possible, and ended up with this: import static java. Improve this question. Add a comment. Active Oldest Votes. You're duplicating a ton of code because each player acts in exactly the same way.
If I were to rewrite your code considering the above, my first pass would look like this: import java. PrintStream; import java. Random; import java. Improve this answer. Eric Stein Eric Stein 5, 11 11 silver badges 16 16 bronze badges.
My approach when I write this program is a little bit different, as I said, so I just ended up with a functional code, not a pretty one. I'm new to Java, and never worked with multiple classes before. Or changing from public to private because I have absolutely no idea what that does the book I'm learning Java from doesn't talk much about it. But even though I didn't learn or know about it, I think I understand most of your fixed code, and I will keep your advices in mind when coding in the future.
Thanks again! I knew the Roll method was redundant. Still, I had other plans for my code when I first started writing it. The object of the puzzle is to make a sequence of moves, each move causing one peg to be removed, such that only one peg remains. A Peg is removed only by "jumping" it with another peg. Hence, a legal move can only be made if there is a peg flanked by both another peg and a hole, arranged in a line.
For example, configuration b shown below is obtained from configuration a by one jump, and then c is obtained from b by making a second jump. I just want to give a real basic overview of the code. The source code in the project is very well commented, and should be easy to follow by adding some breakpoints on the mouse events. A BitArray manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on 1 and false indicates the bit is off 0.
Below is the function InitializeBoard that sets up a new game board:. This puzzle is getting solved by three steps. You start with the empty spot on the board jump and end as you can see in this code below. First, you can choose the location of the hole in the beginning. In one file, you define the grammar while in the other one, you define the actions corresponding to the various elements. The idea is that it should allow you to dynamically redefine grammars.
While it is smartly engineered, it is debatable if it is also smartly designed. You can see that the example JSON grammar it is more lengthy than one expects it to be.
And here is an excerpt from the example parser definition file that defines the actions for the rules for JSON. There is one special case that requires some more commentary: The case in which you want to parse Java code in Java. In this case, we have to suggest using a library named JavaParser. Incidentally, we heavily contribute to JavaParser, but that is not the only reason we suggest it. The fact is that JavaParser is a project with dozens of contributors and thousands of users, so it is pretty robust.
Parsing in Java is a broad topic, and the world of parsers is a bit different from the usual world of programmers. You will find the best tools coming directly from academia, which is typically not the case with software.
Some tools and libraries have been started for a thesis or a research project. The upside is that tools tend to be easily and freely available. The downside is that some authors prefer to have a good explanation of the theory behind what their tools do, rather than good documentation on how to use them.
Also, some tools end up being abandoned as the original authors finish their master's or their Ph. We do not use parser combinators very often, though. It is not because they are bad — they have their uses and, in fact, we wrote an article about one in C.
But for the problems we deal with, they typically lead to less maintainable code. However, they could be easier to start with, so you may want to consider those, especially if, until now, you have hacked something terrible using regular expressions and a half-baked parser written by hand. We cannot really definitely say what software you should use. What it is best for one user might not be the best for somebody else. And we all know that the most technically correct solution might not be ideal in real life with all its constraints.
Still, we have searched and tried many similar tools in our work, and something like these articles would have helped us save some time. With that in mind, we wanted to share what we have learned about the best options for parsing in Java. See the original article here. Thanks for visiting DZone today,. Edit Profile. Sign Out View Profile.
Over 2 million developers have joined DZone. As we conclude the Parsing in Java series, we examine the variety PEG parsers and parser combinators out there to determine when they should be used. Like
0コメント