-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameGrid.java
More file actions
107 lines (94 loc) · 3.4 KB
/
GameGrid.java
File metadata and controls
107 lines (94 loc) · 3.4 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
import java.security.SecureRandom;
import java.util.Scanner;
public class GameGrid {
int counter = 0;
public void run() {
Scanner input = new Scanner(System.in);
System.out.println("\n Welcome to the Game Grid\n");
System.out.println("Instructions: This is a very simple game to play. ");
System.out.println("You will need to not cross the number (1) and escape ");
System.out.println("from any side to Win.\n");
int[][] game = new int[10][10];
SecureRandom ran = new SecureRandom();
int iwallChance = 30;
LinkedList w = new LinkedList();
int iUserRow = 0;
int iUserCol = 0;
boolean exits = false;
//rows
for (int x = 0; x < 10; x++) {
// Cols
for (int y = 0; y < 10; y++) {
if (ran.nextInt(100) < iwallChance) {
game[x][y] = 1;
} else {
game[x][y] = 0;
}
}
}
game[0][0] = 0;
for (int i = 0; i < game.length; i++) {
for (int v = 0; v < game[i].length; v++) {
if (i == iUserRow && v == iUserCol) {
System.out.print("$ ");
} else {
System.out.print(game[i][v] + " ");
}
}
System.out.println();
}
System.out.println("\nChoose from the following");
System.out.println("1) TO MOVE RIGHT ");
System.out.println("2) TO MOVE DOWN ");
while (!exits) {
System.out.println("Enter your choice Here: ");
int choice = 0;
choice = input.nextInt();
w.addHeadNode(iUserCol, iUserRow);
if (choice == 1) {
iUserCol++;
} else if (choice == 2) {
iUserRow++;
} else {
System.out.printf("Wrong input, try again. ");
System.out.println("Enter your choice Here: ");
}
if (game[iUserRow][iUserCol] == 1) {
System.out.printf("You Failed, Game over" + "\n");
// System.out.println("Total moves taken: " + counter + "\n");
exits = true;
} else if (iUserCol == 9 || iUserRow == 9) {
System.out.printf("You Won!!! \n");
exits = true;
}
for (int i = 0; i < game.length; i++) {
for (int v = 0; v < game[i].length; v++) {
if (i == iUserRow && v == iUserCol) {
System.out.print("$ ");
} else {
System.out.print(game[i][v] + " ");
}
}
System.out.println();
}
}
Node t = w.removeHeadNode();
while (t != null) {
game[t.yPosition][t.xPosition] = 9;
t = w.removeHeadNode();
counter++;
}
System.out.println();
for (int i = 0; i < game.length; i++) {
for (int v = 0; v < game[i].length; v++) {
if (i == iUserRow && v == iUserCol) {
System.out.print("$ ");
} else {
System.out.print(game[i][v] + " ");
}
}
System.out.println();
}
System.out.println("Total moves taken: " + counter + "\n");
}
}