<?php
/* Mike's Tetrad Class in PHP
Copyright (c) 2011 Michael Billington <michael.billington@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */
class cls_tetrad {
/* Mike's general class for holding tetrads.
Can store, rotate, and display tetrads as HTML.
See http://mike.bitrevision.com/tetrad for more details. */
public $shape = array();
public $width = 0;
public $height = 0;
public $type = "";
public $exists = false;
public function initialise($value, $type) {
$this->shape = $value;
$this->type = $type;
$width = 0;
$height = 0;
// Count height and width (required to rotate)
foreach($this->shape as $row) {
foreach($row as $col) {
if($this->height == 0) { $this->width++; };
}
$this->height++;
}
$exists = true;
}
public function show() {
/* Output HTML table (styled by tetrad.css) */
$str = "<!-- ".$this->type." block -->\n";
$str .= "<table class=\"tetrad\">\n";
foreach($this->shape as $row) {
$str .= " <tr>\n ";
foreach($row as $col) {
if((int)$col == 0) {
$str .= "<td></td>";
} else {
$str .= "<td class=\"".$this->type."-block\"></td>";
}
}
$str .= "\n </tr>\n";
}
$str .= "</table>\n";
return $str;
}
public function rotate() {
/* Rotate the tetrad 90 degrees anti-clockwise */
$temp_shape = array();
$count_rows = 0;
for($i = $this->width-1; $i>=0; $i -= 1) {
$temp_shape[$count_rows] = $this->get_column($i);
$count_rows++;
}
$this->shape = $temp_shape;
}
function get_column($num) {
/* Returns a column of tetrad, used when rotating (columns are mapped to rows) */
$temp_col = array();
$num_found = 0;
$tick = 0;
foreach($this->shape as $row) {
foreach($row as $col) {
if($tick==$num) {
$temp_col[$num_found] = $col;
$num_found++;
break;
}
$tick++;
}
$tick = 0;
}
return $temp_col;
}
function keypad_follow($num) {
/* Follow tetrad shape on the keypad and return the numbers hit (if the shape doesn't go off the edge)
It reminds me of using the Windows32 BitBlt function somehow.
*/
$keypad = array(array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9),
array(-1,0,-1));
$found = false;
$num_count = 0;
$num_list = array();
$dest_x_start = 0;
$dest_y_start = 0;
$dest_width = 3;
$dest_height = 4;
foreach($keypad as $row) {
foreach($row as $col) {
if($col == $num) { $found = true; break;}
if($dest_y_start == 0) { $dest_x_start ++;}
}
if($found) { break; }
$dest_y_start ++;
}
if(!$found) {
return false;
}
$source_y = 0;
$source_x = 0;
foreach($this->shape as $row) {
foreach($row as $col) {
if($source_x + $dest_x_start >= $dest_width || $source_y + $dest_y_start >= $dest_height) {
/* Off-the-edge check failed */
return false;
}
if((int)$col == 1) {
$num_list[$num_count] = $keypad[$source_y + $dest_y_start][$source_x + $dest_x_start];
if((int)$num_list[$num_count] < 0) { return false; }
$num_count++;
}
$source_x ++;
}
/* Reset X */
$source_x = 0;
$source_y ++;
}
$ret_val = "";
foreach($num_list as $num) {
$ret_val .= $num;
}
return $ret_val;
}
}
function define_tetrads() {
/* This function will populate the $tetrad variable with all 19 possible tetrads */
global $tetrad;
for($i = 1; $i<=19; $i++) {
$tetrad[$i] = new cls_tetrad();
}
/* Box (1 orientation) */
$tetrad[1]->initialise( array( array(1, 1),
array(1, 1) ), "b");
/* Z Block (2 orientations) */
$tetrad[2]->initialise( array( array(1, 1, 0),
array(0, 1, 1) ), "z");
$tetrad[3]->initialise($tetrad[2]->shape, "z");
$tetrad[3]->rotate();
/* S Block (2 orientations) */
$tetrad[4]->initialise( array( array(0, 1, 1),
array(1, 1, 0) ), "s");
$tetrad[5]->initialise($tetrad[4]->shape, "s");
$tetrad[5]->rotate();
/* L Block (4 orientations) */
$tetrad[6]->initialise( array( array(1, 1, 1),
array(1, 0, 0) ), "l");
$tetrad[7]->initialise($tetrad[6]->shape, "l");
$tetrad[7]->rotate();
$tetrad[8]->initialise($tetrad[7]->shape, "l");
$tetrad[8]->rotate();
$tetrad[9]->initialise($tetrad[8]->shape, "l");
$tetrad[9]->rotate();
/* J Block (4 orientations) */
$tetrad[10]->initialise( array( array(1, 1, 1),
array(0, 0, 1) ), "j");
$tetrad[11]->initialise($tetrad[10]->shape, "j");
$tetrad[11]->rotate();
$tetrad[12]->initialise($tetrad[11]->shape, "j");
$tetrad[12]->rotate();
$tetrad[13]->initialise($tetrad[12]->shape, "j");
$tetrad[13]->rotate();
/* I Block (2 orientations) */
$tetrad[14]->initialise( array( array(1, 1, 1, 1) ), "i");
$tetrad[15]->initialise( $tetrad[14]->shape, "i");
$tetrad[15]->rotate();
/* T Block (4 orientations) */
$tetrad[16]->initialise( array( array(0, 1, 0),
array(1, 1, 1) ), "t");
$tetrad[17]->initialise( $tetrad[16]->shape, "t");
$tetrad[17]->rotate();
$tetrad[18]->initialise( $tetrad[17]->shape, "t");
$tetrad[18]->rotate();
$tetrad[19]->initialise( $tetrad[18]->shape, "t");
$tetrad[19]->rotate();
return true;
}
/* Board filler scripts below */
function fill_board() {
/* Pack random pieces in the board */
global $tetrad, $board;
while(1) {
$t = $tetrad[rand(1, count($tetrad))];
if(!$board -> place($t)) {
return true;
}
}
return false;
}
class c_tet_board {
/* Board of tetrads */
public $x_width = 0;
public $y_height = 0;
public $place_start = 0;
public $shape;
function initialise($y_height, $x_width) {
$this -> x_width = $x_width;
$this -> y_height = $y_height;
$this -> shape = Array();
for($y = 0; $y < ($this -> y_height); $y++) {
for($x = 0; $x < ($this -> x_width); $x++) {
$this -> shape[$x][$y] = "";
}
}
$this -> place_start = 0;
$this -> count = 0;
}
function place($shape) {
for($y = $this -> place_start; $y < ($this -> y_height); $y++) {
for($x = 0; $x < ($this -> x_width); $x++) {
if($this -> canplace($shape, $x, $y)) {
$this -> writeto($shape, $x, $y);
return true;
}
}
$this -> place_start = $y;
}
return false;
}
function canplace($shape, $x, $y) {
$shape -> width;
$x_ret = $x;
foreach($shape -> shape as $row) {
$x = $x_ret;
foreach($row as $cell) {
if(!isset($this -> shape[$x][$y]) || (($this -> shape[$x][$y] != "") && (int)$cell != 0)) {
return false;
}
$x++;
}
$y++;
}
return true;
}
function writeto($shape, $x, $y) {
$shape -> width;
$x_ret = $x;
foreach($shape -> shape as $row) {
$x = $x_ret;
foreach($row as $cell) {
if(($this -> shape[$x][$y] == "") && (int)$cell != 0) {
$this -> shape[$x][$y] = $shape -> type;
}
$x++;
}
$y++;
}
return true;
}
public function show() {
/* Output HTML table (styled by tetrad.css) */
$str = "<!-- Table of tetrads -->\n";
$str .= "<table class=\"tetrad\">\n";
foreach($this-> shape as $row) {
$str .= " <tr>\n ";
foreach($row as $col) {
if($col == "") {
$str .= "<td></td>";
} else {
$str .= "<td class=\"".$col."-block\"></td>";
}
}
$str .= "\n </tr>\n";
}
$str .= "</table>\n";
return $str;
}
}
?>