Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by Justin on 7/09/2017.
- */
- import java.util.*;
- import java.util.Scanner;
- import java.util.Arrays;
- public class WordSearch {
- private static int boardBounds;
- private static char[][] board;
- private static char[][] solutionBoard;
- public static void printIntro() {
- System.out.println("Welcome to a Word Search Generator created by Justin!");
- System.out.println("You will be able to choose your size, and create a word search for yourself");
- System.out.println("Please review the options below and have fun!");
- System.out.println("");
- System.out.println("Type in (g) to generate a new word search");
- System.out.println("Type in (p) to print out the word search to console");
- System.out.println("Type in (s) to show the solution to your word search");
- System.out.println("Type in (q) to quit the program :-(");
- }
- public static void inputFromUser() {
- Scanner sc = new Scanner(System.in);
- switch (sc.next()) {
- case "g":
- generate();
- break;
- case "p":
- print();
- break;
- case "s":
- //showSolution();
- break;
- case "q":
- System.exit(0);
- }
- }
- public static void generate() {
- int genWords;
- Scanner genScanner = new Scanner(System.in);
- System.out.println("How many words would you like to be placed into the search?");
- genWords = genScanner.nextInt();
- System.out.println("Please input the words separated by commas (i.e. 'java, cats, beer, cheese, burgers')' ");
- genScanner.nextLine();
- String[] wordInput = genScanner.nextLine().replace(" ", "").split(",");
- while (wordInput.length != genWords) {
- System.out.println("You must input " + genWords + " words");
- wordInput = genScanner.nextLine().replace(" ", "").split(",");
- }
- boardBounds = getGenSize(wordInput);
- initBoard(boardBounds);
- solutionBoard = new char[boardBounds][boardBounds];
- for (String words : wordInput) {
- Random random = new Random();
- int picker = random.nextInt(3);
- for (int i = 0; i < 3; i++) {
- switch ((i + picker) % 3) {
- case 1:
- isVertical(words);
- break;
- case 2:
- isHorizontal(words);
- break;
- case 3:
- isDiagonal(words);
- break;
- }
- }
- }
- for (int i = 0; i < boardBounds; i++) {
- for (int j = 0; j < boardBounds; j++) {
- if (solutionBoard[i][j] != 0) board[i][j] = solutionBoard[i][j];
- }
- }
- }
- public static void isVertical(String words) {
- int tries = 0;
- tries++;
- while (tries < 100) {
- Random r = new Random();
- int i = r.nextInt(boardBounds - words.length() + 1);
- int j = r.nextInt(boardBounds - words.length() + 1);
- if (checkDown(i, j, words)) {
- for (int k = 0; k < words.length(); k++) {
- solutionBoard[i][j + k] = words.charAt(k);
- }
- }
- }
- }
- public static void isHorizontal(String words) {
- int tries = 0;
- tries++;
- while (tries < 100) {
- Random r = new Random();
- int i = r.nextInt(boardBounds - words.length() + 1);
- int j = r.nextInt(boardBounds - words.length() + 1);
- if (checkRight(i, j, words)) {
- for (int k = 0; k < words.length(); k++) {
- solutionBoard[i + k][j] = words.charAt(k);
- }
- }
- }
- }
- public static void isDiagonal(String words) {
- int tries = 0;
- tries++;
- while (tries < 100) {
- Random r = new Random();
- int i = r.nextInt(boardBounds - words.length() + 1);
- int j = r.nextInt(boardBounds - words.length() + 1);
- if (checkRight(i, j, words)) {
- for (int k = 0; k < words.length(); k++) {
- solutionBoard[i + k][j + k] = words.charAt(k);
- }
- }
- }
- }
- public static int getGenSize(String[] words) {
- int longWord = 0;
- for (String word : words)
- if (word.length() > longWord)
- longWord = word.length();
- return longWord;
- }
- public static void initBoard(int bounds) {
- Random random = new Random();
- board = new char[bounds][bounds];
- for (int i = 0; i < bounds; i++) {
- for (int j = 0; j < bounds; j++) {
- board[i][j] = (char) (random.nextInt(26) + 'a');
- }
- }
- }
- public static void print() {
- if (boardBounds == 0) {
- System.out.println("Must generate board first");
- System.out.println();
- printIntro();
- inputFromUser();
- }
- for (int i = 0; i < boardBounds; i++) {
- for (int j = 0; j < boardBounds; j++) {
- System.out.print(board[i][j] + " ");
- }
- }
- }
- public static boolean checkDown(int i, int j, String words) {
- if (j + words.length() < j + boardBounds) {
- return false;
- }
- for (int k = 0; k < words.length(); k++) {
- if (solutionBoard[i][j + k] != words.charAt(k) && solutionBoard[i][j + k] !=' ') {
- return false;
- }
- }
- return true;
- }
- public static boolean checkRight(int i, int j, String words) {
- if (i + words.length() < i + boardBounds) {
- return false;
- }
- for (int k = 0; k < words.length(); k++) {
- if (solutionBoard[i + k][j] != words.charAt(k) && solutionBoard[i + k][j] !=' ') {
- return false;
- }
- }
- return true;
- }
- public static boolean checkDiagonal(int i, int j, String words) {
- if (j + words.length() < j + boardBounds || i + words.length() < i + words.length()){
- return false;
- }
- for (int k = 0; k < words.length(); k++) {
- if (solutionBoard[i + k][j + k] != words.charAt(k) && solutionBoard[i + k][j + k] !=' ') {
- return false;
- }
- }
- return true;
- }
- public static void main(String[] args) {
- printIntro();
- inputFromUser();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement