forked from KhushalGupta305/Game-2048
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleGame.java
More file actions
195 lines (176 loc) · 6.71 KB
/
ConsoleGame.java
File metadata and controls
195 lines (176 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.datumbox.opensource;
import com.datumbox.opensource.ai.AIsolver;
import com.datumbox.opensource.dataobjects.ActionStatus;
import com.datumbox.opensource.game.Board;
import com.datumbox.opensource.dataobjects.Direction;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
/**
* The main class of the Game.
*/
public class ConsoleGame {
/**
* Main function of the game.
*
* @param args
* @throws CloneNotSupportedException
*/
public static void main(String[] args) throws CloneNotSupportedException {
System.out.println("The 2048 Game in JAVA!");
System.out.println("======================");
System.out.println();
while(true) {
printMenu();
int choice;
try {
Scanner sc = new Scanner (System.in);
choice = sc.nextInt();
switch (choice) {
case 1: playGame();
break;
case 2: calculateAccuracy();
break;
case 3: help();
break;
case 4: return;
default: throw new Exception();
}
}
catch(Exception e) {
System.out.println("Wrong choice");
}
}
}
/**
* Prints Help menu
*/
public static void help() {
System.out.println("Seriously?!?!?");
}
/**
* Prints main menu
*/
public static void printMenu() {
System.out.println();
System.out.println("Choices:");
System.out.println("1. Play the 2048 Game");
System.out.println("2. Estimate the Accuracy of AI Solver");
System.out.println("3. Help");
System.out.println("4. Quit");
System.out.println();
System.out.println("Enter a number from 1-4:");
}
/**
* Estimates the accuracy of the AI solver by running multiple games.
*
* @throws CloneNotSupportedException
*/
public static void calculateAccuracy() throws CloneNotSupportedException {
int wins=0;
int total=10;
System.out.println("Running "+total+" games to estimate the accuracy:");
for(int i=0;i<total;++i) {
int hintDepth = 7;
Board theGame = new Board();
Direction hint = AIsolver.findBestMove(theGame, hintDepth);
ActionStatus result=ActionStatus.CONTINUE;
while(result==ActionStatus.CONTINUE || result==ActionStatus.INVALID_MOVE) {
result=theGame.action(hint);
if(result==ActionStatus.CONTINUE || result==ActionStatus.INVALID_MOVE ) {
hint = AIsolver.findBestMove(theGame, hintDepth);
}
}
if(result == ActionStatus.WIN) {
++wins;
System.out.println("Game "+(i+1)+" - won");
}
else {
System.out.println("Game "+(i+1)+" - lost");
}
}
System.out.println(wins+" wins out of "+total+" games.");
}
/**
* Method which allows playing the game.
*
* @throws CloneNotSupportedException
*/
public static void playGame() throws CloneNotSupportedException {
System.out.println("Play the 2048 Game!");
System.out.println("Use 8 for UP, 6 for RIGHT, 2 for DOWN and 4 for LEFT. Type a to play automatically and q to exit. Press enter to submit your choice.");
int hintDepth = 7;
Board theGame = new Board();
Direction hint = AIsolver.findBestMove(theGame, hintDepth);
printBoard(theGame.getBoardArray(), theGame.getScore(), hint);
try {
InputStreamReader unbuffered = new InputStreamReader(System.in, "UTF8");
char inputChar;
ActionStatus result=ActionStatus.CONTINUE;
while(result==ActionStatus.CONTINUE || result==ActionStatus.INVALID_MOVE) {
inputChar = (char)unbuffered.read();
//inputChar = 'a';
if(inputChar=='\n' || inputChar=='\r') {
continue;
}
else if(inputChar=='8') {
result=theGame.action(Direction.UP);
}
else if(inputChar=='6') {
result=theGame.action(Direction.RIGHT);
}
else if(inputChar=='2') {
result=theGame.action(Direction.DOWN);
}
else if(inputChar=='4') {
result=theGame.action(Direction.LEFT);
}
else if(inputChar=='a') {
result=theGame.action(hint);
}
else if(inputChar=='q') {
System.out.println("Game ended, user quit.");
break;
}
else {
System.out.println("Invalid key! Use 8 for UP, 6 for RIGHT, 2 for DOWN and 4 for LEFT. Type a to play automatically and q to exit. Press enter to submit your choice.");
continue;
}
if(result==ActionStatus.CONTINUE || result==ActionStatus.INVALID_MOVE ) {
hint = AIsolver.findBestMove(theGame, hintDepth);
}
else {
hint = null;
}
printBoard(theGame.getBoardArray(), theGame.getScore(), hint);
if(result!=ActionStatus.CONTINUE) {
System.out.println(result.getDescription());
}
}
}
catch (IOException e) {
System.err.println(e);
}
}
/**
* Prints the Board
*
* @param boardArray
* @param score
* @param hint
*/
public static void printBoard(int[][] boardArray, int score, Direction hint) {
System.out.println("-------------------------");
System.out.println("Score:\t" + String.valueOf(score));
System.out.println();
System.out.println("Hint:\t" + hint);
System.out.println();
for(int i=0;i<boardArray.length;++i) {
for(int j=0;j<boardArray[i].length;++j) {
System.out.print(boardArray[i][j] + "\t");
}
System.out.println();
}
System.out.println("-------------------------");
}
}