Word Search Puzzle Class
(PHP 7.0 Version)

Description

This class is used to make word search puzzles with PHP.

Usage Rights

By permission only.

Author

Andrew William Nicholson
E-Mail: awn2be@gmail.com
Phone: +1-720-763-5281
Home Page: nicholson.design

Disclaimer

I may have violated an OOP suggestion that states I should seperate getters and setters. In my defense, the PHP language is already "loose" and I would rather work that way. This might be implemented in a better way... but I do not know how at this time.


Class Usage

Instantiate a Puzzle Instance

Syntax:

require 'word_search.php';

$puzzle = new word_search();

View Default Options

Syntax:

$puzzle->optTitle;

$puzzle->optSize;

Output:

Title: Your Word Search

Size: 14x14

Set Options: Method 1

Syntax:

$puzzle->optTitle('Title II');

$puzzle->optSize('24x24');

$puzzle->optDirections('n, s');

Output:

Title: Title II

Size: 24x24

Directions:

array (
  'n' => 
  array (
    0 => 0,
    1 => -1,
  ),
  's' => 
  array (
    0 => 0,
    1 => 1,
  ),
)

Set Options - Method 2

Syntax:

$puzzle->optTitle = 'Title III';

$puzzle->optSize = '34x34';

*$puzzle->optDirections('ne, sw');

Output:

Title: Title III

Size: 34x34

Directions:

array (
  'ne' => 
  array (
    0 => 1,
    1 => -1,
  ),
  'sw' => 
  array (
    0 => -1,
    1 => 1,
  ),
)

Create Play Board via optSize Property

Syntax:

$puzzle->optSize('4x4');

$puzzle->play_board_create();

$puzzle->play_board_display();

Output:

Size: 4x4

Play Board Array:

array (
  0 => 
  array (
    0 => '',
    1 => '',
    2 => '',
    3 => '',
  ),
  1 => 
  array (
    0 => '',
    1 => '',
    2 => '',
    3 => '',
  ),
  2 => 
  array (
    0 => '',
    1 => '',
    2 => '',
    3 => '',
  ),
  3 => 
  array (
    0 => '',
    1 => '',
    2 => '',
    3 => '',
  ),
)

Play Board Shape:

****
****
****
****

Create Play Board via optShape Property

Syntax:

$shape =
   '  *  ' . "\n" .
   ' *** ' . "\n" .
   '*****';

$puzzle->play_board_create($shape);

$puzzle->play_board_display();

Output:

Play Board Array:

array (
  0 => 
  array (
    0 => NULL,
    1 => NULL,
    2 => '',
    3 => NULL,
    4 => NULL,
  ),
  1 => 
  array (
    0 => NULL,
    1 => '',
    2 => '',
    3 => '',
    4 => NULL,
  ),
  2 => 
  array (
    0 => '',
    1 => '',
    2 => '',
    3 => '',
    4 => '',
  ),
)

Play Board Shape:

  *  
 *** 
*****

optWords

Description:

Word List Set-Up

Syntax:

$word_array = [
   'Word_01', 
   'Word_02', 
   'Word_03'  
];            

$puzzle->optWords($word_array);

Output:

Word Array:

array (
  0 => 
  array (
    0 => 0,
    1 => 'Word_01',
  ),
  1 => 
  array (
    0 => 1,
    1 => 'Word_02',
  ),
  2 => 
  array (
    0 => 2,
    1 => 'Word_03',
  ),
)

This method accepts GET & POST Input:

$_POST[ 'array_name' ]
$_GET [ 'array_name' ]

The original word array is maintained in:

$puzzles->WordLists['OrderIn']

Other word lists include:

$puzzles->WordLists['Placed' ]
$puzzles->WordLists['Remains']
$puzzles->WordLists['Answers']

Answers are now maintained in $puzzles->WordLists['Placed']

SortWords

Description:

Sort a word list for pre-placement purposes or for post-placement display.

Syntax:

$puzzle->SortWords( $optSort, $Word_List )

Valid Options for $optSort:

Order_In
AlphaNumeric_ASC
AlphaNumeric_DESC
Length_ASC
Length_DESC

Valid Options for $Word_List:

$puzzle->WordLists['OrderIn']
$puzzle->WordLists['Placed']
$puzzle->WordLists['Remains']

Caveats:

The AlphaNumeric_ASC and AlphaNumeric_DESC sort orders currently rely on the Linux OS locale setting of 'en_US.UTF-8'. When not available, a warning is issued. This can be an issue if the PHP code is run on a Windows Server.

Note:

Click here to view a usage sample.

optMatch

Description:

Toggles letter matching. When turned on, all words are placed by attempting to find a matching letter in the puzzle.

Syntax:

$puzzle->optMatch($input)

PHP Input & Resulting Output:

$puzzle->optMatch(true) = true

$puzzle->optMatch(false) = false

MySQL Input & Resulting Output:

$puzzle->optMatch(1) = true

$puzzle->optMatch(0) = false

Web Form Input & Resulting Output:

$puzzle->optMatch('true') = true

$puzzle->optMatch('TRUE') = true

$puzzle->optMatch('TrUe') = true


$puzzle->optMatch('false') = false

$puzzle->optMatch('FALSE') = false

$puzzle->optMatch('FaLsE') = false

Unexpected Input & Resulting Output

$puzzle->optMatch( -1 ) = true

$puzzle->optMatch( NULL ) = true

$puzzle->optMatch( array() ) = true

$puzzle->optMatch( [ 1, 2, 3, 'etc' ] ) = true

$puzzle->optMatch( ) = true

$puzzle->optMatch( '' ) = true

$puzzle->optMatch( ' ' ) = true

$puzzle->optMatch( 2 ) = true

$puzzle->optMatch( '1' ) = true

$puzzle->optMatch( '0' ) = true

$puzzle->optMatch( '-1' ) = true

$puzzle->optMatch( 'php' ) = true

optMatchLimit

Description:

Limits the number of times a word letter may overlay an existing puzzle letter.

Syntax:

$puzzle->optMatchLimit($numerical_input)

optFirstWord

Description:

Determines where the first word is placed and in what direction.
Caveats: Issues exist when puzzle is even and word is even. This needs to be looked into. Also, this does not apply to shaped puzzles... I have no idea if there is a center to the puzzle... Example shape... a donut.

Syntax:

$puzzle->optFirstWord($option)

Valid Options:

'C_D' - Center Diagnol
'C_P_D' - Center with specific direction(s)
'A_P_D' - Anywhere with specific direction(s)

Creating the Puzzle

Method 1: $puzzle->place_word_all()

Syntax:

require 'word_search.php';

$puzzle = new word_search();

$puzzle->optSize('12x12');

$puzzle->play_board_create();

$puzzle->optWords(
      [
         'Abra', 
         'Cadabra', 
         'Hocus',
         'Pocus',
         'Open',
         'Sesame',
         'Shazam'
      ]
);

$puzzle->optDirections('e, s');

$puzzle->optMatch(true);

$puzzle->place_word_all();

$puzzle->puzzle_display();

Output:

**********O*
*P********p*
Hocus***Open
oc**S**A**n*
cu**e*b**S**
us**sr***h**
s**Cadabra**
****m****z**
****e****a**
*********m**
************
************

Additional Puzzle Options:

Syntax

$puzzle->put_word_anywhere( $array_ref, $word )

$puzzle->place_word_all()

$puzzle->optWordDisplayFormat()

$puzzle->optFill()