{"id":437,"date":"2010-01-04T14:10:05","date_gmt":"2010-01-04T14:10:05","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=437"},"modified":"2010-01-11T09:32:29","modified_gmt":"2010-01-11T09:32:29","slug":"wordsearch-php-grid-class","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/04\/wordsearch-php-grid-class\/","title":{"rendered":"Wordsearch php &#8211; grid class"},"content":{"rendered":"<p>From the post on the projects page, <a href=\"http:\/\/www.codingfriends.com\/index.php\/projects\/word-search\">wordsearch<\/a>, here is a basic design of a grid class that can be used to link together with the other aspects of a wordsearch game.  This will be able to create a basic grid and populate it with characters.<\/p>\n<p>The reason why I created the createGridRow function was because this class could be inherited and thus alter any aspects of the class to suit the future needs.<\/p>\n<p>The <\/p>\n<pre lang=\"php\">$this-><\/pre>\n<p> within the code references the local variables of the class and not any other variables e.g. local to that function.<\/p>\n<p>The <\/p>\n<pre lang=\"php\">chr(rand(0,25) + 97);<\/pre>\n<p> means create a chr (character) from a random generated value within a range of 0 to 25 (26 letters in the English alphabet) and then add the value 97 to it, because the integer value of 97 &#8211; 125 are the ascii codes for a-z characters.<\/p>\n<pre lang=\"php\">\r\n<?php\r\nclass Grid {\r\n  \r\n  \/* private section of the class grid *\/\r\n\r\n  \/\/ store the size and the grid\r\n    private $_size = 0;\r\n    private $_grid;\r\n      \r\n\r\n  \/* the public section of the class grid *\/\r\n  \r\n    \/\/ construct the grid with a given size\r\n    public function __construct($size=0) \r\n    { \r\n      \/\/ set the private variables\r\n      $this->_size = $size; \r\n      $this->_grid = array();\r\n      for ($i =1; $i <= $this->_size; $i++)\r\n      {\r\n\t\/\/ create each row of the grid with the class function createGridRow\r\n\t$this->_grid[$i] = $this->createGridRow();\r\n      }\r\n    }\r\n    \r\n    \/\/ delete the grid \r\n    public function __destruct()\r\n    {\r\n      \/\/ clear up the grid, each line and then the whole object.\r\n      for ($i = 1; $i <= $this->_size; $i++)\r\n      {\r\n\tunset($this->_grid[$i]);\r\n      }\r\n      unset($this->_grid);\r\n    }\r\n    \r\n    \/* createGridRow, this can be inherited and thus altered to create any type of character in the grid *\/\r\n    public function createGridRow()\r\n    {\r\n\t$_grid_insert = array();\r\n\tfor ($j = 1; $j <= $this->_size; $j++)\r\n\t{\r\n\t  $_grid_insert[$j] = chr(rand(0,25) + 97);\r\n\t}\r\n\treturn $_grid_insert;\r\n    }\r\n    \r\n    \/* basic print out of a grid *\/\r\n    public function printOut()\r\n    {\r\n      echo \"The grid<br\/>\";\r\n      for ($i = 1; $i <= $this->_size; $i++)\r\n      {\r\n\techo \"$i .. \";\r\n\tfor ($j = 1; $j <= $this->_size; $j++)\r\n\t{\r\n\t    echo \" $j = \".$this->_grid[$i][$j];\r\n\t}\r\n\techo \"<br\/>\";\r\n      }\r\n    }\r\n\r\n    \/* return the size of the grid *\/\r\n    public function returnSize()\r\n    {\r\n      return $this->_size;\r\n    }\r\n    \r\n    \/* insert a character into the grid at position X, Y and the character *\/\r\n    public function insertCharIntoGrid($pX, $pY, $char)\r\n    {\r\n\t$this->_grid[$pY][$pX] = $char;\r\n    }\r\n    \r\n    \/* return the grid reference *\/\r\n    public function returnGrid()\r\n    {\r\n      return $this->_grid;\r\n    }\r\n}\r\n$wordsearch_grid = new Grid(6);\r\n$wordsearch_grid->printOut();\r\n?>\r\n<\/pre>\n<p>The output of the code as above is<\/p>\n<pre lang=\"html\">\r\nThe grid\r\n1 .. 1 = l 2 = l 3 = z 4 = m 5 = h 6 = m\r\n2 .. 1 = u 2 = r 3 = z 4 = s 5 = k 6 = y\r\n3 .. 1 = f 2 = k 3 = j 4 = n 5 = k 6 = p\r\n4 .. 1 = h 2 = p 3 = x 4 = o 5 = c 6 = q\r\n5 .. 1 = s 2 = s 3 = x 4 = r 5 = u 6 = v\r\n6 .. 1 = r 2 = f 3 = g 4 = q 5 = s 6 = o\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>From the post on the projects page, wordsearch, here is a basic design of a grid class that can be used to link together with the other aspects of a wordsearch game. This will be able to create a basic grid and populate it with characters. The reason why I created the createGridRow function was &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/04\/wordsearch-php-grid-class\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Wordsearch php &#8211; grid class<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[47,46,45],"class_list":["post-437","post","type-post","status-publish","format-standard","hentry","category-php","tag-class","tag-grid","tag-wordsearch"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=437"}],"version-history":[{"count":8,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/437\/revisions"}],"predecessor-version":[{"id":442,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/437\/revisions\/442"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}