Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Matt Tower
205_hw02
Commits
67f226a4
Commit
67f226a4
authored
Nov 11, 2014
by
Matt Tower
Browse files
RandomPlayer class at 8:34 Nov 11
parent
4d538c31
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/model/player/RandomPlayer.java
View file @
67f226a4
...
...
@@ -7,6 +7,7 @@
*/
package
model.player
;
import
java.util.ArrayList
;
import
java.util.Random
;
import
model.Board
;
...
...
@@ -15,14 +16,26 @@ import model.Mark;
/**
* @author mjt023
*
* This is the RandomPlayer class that is an AI player that chooses
* moves randomly.
*/
public
class
RandomPlayer
extends
Player
{
public
class
RandomPlayer
extends
Player
implements
AutoPlayable
{
/*
* declares the board, AI mark, Random object, random number variable,
* Arraylists to store rows and columns of free spaces on the current board
*/
private
Board
theBoard
;
private
Mark
myMark
;
private
Random
rand
=
new
Random
();
private
int
randNum
;
private
ArrayList
<
Integer
>
rowOptions
=
new
ArrayList
<
Integer
>();
private
ArrayList
<
Integer
>
colOptions
=
new
ArrayList
<
Integer
>();
/**
* This is the constructor for the RandomPlayer class that takes in the
* current board and the mark of the AI player.
*
* @param theBoard
* @param myMark
*/
...
...
@@ -30,6 +43,15 @@ public class RandomPlayer extends Player {
super
(
theBoard
,
myMark
);
}
/**
* returns the board
*
* @return - the board
*/
public
Board
getBoard
()
{
return
this
.
theBoard
;
}
/**
* generates a random number within board dimensions, used for choosing
* random coordinates
...
...
@@ -37,17 +59,32 @@ public class RandomPlayer extends Player {
* @return - randomly generated number within board dimensions
*/
public
int
genRandNum
()
{
return
this
.
rand
.
nextInt
(
theBoard
.
getSize
());
}
return
this
.
rand
.
nextInt
(
3
);
/**
* finds the free spaces in the board currently
*/
private
void
findFreeSpaces
()
{
for
(
int
i
=
0
;
i
<
theBoard
.
getSize
();
i
++)
{
for
(
int
j
=
0
;
j
<
theBoard
.
getSize
();
j
++)
{
if
(
theBoard
.
isEmpty
(
i
,
j
))
{
rowOptions
.
add
(
i
);
colOptions
.
add
(
j
);
}
}
}
}
/**
*
AI
makes random move
s until an empty space is found
*
RandomPlayer
makes
a
random move
*
* @return
* @return
- true if move was made successfully, false otherwise
*/
public
boolean
setMark
()
{
@Override
public
boolean
decideMove
()
{
findFreeSpaces
();
randNum
=
rand
.
nextInt
(
theBoard
.
getSize
());
return
move
(
rowOptions
.
get
(
randNum
),
colOptions
.
get
(
randNum
));
}
}
Write
Preview
Supports
Markdown
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