Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ISC2
oop
project
Commits
b98f6112
Commit
b98f6112
authored
1 year ago
by
iliya
Browse files
Options
Downloads
Patches
Plain Diff
feat: part advanced AIs fully done
parent
8ed2be0e
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/main/java/hepia/AIPlayer.java
+25
-8
25 additions, 8 deletions
src/main/java/hepia/AIPlayer.java
src/main/java/hepia/App.java
+7
-4
7 additions, 4 deletions
src/main/java/hepia/App.java
src/main/java/hepia/GameManager.java
+44
-16
44 additions, 16 deletions
src/main/java/hepia/GameManager.java
with
76 additions
and
28 deletions
src/main/java/hepia/AIPlayer.java
+
25
−
8
View file @
b98f6112
...
@@ -16,6 +16,7 @@ public class AIPlayer implements IPlayer {
...
@@ -16,6 +16,7 @@ public class AIPlayer implements IPlayer {
private
boolean
hasInsurance
;
private
boolean
hasInsurance
;
private
int
insuranceBet
;
private
int
insuranceBet
;
private
boolean
hasDoubledBet
;
private
boolean
hasDoubledBet
;
private
boolean
isAdvanced
;
private
static
Map
<
Integer
,
Map
<
Integer
,
Action
>>
strategyTableWithoutAces
=
new
HashMap
<>();
private
static
Map
<
Integer
,
Map
<
Integer
,
Action
>>
strategyTableWithoutAces
=
new
HashMap
<>();
private
static
Map
<
Integer
,
Map
<
Integer
,
Action
>>
strategyTableWithAces
=
new
HashMap
<>();
private
static
Map
<
Integer
,
Map
<
Integer
,
Action
>>
strategyTableWithAces
=
new
HashMap
<>();
...
@@ -397,9 +398,10 @@ public class AIPlayer implements IPlayer {
...
@@ -397,9 +398,10 @@ public class AIPlayer implements IPlayer {
return
hand
;
return
hand
;
}
}
public
AIPlayer
(
int
bet
)
{
public
AIPlayer
(
int
bet
,
boolean
isAdvanced
)
{
placeBet
(
bet
);
placeBet
(
bet
);
this
.
balance
=
0.0
;
this
.
balance
=
0.0
;
this
.
isAdvanced
=
isAdvanced
;
}
}
@Override
@Override
...
@@ -421,7 +423,11 @@ public class AIPlayer implements IPlayer {
...
@@ -421,7 +423,11 @@ public class AIPlayer implements IPlayer {
public
String
toString
()
{
public
String
toString
()
{
StringBuilder
builder
=
new
StringBuilder
();
StringBuilder
builder
=
new
StringBuilder
();
builder
.
append
(
"========= AI Player =========\n"
);
if
(
this
.
isAdvanced
)
{
builder
.
append
(
"========= Advanced AI Player =========\n"
);
}
else
{
builder
.
append
(
"========= Basic AI Player =========\n"
);
}
builder
.
append
(
"Value of bet: "
+
this
.
bet
+
"\n"
);
builder
.
append
(
"Value of bet: "
+
this
.
bet
+
"\n"
);
builder
.
append
(
"Current balance: "
+
this
.
balance
+
"\n"
);
builder
.
append
(
"Current balance: "
+
this
.
balance
+
"\n"
);
builder
.
append
(
"Hand: "
+
this
.
hand
+
"\n"
);
builder
.
append
(
"Hand: "
+
this
.
hand
+
"\n"
);
...
@@ -432,7 +438,14 @@ public class AIPlayer implements IPlayer {
...
@@ -432,7 +438,14 @@ public class AIPlayer implements IPlayer {
return
str
;
return
str
;
}
}
public
void
basicStrategy
(
Deck
currentGame
)
{
public
void
basicStrategy
(
Deck
currentGame
,
Hand
croupierHand
)
{
for
(
Card
card
:
croupierHand
)
{
if
(
card
.
getRankName
().
equals
(
"As"
))
{
System
.
out
.
println
(
"AI has asked for an insurance"
);
this
.
setHasInsurance
(
true
);
}
}
while
(
this
.
getHand
().
computeHandValue
()
<
17
)
{
while
(
this
.
getHand
().
computeHandValue
()
<
17
)
{
this
.
displayPlayerHand
();
this
.
displayPlayerHand
();
...
@@ -461,7 +474,8 @@ public class AIPlayer implements IPlayer {
...
@@ -461,7 +474,8 @@ public class AIPlayer implements IPlayer {
}
}
}
}
public
void
playHand
(
boolean
handContainsAce
,
Map
<
Integer
,
Action
>
innerMap
,
Deck
currentGame
,
Hand
croupierHand
)
{
public
void
playHand
(
boolean
handContainsAce
,
Map
<
Integer
,
Action
>
innerMap
,
Deck
currentGame
,
Hand
croupierHand
,
List
<
Hand
>
bustedHands
)
{
while
(
this
.
getHand
().
computeHandValue
()
<
21
)
{
while
(
this
.
getHand
().
computeHandValue
()
<
21
)
{
this
.
displayPlayerHand
();
this
.
displayPlayerHand
();
...
@@ -472,7 +486,10 @@ public class AIPlayer implements IPlayer {
...
@@ -472,7 +486,10 @@ public class AIPlayer implements IPlayer {
System
.
out
.
println
(
"AI assigned the value 11 to the ace"
);
System
.
out
.
println
(
"AI assigned the value 11 to the ace"
);
card
.
setWeight
(
11
);
card
.
setWeight
(
11
);
if
(
this
.
getHand
().
computeHandValue
()
==
21
)
{
if
(
this
.
getHand
().
computeHandValue
()
>=
21
)
{
if
(
this
.
getHand
().
computeHandValue
()
>
21
)
{
bustedHands
.
add
(
this
.
getHand
());
}
break
;
break
;
}
}
}
}
...
@@ -522,7 +539,7 @@ public class AIPlayer implements IPlayer {
...
@@ -522,7 +539,7 @@ public class AIPlayer implements IPlayer {
}
}
}
}
public
void
advancedStrategy
(
Deck
currentGame
,
Hand
croupierHand
)
{
public
void
advancedStrategy
(
Deck
currentGame
,
Hand
croupierHand
,
List
<
Hand
>
bustedHands
)
{
boolean
handContainsAce
=
false
;
boolean
handContainsAce
=
false
;
Map
<
Integer
,
Action
>
innerMap
=
null
;
Map
<
Integer
,
Action
>
innerMap
=
null
;
...
@@ -542,10 +559,10 @@ public class AIPlayer implements IPlayer {
...
@@ -542,10 +559,10 @@ public class AIPlayer implements IPlayer {
for
(
int
i
=
0
;
i
<
this
.
getSplittedHand
().
size
();
i
++)
{
for
(
int
i
=
0
;
i
<
this
.
getSplittedHand
().
size
();
i
++)
{
System
.
out
.
println
(
"======= AI's n°"
+
(
i
+
1
)
+
" hand ======="
);
System
.
out
.
println
(
"======= AI's n°"
+
(
i
+
1
)
+
" hand ======="
);
this
.
setHand
(
this
.
getSplittedHand
().
get
(
i
));
this
.
setHand
(
this
.
getSplittedHand
().
get
(
i
));
playHand
(
handContainsAce
,
innerMap
,
currentGame
,
croupierHand
);
playHand
(
handContainsAce
,
innerMap
,
currentGame
,
croupierHand
,
bustedHands
);
}
}
}
else
{
}
else
{
playHand
(
handContainsAce
,
innerMap
,
currentGame
,
croupierHand
);
playHand
(
handContainsAce
,
innerMap
,
currentGame
,
croupierHand
,
bustedHands
);
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
src/main/java/hepia/App.java
+
7
−
4
View file @
b98f6112
...
@@ -8,15 +8,18 @@ import java.util.Scanner;
...
@@ -8,15 +8,18 @@ import java.util.Scanner;
public
class
App
{
public
class
App
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
int
nbAIplayers
=
6
;
int
nbDecks
=
5
;
int
nbDecks
=
5
;
Scanner
scanner
=
new
java
.
util
.
Scanner
(
System
.
in
);
Scanner
scanner
=
new
java
.
util
.
Scanner
(
System
.
in
);
System
.
out
.
print
(
"Please specify the amount of players: "
);
System
.
out
.
print
(
"Please specify the amount of human players: "
);
int
nbPlayers
=
scanner
.
nextInt
();
int
nbHumanPlayers
=
scanner
.
nextInt
();
System
.
out
.
print
(
"Please specify the amount of BASIC AI players: "
);
int
nbBasicAi
=
scanner
.
nextInt
();
System
.
out
.
print
(
"Please specify the amount of ADVANCED AI players: "
);
int
nbAdvancedAi
=
scanner
.
nextInt
();
GameManager
foo
=
new
GameManager
(
Game
.
BLACKJACK
,
nbPlayers
,
nb
AIplayers
,
nbDecks
);
GameManager
foo
=
new
GameManager
(
Game
.
BLACKJACK
,
nb
Human
Players
,
nb
BasicAi
,
nbAdvancedAi
,
nbDecks
);
foo
.
runGame
();
foo
.
runGame
();
...
...
This diff is collapsed.
Click to expand it.
src/main/java/hepia/GameManager.java
+
44
−
16
View file @
b98f6112
...
@@ -12,6 +12,10 @@ public class GameManager {
...
@@ -12,6 +12,10 @@ public class GameManager {
private
List
<
HumanPlayer
>
humanPlayers
;
private
List
<
HumanPlayer
>
humanPlayers
;
private
List
<
AIPlayer
>
aiPlayers
;
private
List
<
AIPlayer
>
aiPlayers
;
private
List
<
AIPlayer
>
basicAi
;
private
List
<
AIPlayer
>
advancedAi
;
private
CroupierPlayer
croupier
;
private
CroupierPlayer
croupier
;
private
List
<
IPlayer
>
players
;
private
List
<
IPlayer
>
players
;
...
@@ -238,19 +242,32 @@ public class GameManager {
...
@@ -238,19 +242,32 @@ public class GameManager {
}
while
(
true
);
}
while
(
true
);
}
}
public
GameManager
(
public
GameManager
(
Game
game
,
int
nbHumanPlayers
,
int
nbBasicAi
,
int
nbAdvancedAi
,
int
nbDecks
)
Game
game
,
throws
IllegalArgumentException
{
int
nbHumanPlayers
,
if
(
nbHumanPlayers
<
1
)
{
int
nbAIPlayers
,
throw
new
IllegalArgumentException
(
"There must be at least 1 human player"
);
int
nbDecks
)
{
}
ensureValidPlayerCount
(
nbHumanPlayers
+
nbAIPlayers
);
if
(
nbBasicAi
<
1
||
nbBasicAi
>
2
)
{
throw
new
IllegalArgumentException
(
"There must be at least 1 or at most 2 basic AI players"
);
}
if
(
nbAdvancedAi
<
1
||
nbAdvancedAi
>
2
)
{
throw
new
IllegalArgumentException
(
"There must be at least 1 or at most 2 basic AI players"
);
}
int
playersExcludingDealer
=
nbHumanPlayers
+
nbBasicAi
+
nbAdvancedAi
;
ensureValidPlayerCount
(
playersExcludingDealer
);
ensureValidDeckCount
(
nbDecks
);
ensureValidDeckCount
(
nbDecks
);
this
.
game
=
game
;
this
.
game
=
game
;
this
.
humanPlayers
=
new
ArrayList
<>(
nbHumanPlayers
);
this
.
humanPlayers
=
new
ArrayList
<>(
nbHumanPlayers
);
this
.
aiPlayers
=
new
ArrayList
<>(
nbAIPlayers
);
this
.
basicAi
=
new
ArrayList
<>(
nbBasicAi
);
this
.
advancedAi
=
new
ArrayList
<>(
nbAdvancedAi
);
this
.
aiPlayers
=
new
ArrayList
<>(
nbBasicAi
+
nbAdvancedAi
);
this
.
players
=
new
ArrayList
<>(
nbHumanPlayers
+
nbAIPlay
er
s
+
1
);
this
.
players
=
new
ArrayList
<>(
playersExcludingDeal
er
+
1
);
this
.
bustedPlayers
=
new
ArrayList
<>();
this
.
bustedPlayers
=
new
ArrayList
<>();
this
.
blackJackHands
=
new
ArrayList
<>();
this
.
blackJackHands
=
new
ArrayList
<>();
...
@@ -265,16 +282,23 @@ public class GameManager {
...
@@ -265,16 +282,23 @@ public class GameManager {
}
}
}
}
initBets
(
nbHumanPlayers
,
nb
AIPlayers
);
initBets
(
nbHumanPlayers
,
nb
BasicAi
,
nbAdvancedAi
);
}
}
private
void
initBets
(
int
nbHumanPlayers
,
int
nbAIPlayers
)
{
private
void
initBets
(
int
nbHumanPlayers
,
int
basicAi
,
int
advancedAi
)
{
askPlayersForBets
(
nbHumanPlayers
);
askPlayersForBets
(
nbHumanPlayers
);
for
(
int
i
=
0
;
i
<
nbAIPlayers
;
i
++)
{
for
(
int
i
=
0
;
i
<
basicAi
;
i
++)
{
int
aiBet
=
new
Random
().
nextInt
((
100
-
MIN_BET
+
1
)
+
MIN_BET
);
int
aiBet
=
new
Random
().
nextInt
((
100
-
MIN_BET
+
1
)
+
MIN_BET
);
AIPlayer
ai
=
new
AIPlayer
(
aiBet
);
AIPlayer
ai
=
new
AIPlayer
(
aiBet
,
false
);
this
.
basicAi
.
add
(
ai
);
this
.
aiPlayers
.
add
(
ai
);
}
for
(
int
i
=
0
;
i
<
advancedAi
;
i
++)
{
int
aiBet
=
new
Random
().
nextInt
((
100
-
MIN_BET
+
1
)
+
MIN_BET
);
AIPlayer
ai
=
new
AIPlayer
(
aiBet
,
true
);
this
.
advancedAi
.
add
(
ai
);
this
.
aiPlayers
.
add
(
ai
);
this
.
aiPlayers
.
add
(
ai
);
}
}
...
@@ -344,7 +368,7 @@ public class GameManager {
...
@@ -344,7 +368,7 @@ public class GameManager {
if
(
player
.
getHand
().
computeHandValue
()
>=
21
)
{
if
(
player
.
getHand
().
computeHandValue
()
>=
21
)
{
if
(
player
.
getHand
().
computeHandValue
()
>
21
)
{
if
(
player
.
getHand
().
computeHandValue
()
>
21
)
{
// this.bustedPlayers.add(player);
// this.bustedPlayers.add(player);
//
this.bustedHands.add(player.getHand());
this
.
bustedHands
.
add
(
player
.
getHand
());
}
}
break
;
break
;
}
}
...
@@ -352,10 +376,14 @@ public class GameManager {
...
@@ -352,10 +376,14 @@ public class GameManager {
}
}
}
}
for
(
AIPlayer
aiPlayer
:
this
.
aiPlayers
)
{
for
(
AIPlayer
basic
:
this
.
basicAi
)
{
System
.
out
.
println
(
aiPlayer
);
System
.
out
.
println
(
basic
);
basic
.
basicStrategy
(
this
.
currentGame
,
this
.
croupier
.
getHand
());
}
aiPlayer
.
advancedStrategy
(
this
.
currentGame
,
this
.
croupier
.
getHand
());
for
(
AIPlayer
advanced
:
this
.
advancedAi
)
{
System
.
out
.
println
(
advanced
);
advanced
.
advancedStrategy
(
this
.
currentGame
,
this
.
croupier
.
getHand
(),
this
.
bustedHands
);
}
}
// Croupier's turn
// Croupier's turn
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment