-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcentrationRunner.java
More file actions
142 lines (118 loc) · 3.94 KB
/
ConcentrationRunner.java
File metadata and controls
142 lines (118 loc) · 3.94 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
/**
* Play the game of Concnetration
*
* The runner creates a game board and shows the board. It gets a 2-tile selection from the user,
* checks if the cards at the 2 tile locations match, and then re-displays the board.
*/
import java.util.Scanner;
public class ConcentrationRunner
{
// create the game
private static Concentration game = new Concentration();
public static void main(String[] args)
{
// input object for the BlueJ console
Scanner in = new Scanner(System.in);
// instructions
System.out.println("Welcome to the game of Concentration.");
System.out.println("Select the tile locations you want to see,");
System.out.println("or enter any non-integer character to quit.");
System.out.println("(You will need to know 2-dim arrays to play!)");
System.out.println("\nPress Enter to continue...");
in.nextLine();
System.out.print('\u000C');
// play until all tiles are matched
while(!game.allTilesMatch()) {
displayBoard();
// player selects first tile, if not an integer, quit game
int i1 = -1;
int j1 = -1;
System.out.print("First choice (i j): ");
if (in.hasNextInt())
{
i1 = in.nextInt();
}
else
{
//quitGame();
}
if (in.hasNextInt())
{
j1 = in.nextInt();
}
else {
//quitGame();
}
in.reset();
// ignore any extra input
//System.out.print('\u000C');
displayBoard();
// display board with first tile face up
game.showFaceUp(i1, j1);
displayBoard();
// player selects second tile, if not an integer, quit game
int i2 = -1;
int j2 = -1;
System.out.print("Second choice (i j): ");
if (in.hasNextInt())
{
i2 = in.nextInt();
}
else
{
quitGame();
}
if (in.hasNextInt())
{
j2 = in.nextInt();
}
else
{
quitGame();
}
in.reset(); // ignore any extra input
// display board with additional second tile face up
game.showFaceUp(i2, j2);
//System.out.print('\u000C');
displayBoard();
// determine if a match was found
String matched = game.checkForMatch(i1, j1, i2, j2);
System.out.println(matched);
//displayBoard();
}
displayBoard();
System.out.println("You Win!");
System.out.println("Game Over!");
}
/**
* Clear the console and show the game board
* Tiles can either indicate the card is face up or face down
*/
public static void displayBoard() {
System.out.println(game);
//System.out.println("█▀▀▀▀▀█▀▀▀▀▀█▀▀▀▀▀█");
System.out.println("█ 0 0 █ 0 1 █ 0 2 █");
// System.out.println("█▀▀▀▀▀█▀▀▀▀▀█▀▀▀▀▀█");
System.out.println("█ 1 0 █ 1 1 █ 1 2 █");
//System.out.println("█▄▄▄▄▄█▄▄▄▄▄█▄▄▄▄▄█");
System.out.println("█ 2 0 █ 2 1 █ 2 2 █\n");
}
/**
* Wait n second before clearing the console
*/
private static void wait(int n) {
try {
Thread.sleep(n * 1000);
}
catch (InterruptedException e) {
System.out.println(e);
}
}
/**
* User quits game
*/
private static void quitGame() {
System.out.println("Quit game!");
System.exit(0);
}
}