Skip to content

Commit

Permalink
AI completed
Browse files Browse the repository at this point in the history
  • Loading branch information
maghasemzadeh committed May 7, 2019
1 parent 0e69618 commit 001fdb3
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 24 deletions.
153 changes: 133 additions & 20 deletions src/models/AI.java
Original file line number Diff line number Diff line change
@@ -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<String> 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 {
Expand Down Expand Up @@ -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;
}
}
1 change: 0 additions & 1 deletion src/models/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public Player(Account account) {

public String getDecision() {
AI ai = (AI)account;
ai.decide();
return ai.getDecision();
}

Expand Down
5 changes: 2 additions & 3 deletions src/models/match/Match.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 001fdb3

Please sign in to comment.