-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWumpusGame.java
147 lines (128 loc) · 4.14 KB
/
WumpusGame.java
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
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Random;
public class WumpusGame {
private int boardSize;
private int wumpus;
private Space[][] board;
private Random random = new Random();
private HashMap probabilityGeneration;
private int[] prob;
private byte[][] perceptBoard;
private final byte BREEZE = 0b00000001;
private final byte STENTCH = 0b0000010;
private final byte BUMP = 0b00000100;
private final byte GLITTER = 0b00001000;
private final byte DEATH_BY_WUMPUS = 0b00010000;
private final byte DEATH_BY_PIT = 0b00100000;
private final byte SCREAM = 0b01000000;
private PrintWriter out = new PrintWriter(new File("PerceptBoard.txt"));
public WumpusGame(int boardSize, int[] prob) throws FileNotFoundException {
this.boardSize = boardSize;
this.prob = prob;
perceptBoard = new byte[boardSize][boardSize];
board = new Space[boardSize][boardSize];
setBoard();
initializeBoard();
PrintStream out = new PrintStream(new FileOutputStream("world.txt"));
System.setOut(out);
printBoards();
}
public void setBoard() {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
board[i][j] = new Space();
}
}
}
public void initializeBoard() {
for (int i = 0; i < board.length; i++) {
for (int j = board[i].length - 1; j >= 0; j--) {
chooseState(i, j);
}
}
}
public void chooseState(int x, int y) {
if ((random.nextInt(100) + 1) <= prob[0]) {
placePit(x, y);
} else if ((random.nextInt(100) + 1) <= prob[1]) {
placeObstacle(x, y);
} else if ((random.nextInt(100) + 1) <= prob[2]) {
placeWumpus(x, y);
}
}
public void placePercept(int x, int y, byte percept) {
perceptBoard[x][y] |= percept;
}
public void placeAdjacentPercept(int x, int y, byte percept) {
if (x > 0) {
placePercept(x - 1, y, percept);
}
if (x < boardSize - 1) {
placePercept(x + 1, y, percept);
}
if (y > 0) {
placePercept(x, y - 1, percept);
}
if (y < boardSize - 1) {
placePercept(x, y + 1, percept);
}
}
public void placeObstacle(int x, int y) {
board[x][y].setHasObstacle(true);
placePercept(x, y, BUMP);
}
public void placePit(int x, int y) {
board[x][y].setHasHole(true);
placeAdjacentPercept(x, y, BREEZE);
placePercept(x, y, DEATH_BY_PIT);
}
public void placeGold(int x, int y) {
board[x][y].setHasGold(true);
placePercept(x, y, GLITTER);
}
public void placeWumpus(int x, int y) {
board[x][y].toggleWumpus();
placeAdjacentPercept(x, y, STENTCH);
placePercept(x, y, DEATH_BY_WUMPUS);
wumpus++;
}
public void checkBlockedStart() {
}
public void printBoards() {
printBoard();
printPerceptBoard();
}
public void printPerceptBoard() {
out.println(boardSize);
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
out.print(perceptBoard[i][j] + " ");
}
out.println();
}
out.close();
}
public void printBoard() {
System.out.println(boardSize);
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j].isHasWumpus()) {
System.out.print("W ");
} else if (board[i][j].isHasHole()) {
System.out.print("H ");
} else if (board[i][j].isHasObstacle()) {
System.out.print("I ");
} else {
System.out.print("0 ");
}
}
System.out.println("");
}
}
}