-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBarrel.java
executable file
·170 lines (157 loc) · 4.72 KB
/
Barrel.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Part of Ladder, a game.
* Copyright (C) 1999, 2000 Stephen Ostermiller
* http://ostermiller.org/contact.pl?regarding=Ladder
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* See COPYING.TXT for details.
*/
package com.Ostermiller.Ladder;
import java.util.*;
/**
* This class defines a barrel. Barrels appear as an 'o' on the screen.
* The roll down the screen and try to crush the lad.
*/
public class Barrel extends Creature{
/**
* Random Number Generator for this Barrel.
*/
private static Random rnum = new Random();
/**
* Create a new barrel.
*/
public Barrel(){
this(0,0);
}
/**
* Create a new barrel in the given position.
*
* @param xpos the x coordinate of the barrel's position
* @param ypos the y coordinate of the barrel's position
*/
public Barrel(int xpos, int ypos){
this(xpos, ypos, Creature.STATIONARY);
}
/**
* Create a new barrel in the given position, going the proper direction
*
* @param xpos the x coordinate of the barrel's position
* @param ypos the y coordinate of the barrel's position
* @param direction the direction in which the barrel is initially moving
*/
public Barrel(int xpos, int ypos, int direction){
this.xpos = xpos;
this.ypos = ypos;
this.direction = direction;
symbol = 'o';
}
/**
* The command go down.
*/
private static final int DOWN = 2;
/**
* The command go left.
*/
private static final int LEFT = 4;
/**
* The command go right.
*/
private static final int RIGHT = 6;
/**
* The command stop.
*/
private static final int STOP = 5;
/**
* Cause this barrel to update itself. This will tell the barrel i
* is allowed to move. The context around the barrel is passed to the barrel.
* The barrel uses this context, and its current direction to decide
* where it will be next. Basically the barrel will fall down if nothing is
* under it, will move in the direction it was moving if it is not blocked,
* and will move randomly right or left if it hits some obstacle.
* The context is passed as 9 characters. The characters are numbered like the
* number keypad for easy reference.
*
* @param one the character an the screen in the one position
* @param two the character an the screen in the two position
* @param three the character an the screen in the three position
* @param four the character an the screen in the four position
* @param five the character an the screen in the five position
* @param six the character an the screen in the six position
* @param seven the character an the screen in the seven position
* @param eight the character an the screen in the eight position
* @param nine the character an the screen in the nine position
*/
public void update(char one, char two, char three, char four, char five, char six,
char seven, char eight, char nine){
int go = Barrel.STOP;
if (two == 'H' && five == 'H' && direction == Creature.DOWN){
go = Barrel.DOWN;
} else if (five == 'H' && two == 'H'){
double num = rnum.nextDouble();
if (num < .25){
go = Barrel.STOP;
} else if (num < .5){
go = Barrel.DOWN;
} else if (num < .75){
go = Barrel.RIGHT;
} else {
go = Barrel.LEFT;
}
} else if (two != '=' && two != '-' && two != '|'){
go = Barrel.DOWN;
} else if (five == 'H'){
double num = rnum.nextDouble();
if (num < (double)1/3){
go = Barrel.STOP;
} else if (num < (double)2/3){
go = Barrel.RIGHT;
} else {
go = Barrel.LEFT;
}
} else if (direction == Creature.LEFT){
go = Barrel.LEFT;
if (four == '=' || four == '-' || four == '|'){
go = Barrel.RIGHT;
}
} else if (direction == Creature.RIGHT){
go = Barrel.RIGHT;
if (six == '=' || six == '-' || six == '|'){
go = Barrel.LEFT;
}
} else {
double num = rnum.nextDouble();
if (num < (double)1/3){
go = Barrel.STOP;
} else if (num < (double)2/3){
go = Barrel.RIGHT;
} else {
go = Barrel.LEFT;
}
}
if (go == Barrel.RIGHT){
if (six != '=' && six != '-' && six != '|'){
xpos++;
direction = Creature.RIGHT;
}
} else if (go == Barrel.LEFT){
if (four != '=' && four != '-' && four != '|'){
xpos--;
direction = Creature.LEFT;
}
} else if (go == Barrel.DOWN){
if (two != '=' && two != '-' && two != '|'){
ypos++;
direction = Creature.DOWN;
}
}
}
}