Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
Noughts And Crosses Formative
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
jordan.dalby
Noughts And Crosses Formative
Commits
e21e43e9
Commit
e21e43e9
authored
Feb 24, 2020
by
jordan.dalby
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
+ Added restart button
parent
b60e53bf
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
76 additions
and
69 deletions
+76
-69
Main.java
src/fx/Main.java
+76
-69
No files found.
src/fx/Main.java
View file @
e21e43e9
...
...
@@ -21,7 +21,6 @@ public class Main extends Application
private
Turn
currentTurn
=
Turn
.
CROSSES
;
private
boolean
gameOver
=
false
;
private
boolean
firstTurn
=
true
;
private
Random
random
=
new
Random
();
...
...
@@ -84,6 +83,20 @@ public class Main extends Application
label
.
setPrefHeight
(
128
);
label
.
setPrefWidth
(
256
);
Button
button
=
new
Button
();
button
.
setText
(
"Restart"
);
button
.
setOnAction
(
new
EventHandler
<
ActionEvent
>()
{
@Override
public
void
handle
(
ActionEvent
arg0
)
{
restart
();
}
});
pane
.
add
(
button
,
128
*
5
,
128
*
4
);
winLabel
=
label
;
pane
.
add
(
label
,
128
*
4
,
128
*
4
);
...
...
@@ -94,6 +107,17 @@ public class Main extends Application
s
.
show
();
}
public
void
restart
()
{
gameOver
=
false
;
currentTurn
=
Turn
.
CROSSES
;
for
(
Button
b
:
buttons
)
{
b
.
setStyle
(
"-fx-font-size:40"
);
b
.
setText
(
""
);
}
}
public
void
updateButton
(
Button
button
)
{
if
(
gameOver
)
...
...
@@ -172,88 +196,71 @@ public class Main extends Application
{
// AI PRIORITISES AGGRESSION BUT CAN ALSO BE VERY DEFENSIVE
if
(
firstTurn
)
String
turnCheck
=
""
;
String
opponentCheck
=
""
;
switch
(
currentTurn
)
{
firstTurn
=
false
;
int
i
=
-
1
;
int
count
=
0
;
while
(!
isSlotAvailable
(
i
)
&&
count
<
100000
)
{
count
++
;
i
=
random
.
nextInt
(
buttons
.
size
())
;
}
updateButton
(
buttons
.
get
(
i
))
;
case
CROSSES:
turnCheck
=
"X"
;
opponentCheck
=
"O"
;
break
;
case
NOUGHTS:
turnCheck
=
"O"
;
opponentCheck
=
"X"
;
break
;
default
:
break
;
}
else
{
String
turnCheck
=
""
;
String
opponentCheck
=
""
;
switch
(
currentTurn
)
{
case
CROSSES:
turnCheck
=
"X"
;
opponentCheck
=
"O"
;
break
;
case
NOUGHTS:
turnCheck
=
"O"
;
opponentCheck
=
"X"
;
break
;
default
:
break
;
}
System
.
out
.
println
(
"CHECKING IF IT CAN WIN"
);
System
.
out
.
println
(
"CHECKING IF IT CAN WIN"
);
for
(
String
s
:
checks
)
// AI checks for winning positions for itself
{
String
[]
split
=
s
.
split
(
","
);
int
x
=
Integer
.
parseInt
(
split
[
0
]);
int
y
=
Integer
.
parseInt
(
split
[
1
]);
int
z
=
Integer
.
parseInt
(
split
[
2
]);
for
(
String
s
:
checks
)
// AI checks for winning positions for itself
{
String
[]
split
=
s
.
split
(
","
);
int
x
=
Integer
.
parseInt
(
split
[
0
]);
int
y
=
Integer
.
parseInt
(
split
[
1
]);
int
z
=
Integer
.
parseInt
(
split
[
2
]);
int
i
=
willBeWin
(
x
,
y
,
z
,
turnCheck
);
if
(
i
!=
-
1
)
{
updateButton
(
buttons
.
get
(
i
));
return
;
}
}
System
.
out
.
println
(
"IT CAN'T"
);
System
.
out
.
println
(
"CHECKING IF IT CAN BLOCK"
);
for
(
String
s
:
checks
)
// AI checks for winning positions for opponent
int
i
=
willBeWin
(
x
,
y
,
z
,
turnCheck
);
if
(
i
!=
-
1
)
{
String
[]
split
=
s
.
split
(
","
);
int
x
=
Integer
.
parseInt
(
split
[
0
]);
int
y
=
Integer
.
parseInt
(
split
[
1
]);
int
z
=
Integer
.
parseInt
(
split
[
2
]);
int
i
=
willBeWin
(
x
,
y
,
z
,
opponentCheck
);
if
(
i
!=
-
1
)
{
updateButton
(
buttons
.
get
(
i
));
return
;
}
updateButton
(
buttons
.
get
(
i
));
return
;
}
}
System
.
out
.
println
(
"IT CAN'T"
);
// AI last resort, can't figure anything else out
System
.
out
.
println
(
"IT CAN'T"
);
System
.
out
.
println
(
"CHECKING IF IT CAN BLOCK"
);
int
i
=
-
1
;
int
count
=
0
;
while
(!
isSlotAvailable
(
i
)
&&
count
<
100000
)
for
(
String
s
:
checks
)
// AI checks for winning positions for opponent
{
String
[]
split
=
s
.
split
(
","
);
int
x
=
Integer
.
parseInt
(
split
[
0
]);
int
y
=
Integer
.
parseInt
(
split
[
1
]);
int
z
=
Integer
.
parseInt
(
split
[
2
]);
int
i
=
willBeWin
(
x
,
y
,
z
,
opponentCheck
);
if
(
i
!=
-
1
)
{
count
++
;
i
=
random
.
nextInt
(
buttons
.
size
())
;
updateButton
(
buttons
.
get
(
i
))
;
return
;
}
}
updateButton
(
buttons
.
get
(
i
)
);
System
.
out
.
println
(
"IT CAN'T"
);
// AI last resort, can't figure anything else out
int
i
=
-
1
;
int
count
=
0
;
while
(!
isSlotAvailable
(
i
)
&&
count
<
100000
)
{
count
++;
i
=
random
.
nextInt
(
buttons
.
size
());
}
updateButton
(
buttons
.
get
(
i
));
}
public
void
checkForWin
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment