Skip to content

Instantly share code, notes, and snippets.

@OneRaynyDay
Last active August 29, 2015 14:09
Show Gist options
  • Save OneRaynyDay/305cf08ef7cb6a36d8fe to your computer and use it in GitHub Desktop.
Save OneRaynyDay/305cf08ef7cb6a36d8fe to your computer and use it in GitHub Desktop.
PAD Solver - Ray Zhang and Young Guo
int[] state = NIL; // <-- enter threshold from constructor
int currentState = 0; <-- state[currentState] = current state
int color;
public Orb(Color c)
color = c;
int[] state = new int[1];
state[0] = NIL;
public Orb(Color c, State s)
color = c;
int[] state = new int[1];
state[0] = s;
public Orb(Color c, State s, Threshold t)
color = c;
int[] state = new int[t/2 + 1];
state[0] = s;
public setState(int direction)
state[currentState] = direction;
currentState++;
public refresh()
currentState = 0;
public getState()
return state[currentState];
SINGLE_ADD = 0.25
3COMBO_MULT = 0.25;
XSIZE = 5
YSIZE = 6
//create 2d array mArr[XSIZE][YSIZE] <-- 5 rows 6 columns
for int i = 0; i < 30; i++
mArr[i/5][i%6] = new Orb(color, NIL, threshold)
ArrayList listOfCombos;
maxCombos = 0;
maxSequence = null;
for int i = 0; i < 30; i++
findPath(i/5, i%6, new arr[XSIZE][YSIZE], mArr[i/XSIZE][i%YSIZE])
for(arrSequence : listOfCombos)
arrSequence = mergeArrs(arrSequence);
//start again
combos = 0;
while(true)
int combos += countCombos(arrSequence); //gives blank map to fill in
if(!skyFall(arrSequence)) //means there are skyfalls
break;
if(combos > maxCombos)
maxSequence = arrSequence;
maxCombos = combos;
//DRIVER METHOD HERE
//we are assuming player does not move diagonally
//param: x & y are starting coordinates
findPath(y, x, arr, counter, prev)
//swapping w/ previous
Orb next = (arr[y][x] != null) ? arr[y][x] : mArr[y][x];
Orb temp = next;
swap(next, prev); <-- must do swap and then place the result into arr
Orb prev = temp;
if(counter == 0)
return;
if(y > 0) //can go up
setEitherArrayState(x, y-1, mArr, arr, DOWN)
findPath(x, y-1, arr, --counter, prev)
if(x > 0) //can go left
setEitherArrayState(x-1, y, mArr, arr, RIGHT);
findPath(x-1, y, arr, --counter, prev)
if(y < YSIZE)
setEitherArrayState(x, y+1, mArr, arr, UP);
findPath(x, y+1, arr, --counter, prev)
if(x < XSIZE)
setEitherArrayState(x+1, y, mArr, arr, LEFT);
findPath(x+1, y, arr, --counter)
listOfCombos.add(arr);//add this specific variation
//assistance function
setEitherArrayState(x, y, arrCtrl, arrBuild, direction)
if(arr[x][y] != null)//if it already modified
arrBuild[x][y].setState(direction);
else //never modified, we need to add
arrCtrl[x][y].setState(direction);
//assistance function
refreshBoard(arr)
for int i = 0; i < 30; i++
arr[i/5][i%6].refresh();
//creates board w/ new swaps
mergeArrs(arr)
new newArr [XSIZE][YSIZE];
for int i = 0; i < 30; i++
Orb mo = mArr[i/5][i%6]
Orb o = arr[i/5][i%6]
if(o == null)
newArr[i/5][i%6] = mo;
else
newArr[i/5][i%6] = o;
return newArr;
//returns # of combos in the board
countCombos(arr)
int combos = 0;
for int i = 0; i < 30
Orb o = arr[i/5][i%6] <-- not null?
int horCount = 0;
int verCount = 0;
//scan down
for(int j = 1; o.color = arr[i/5 + j][i%6].color; j++) horCount++;
//scan up
for(int j = -1; o.color = arr[i/5 + j][i%6].color; j--) horCount++;
for(int j = 1; o.color = arr[i/5 + j][%6].color; j++) verCount++;
for(int j = -1; o.color = arr[i/5 + j][%6].color; j--) verCount++;
if(Math.max(horCount, verCount) > 2)
combos++; //is combo?
removeComboFromBoard(i/5, i%6, arr);
return combos;
//need to remove combos so there's no duplicates
removeComboFromBoard(x,y,arr)
arr[x][y] == null;
if(x < XSIZE)
removeComboFromBoard(++x,y,arr);
if(x > 0)
removeComboFromBoard(--x,y,arr);
if(y < YSIZE)
removeComboFromBoard(x, ++y, arr);
if(y > 0)
removeComboFromBoard(x, --y, arr);
skyFall(arr)
boolean flag;
for int i = 29; i >= 0; i--
Orb orb = arr[i/5][i%6];
rowU = rowD = i/5;
counterD = 0;
if(arr[rowD+1][i%6] == null)//there's a gap
flag = true;//there are skyfalls
while(arr[rowD++][i%6] == null)
counterD++;
//swap all down
while(rowU != 0 && arr[rowU][i%6] != null)
swap(arr[rowU][i%6], arr[rowU + counterD][i%6]);
rowU--;
swap(Orb o1, Orb o2)
//swapping with nothing
if(o1 == null)
o1 = o2;
o2 = null;
return;
if(o2 == null)
o2 = o1;
o1 = null;
return;
//just swapping colors
temp = o1.color;
o1.color = o2.color;
o2.color = temp;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment