Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
artificial-intelligence
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
jak.moore
artificial-intelligence
Commits
5e025bd1
Commit
5e025bd1
authored
Mar 02, 2018
by
jak.moore
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updates from week 4 lecture.
parents
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
363 additions
and
0 deletions
+363
-0
Actions.java
src/Actions.java
+11
-0
GameState.java
src/GameState.java
+43
-0
Main.java
src/Main.java
+309
-0
No files found.
src/Actions.java
0 → 100644
View file @
5e025bd1
public
enum
Actions
{
// Possible actions
MOVE_BOAT_LEFT_TO_RIGHT
,
MOVE_BOAT_RIGHT_TO_LEFT
,
WOLF_LEFT_TO_BOAT
,
WOLF_RIGHT_TO_BOAT
,
WOLF_BOAT_TO_LEFT
,
WOLF_BOAT_TO_RIGHT
,
CHICKEN_RIGHT_TO_BOAT
,
CHICKEN_LEFT_TO_BOAT
,
CHICKEN_BOAT_TO_RIGHT
,
CHICKEN_BOAT_TO_LEFT
,
}
src/GameState.java
0 → 100644
View file @
5e025bd1
import
java.util.ArrayList
;
public
class
GameState
{
// Class member variables
public
ArrayList
<
Integer
>
leftBank
;
// Chicken = 1, wolf = 2
public
ArrayList
<
Integer
>
rightBank
;
public
ArrayList
<
Integer
>
raft
;
public
boolean
isRaftOnLeftBank
;
// Constructor for GameState class
public
GameState
(
ArrayList
<
Integer
>
left
,
ArrayList
<
Integer
>
right
,
ArrayList
<
Integer
>
raft
,
boolean
raftBank
)
{
leftBank
=
left
;
rightBank
=
right
;
this
.
raft
=
raft
;
isRaftOnLeftBank
=
raftBank
;
}
// Return a clone of a Game state object
@SuppressWarnings
(
"unchecked"
)
public
GameState
cloneObject
()
{
ArrayList
<
Integer
>
lbClone
,
rbClone
,
raftClone
;
// Cloning array lists
lbClone
=
(
ArrayList
<
Integer
>)
leftBank
.
clone
();
rbClone
=
(
ArrayList
<
Integer
>)
leftBank
.
clone
();
raftClone
=
(
ArrayList
<
Integer
>)
raft
.
clone
();
// Creating a new, cloned object to return
GameState
clone
=
new
GameState
(
lbClone
,
rbClone
,
raftClone
,
isRaftOnLeftBank
);
return
clone
;
}
}
\ No newline at end of file
src/Main.java
0 → 100644
View file @
5e025bd1
import
java.util.ArrayList
;
public
class
Main
{
private
boolean
planCompleted
=
false
;
public
static
void
main
(
String
[]
args
)
{
// Starting game state
ArrayList
<
Integer
>
lBank
=
new
ArrayList
()
{
};
ArrayList
<
Integer
>
rBank
=
new
ArrayList
()
{
};
ArrayList
<
Integer
>
raft
=
new
ArrayList
()
{
};
// Instantiating a new Game State object
GameState
gs
=
new
GameState
(
lBank
,
rBank
,
raft
,
true
);
// Invoking get actions method and storing the results in an array list
ArrayList
<
Actions
>
validActions
=
getActions
(
gs
);
// Array list to hold game states
ArrayList
<
GameState
>
newStates
=
new
ArrayList
();
// Iterate through valid actions simulating their game state
for
(
Actions
a
:
validActions
)
{
gs
=
Simulate
(
gs
,
a
);
// Storing possible game states in array list
newStates
.
add
(
gs
);
}
}
// Get list of possible actions
private
static
ArrayList
<
Actions
>
getActions
(
GameState
gs
)
{
// Array list to hold possible actions
ArrayList
<
Actions
>
possibleActions
=
new
ArrayList
<>();
// Counters to hold game information
int
wolvesOnLBank
=
0
;
int
wolvesOnRBank
=
0
;
int
wolvesOnBoat
=
0
;
int
chicksOnLBank
=
0
;
int
chicksOnRBank
=
0
;
int
chicksOnBoat
=
0
;
boolean
raftFull
=
false
;
// Getting current state of game
if
(
gs
.
raft
.
size
()
>
1
)
{
raftFull
=
true
;
}
for
(
int
i
:
gs
.
leftBank
)
{
if
(
i
==
1
)
{
wolvesOnLBank
++;
}
if
(
i
==
2
)
{
chicksOnLBank
++;
}
}
for
(
int
i
:
gs
.
rightBank
)
{
if
(
i
==
1
)
{
wolvesOnRBank
++;
}
if
(
i
==
2
)
{
chicksOnRBank
++;
}
}
for
(
int
i
:
gs
.
raft
)
{
if
(
i
==
1
)
{
wolvesOnBoat
++;
}
if
(
i
==
2
)
{
chicksOnBoat
++;
}
}
// Raft is on left bank
if
(
gs
.
isRaftOnLeftBank
)
{
possibleActions
.
add
(
Actions
.
MOVE_BOAT_LEFT_TO_RIGHT
);
if
(!
raftFull
)
{
if
(
chicksOnLBank
>
0
)
{
possibleActions
.
add
(
Actions
.
CHICKEN_LEFT_TO_BOAT
);
}
if
(
wolvesOnLBank
>
0
)
{
possibleActions
.
add
(
Actions
.
WOLF_LEFT_TO_BOAT
);
}
}
if
(
wolvesOnBoat
>=
1
)
{
possibleActions
.
add
(
Actions
.
WOLF_BOAT_TO_LEFT
);
}
if
(
chicksOnBoat
>=
1
)
{
possibleActions
.
add
(
Actions
.
CHICKEN_BOAT_TO_LEFT
);
}
}
// Raft is on right bank
else
{
possibleActions
.
add
(
Actions
.
MOVE_BOAT_RIGHT_TO_LEFT
);
if
(!
raftFull
)
{
if
(
chicksOnRBank
>
0
)
{
possibleActions
.
add
(
Actions
.
CHICKEN_RIGHT_TO_BOAT
);
}
if
(
wolvesOnRBank
>
0
)
{
possibleActions
.
add
(
Actions
.
WOLF_RIGHT_TO_BOAT
);
}
if
(
wolvesOnBoat
>
0
)
{
possibleActions
.
add
(
Actions
.
WOLF_BOAT_TO_RIGHT
);
}
if
(
chicksOnBoat
>
0
)
{
possibleActions
.
add
(
Actions
.
CHICKEN_BOAT_TO_RIGHT
);
}
}
}
return
possibleActions
;
}
// Takes a game state and simulate its possible actions
private
static
GameState
Simulate
(
GameState
gs
,
Actions
a
)
{
// Get clone of Game State object
gs
=
gs
.
cloneObject
();
// Simulate what happens for all possible actions
switch
(
a
)
{
case
CHICKEN_BOAT_TO_LEFT:
gs
.
raft
.
remove
(
1
);
gs
.
leftBank
.
add
(
1
);
break
;
case
CHICKEN_BOAT_TO_RIGHT:
gs
.
raft
.
remove
(
1
);
gs
.
rightBank
.
add
(
1
);
break
;
case
CHICKEN_LEFT_TO_BOAT:
gs
.
leftBank
.
remove
(
1
);
gs
.
raft
.
add
(
1
);
break
;
case
CHICKEN_RIGHT_TO_BOAT:
gs
.
rightBank
.
remove
(
1
);
gs
.
raft
.
add
(
1
);
break
;
case
WOLF_BOAT_TO_LEFT:
gs
.
raft
.
remove
(
2
);
gs
.
leftBank
.
add
(
2
);
break
;
case
WOLF_BOAT_TO_RIGHT:
gs
.
raft
.
remove
(
2
);
gs
.
rightBank
.
add
(
2
);
break
;
case
WOLF_LEFT_TO_BOAT:
gs
.
leftBank
.
remove
(
2
);
gs
.
raft
.
add
(
2
);
break
;
case
WOLF_RIGHT_TO_BOAT:
gs
.
rightBank
.
remove
(
2
);
gs
.
raft
.
add
(
2
);
break
;
case
MOVE_BOAT_LEFT_TO_RIGHT:
gs
.
isRaftOnLeftBank
=
false
;
break
;
case
MOVE_BOAT_RIGHT_TO_LEFT:
gs
.
isRaftOnLeftBank
=
true
;
break
;
default
:
break
;
}
return
gs
;
}
// Goal test method
public
boolean
goalTest
(
GameState
gs
)
{
int
lengthR
=
gs
.
rightBank
.
size
();
int
lengthL
=
gs
.
leftBank
.
size
();
int
numberOfWolves
=
0
;
int
numberOfChickens
=
0
;
// Get number of wolves/chickens on right bank
for
(
int
x
=
0
;
x
<=
lengthR
;
x
++)
{
if
(
gs
.
rightBank
.
get
(
x
).
equals
(
1
))
{
numberOfChickens
++;
}
else
if
(
gs
.
rightBank
.
get
(
x
).
equals
(
2
))
{
numberOfWolves
++;
}
// Check if problem completed
if
(
completed
(
numberOfWolves
,
numberOfChickens
))
{
return
true
;
}
else
{
numberOfWolves
=
0
;
numberOfChickens
=
0
;
}
}
// Get number of chickens/wolves on right bank
for
(
int
y
=
0
;
y
<=
lengthL
;
y
++)
{
if
(
gs
.
leftBank
.
get
(
y
).
equals
(
1
))
{
numberOfChickens
++;
}
else
if
(
gs
.
leftBank
.
get
(
y
).
equals
(
2
))
{
numberOfChickens
++;
}
}
return
completed
(
numberOfWolves
,
numberOfChickens
);
}
// Check final counts
private
boolean
completed
(
int
wolves
,
int
chickens
)
{
if
((
wolves
==
3
)
&&
(
chickens
==
3
))
{
planCompleted
=
true
;
return
true
;
}
else
{
return
false
;
}
}
}
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