rendered paste bodyimport java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Random;
import java.util.Scanner;
/*
* GeneratePuzzle.java
*
* Version:
* $Id: GeneratePuzzle.java,v 1.2 2007/04/02 01:13:37 djm5320 Exp $
*
* Revisions:
* $Log: GeneratePuzzle.java,v $
* Revision 1.2 2007/04/02 01:13:37 djm5320
* second revision
*
* Revision 1.1 2007/04/01 23:41:18 djm5320
* inititial revision
*
*/
/**
* @author danielmoore
*
*/
public class GeneratePuzzle {
/**
* [Insert class description here]
*
* @author danielmoore
*/
private Puzzle NewPuzzle;
private char[][] puzzle;
private String wordlist;
public GeneratePuzzle(String savename, String listname, int row, int col){
NewPuzzle = new Puzzle(row, col);
System.out.println("Puzzle Created");
puzzle = NewPuzzle.getPuzzle();
wordlist = wordlist(listname);
System.out.println("Wordlist Created");
System.out.println(wordlist);
generator(wordlist);
System.out.println("Puzzle Generated");
write(savename);
System.out.println("Puzzle Written");
System.out.println("Time to smoke pot");
}
public GeneratePuzzle(String savename, int row, int col){
NewPuzzle = new Puzzle(row, col);
puzzle = NewPuzzle.getPuzzle();
wordlist = wordlist();
generator(wordlist);
write(savename);
}
public String wordlist(String listname){
String list = null;
RandomAccessFile file;
try {
file = new RandomAccessFile(listname, "r");
byte[] array = new byte[(int) file.length()];
file.read(array);
list = new String(array);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
public String wordlist(){
String list = "";
Scanner sc = new Scanner(System.in);
System.out.println("Please enter your word list:");
while(sc.hasNext()){
list+=sc.next();
}
return list;
}
public void generator(String wordlist){
System.out.println("generator()");
String word;
Scanner sc = new Scanner(wordlist);
while(sc.hasNext()){
word = sc.next();
System.out.println("word: "+ word);
place(word);
}
}
public void place(String string){
System.out.println("place()");
Random num = new Random();
String word = string;
int count = word.length();
int x = num.nextInt(puzzle.length);
int y = num.nextInt(puzzle[0].length);
while(puzzle[x][y] !='ø'){
x = num.nextInt(puzzle.length-1);
y = num.nextInt(puzzle[0].length-1);
}
int[] dir = direction();
int incX = dir[0];
int incY = dir[1];
while(check(y, x, incX, incY, word) == false){
dir = direction();
incY = dir[1];
incX = dir[0];
}
if(check(y,x,incX, incY, word)==true){
for(int i = 0; i < word.length(); i++){
puzzle[x][y] = word.charAt(i);
x = (x+incX+puzzle.length)%puzzle.length;
y = (y+incY+puzzle[0].length)%puzzle[0].length;
}
System.out.println("placed");
}
}
public int[] direction(){
System.out.println("direction()");
int[] dir = new int[2];
Random num = new Random();
int rand = num.nextInt(8);
switch (rand){
case 0:
dir[0] = -1;
dir[1] = 0;
break;
case 1:
dir[0] = -1;
dir[1] = 1;
break;
case 2:
dir[0] = 0;
dir[1] = 1;
break;
case 3:
dir[0] = 1;
dir[1] = 1;
break;
case 4:
dir[0] = 1;
dir[1] = 0;
break;
case 5:
dir[0] = 1;
dir[1] = -1;
break;
case 6:
dir[0] = 0;
dir[1] = -1;
break;
case 7:
dir[0] = -1;
dir[1] = -1;
break;
}
return dir;
}
public boolean check(int col, int row, int x, int y, String z){
System.out.println("check()");
boolean rEturn = false;
String word = z;
int incCol = x;
int incRow = y;
int COL = col;
int ROW = row;
if(incCol == 0 || incRow == 0){
if(word.length() > puzzle.length || word.length() > puzzle[0].length){
rEturn = false;
}
else{
int wordLength = word.length();
while(puzzle[ROW][COL] == 'ø' && wordLength>0){
ROW = (ROW+incRow+puzzle.length)%puzzle.length;
COL = (COL+incCol+puzzle[0].length)%puzzle[0].length;
wordLength--;
}
if(wordLength==0){
rEturn = true;
}
}
}
return rEturn;
}
public String toString(){
System.out.println("toSring()");
Random num = new Random();
String string = ""+puzzle.length+"\n"+puzzle[0].length+"\n";
for(int h = 0; h < puzzle[0].length; h++){
for(int i = 0; i < puzzle.length; i++){
if(puzzle[i][h]=='ø'){
puzzle[i][h] = (char) ('A'+num.nextInt(25));
}
}
}
for(int h = 0; h < puzzle[0].length; h++){
for(int i = 0; i < puzzle.length; i++){
string += puzzle[i][h];
}
string += "\n";
}
System.out.println(string);
string.toUpperCase();
return string;
}
public void write(String savename){
System.out.println("write()");
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(savename+".pz"));
writer.write(toString());
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]){
if(args.length == 3){
String savename = args[0];
int row = Integer.parseInt(args[1]);
int col = Integer.parseInt(args[2]);
GeneratePuzzle puzzle = new GeneratePuzzle(savename, col, row);
}
else if(args.length == 4){
String savename = args[0];
String listname = args[3];
int row = Integer.parseInt(args[1]);
int col = Integer.parseInt(args[2]);
GeneratePuzzle puzzle = new GeneratePuzzle(savename, listname, col, row);
}
else{
System.err.println("Usage: sage: java GeneratePuzzle puzzle-name number-rows number-columns [wordfile-name]");
}
}
}