FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaExercises/Java 1/TicTacToe.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
/
Java 1
/
TicTacToe.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
119 lines (107 loc) · 3.23 KB
Breadcrumbs
JavaExercises
/
Java 1
/
TicTacToe.java
Copy path
File metadata and controls
119 lines (107 loc) · 3.23 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
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
/**
* Java 1. Lesson 4. Tic-tac-toe in console
*
* @author Sergey Iryupin
* @version dated Jun 12, 2019
*/
import
java
.
util
.
Random
;
import
java
.
util
.
Scanner
;
class
TicTacToe
{
final
int
SIZE
=
3
;
final
char
DOT_X
=
'x'
;
final
char
DOT_O
=
'o'
;
final
char
DOT_EMPTY
=
'.'
;
char
[][]
map
;
Random
random
;
Scanner
scanner
;
public
static
void
main
(
String
[]
args
) {
new
TicTacToe
().
game
();
}
TicTacToe
() {
random
=
new
Random
();
scanner
=
new
Scanner
(
System
.
in
);
map
=
new
char
[
SIZE
][
SIZE
];
}
void
game
() {
initMap
();
while
(
true
) {
humanTurn
();
if
(
checkWin
(
DOT_X
)) {
System
.
out
.
println
(
"YOU WON!"
);
break
;
}
if
(
isMapFull
()) {
System
.
out
.
println
(
"Sorry, DRAW!"
);
break
;
}
aiTurn
();
printMap
();
if
(
checkWin
(
DOT_O
)) {
System
.
out
.
println
(
"AI WON!"
);
break
;
}
if
(
isMapFull
()) {
System
.
out
.
println
(
"Sorry, DRAW!"
);
break
;
}
}
System
.
out
.
println
(
"GAME OVER."
);
printMap
();
}
void
initMap
() {
for
(
int
i
=
0
;
i
<
SIZE
;
i
++)
for
(
int
j
=
0
;
j
<
SIZE
;
j
++)
map
[
i
][
j
] =
DOT_EMPTY
;
}
void
printMap
() {
for
(
int
i
=
0
;
i
<
SIZE
;
i
++) {
for
(
int
j
=
0
;
j
<
SIZE
;
j
++)
System
.
out
.
print
(
map
[
i
][
j
] +
" "
);
System
.
out
.
println
();
}
System
.
out
.
println
();
}
void
humanTurn
() {
int
x
,
y
;
do
{
System
.
out
.
println
(
"Enter X and Y (1..3):"
);
x
=
scanner
.
nextInt
() -
1
;
y
=
scanner
.
nextInt
() -
1
;
}
while
(!
isCellValid
(
x
,
y
));
map
[
y
][
x
] =
DOT_X
;
}
void
aiTurn
() {
int
x
,
y
;
do
{
x
=
random
.
nextInt
(
SIZE
);
y
=
random
.
nextInt
(
SIZE
);
}
while
(!
isCellValid
(
x
,
y
));
map
[
y
][
x
] =
DOT_O
;
}
boolean
checkWin
(
char
dot
) {
// check horizontals
if
(
map
[
0
][
0
] ==
dot
&&
map
[
0
][
1
] ==
dot
&&
map
[
0
][
2
] ==
dot
)
return
true
;
if
(
map
[
1
][
0
] ==
dot
&&
map
[
1
][
1
] ==
dot
&&
map
[
1
][
2
] ==
dot
)
return
true
;
if
(
map
[
2
][
0
] ==
dot
&&
map
[
2
][
1
] ==
dot
&&
map
[
2
][
2
] ==
dot
)
return
true
;
// check verticals
if
(
map
[
0
][
0
] ==
dot
&&
map
[
1
][
0
] ==
dot
&&
map
[
2
][
0
] ==
dot
)
return
true
;
if
(
map
[
0
][
1
] ==
dot
&&
map
[
1
][
1
] ==
dot
&&
map
[
2
][
1
] ==
dot
)
return
true
;
if
(
map
[
0
][
2
] ==
dot
&&
map
[
1
][
2
] ==
dot
&&
map
[
2
][
2
] ==
dot
)
return
true
;
// check diagonals
if
(
map
[
0
][
0
] ==
dot
&&
map
[
1
][
1
] ==
dot
&&
map
[
2
][
2
] ==
dot
)
return
true
;
if
(
map
[
2
][
0
] ==
dot
&&
map
[
1
][
1
] ==
dot
&&
map
[
0
][
2
] ==
dot
)
return
true
;
return
false
;
}
boolean
isMapFull
() {
for
(
int
i
=
0
;
i
<
SIZE
;
i
++)
for
(
int
j
=
0
;
j
<
SIZE
;
j
++)
if
(
map
[
i
][
j
] ==
DOT_EMPTY
)
return
false
;
return
true
;
}
boolean
isCellValid
(
int
x
,
int
y
) {
if
(
x
<
0
||
y
<
0
||
x
>=
SIZE
||
y
>=
SIZE
)
return
false
;
return
map
[
y
][
x
] ==
DOT_EMPTY
;
// by DSerov
}
}
Back
|
FazBrowse Home
|
New Git URL