Created
March 5, 2017 21:26
-
-
Save hanss314/c17aed83d88d05fb053c3a0ab1429547 to your computer and use it in GitHub Desktop.
Game string parser and maker
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
final String charTable = "0123456789:;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | |
public String getCaryString(){ | |
int humanNum = 0; | |
int timeLimitNum=99999; | |
int timeBonusNum=99999; | |
if(redBot){ | |
humanNum+=(2*redDepth+4);//(redDepth/2+1)*4 | |
} | |
if(blueBot){ | |
humanNum+=(blueDepth/2+1); | |
} | |
if(timeLimit[1]!=Integer.MAX_VALUE){ | |
timeLimitNum = timeLimit[1]; | |
timeBonusNum = timeBonus; | |
} | |
if(screen==2){ | |
boardStart = getBoard(); | |
moveSequence = ""; | |
} | |
String result = rules+","+Math.max(WIDTH,HEIGHT)+","+timeLimitNum+","+timeBonusNum+","+humanNum+","+boardStart+moveSequence; | |
return result; | |
} | |
public String getBoard(){ | |
String result=""; | |
int counter=2; | |
int sum=0; | |
ArrayList<Integer> triplets = new ArrayList<Integer>(); | |
for(int x=0; x<WIDTH; x++){ | |
for(int y=HEIGHT-1;y>=0;y--){ | |
Tile t=allTiles[x][y]; | |
sum+=(Math.pow(4,counter)*t.getState()); | |
if(counter==0){ | |
triplets.add(sum); | |
sum=0; | |
counter=3; | |
} | |
counter--; | |
} | |
} | |
if(counter!=2){ | |
triplets.add(sum); | |
} | |
for(int i:triplets){ | |
result+=charTable.toCharArray()[i]; | |
} | |
return result | |
} | |
public void stringToBoard(String in,boolean update){ | |
int position = 0; | |
int sum = 0; | |
int positionInString = 0; | |
char[] s = in.toCharArray(); | |
reds.clear(); | |
blues.clear(); | |
int exp = 3; | |
int bas = 4; | |
removeObjects(getObjects(Tile.class)); | |
allTiles = new Tile[WIDTH][HEIGHT]; | |
createGrid(); | |
try{ | |
for (int x = 0; x < WIDTH; x++) { | |
for (int y = HEIGHT-1; y >=0 ; y--) { | |
if (position == 0) { | |
sum = charTable.indexOf(s[positionInString]); | |
positionInString++; | |
} | |
int value = (int)(sum / Math.pow(bas,(exp-1)-position)); | |
sum -= value * (int)Math.pow(bas,(exp-1)-position); | |
allTiles[x][y].setState(value); | |
position++; | |
if (position >= exp) { | |
position = 0; | |
} | |
} | |
} | |
}catch(Exception e){ | |
for (Tile[] ts:allTiles) { | |
for (Tile t:ts) { | |
t.setState(0); | |
} | |
} | |
} | |
for (Tile[] ts:allTiles) { | |
for (Tile t:ts) { | |
if(update){ | |
t.preupdate(allTiles); | |
}else{ | |
t.willDie = t.isDead; | |
t.updateImg(); | |
} | |
} | |
} | |
moveNumber = 0; | |
totalMoves = 0; | |
updateLists(); | |
} | |
public void readBoard(String in,boolean update){ | |
moveSequence = ""; | |
redTurn = true; | |
blueTurn = false; | |
try{ | |
String[] parts; | |
boolean newMode = false; | |
char[] id = in.toCharArray(); | |
if(id[0]=='B'){ | |
int indexOfFirstComma = in.indexOf(","); | |
int indexOfFirstSlash = in.indexOf("/"); | |
birth.clear(); | |
survive.clear(); | |
for(int i=1;i<indexOfFirstSlash;i++){ | |
birth.add(Character.getNumericValue(id[i])); | |
} | |
for(int i=indexOfFirstSlash+2;i<indexOfFirstComma;i++){ | |
survive.add(Character.getNumericValue(id[i])); | |
} | |
parts = in.substring(indexOfFirstComma+1).split(","); | |
}else{ | |
parts = in.split(","); | |
} | |
if (parts.length < 5) { | |
throw new IOException(); | |
} | |
WIDTH = HEIGHT = Math.min(Math.max(Integer.parseInt(parts[0]),3),20); | |
timeLimit[0]=0; | |
timeLimit[1] = Integer.parseInt(parts[1]); | |
timeLimit[2]=0; | |
timeLimit[3]=0; | |
timeBonus = Integer.parseInt(parts[2]); | |
int humanNum = Integer.parseInt(parts[3]); | |
stringToBoard(parts[4],update); | |
/*redDepth = (int)(((humanNum/4)-1)*2); | |
blueDepth = (((humanNum%4)-1)*2); | |
if(redDepth<0){ | |
redBot = false; | |
}else{ | |
redBot = true; | |
} | |
if(blueDepth<0){ | |
blueBot = false; | |
}else{ | |
redBot = true; | |
}*/ | |
int noSwapsUntil = -1; | |
String[] moves = new String[parts.length-5]; | |
for(int i = 0; i<moves.length; i++){ | |
moves[i]=parts[i+5]; | |
} | |
int moveLimit = Integer.MAX_VALUE; | |
if(screen==4){ | |
moveArray = moves; | |
totalMoves = moveArray.length; | |
moveNumber = 0; | |
saveTiles(); | |
}else{ | |
saveTiles(); | |
for (int t = 0; t < moves.length; t++) { // Go through all the moves. | |
makeMove(moves[t]); | |
} | |
} | |
updateNumbers(); | |
updateLists(); | |
boardStart = getBoard(); | |
}catch (Exception e) { | |
JFrame frame = new JFrame("Error"); | |
JOptionPane.showMessageDialog(frame, "Illegal game string", "Error", 0); | |
} | |
} | |
public void makeMove(String s){ | |
String[] moveParts = s.split("\\+"); | |
if (moveParts.length == 3) { | |
redTimer.set(Integer.parseInt(moveParts[1])*(long)(0.01f)); | |
blueTimer.set(Integer.parseInt(moveParts[2])*(long)(0.01f)); | |
}else{ | |
redTimer.set(0); | |
blueTimer.set(0); | |
} | |
int moveType = charTable.indexOf(moveParts[0].charAt(moveParts[0].length()-1))-12; | |
if (moveType <= 3) { | |
int x = charTable.indexOf(moveParts[0].charAt(0))-12; | |
int y = charTable.indexOf(moveParts[0].charAt(1))-12; | |
if(moveType==1){ | |
if(redTurn){ | |
allTiles[x][HEIGHT-y-1].setState(1); | |
}else if(blueTurn){ | |
allTiles[x][HEIGHT-y-1].setState(2); | |
} | |
}else{ | |
int swapWith = 0; | |
allTiles[x][HEIGHT-y-1].setState(0); | |
} | |
allTiles[x][HEIGHT-y-1].preupdate(allTiles); | |
allTiles[x][HEIGHT-y-1].updateNeighbours(allTiles); | |
}else if (moveType == 4){ | |
//moveNumber = 0; | |
//totalMoves = 0; | |
doTurn(); | |
}else{ | |
int endGameCause = moveType-5; | |
endGameCause = 2+3*(moveType-5); | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment