From 001fdb33cd9855156df5a15aaeb80769ea17cf65 Mon Sep 17 00:00:00 2001 From: maghasemzadeh Date: Tue, 7 May 2019 23:54:21 +0430 Subject: [PATCH] AI completed --- src/models/AI.java | 153 +++++++++++++++++++++++++++++++----- src/models/Player.java | 1 - src/models/match/Match.java | 5 +- 3 files changed, 135 insertions(+), 24 deletions(-) diff --git a/src/models/AI.java b/src/models/AI.java index bb31339..11fbf68 100644 --- a/src/models/AI.java +++ b/src/models/AI.java @@ -1,19 +1,151 @@ package models; import controllers.Manager; +import models.map.Cell; +import models.map.Map; import models.match.Match; import views.Output; +import java.util.*; + public class AI extends Account { Match match; - private String decision; + private Queue decisions = new LinkedList<>(); + /// state[distanceToHero][oppHealth][myHealth][action] int[][][][] state = new int[10][10][10][3]; + private final int ACTIONS_NUMBER = 3; + int[] lastState = new int[3]; public AI(String username, String password) { super(username, password); } + private int distanceToOppHero() { + int distance = 0; + try { + distance = Cell.manhattanDistance(match.getPlayer1().getHero().getCell(), + match.getPlayer2().getHero().getCell()) - 1; + if (distance >= 9) + distance = 9; + } catch (Exception ignored) { + Output.err("Ignored in AI"); + } + return distance; + } + + private int getHealth() { + int health = 0; + try { + health = match.getPlayer2().getHero().getCurrentHealth() / 5; + if (health >= 9) + health = 9; + } catch (Exception ignored) { + Output.err("Ignored in AI"); + } + return health; + } + + private int getOppHealth() { + int health = 0; + try { + health = match.getPlayer1().getHero().getCurrentHealth() / 5; + if (health >= 9) + health = 9; + } catch (Exception ignored) { + Output.err("Ignored in AI"); + } + return health; + } + + int t = 0; + + public void decide() { + reward(); + int distance = lastState[0] = distanceToOppHero(); + int myHealth = lastState[1] = getHealth(); + int oppHealth = lastState[2] = getOppHealth(); + int max = 0; + for (int i = 0; i < ACTIONS_NUMBER; i++) { + max = Math.max(max, state[distance][oppHealth][myHealth][i]); + } + if (max < 100) { + chooseAction(new Random().nextInt() % 3); + return; + } + for (int i = 0; i < ACTIONS_NUMBER; i++) { + if (state[distance][oppHealth][myHealth][i] == max) { + chooseAction(i); + return; + } + } +// if (t % 2 == 0) +// this.decision = "show hand"; +// else +// this.decision = "end turn"; +// t = (t + 1) % 2; + } + + private void reward() { + + } + + private void chooseAction(int action) { + switch (action) { + case 0: + insertCard(); + break; + case 1: + moveCard(); + break; + case 2: + attack(); + break; + default: + decisions.add("end turn"); + } + } + + private void attack() { + selectCard(); + try { + decisions.add("attack " + match.getPlayer1().getHero().getID()); + } catch (Player.HeroDeadException e) { + Output.err("ignored in attack in AI"); + } + } + + private void insertCard() { + decisions.add("insert " + match.getPlayer2().getHand().getCards().get(Match.random(0, 4)).getID() + " in " + + Match.random(1, Map.ROW_NUMBER) + ", " + Match.random(1, Map.COLUMN_NUMBER)); + } + + private void selectCard() { + try { + decisions.add("select " + match.getPlayer2().getHero().getID()); + } catch (Player.HeroDeadException e) { + Output.err("ignored in insertCard in AI"); + } + } + + private void moveCard() { + selectCard(); + decisions.add("move to " + Match.random(1, Map.ROW_NUMBER) + ", " + Match.random(1, Map.COLUMN_NUMBER)); + } + + + public String getDecision() { + if (match.getPlayer2().getMana() < 2) + return "end turn"; + if (decisions.size() == 0) + decide(); + return decisions.poll(); + } + + public void setMatch(Match playingMatch) { + this.match = playingMatch; + } + public static AI getAIAccount() { AI aiAccount = new AI("AI", "password"); try { @@ -66,23 +198,4 @@ public static AI getAIAccount() { } return aiAccount; } - - int t = 0; - public void decide() { - if (t % 2 == 0) - this.decision = "show hand"; - else - this.decision = "end turn"; - t = (t + 1) % 2; - } - - - - public String getDecision() { - return decision; - } - - public void setMatch(Match playingMatch) { - this.match = playingMatch; - } } diff --git a/src/models/Player.java b/src/models/Player.java index e342649..6fd55c1 100644 --- a/src/models/Player.java +++ b/src/models/Player.java @@ -47,7 +47,6 @@ public Player(Account account) { public String getDecision() { AI ai = (AI)account; - ai.decide(); return ai.getDecision(); } diff --git a/src/models/match/Match.java b/src/models/match/Match.java index ac1561d..0fd3ba6 100644 --- a/src/models/match/Match.java +++ b/src/models/match/Match.java @@ -69,9 +69,8 @@ private void turnPreparing(Player player) { ); } - public int random(int min, int max) { - Random random = new Random(); - return min + random.nextInt() % (max - min + 1) ; + public static int random(int min, int max) { + return (int)(Math.random() * ((max - min) + 1)) + min ; } public void setFlagsInMap(int flagsCount) {