Skip to content

Instantly share code, notes, and snippets.

@nishidy
Created August 28, 2016 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nishidy/60b3b4a36356ea377592b835a2406e75 to your computer and use it in GitHub Desktop.
Save nishidy/60b3b4a36356ea377592b835a2406e75 to your computer and use it in GitHub Desktop.
Dynamic Programming for board game in Java
import java.util.*;
import org.apache.commons.codec.digest.DigestUtils;
public class BoardGame {
Random rnd;
List<StringBuilder> board;
int cols;
int holes;
List<Integer> hole_idx;
List<String> memo;
int hits;
public static void main(String argv[]){
BoardGame game = new BoardGame();
if(argv.length>0){
game.start_domino(argv);
}
}
void start_domino(String argv[]){
cols = Integer.parseInt(argv[0]);
rnd = new Random();
holes = rnd.nextInt((cols*cols+1)/2)*2;
memo = new ArrayList<>();
System.out.printf("%d x %d : # of holes(+) = %d\n",cols,cols,holes);
board = new ArrayList<StringBuilder>();
init_board();
hole_idx = new ArrayList<>();
make_hole_idx();
make_board();
print();
if(domino(0,0)){
System.out.printf("Completed. (# of hits = %d)\n",hits);
print();
}else{
System.out.printf("Not completed... (# of hits = %d)\n",hits);
}
}
void init_board(){
for(int i=0;i<cols;i++){
board.add(new StringBuilder(""));
for(int j=0;j<cols;j++){
board.get(i).append("_");
}
}
}
void make_hole_idx(){
int x;
while(hole_idx.size()<holes){
x = rnd.nextInt(cols*cols);
if(hole_idx.indexOf(x) == -1){
hole_idx.add(x);
}
}
}
void make_board(){
int x;
for(int i=0;i<hole_idx.size();i++){
x = hole_idx.get(i);
board.get(x/cols).replace(x%cols,x%cols+1,"+");
}
}
void print(){
for(int i=0;i<cols;i++){
for(int j=0;j<cols;j++){
System.out.print(board.get(i).charAt(j)+" ");
}
System.out.println();
}
System.out.println();
}
boolean complete(){
for(int i=0;i<cols;i++){
for(int j=0;j<cols;j++){
if(board.get(i).charAt(j) == '_'){
return false;
}
}
}
return true;
}
boolean check_redundancy(String hex){
for(int i=0;i<memo.size();i++){
//System.out.printf("%s <-> %s\n",hex,memo.get(i));
if( hex.equals( memo.get(i) ) ){
return true;
}
}
return false;
}
List<StringBuilder> get_copy(){
char c;
List<StringBuilder> copy = new ArrayList<>();;
for(int i=0;i<cols;i++){
copy.add(new StringBuilder());
for(int j=0;j<cols;j++){
c = board.get(i).charAt(j);
if(c == '^' ||
c == 'v' ||
c == '<' ||
c == '>' ){
copy.get(i).append(".");
}else{
copy.get(i).append(c);
}
}
}
return copy;
}
void append_memo(){
List<StringBuilder> copy = get_copy();
String hex = DigestUtils.md5Hex(String.join("",copy));
if(!check_redundancy(hex)){
//System.out.printf("%s appended to memo.\n",hex);
memo.add(hex);
}
}
boolean check_memo(){
List<StringBuilder> copy = get_copy();
String hex = DigestUtils.md5Hex(String.join("",copy));
return check_redundancy(hex);
}
boolean domino(int x, int y){
//print();
if(cols<=y){
y=0;
x+=1;
}
if(cols<=x){
return complete();
}
if(check_memo()){
hits += 1;
return false;
}
if(board.get(x).charAt(y) == '_'){
if(x+1<cols && board.get(x+1).charAt(y) == '_'){
board.get(x).replace(y,y+1,"^");
board.get(x+1).replace(y,y+1,"v");
if(domino(x,y+1)){
return true;
}else{
append_memo();
board.get(x).replace(y,y+1,"_");
board.get(x+1).replace(y,y+1,"_");
}
}
if(y+1<cols && board.get(x).charAt(y+1) == '_'){
board.get(x).replace(y,y+1,"<");
board.get(x).replace(y+1,y+2,">");
if(domino(x,y+2)){
return true;
}else{
append_memo();
board.get(x).replace(y,y+1,"_");
board.get(x).replace(y+1,y+2,"_");
}
}
return false;
}
return domino(x,y+1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment