FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaExercises/Head First Java/DotComGameHelper.java at master · biblelamp/JavaExercises · GitHub
biblelamp
/
JavaExercises
Public
Notifications
You must be signed in to change notification settings
Fork
130
Star
153
Code
Issues
1
Pull requests
9
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaExercises
/
Head First Java
/
DotComGameHelper.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
77 lines (71 loc) · 2.45 KB
Breadcrumbs
JavaExercises
/
Head First Java
/
DotComGameHelper.java
Copy path
File metadata and controls
77 lines (71 loc) · 2.45 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
java
.
io
.*;
import
java
.
util
.*;
public
class
DotComGameHelper
{
private
static
final
String
alphabet
=
"abcdefg"
;
private
int
gridLength
=
7
;
private
int
gridSize
=
49
;
private
int
[]
grid
=
new
int
[
gridSize
];
private
int
comCount
=
0
;
public
String
getUserInput
(
String
prompt
) {
String
inputLine
=
null
;
System
.
out
.
print
(
prompt
+
" "
);
try
{
BufferedReader
is
=
new
BufferedReader
(
new
InputStreamReader
(
System
.
in
));
inputLine
=
is
.
readLine
();
if
(
inputLine
.
length
() ==
0
)
return
null
;
}
catch
(
IOException
e
) {
System
.
out
.
println
(
"IOException: "
+
e
);
}
return
inputLine
.
toLowerCase
();
}
public
ArrayList
<
String
>
placeDotCom
(
int
comSize
) {
ArrayList
<
String
>
alphaCells
=
new
ArrayList
<
String
>();
String
[]
alphacoords
=
new
String
[
comSize
];
String
temp
=
null
;
int
[]
coords
=
new
int
[
comSize
];
int
attempts
=
0
;
boolean
success
=
false
;
int
location
=
0
;
comCount
++;
int
incr
=
1
;
if
((
comCount
%
2
) ==
1
) {
incr
=
gridLength
;
}
while
(!
success
&
attempts
++ <
200
) {
location
= (
int
) (
Math
.
random
() *
gridSize
);
//System.out.print(" try " + location);
int
x
=
0
;
success
=
true
;
while
(
success
&&
x
<
comSize
) {
if
(
grid
[
location
] ==
0
) {
coords
[
x
++] =
location
;
location
+=
incr
;
if
(
location
>=
gridSize
){
success
=
false
;
}
if
(
x
>
0
&& (
location
%
gridLength
==
0
)) {
success
=
false
;
}
}
else
{
// System.out.print(" used " + location);
success
=
false
;
}
}
}
int
x
=
0
;
int
row
=
0
;
int
column
=
0
;
// System.out.println("\n" );
while
(
x
<
comSize
) {
grid
[
coords
[
x
]] =
1
;
row
= (
int
) (
coords
[
x
] /
gridLength
);
column
=
coords
[
x
] %
gridLength
;
temp
=
String
.
valueOf
(
alphabet
.
charAt
(
column
));
alphaCells
.
add
(
temp
.
concat
(
Integer
.
toString
(
row
)));
x
++;
// System.out.print(" coord " + x + " = " + alphaCells.get(x-l));
}
// System.out.println("\n" );
return
alphaCells
;
}
}
Back
|
FazBrowse Home
|
New Git URL