Skip to content

Commit

Permalink
Minor bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hanss314 authored Feb 13, 2017
1 parent bb0abfb commit e30852c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 37 deletions.
Binary file modified GOLAD/AI.class
Binary file not shown.
61 changes: 33 additions & 28 deletions GOLAD/AI.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@
import java.util.concurrent.Future;
import java.util.ArrayList;
class AI implements Callable<int[][]> {
private Tile[][] allTiles = new Tile[20][20];
private Tile[][] allTiles = new Tile[0][0];
private final int depth;
private ArrayList<Tile> reds = new ArrayList();
ArrayList<Tile> blues = new ArrayList();
private final boolean redTurn;
private final boolean blueTurn;
private final MyWorld w;
private final int WIDTH;
private final int HEIGHT;
public AI(Tile[][] board, int depth,boolean redTurn, boolean blueTurn, MyWorld world){
for(int i=0;i<20;i++){
for(int j=0;j<20;j++){
WIDTH = board.length;
HEIGHT = board[0].length;
allTiles = new Tile[WIDTH][HEIGHT];
for(int i=0;i<board.length;i++){
for(int j=0;j<board[0].length;j++){
allTiles[i][j] = new Tile(world,i,j);
}
}
for(int i=0;i<20;i++){
for(int j=0;j<20;j++){
for(int i=0;i<board.length;i++){
for(int j=0;j<board[0].length;j++){
Tile toClone = board[i][j];
Tile newTile = allTiles[i][j];
newTile.isRed = toClone.isRed;
Expand All @@ -42,22 +47,22 @@ public int[][] call() {
return makeRandomMove();
}
double[] move = {0,0,0};
Tile[][] board = new Tile[20][20];
Tile[][] board = new Tile[WIDTH][HEIGHT];
if(blueTurn){
move=new double[]{0,0,2147483647};
}
for(int x = 0; x < 20; x++){
for(int y = 0; y < 20; y++){
for(int x = 0; x < WIDTH; x++){
for(int y = 0; y < HEIGHT; y++){
Tile toAdd = new Tile(w, x, y);
board[x][y]=toAdd;
}
}
for(int x=0; x<20; x++){
for(int y=0; y<20; y++){
for(int x=0; x<WIDTH; x++){
for(int y=0; y<HEIGHT; y++){
if(allTiles[x][y].getState()==1||allTiles[x][y].getState()==2){
int[] coords = {x,y};
for(int tileX=0; tileX<20; tileX++){
for(int tileY=0; tileY<20; tileY++){
for(int tileX=0; tileX<WIDTH; tileX++){
for(int tileY=0; tileY<HEIGHT; tileY++){
Tile newTile = board[tileX][tileY];
Tile toClone = allTiles[tileX][tileY];
newTile.isRed = toClone.isRed;
Expand Down Expand Up @@ -141,7 +146,7 @@ public double getFutureRatio(int[] square, int depth, Tile[][] board){
toKill.isDead = true;
toKill.updateNeighbours(board);
//writeBoard("before.txt", board);
iterate(depth, board);
iterate(2, board);
//writeBoard("after.txt", board);
for(Tile[] ts:board){
for(Tile t:ts){
Expand All @@ -167,8 +172,8 @@ public int[][] checkForSquares(){
int sacra1Y=0;
int sacra2Y=0;
findSquare:
for(x=0; x<20; x++){
for(y=0; y<20; y++){
for(x=0; x<WIDTH; x++){
for(y=0; y<HEIGHT; y++){
Tile toCheck = allTiles[x][y];
if((toCheck.isBlue && redTurn) || (toCheck.isRed && blueTurn)){
int state=toCheck.getState();
Expand All @@ -185,8 +190,8 @@ public int[][] checkForSquares(){
}
}
findEasySacrafices:
for(int x2=0; x2<20; x2++){
for(int y2=0; y2<20; y2++){
for(int x2=0; x2<WIDTH; x2++){
for(int y2=0; y2<HEIGHT; y2++){
Tile toCheck = allTiles[x2][y2];
if(((toCheck.isRed && redTurn) || (toCheck.isBlue && blueTurn)) && toCheck.willDie){
sacraficeList.add(toCheck);
Expand All @@ -205,8 +210,8 @@ public int[][] checkForSquares(){
}
if(sacraficeList.size()<2){
findOtherSacrafices:
for(int x2=0; x2<20; x2++){
for(int y2=0; y2<20; y2++){
for(int x2=0; x2<WIDTH; x2++){
for(int y2=0; y2<HEIGHT; y2++){
Tile toCheck = allTiles[x2][y2];
int[] neighbours = toCheck.getNeighbourCount(allTiles);
int neighbourCount = neighbours[0]+neighbours[1]+neighbours[2];
Expand Down Expand Up @@ -267,8 +272,8 @@ public int[][] squareCrawl(){
int blueX=0;
int blueY=0;
findRedSquare:
for(redX=0; redX<20; redX++){
for(redY=0; redY<20; redY++){
for(redX=0; redX<WIDTH; redX++){
for(redY=0; redY<HEIGHT; redY++){
Tile toCheck = allTiles[redX][redY];
if(toCheck.isRed){
int state=toCheck.getState();
Expand All @@ -288,8 +293,8 @@ public int[][] squareCrawl(){
}
}
findBlueSquare:
for(blueX=0; blueX<20; blueX++){
for(blueY=0; blueY<20; blueY++){
for(blueX=0; blueX<WIDTH; blueX++){
for(blueY=0; blueY<HEIGHT; blueY++){
Tile toCheck = allTiles[blueX][blueY];
if(toCheck.isBlue){
int state=toCheck.getState();
Expand Down Expand Up @@ -394,15 +399,15 @@ public int[][] squareCrawl(){
return returnValue;
}
public int[][] makeRandomMove(){
int tileX = Greenfoot.getRandomNumber(20);
int tileY = Greenfoot.getRandomNumber(20);
int tileX = Greenfoot.getRandomNumber(WIDTH);
int tileY = Greenfoot.getRandomNumber(HEIGHT);
Tile t = allTiles[tileX][tileY];
while(!((t.isBlue && redTurn)||(t.isRed && blueTurn))){
tileX = Greenfoot.getRandomNumber(20);
tileY = Greenfoot.getRandomNumber(20);
tileX = Greenfoot.getRandomNumber(WIDTH);
tileY = Greenfoot.getRandomNumber(HEIGHT);
t = allTiles[tileX][tileY];
}
int[] changes = {tileX,tileY,t.getState()};
int[] changes = {tileX,tileY,0};
int[][] returnValue = {changes};
return returnValue;
}
Expand Down
Binary file modified GOLAD/MyWorld.class
Binary file not shown.
20 changes: 11 additions & 9 deletions GOLAD/MyWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public class MyWorld extends World
ArrayList<Integer> survive = new ArrayList<>(Arrays.asList(2,3));
ArrayList<Tile> reds = new ArrayList();
ArrayList<Tile> blues = new ArrayList();
Tile[][] allTiles = new Tile[20][20];
int WIDTH=20;
int HEIGHT=20;
Tile[][] allTiles = new Tile[WIDTH][HEIGHT];
boolean redTurn = true;
boolean blueTurn = false;
//int redMoves = 1;
Expand Down Expand Up @@ -163,8 +165,8 @@ public void mainMenu(){
screen = 0;
}
public void createGrid(){
for(int x = 0; x < 20; x++){
for(int y = 0; y < 20; y++){
for(int x = 0; x < WIDTH; x++){
for(int y = 0; y < HEIGHT; y++){
Tile toAdd = new Tile(this, x, y);
allTiles[x][y]=toAdd;
addObject(toAdd, 30*x+15, 30*y+15);
Expand Down Expand Up @@ -247,10 +249,10 @@ public void doTurn(){
doneTurn = false;
}
public void randomizeGrid(){
for(int x = 0; x<10; x++){
for(int y = 0; y<20; y++){
for(int x = 0; x<(WIDTH/2); x++){
for(int y = 0; y<HEIGHT; y++){
Tile t = allTiles[x][y];
Tile pair = allTiles[19-x][19-y];
Tile pair = allTiles[WIDTH-1-x][HEIGHT-1-y];
int state = Greenfoot.getRandomNumber(5);
if(state == 0){
t.isRed = true;
Expand Down Expand Up @@ -341,8 +343,8 @@ public void writeBoard(String filename, Tile[][] board){
try{
writer = new PrintWriter("saves/"+filename,"UTF-8");
Tile t = null;
for(int y = 0; y<20; y++){
for(int x = 0; x<20; x++){
for(int y = 0; y<HEIGHT; y++){
for(int x = 0; x<WIDTH; x++){
t = board[x][y];
if(t.isRed){
writer.print("R");
Expand Down Expand Up @@ -405,7 +407,7 @@ public void readBoard(String filename){
}
x=0;
y++;
if(y==20){
if(y==HEIGHT){
break;
}
}
Expand Down

0 comments on commit e30852c

Please sign in to comment.