-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jan Fic
committed
Feb 14, 2022
1 parent
67b07f8
commit c8719bf
Showing
6 changed files
with
143 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...utercombat/core/src/com/janfic/games/computercombat/model/players/HeuristicBotPlayer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.janfic.games.computercombat.model.players; | ||
|
||
import com.janfic.games.computercombat.model.Deck; | ||
import com.janfic.games.computercombat.model.GameRules; | ||
import com.janfic.games.computercombat.model.Player; | ||
import com.janfic.games.computercombat.model.match.MatchResults; | ||
import com.janfic.games.computercombat.model.match.MatchState; | ||
import com.janfic.games.computercombat.model.moves.Move; | ||
import com.janfic.games.computercombat.model.moves.MoveResult; | ||
import com.janfic.games.computercombat.model.players.heuristicanalyzers.ExtraTurnHeuristicAnalyzer; | ||
import com.janfic.games.computercombat.model.players.heuristicanalyzers.HeuristicAnalyzer; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
/** | ||
* | ||
* @author janfc | ||
*/ | ||
public class HeuristicBotPlayer extends Player { | ||
|
||
MatchState currentState; | ||
|
||
List<HeuristicAnalyzer> priorityList; | ||
|
||
public HeuristicBotPlayer() { | ||
} | ||
|
||
public HeuristicBotPlayer(String uid, Deck deck) { | ||
super(uid, deck); | ||
} | ||
|
||
@Override | ||
public void beginMatch(MatchState state, Player opponent) { | ||
this.currentState = state; | ||
this.priorityList = new ArrayList<>(); | ||
this.priorityList.add(new ExtraTurnHeuristicAnalyzer()); | ||
} | ||
|
||
@Override | ||
public Move getMove() { | ||
List<Move> moves = GameRules.getAvailableMoves(currentState); | ||
Collections.shuffle(moves); | ||
|
||
for (Move move : moves) { | ||
List<MoveResult> results = GameRules.makeMove(currentState, move); | ||
double totalScore = 0; | ||
for (int i = 0; i < priorityList.size(); i++) { | ||
HeuristicAnalyzer analyzer = priorityList.get(i); | ||
double baseScore = analyzer.analyze(results); | ||
double priorityScalar = Math.pow(2, i); | ||
double priorityScore = priorityScalar * baseScore; | ||
totalScore += priorityScore; | ||
} | ||
move.setValue(totalScore); | ||
} | ||
|
||
moves.sort(new MoveValueComparator()); | ||
|
||
return moves.get(0); | ||
} | ||
|
||
@Override | ||
public void updateState(List<MoveResult> state) { | ||
this.currentState = state.get(state.size() - 1).getNewState(); | ||
} | ||
|
||
@Override | ||
public void gameOver(MatchResults results) { | ||
} | ||
|
||
public class MoveValueComparator implements Comparator<Move> { | ||
|
||
@Override | ||
public int compare(Move a, Move b) { | ||
return (int) Math.ceil(b.getValue() - a.getValue()); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...fic/games/computercombat/model/players/heuristicanalyzers/ExtraTurnHeuristicAnalyzer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.janfic.games.computercombat.model.players.heuristicanalyzers; | ||
|
||
import com.janfic.games.computercombat.model.match.MatchState; | ||
import com.janfic.games.computercombat.model.moves.MoveResult; | ||
import java.util.List; | ||
|
||
/** | ||
* Analyzes a move's results to determine if an extra-move has been gained. | ||
* | ||
* @author janfc | ||
*/ | ||
public class ExtraTurnHeuristicAnalyzer extends HeuristicAnalyzer { | ||
|
||
@Override | ||
public float analyze(List<MoveResult> results) { | ||
float extraMove = 0; | ||
|
||
MatchState lastState = results.get(results.size() - 1).getNewState(); | ||
MatchState firstState = results.get(0).getOldState(); | ||
boolean gainedExtra = lastState.currentPlayerMove.equals(firstState.currentPlayerMove); | ||
|
||
extraMove = gainedExtra ? 1 : 0; | ||
|
||
return extraMove; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...c/com/janfic/games/computercombat/model/players/heuristicanalyzers/HeuristicAnalyzer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.janfic.games.computercombat.model.players.heuristicanalyzers; | ||
|
||
import com.janfic.games.computercombat.model.moves.MoveResult; | ||
import java.util.List; | ||
|
||
/** | ||
* | ||
* @author janfc | ||
*/ | ||
public abstract class HeuristicAnalyzer { | ||
|
||
public abstract float analyze(List<MoveResult> results); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters