I was recently attempting to code a game in java called nim. I was having troubl
ID: 3838495 • Letter: I
Question
I was recently attempting to code a game in java called nim. I was having trouble and was wondering if someone could show me the way they would do it. They rubric for the project can be found using this link.
8 (8%)
Methods written but they are not used or called
6 (6%)
there are errors sending or returning data
4 (4%)
used to segment code to perform a function
2 (2%)
used to segment code to perform functions
0 (0%)
Only one main method
8 (8%)
Used 3+ classes and/or inheritance
6 (6%)
Used classes and/or inheritance once
4 (4%)
More than one class present but there are errors
2 (2%)
More than one class is present but not implemented
0 (0%)
One class used
8 (8%)
Arrays used and perform as designed
6 (6%)
Arrays used but a minor error
4 (4%)
Arrays used but major error
2 (2%)
Array exists but is not used
0 (0%)
No arrays in code
8 (8%)
Verbose comments
6 (6%)
Some descriptions
4 (4%)
Sparse comments
2 (2%)
Nonsense comments
0 (0%)
No comment or documentation
8 (8%)
Appropriate spacing/indent
6 (6%)
Some minor spacing issues
4 (4%)
Neglects spacing or indentation
2 (2%)
Difficult to follow code
0 (0%)
Impossible to follow code lines
8 (8%)
Loops, recursion and methods perform
6 (6%)
Loops, recursion and methods perform but no returns
4 (4%)
Fragmented code that performs no functionality
2 (2%)
Everything done in multiple lines of If-Else
0 (0%)
Repeated functions that could be in a method
8 (8%)
User feels comfortable using
6 (6%)
Minor guess work on what to do
4 (4%)
Troubling guess work on what to do
2 (2%)
Must look in code on how to interface
0 (0%)
Frustrating to use application
8 (8%)
Compiles without error
0 (0%)
0 (0%)
0 (0%)
0 (0%)
Does not compile or compile error
8 (8%)
Robust and tested well
0 (0%)
0 (0%)
0 (0%)
0 (0%)
Frail and crashes easily
8 (8%)
Yes
0 (0%)
0 (0%)
0 (0%)
0 (0%)
No
10 (10%)
Peer review Excellent
7 (7%)
Peer review Great
4 (4%)
Peer review Good
1 (1%)
Peer review Poor
0 (0%)
Peer review None
10 (10%)
Reviewed 3
6 (6%)
Reviewed 2
3 (3%)
Reviewed 1
0 (0%)
Reviewed 0
0 (0%)
The game would need to have 5 stacks of blocks, the first stack would have 1 block the second with 3 the third with 5 the 4th with 7 and the last with 9. Then when the game starts it would ask the user which stack does the user want to choose from and how many blocks does the user want to remove. The goal being the one to remove the last block from the last stack.
The methods and classes can be named something that relates to NIM like class NIM. Otherwise there are no requirements for the code as long as the rubric is followed
Thanks!
Methods Points:8 (8%)
Methods written but they are not used or called
Points:6 (6%)
there are errors sending or returning data
Points:4 (4%)
used to segment code to perform a function
Points:2 (2%)
used to segment code to perform functions
Points:0 (0%)
Only one main method
Classes Points:8 (8%)
Used 3+ classes and/or inheritance
Points:6 (6%)
Used classes and/or inheritance once
Points:4 (4%)
More than one class present but there are errors
Points:2 (2%)
More than one class is present but not implemented
Points:0 (0%)
One class used
Array Points:8 (8%)
Arrays used and perform as designed
Points:6 (6%)
Arrays used but a minor error
Points:4 (4%)
Arrays used but major error
Points:2 (2%)
Array exists but is not used
Points:0 (0%)
No arrays in code
Documented Points:8 (8%)
Verbose comments
Points:6 (6%)
Some descriptions
Points:4 (4%)
Sparse comments
Points:2 (2%)
Nonsense comments
Points:0 (0%)
No comment or documentation
Formatted Points:8 (8%)
Appropriate spacing/indent
Points:6 (6%)
Some minor spacing issues
Points:4 (4%)
Neglects spacing or indentation
Points:2 (2%)
Difficult to follow code
Points:0 (0%)
Impossible to follow code lines
Efficiency Points:8 (8%)
Loops, recursion and methods perform
Points:6 (6%)
Loops, recursion and methods perform but no returns
Points:4 (4%)
Fragmented code that performs no functionality
Points:2 (2%)
Everything done in multiple lines of If-Else
Points:0 (0%)
Repeated functions that could be in a method
User Friendliness Points:8 (8%)
User feels comfortable using
Points:6 (6%)
Minor guess work on what to do
Points:4 (4%)
Troubling guess work on what to do
Points:2 (2%)
Must look in code on how to interface
Points:0 (0%)
Frustrating to use application
Compile Error Points:8 (8%)
Compiles without error
Points:0 (0%)
Points:0 (0%)
Points:0 (0%)
Points:0 (0%)
Does not compile or compile error
Runtime Error Points:8 (8%)
Robust and tested well
Points:0 (0%)
Points:0 (0%)
Points:0 (0%)
Points:0 (0%)
Frail and crashes easily
Appropriate Content Points:8 (8%)
Yes
Points:0 (0%)
Points:0 (0%)
Points:0 (0%)
Points:0 (0%)
No
Peer Reviews Points:10 (10%)
Peer review Excellent
Points:7 (7%)
Peer review Great
Points:4 (4%)
Peer review Good
Points:1 (1%)
Peer review Poor
Points:0 (0%)
Peer review None
Peer Reviewer Points:10 (10%)
Reviewed 3
Points:6 (6%)
Reviewed 2
Points:3 (3%)
Reviewed 1
Points:0 (0%)
Reviewed 0
Points:0 (0%)
Explanation / Answer
import orbital.algorithm.template.*; import orbital.logic.functor.Function; import orbital.logic.functor.MutableFunction; import orbital.math.*; import java.util.*; import java.util.zip.*; import java.io.*; import java.text.*; import java.awt.Color; import util.Basic; /** * Solves Rubik's Cube with state search. * Since Rubik's Cube has a rather large state space, solving an * arbitrary cube might still take a while. ** For a 2 by 2 cube the (reachable) state space is of size * ≤ 38/sup>·8!. * An upper bound for the size of the state space reachable by performing at most d actions is: * (2*3)d. * So for a depth of 14 steps its size limit is 78364164096. * The exact size of the state space is somewhat difficult to calculate due to the * various interpretations of states and their symmetries. * I still do not know the number of moves neccessary to reach every possible state. *
** For a 3 by 3 cube the (reachable) state space is of size * 212·12!·38·8!/12 ≈ 4.3*1019. * An upper bound for the size of the state space reachable by performing at most d actions is: * 1 + (6*2) + (6*2)*111 + (6*2)*112 + ... + (6*2)*11n-1 = 1 + (6*2) * 11n-1 / (11 - 1) * Because *
- *
- there is the single initial state *
- with the first action you can chose either of the * six sides and turn them into one of two directions *
- with the following action you cannot reach another 6*2 new states * since there is one action that takes us back to the previous state. * So new states are at most 6*2-1 = 11. In fact, for the third step this number drops again. * But this calculation still leads to an upper bound. *
* By comparing the size of the state space and the size of the state space reachable by performing * at most d actions, one can deduce that no algorithm can exist * that is able to solve every initial cube with 20 moves or less. * Note that this minimum depth required still depends upon the exact * moves allowed.
* * @version $Id: RubiksCube.java 1728 2006-08-30 18:06:46Z andre $ * @author Ute Platzer * @author André Platzer * @see Visualization of Rubik's Cube */ public class RubiksCube implements GeneralSearchProblem { /** * Up to which depth to search for a solution. */ public static final int MAX_STEPS = 16; // enum for SEQUENCE /** * The SEQUENCE mode with a random sequence of at most depth MAX_STEPS. */ public static final int RANDOM = 0; /** * The SEQUENCE mode with a complex sequence of swapping two edges without distrubing the rest. * Has a known minimum step depth of 12. * The complex sequence cannot be solved, with MAX_STEPS < 12. */ public static final int COMPLEX = 1; /** * The SEQUENCE mode with a standard sequence * The complex sequence cannot be solved, if MAX_STEPS is too small. */ public static final int STANDARD = 2; /** * Choose which SEQUENCE to solve. */ public static int SEQUENCE = RANDOM; /** * Whether to restrict actions "canonically", and allow only * turning front/right/down. These "canonical" actions * can(@todo?) sometimes emulate the others, but will generally * require more moves. Except for rotational symmetries of the * whole cube, which would allow the restriction without changing * the search depth. */ private static final boolean RESTRICT_TO_CANONICAL_ACTIONS = true; /** * The size of the Rubik's cube to solve. */ public static final int SIZE = 2; public static void main(String arg[]) throws Exception { DateFormat df = new SimpleDateFormat("H:mm:ss:S"); df.setTimeZone(TimeZone.getTimeZone("Greenwich/Meantime")); Date loadeta; Function h; try { File databaseFile = new File(RubiksCubeCreatePattern.patternDatabaseFile); if (!databaseFile.exists()) { System.err.println("File "" + databaseFile + "" does not exist. Will create one."); RubiksCubeCreatePattern.main(arg); } // load patterns System.out.println("Loading"); long loading = System.currentTimeMillis(); InputStream fis = new FileInputStream(databaseFile); if (RubiksCubeCreatePattern.compressed) fis = new InflaterInputStream(fis); ObjectInputStream is = new ObjectInputStream(fis); final int patternDepth = is.readInt(); final Map patternDatabase = (Map) is.readObject(); is.close(); h = new Function() { final Real UNDERESTIMATE = Values.getDefaultInstance().valueOf(patternDepth + 1); public Object apply(Object o) { // out of exact pattern heuristic, so we need more than patternDepth steps // XXX: need better heuristics!! Count number of tiles in wrong position, etc. //@fixme also we only measure the distance to one solved state, not to all solved states that result from rotating the whole cube, so our database heuristic as well as our UNDERESTIMATE heuristic may be wrong if reaching the other goal states is ok. return UNDERESTIMATE; } }; h = new HeuristicAlgorithm.PatternDatabaseHeuristic(h, patternDatabase); loadeta = new Date(System.currentTimeMillis() - loading); System.out.println("Completed loading " + df.format(loadeta)); if (patternDepth != RubiksCubeCreatePattern.MAX_STEPS) { System.out.println("Warning: File "" + databaseFile + "" does not seem up to date. Consider calling"); System.out.println(" java RubiksCubeCreatePattern"); System.out.println("to re-create "" + databaseFile + ""."); } } catch (IOException x) { System.err.println(x); System.err.println("Make sure that the pattern database file has been created by calling"); System.err.println(" java RubiksCubeCreatePattern"); return; } System.out.println("Start"); long start = System.currentTimeMillis(); GeneralSearch s; // here we decide which exact search algorithm to use // the single difference in using another search algorithm // would only concern the constructor call //s = new IterativeDeepeningAStar(h); //s = new BranchAndBound(h, MAX_STEPS + 1); s = new IterativeExpansion(h); // really solve our problem Cube solution = (Cube) s.solve(new RubiksCube(SIZE)); Date eta = new Date(System.currentTimeMillis() - start); console.color(Color.white); if (solution != null) { System.out.println("Found: " + solution + " "); printcubus(solution.feld, 2, 30); console.color(Color.white); console.LOCATE(4, 9); console.print("total accumulated cost of " + NumberFormat.getInstance().format(solution.accumulatedCost) + " steps", false); } else { System.out.println("NO solution"); console.LOCATE(30, 2); console.print("NO solution"); } console.LOCATE(4, 10); console.print("Duration " + df.format(eta), false); console.LOCATE(4, 11); console.print("Load Time " + df.format(loadeta), false); } // sides of the cube public static final int left = 0; public static final int back = 1; public static final int top = 2; public static final int front = 3; public static final int right = 4; public static final int down = 5; private static final String names[] = { "L", "B", "T", "F", "R", "D" }; // different colors for tiles public static final int orange = 0; public static final int blue = 1; public static final int yellow = 2; public static final int white = 3; public static final int red = 4; public static final int green = 5; private static final Color colors[] = { Color.orange.darker(), Color.blue, Color.yellow.brighter(), Color.white, Color.red, Color.green }; /** * The size of the Rubik's cube. */ protected final int size; /** * The actual state of the cube to solve. */ private final Cube _initialState; public RubiksCube(int size) { if (size != 2) throw new InternalError("only implemented for size 2"); this.size = size; this._initialState = constructInitialState(); } /** * Pose the problem by constructing the initial Rubik's cube state. */ private Cube constructInitialState() { // 'mache einen heilen Wuerfel: Cube c = new Cube(size, 0.0); switch (SEQUENCE) { case COMPLEX: // '2 ecken gedreht c.feld[16] = blue; c.feld[6] = yellow; c.feld[9] = red; c.feld[10] = red; c.feld[19] = white; c.feld[13] = yellow; break; case STANDARD: // 'test: einige drehungen c.drehe(2, -1); c.drehe(2, -1); c.drehe(1, 1); c.drehe(2, 1); //c.drehe(1, 1); //c.drehe(2, -1); c.drehe(2, -1); c.drehe(3, -1); c.drehe(2, -1); c.drehe(3, 1); c.drehe(2, -1); break; case RANDOM: { Random random = new Random(); final int MIN_ACTION = RESTRICT_TO_CANONICAL_ACTIONS ? front : left; for (int i = 0; i = 0) actionLog.append('+'); actionLog.append(direction); } /** * Clears the log of action sequence. */ public void clearActionLog() { actionLog = new StringBuffer(); } } private static Basic console = null; /** * Display the cube. */ protected static void printcubus(int feld[], int y, int x) { // lazy instantiation of the frame for displaying if (console == null) Basic.show(console = new Basic(40, 10), true, true); String f = "#"; if (x == 0 || y == 0) { y = console.CSRLIN(); x = console.POS(0); if (y > 19) { console.inkey(); console.cls(); x = y = 1; } } console.LOCATE(x + 2 * f.length(), y); console.color(colors[feld[4]]); console.print(f, false); console.color(colors[feld[5]]); console.print(f, false); console.LOCATE(x + 2, y + 1); console.color(colors[feld[7]]); console.print(f, false); console.color(colors[feld[6]]); console.print(f, false); console.LOCATE(x, y + 2); for (int i = 0; i < 17; i += 8) { console.color(colors[feld[i]]); console.print(f, false); console.color(colors[feld[i + 1]]); console.print(f, false); } console.color(colors[feld[20]]); console.print(f, false); console.color(colors[feld[21]]); console.print(f, false); console.LOCATE(x, y + 3); for (int i = 3; i < 20; i += 8) { console.color(colors[feld[i]]); console.print(f, false); console.color(colors[feld[i - 1]]); console.print(f, false); } console.color(colors[feld[23]]); console.print(f, false); console.color(colors[feld[22]]); console.print(f, false); console.LOCATE(x + 2 * f.length(), y + 4); console.color(colors[feld[12]]); console.print(f, false); console.color(colors[feld[13]]); console.print(f, false); console.LOCATE(x + 2, y + 5); console.color(colors[feld[15]]); console.print(f, false); console.color(colors[feld[14]]); console.print(f); } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.