Last active
January 20, 2016 05:53
-
-
Save urbanintell/4166d3d6868e3a4b37ff to your computer and use it in GitHub Desktop.
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
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Random; | |
/** | |
* Created by newuser on 1/9/16. | |
*/ | |
public class Experiment { | |
private TileFloor floor; | |
private Map<Tile,Boolean> hasVisited; | |
public Experiment(TileFloor floor){ | |
this.floor=floor; | |
hasVisited = new HashMap<Tile,Boolean>(); | |
} | |
public Experiment(){ | |
floor = new TileFloor(7,7); | |
hasVisited = new HashMap<Tile,Boolean>(); | |
} | |
public boolean visitedAllCells(){ | |
return hasVisited.size()==floor.getNumberOfTiles(); | |
} | |
public void simulate(){ | |
int iterationLimit =0; | |
Random rand = new Random(); | |
while(!visitedAllCells()){ | |
// value for decision of | |
int move = rand.nextInt(8); | |
iterationLimit++; | |
Tile tile = floor.getRoach(); | |
int x = tile.getX()+RoachMove.values()[move].getX(); | |
int y = tile.getY()+RoachMove.values()[move].getY(); | |
if(floor.checkBounds(x,y)){ | |
hasVisited.put(tile,true); | |
floor.getRoach().setHasRoach(false); | |
floor.hasRoach(x,y); | |
} | |
if (iterationLimit==500000){ | |
System.err.println("Iteration limit met"); | |
break; | |
} | |
} | |
System.out.println("Number of legal moves: "+legalMoves()); | |
floor.display(); | |
} | |
public int legalMoves(){ | |
int sum=0; | |
for(Tile m : hasVisited.keySet()){ | |
sum+= m.getValue(); | |
} | |
return sum; | |
} | |
} |
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
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
// write your code here | |
Scanner keyboard = new Scanner(System.in); | |
System.out.print("Enter rows: "); | |
int rows = keyboard.nextInt(); | |
System.out.print("Enter columns: "); | |
int columns = keyboard.nextInt(); | |
TileFloor floor = new TileFloor(rows,columns); | |
int x,y; | |
System.out.println("Enter initial roach position"); | |
do { | |
System.out.print("X = "); | |
x = keyboard.nextInt(); | |
System.out.print("Y = "); | |
y = keyboard.nextInt(); | |
}while(!floor.checkBounds(x,y)); | |
floor.hasRoach(x,y); | |
Experiment experiment = new Experiment(floor); | |
experiment.simulate(); | |
} | |
} |
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
/** | |
* Created by newuser on 1/9/16. | |
*/ | |
public enum RoachMove { | |
ZERO(-1,1), | |
ONE(0,1), | |
TWO(1,1), | |
THREE(1,0), | |
FOUR(1,-1), | |
FIVE(0,-1), | |
SIX(-1,-1), | |
SEVEN(-1,0); | |
private int x; | |
private int y; | |
RoachMove(int x, int y){ | |
this.x=x; | |
this.y=y; | |
} | |
public int getX(){ | |
return x; | |
} | |
public int getY(){ | |
return y; | |
} | |
} |
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
/** | |
* Created by newuser on 1/9/16. | |
*/ | |
public class Tile implements Comparable<Tile>{ | |
private int x; | |
private int y; | |
private int value; | |
private boolean hasRoach; | |
public Tile(int x, int y){ | |
this.x = x; | |
this.y = y; | |
value = 0; | |
hasRoach=false; | |
} | |
public void incrementValue(){ | |
++value; | |
} | |
public void setHasRoach(boolean hasRoach){ | |
this.hasRoach=hasRoach; | |
} | |
public int getValue(){ | |
return value; | |
} | |
public int getX() { | |
return x; | |
} | |
public int getY() { | |
return y; | |
} | |
@Override | |
public int compareTo(Tile tile) { | |
if(tile.getX()!=x&& tile.getY()!=y){ | |
return -1; | |
} | |
return 0; | |
} | |
} |
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
/** | |
* Created by newuser on 1/9/16. | |
*/ | |
public class TileFloor { | |
private Tile[][] floor; | |
Tile roach; | |
private int rows; | |
private int columns; | |
public TileFloor(int rows, int columns){ | |
this.rows = rows; | |
this.columns=columns; | |
floor = new Tile[rows][columns]; | |
initCells(); | |
} | |
private void initCells(){ | |
for(int i=0;i<rows;i++){ | |
for(int j=0;j<columns;j++){ | |
floor[i][j] = new Tile(i,j); | |
} | |
} | |
} | |
public void hasRoach(int x, int y){ | |
roach = floor[x][y]; | |
floor[x][y].setHasRoach(true); | |
floor[x][y].incrementValue(); | |
} | |
public Tile getRoach(){ | |
return roach; | |
} | |
public boolean checkBounds(int x_position, int y_position){ | |
if(x_position<0||y_position<0){ | |
return false; | |
} | |
if(x_position>=rows||y_position>=columns){ | |
return false; | |
} | |
return true; | |
} | |
public void display(){ | |
for(int i=0;i<rows;i++){ | |
for(int j=0;j<columns;j++){ | |
Tile tile = floor[i][j]; | |
System.out.print(tile.getValue()+"\t"); | |
} | |
System.out.println(); | |
} | |
} | |
public int getNumberOfTiles(){ | |
return rows*columns; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SOLUTION