-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmallBusiness.java
176 lines (152 loc) · 6.4 KB
/
SmallBusiness.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package brain;
import actor.Block;
import actor.BotBrain;
import actor.Business;
import actor.GameObject;
import actor.JobSite;
import actor.SubwayStation;
import grid.Location;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
/**
*
* @author 20lambi
*/
public class SmallBusiness extends BotBrain {
private ArrayList<Location> JobsiteLoc = new ArrayList();
private Location prevLoc = getLocation();
@Override
public int chooseAction() {
Business[][] businessGrid = getBusinessArray();
getJobsites(businessGrid);
Location prevLocA = getLocation(); //saving previous location, so bot will not move on a previous location
int direction = evadeDir(); //calls method to find best move based on whether or not there is a "Block" and is the chepest move
if (this.getScore() < 180000) {
if (this.getBusinessArray()[this.getRow()][this.getCol()].getColor().equals(this.getColor())) {
if (this.getBusinessArray()[this.getRow()][this.getCol()].getChargeAmount() < 350) {
return 3060;
} else {
return direction;
}
}
return 2000;
}
prevLoc = prevLocA;
return direction;
}
public void getJobsites(Business[][] businessGrid) {
for (int r = 0; r <= 20; r++) {
for (int c = 0; c <= 20; c++) {
if (businessGrid[r][c] instanceof JobSite) {
if ((r != 0) && (c != 0) || (r != 20) && (c != 0) || (r != 20) && (c != 0) || (r != 20) && (c != 20)) {
JobsiteLoc.add(new Location(r, c));
}
}
}
}
}
public int getClosest(Location currentLoc) {
ArrayList<Integer> distances = new ArrayList();
int goTo = 0;
for (int c = 0; c < JobsiteLoc.size(); c++) {
distances.add(currentLoc.distanceTo(JobsiteLoc.get(c)));
}
Collections.sort(distances);
for (int c = 0; c < JobsiteLoc.size(); c++) {
if (distances.get(0) == currentLoc.distanceTo(JobsiteLoc.get(c))) {
goTo = c;
}
}
return goTo;
}
//Finds location
public Location getLocation() {
return new Location(getRow(), getCol());
}
//Finds best random path based on Blocked Locations and cheapest cells
public int evadeDir() {
Business[][] businessArray = this.getBusinessArray();
GameObject[][] objectArray = this.getArena();
ArrayList<Location> locs = new ArrayList();
//Creates an arraylist of all adjacent locations
for (int d = 0; d < 360; d = d + 90) {
if (inBounds(getLocation().getAdjacentLocation(d)) == true) {
locs.add(getLocation().getAdjacentLocation(d));
}
}
//Prints out adjacent location
//Creates an arraylist of the costs of adjacent locations
/*
ArrayList<Integer> costs = new ArrayList();
for (int c = 0; c < 4; c++) {
costs.add(costN(locs.get(c), businessArray));
}
*/
//Makes direction equal to the ideal next move as found in the nextMov method
int direction = nextMov(locs, objectArray, businessArray, prevLoc);
return direction;
}
//Finds cost of a location if it is valid
public int costN(Location next, Business[][] businessArray) {
int costN = -1;
costN = businessArray[next.getRow()][next.getCol()].getChargeAmount();
return costN;
}
//Finds whether a space is blocked or has a subwaystation
public boolean isBlock(Location next, GameObject[][] objectArray, Business[][] businessArray) {
boolean isBlock = true;
if ((!(objectArray[next.getRow()][next.getCol()] instanceof Block)) && (!(businessArray[next.getRow()][next.getCol()] instanceof SubwayStation))) {
isBlock = false;
}
return isBlock;
}
//Determines next move based on whether a cell is blocked or contains a subwaystation and if it is the cheapest move, additionally, this method makes sure that the bot doesn't move on a previous location
public int nextMov(ArrayList<Location> locs, GameObject[][] objectArray, Business[][] businessArray, Location prevLoc) {
int direction = 0;
ArrayList<Location> validLocs = new ArrayList();
for (int c = 0; c < locs.size(); c++) {
if (((isBlock(locs.get(c), objectArray, businessArray)) == false)) {
validLocs.add(locs.get(c));
}
}
ArrayList<Integer> costsValidLocs = new ArrayList();
for (int c = 0; c < validLocs.size(); c++) {
costsValidLocs.add(costN(validLocs.get(c), businessArray));
}
ArrayList<Location> cheapestMoves = new ArrayList();
for (int c = 0; c < validLocs.size(); c++) {
if ((costN(validLocs.get(c), businessArray) == Collections.min(costsValidLocs))) {
cheapestMoves.add(validLocs.get(c));
}
}
for (int c = 0; c < cheapestMoves.size(); c++) {
if ((isBlock(cheapestMoves.get(c), objectArray, businessArray) == true)) {
cheapestMoves.remove(c);
}
}
if (cheapestMoves.size() > 1) {
Random r = new Random();
int validLocNum = r.nextInt(cheapestMoves.size());
direction = getLocation().getDirectionToward(cheapestMoves.get(validLocNum));
} else if (cheapestMoves.size() == 1) {
direction = getLocation().getDirectionToward(cheapestMoves.get(0));
} else {
direction = (int) (Math.random() * 4) * 90;
}
return direction;
}
//Checks to see if a given location is within the competition bounds
public boolean inBounds(Location next) {
boolean inBounds = false;
if ((next.getRow() >= 0 && next.getRow() <= 20) && (next.getCol() >= 0 && next.getCol() <= 20)) {
inBounds = true;
}
return inBounds;
}
}