I made a 2 dimensional array which checks if 3 of the same values are equal to each other.
My problem is that when the code detects 3 in a row it changes values, but it doesn't update to the original array ($aww_ar).
Is there a way to do this?
Code:
<?php
$aww_ar = array();
echo"<table>";
for($y = 0; $y < 6; $y++){
echo"<tr>";
for($x = 0; $x < 6; $x++){
$randomise = rand(1,4);
$aww_ar[$x][$y] = "<td>".$randomise."</td>";
echo $aww_ar[$x][$y];
}
echo"</tr>";
}
echo"</table>";
$counter = 0;
for ($h=0; $h < count($aww_ar); $h++) {
for ($u=0; $u < count($aww_ar[0]); $u++) {
if($aww_ar[$h][$u] == $aww_ar[$h][$u-1] && $aww_ar[$h][$u] == $aww_ar[$h][$u+1]){
$counter++;
$aww_ar[$h][$u] = 'X';
$aww_ar[$h][$u+1] = 'X';
$aww_ar[$h][$u-1] = 'X';
echo $aww_ar[$h][$u];
echo $aww_ar[$h][$u+1];
echo $aww_ar[$h][$u-1];
}
}
}
echo $counter;
It might be related to Passing By Reference
for ($h = 0; $h < count($aww_ar); $h++) {
for ($u = 0; $u < count($aww_ar[0]); $u++) {
if ($aww_ar[$h][$u] == $aww_ar[$h][$u - 1] && $aww_ar[$h][$u] == $aww_ar[$h][$u + 1]) {
$aww_ar[$h][$u] = 'X';
$aww_ar[$h][$u + 1] = &$aww_ar[$h][$u];
$aww_ar[$h][$u - 1] = &$aww_ar[$h][$u];
echo $aww_ar[$h][$u];
echo $aww_ar[$h][$u + 1];
echo $aww_ar[$h][$u - 1];
}
}
}
// Write this in simplified form use "foreach"
foreach ($aww_ar as $key=>&$value) {
foreach ($aww_ar[0] as $k=>$v) {
if ($k > 0 && $aww_ar[$key][$k] == $aww_ar[$key][$k - 1] && $aww_ar[$key][$k] == $aww_ar[$key][$k + 1]) {
$aww_ar[$key][$k] = 'X1';
$aww_ar[$key][$k + 1] = 'X1';
$aww_ar[$key][$k - 1] = 'X1';
echo $aww_ar[$key][$k];
echo $aww_ar[$key][$k + 1];
echo $aww_ar[$key][$k - 1];
}
}
}
Ive worked it out a bit with the help of Lopes Wang
<?php
$aww_ar = array();
// Create array
for($y = 0; $y < 9; $y++){
for($x = 0; $x < 9; $x++){
$randomise = rand(1,4);
$aww_ar[$x][$y] = "<td>".$randomise."</td>";
}
}
// Detects matches and marks them with X
foreach ($aww_ar as $key=>&$value) {
foreach ($aww_ar[0] as $k=>$v) {
if ($k > 0 && $aww_ar[$key][$k] == $aww_ar[$key][$k - 1] && $aww_ar[$key][$k] == $aww_ar[$key][$k + 1]) {
$aww_ar[$key][$k] = 'X';
$aww_ar[$key][$k + 1] = 'X';
$aww_ar[$key][$k - 1] = 'X';
}
}
}
echo"<table border='2px;'>";
for($y = 0; $y < 9; $y++){
echo"<tr>";
for($x = 0; $x < 9; $x++){
echo "<td>" .$aww_ar[$x][$y]."</td>";
}
echo"</tr>";
}
echo"</table>";
Related
Currently working on a project like bejuweld (candy crush type)
I've got a 2 dimensional table generated which contains 0 matches of 3 in a row. currently i want to click any of the numbers inside the table. I've got it clickable but I don't know how to make it an individual position.
When you click on the 4th row and 5th column I want it to output an int which says 45.
This is the part where it is about:
// Create the playing field
echo"<table border='2px;'>";
for($y = 0; $y < $th; $y++){
echo"<tr>";
for($x = 0; $x < $tw; $x++){
echo "<td><a href='#'>" .$aww_ar[$x][$y]."</a></td>";
}
echo"</tr>";
}
echo"</table>";
Which looks like this->
Inside the TD i made a href, I want the href to output the current position it's in.
I've Tried to do this ->
But it outputs the current value of position.
// Create the playing field
echo"<table border='2px;'>";
for($y = 0; $y < $th; $y++){
echo"<tr>";
for($x = 0; $x < $tw; $x++){
echo "<td><a href='".$aww_ar[$x][$y]."'>" .$aww_ar[$x][$y]."</a></td>"; // <---
}
echo"</tr>";
}
echo"</table>";
Here's all of the code ->
<?php
error_reporting('0');
echo("<link rel='stylesheet' type='text/css' href='./stylesheet.css'>");
$aww_ar = array();
// SELECT PLAYING FIELD VALUES
$th = 10;
$tw = 10;
// Amount of numbers
$amount = 5;
// Create array
for($y = 0; $y < $th; $y++){
for($x = 0; $x < $tw; $x++){
$randomise = rand(1,$amount);
$aww_ar[$x][$y] = $randomise ;
}
}
$numb = 0;
$match = true;
$counting = 0;
while ($match){
// Deletes matches and replaces with the value above
foreach ($aww_ar as $key=>&$value) {
foreach ($aww_ar[0] as $k=>$v) {
if($aww_ar[$key][$k] == 'X'){
$aww_ar[$key][$k] = $aww_ar[$key ][$k - 1];
}
}
}
// Fills in empty space
foreach ($aww_ar as $key=>&$value) {
foreach ($aww_ar[0] as $k=>$v) {
if($aww_ar[$key][$k] == ""){
$aww_ar[$key][$k] = rand(1,$amount);
}
}
}
// Detects matches and marks them with X
foreach ($aww_ar as $key=>&$value) {
foreach ($aww_ar[0] as $k=>$v) {
if ($k > 0 && $k < $tw &&$aww_ar[$key][$k] == $aww_ar[$key][$k - 1] && $aww_ar[$key][$k] == $aww_ar[$key][$k + 1]) {
$aww_ar[$key][$k] = 'X';
$aww_ar[$key][$k + 1] = 'X';
$aww_ar[$key][$k - 1] = 'X';
}
if ($key > 0 && $key < $th && $aww_ar[$key][$k] == $aww_ar[$key - 1][$k] && $aww_ar[$key][$k] == $aww_ar[$key + 1][$k]) {
$aww_ar[$key][$k] = 'X';
$aww_ar[$key + 1][$k] = 'X';
$aww_ar[$key - 1][$k] = 'X';
}
}
}
// Checks if X is in playing field, Else it will call it a day
for ($i=0; $i < $th; $i++) {
if(!in_array('X',$aww_ar[$i])){
$numb++;
if($numb == $th){
echo "The playing field has developed: ".$counting." Times";
$match = false;
}else {
$counting++;
}
}
}
if($numb != 0){
$numb = 0;
}
}
// Create the playing field
echo"<table border='2px;'>";
for($y = 0; $y < $th; $y++){
echo"<tr>";
for($x = 0; $x < $tw; $x++){
echo "<td><a href='#'>" .$aww_ar[$x][$y]."</a></td>";
}
echo"</tr>";
}
echo"</table>";
I'm supposed to use nested for loops to create this shape: https://imgur.com/a/prh6zwj
This is what I currently have:
<?php
for ($x = 1; $x <= 10; $x++){
for ($y = 1; $y <= 6; $y++){
echo "Y";
}
}
?>
I have no clue what to do.
Thanks in advance!
<?php
$position = 1;
for ($x = 1; $x <= 11; $x++){
for ($y = 1; $y <= 6; $y++){
if ($y == $position) {
echo "Y";
} else {
echo "0";
}
}
if ($x < 6) {
$position++;
} else {
$position--;
}
echo "\n";
}
<?php
$length = 6; // change this to change height width
$pos = 0;
for ($x = 1; $x <= (($length*2)-1); $x++){
if($x <= $length)
{$pos = $pos+1; }
else
{$pos = $pos-1; }
for ($y = 1; $y <= $length; $y++){
if($y == $pos)
echo "Y";
else
echo "O";
}
echo "\n";
}
There are many possible ways to achieve this when I started programming I never cared about the code quality and just focused on the output. I have added two examples to help you understand it better!
<?php
//We have 6 columns & 11 rows so we need two loops one size of 11 and second size of 6
$counter = 1;
for ($i = 1; $i <= 11; $i++){
for ($j = 1; $j <= 6; $j++){
if ($j == $counter) {
echo "Y";
} else {
echo "O";
}
}
if ($i < 6) {
$counter++;
} else {
$counter--;
}
echo "<br/>";
}
echo "**************************** METHOD TWO ****************************";
//Following is not efficient But its also printing the same results
for ($i = 0 ; $i < 66 ; $i++){
if($i == 65)
{
echo "O";
break;
}
if($i % 6 == 0){
echo "<br/>";
}
if($i <= 36)
{
if ($i % 7 == 0){
echo "Y";
}else{
echo "O";
}
}else{
if ($i % 5 == 0){
echo "Y";
}else{
echo "O";
}
}
}
?>
$k=2; // for calculating position from backside
for($i=1;$i<=11;$i++) //for row
{
for($j=1;$j<=6;$j++) //column
{
if($j==$i && $i<=6) //logic for printing "Y" till the end of row
echo "Y";
else if($i>6 && $j==($i-$k)) //logic for printing "Y" in reversal order from the end of row
{
echo "Y";
$k+=2;
}
else
echo "O"; // filling rest places with "O"
}
echo"\n"; // jumping to new Row;
}
Hope you can understand it easily.
Why the following code do not return me 0 => 'Zero' for the first line but 0 => 0 ?
for ($i = 0; $i <= 30; $i += 1) {
if($i == 0) { $array[$i] = 'Zero'; }
$array[$i] = $i;
}
for ($i = 30; $i <= 100; $i += 5) {
$array[$i] = $i;
}
for ($i = 100; $i <= 200; $i += 10) {
$array[$i] = $i;
}
return $array;
Thanks.
You set element 0 to Zero and then overwrite it in the next line with 0.
if($i == 0) { $array[$i] = 'Zero'; }
$array[$i] = $i;
You probably want an else...
if($i == 0) {
$array[$i] = 'Zero'; }
else {
$array[$i] = $i;
}
for ($i = 0; $i <= 30; $i += 1)
{
if($i == 0)
{
$array[$i] = 'Zero';
}
$array[$i] = $i;
}
should be:
for ($i = 0; $i <= 30; $i += 1)
{
if($i == 0)
{
$array[$i] = 'Zero';
}
else
{
$array[$i] = $i;
}
}
There is Fatal Error in this code:
Call-time pass-by-reference has been removed on line 108. this is
Boyer Moore Algorithm
function suffixes($pattern, &$suffixes)
{
$m = strlen($pattern);
$suffixes[$m - 1] = $m;
$g = $m - 1;
for ($i = $m - 2; $i >= 0; --$i) {
if ($i > $g && $suffixes[$i + $m - 1 - $f] < $i - $g) {
$suffixes[$i] = $suffixes[$i + $m - 1 - $f];
} else {
if ($i < $g) {
$g = $i;
}
$f = $i;
while ($g >= 0 && $pattern[$g] == $pattern[$g + $m - 1 - $f]) {
$g--;
}
$suffixes[$i] = $f - $g;
}
}
}
function badCharacters
function badCharacters($pattern, &$badChars)
{
$m = strlen($pattern);
for ($i = 0; $i < $m - 1; ++$i) {
$badChars[$pattern{$i}] = $m - $i - 1;
}
}
function goodSuffixes
function goodSuffixes($pattern, &$goodSuffixes)
{
$m = strlen($pattern);
$suff = array();
suffixes($pattern, $suff);
for ($i = 0; $i < $m; $i++) {
$goodSuffixes[$i] = $m;
}
for ($i = $m - 1; $i >= 0; $i--) {
if ($suff[$i] == $i + 1) {
for ($j = 0; $j < $m - $i - 1; $j++) {
if ($goodSuffixes[$j] == $m) {
$goodSuffixes[$j] = $m - $i - 1;
}
}
}
}
for ($i = 0; $i < $m - 2; $i++) {
$goodSuffixes[$m - 1 - $suff[$i]] = $m - $i - 1;
}
}
function boyer_moore
function boyer_moore($pattern, $text)
{
$n = strlen($text);
$m = strlen($pattern);
$goodSuffixes = array();
$badCharacters = array();
//this is the line 108
goodSuffixes($pattern, &$goodSuffixes);
badCharacters($pattern, &$badCharacters);
$j = 0;
while ($j < $n - $m) {
for ($i = $m - 1; $i >= 0 && $pattern[$i] == $text[$i + $j]; $i--);
if ($i < 0) {
echo $j;
$j += $goodSuffixes[0];
} else {
$j += max($goodSuffixes[$i], $badCharacters[$text[$i + $j]] - $m + $i + 1);
}
}
}
boyer_moore($pattern, $text);
What is causing this error?
It's clear from the error message:
goodSuffixes($pattern, &$goodSuffixes);
badCharacters($pattern, &$badCharacters);
The '&' is the character that is attempting to pass by reference at calltime. Remove the '&' from in front of the 2 variable names.
goodSuffixes($pattern, $goodSuffixes);
badCharacters($pattern, $badCharacters);
Is it possible to add or concatenate something into a variable name in a PHP variable? For example:
for($g = 7; $g <= 10; $g++){
for($i = 0; $i <= 4; $i++){
$counter = $g - 7;
if($i != $counter){
continue;
} else {
$grade.[$g] = $grades[$i];
}
}
}
I want this to happen:
$grade7 = 0
$grade8 = 1
$grade9 = 2
$grade10 = 3
Concatenates the $g with grade and make this value a variable by adding a $ sign at the starting line...
The example given below:
for($g = 7; $g <= 10; $g++){
for($i = 0; $i <= 4; $i++){
$counter = $g - 7;
if($i != $counter){
continue;
} else {
${"grade".$g} = $grades[$i];
}
}
}
echo $grade7; // 0
echo $grade8; // 1
echo $grade9; // 2
echo $grade10;// 3
One solution would be to create the variables dynamically:
for ($g = 7; $g <= 10; $g++) {
for ($i = 0; $i <= 4; $i++) {
$counter = $g - 7;
if ($i == $counter) {
${'grade' . $g} = $grades[$i];
}
}
}
You should use ARRAY instead of your method. :)
Try to look at variable named like "$$".
EDIT: Maybe something like
var $array = array();
for($g=7; $g<=10; $g++)
{
for($i=0; $i<=4; $i++)
{
$counter = $g - 7;
if($i != $counter) continue;
else $array[$grade.[$g]] = $grades[$i];
}
}
$data = new Array();
for($g = 7; $g <= 10; $g++){
for($i = 0; $i <= 4; $i++){
$counter = $g - 7;
if($i != $counter){
continue;
} else {
$data[$grade.[$g]] = $grades[$i]);
}
}
}