I am having problems with the output of my array - php
I have a class that I will later break up into multiple classes creating a BlackJack card game. I have the the decks created and accounted for correctly. I am having a problem with the output of my player array in my method "deal". The output only displays one player but my array of outputs should be returning two. The amount of cards that are missing from my deck also indicate that I am missing 4 cards, which makes sense since I have 2 players being dealt 2 cards. Can someone please help me return both players and display both of their respective cards.
<?php
/* creating a deck of cards class to be used in a blackJack game*/
class Deck{
public $cards = array();
public $player = [];
//creates an instance of a deck of cards (works)
public function __construct(){
$values =array('2','3','4','5','6','7','8','9','10','J','Q','K','A');
$suits =array('Diamond','Club','Heart','Spade');
foreach ($suits as $suit) {
foreach($values as $value){
$this->cards[] = "$value of $suit's";
//$deck = $this->cards;
}
}
}
/*add more decks to increase number of total cards
in my array (works)*/
public function numberOfDecks($number){
$cards = $this->cards;
$this->number = $number;
for($i = 0 ; $i < $number-1; $i++){
$this->cards = array_merge($this->cards, $cards);
}
return $cards;
}
/*adding elements to a player as expected need to return multiple players and their cards multiple players (does not currently work)*/
public function deal($numberOfPlayers){
$this->numberOfPlayers = $numberOfPlayers;
$player = $this->player;
$number = 2;
for($i = 0; $i < $number; $i++){
for($j = 0; $j < $numberOfPlayers; $j++){
$this->player[$j] = $this->cards[0];
array_shift($this->cards);
}
}
for($k = 0; $k < $numberOfPlayers; $k++)
return $player[$k];
}
}
$deck = new Deck();//works as expected
$deck->numberOfDecks(3);//works as expec
$shuffled = shuffle($deck->cards);//works as expected
$deck->deal(2);
var_dump($deck);
here is the output I am currently getting back
The problem is in this chunk here:
for($i = 0; $i < $number; $i++){
for($j = 0; $j < $numberOfPlayers; $j++){
$this->player[$j] = $this->cards[0]; // <--- fix me!
array_shift($this->cards);
}
}
When that outer loop goes around again, it just straight overwrites the cards issued. Try something like this:
for($i = 0; $i < $number; $i++){
for($j = 0; $j < $numberOfPlayers; $j++){
$this->player[$j][] = $this->cards[0]; // <- push into an array
array_shift($this->cards);
}
}
This one's creating and pushing the cards into an array for each player, rather than there being just the one (repeatedly overwritten) card.
Related
Get first value of array and store in new array php
I am trying to make card game. I have 6 variable that are stored in array. Than I use fisherYates method to randomize array, and display four of them. Problem is, when I randomize it this way only, it will give only random output of those six, with all different types. So I want that some repeats like, if you draw four cards, you get output of ex: club, club, diamond,heart, or heart, star,star,heart.. if you get a point.. I thought to do it like this way: put the array in loop of 4 times, and every time it loops, it stores first, or last value in new array, so that way, I can have greater chances of combination of same cards in output array. But I'm stuck, and I don't know how to do it :/ this is what I've tried so far $diamond = 'cube.jpg'; $heart = 'heart.jpg'; $spade = 'spade.jpg'; $club = 'tref.jpg'; $star='star.jpg'; $qmark='qmark.jpg'; $time=microtime(35); $arr=[$diamond,$heart,$spade,$club,$star,$qmark]; function fisherYatesShuffle(&$items, $time) { for ($i = count($items) - 1; $i > 0; $i--) { $j = #mt_rand(0, $i); $tmp = $items[$i]; $items[$i] = $items[$j]; $items[$j] = $tmp; } return $items; } $i=0; do { $niz[$i]=fisherYatesShuffle($arr,$time); reset($niz); $i++; } while ($i <= 3);
Got a solution. Was just to simply do foreach of first element of multidimensional array :) Code goes like this: $diamond = 'cube.jpg'; $heart = 'heart.jpg'; $spade = 'spade.jpg'; $club = 'tref.jpg'; $star='star.jpg'; $qmark='qmark.jpg'; $time=microtime(35); $arr=[$diamond,$heart,$spade,$club,$star,$qmark]; $niz=array(); $i=0; do { $niz[$i]=fisherYatesShuffle($arr,$time); //reset($niz); $i++; } while ($i <= 3); foreach ($niz as $key ) { $randomArr[]=$key[0]; ; } function fisherYatesShuffle(&$items, $time) { for ($i = count($items) - 1; $i > 0; $i--) { $j = #mt_rand(0, $i); $tmp = $items[$i]; $items[$i] = $items[$j]; $items[$j] = $tmp; } return $items; } print_r($randomArr);
Php getting all combinations for a n dimensional array
I have an array of route elements (point1, point2, ...) to provide to a map engine. I don't know how many points I have. Each point is an array of possible addresses. I need to perform a check for every combination possible of these points, but only one successful check is required. My array looks like something akin to: $point[0] = array($address1, $address2, $adress3); $point[1] = array($address1, $address2); $point[2] = array($address1, $address2, $adress3, $adress4); $point[n] = ... I want to perform a test for combination: $point[0][0] - $point[1][0] - $point[2][0], $point[0][1] - $point[1][0] - $point[2][0], and so on ! :) The first successful test (route found) should end the function. I'm trying to do something with recursion but have spent many hours on this without success.
If I got you right, you want to have a "cartesian product". This is an example function for it: It first checks, if there are any subvalues in one of the subarrays and then it creates an array with all possible arraycombinations and returns it. <?php function array_cartesian_product($arrays) { $result = array(); $arrays = array_values($arrays); $sizeIn = sizeof($arrays); $size = $sizeIn > 0 ? 1 : 0; foreach ($arrays as $array) $size = $size * sizeof($array); for ($i = 0; $i < $size; $i ++) { $result[$i] = array(); for ($j = 0; $j < $sizeIn; $j ++) array_push($result[$i], current($arrays[$j])); for ($j = ($sizeIn -1); $j >= 0; $j --) { if (next($arrays[$j])) break; elseif (isset ($arrays[$j])) reset($arrays[$j]); } } return $result; } ?>
Iterating array of objects in PHP
for($i = 0; $i < count($prices); $i++){ error_log($prices[$i]->anObjectVariable); } or foreach ($prices as $price){ error_log($price->anObjectVariable); } None of these seems to work, here are the errors I get: PHP Notice: Undefined property: price::$anObjectVariable this is the code which I use to prepare the object(s) and the array. class price { public $anObjectVariable; } $prices = array(); $p = new price(); $p->anObjectVariable = "PRINT ME IN ERROR LOG!"; array_push($prices, $p);
I just tested it locally and the following code works fine if you define $prices as an array before you use it. class price { public $anObjectVariable; } $prices = array(); $p = new price(); $p->anObjectVariable = "PRINT ME IN ERROR LOG!"; array_push($prices, $p); for($i = 0; $i < count($prices); $i++){ echo($prices[$i]->anObjectVariable); } Are you actually testing the code you show us above (i.e. the one I just posted above) or are you working on a derivative? Can you confirm that this exact snippet above works for you correctly?
Then if it's not a typo for ($i = 0; $i < count($prices); $i++) { error_log($prices[$i]->anObjectVariable); } should work
PHP Game weapon accuracy
I'm trying to come up with a way for players to fire their weapons and only hit for a certain percentage. For example, one gun can only hit 70% of the time while another only hits 34% of the time. So far all I could come up with is weighted arrays. Attempt 1: private function weighted_random(&$weight) { $weights = array(($weight/100), (100-$weight)/100); $r = mt_rand(1,1000); $offset = 0; foreach($weights as $k => $w) { $offset += $w*1000; if($r <= $offset) return $k; } } Attempt 2: private function weapon_fired(&$weight) { $hit = array(); for($i = 0; $i < $weight; $i++) $hit[] = true; for($i = $weight; $i < 100; $i++) $hit[] = false; shuffle($hit); return $hit[mt_rand(0,100)]; } It doesn't seem that the players are hitting the correct percentages but I'm not really sure why. Any ideas or suggestions? Is anything glaringly wrong with these? Thanks
private function weapon_fired($weight) { return mt_rand(0, 99) < $weight; }
php array output problem
In php is there a function to increment the values of subsequent values twice(*2) in an array column based on an initial value? $beta = array( array('5', '1''1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'), array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'), array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'), array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'), array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'), array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'), array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'), array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'), array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'), array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2') ); /*Example: '5' will be '10' (5*2 =10 and output 10 to web) '2' will be '4' (2*2 = 4 and output 4 to web) The next '2' will be '16' (4*2 = 8 and output 8 to web) The next '2' will be '32' (8*2 = 16 and output 16 to web) And so forth? */ Furthermore is there an easier way to construct this array, cause I firmly believe there is, but not something too complicated in terms of construct such that a noob will not understand it, again thanks. [Disclaimer: I have spent 3 days trying to understand arrays, I now understand them; however, I am still new and am currently having some issues when trying to manipulate the values in my array and output them to the web.And I am still pretty sure I have a lot to read and learn, so please no flamers, I just need some help, found this problem in this C++ book: [http://books.google.com/books?id=4Fn_P7tdOZgC&pg=PT424&lpg=PT424&dq=subsequent+++column+is+twice+the+value&source=bl&ots=gSvQ_LhxoI&sig=dG_Ilf1iLO86lqX936cT1PpkPc8&hl=en&ei=OEEBS_eODYyotgOFtJD3CQ&sa=X&oi=book_result&ct=result&resnum=1&ved=0CAgQ6AEwAA#v=onepage&q=subsequent%20%20%20column%20is%20twice%20the%20value&f=false][1]
You can try array_map: <?php function increase($n) { return is_array($n) ? array_map('increase', $n) : $n * 2; } $new_beta = array_map("increase", $beta); As for constructing the array, there are other methods to do so but I believe this is the most performent and clean.
Here is an answer for each question in that section of the book, enjoy! <?php // Declare an array alpha of 10 rows and 20 columns of type int // Initialise the array alpha to 0 $alpha = array(array()); for($i = 0; $i < 10; $i++) { for($j = 0; $j < 20; $j++) { $alpha[$i][$j] = 0; } } // Store 1 in the first row and 2 in the remaining rows for($i = 0; $i < 10; $i++) { for($j = 0; $j < 20; $j++) { if($i == 0) { $alpha[$i][$j] = 1; } else { $alpha[$i][$j] = 2; } } } // Store 5 in the first column, and make sure that the value in // each subsequent column is twice the value in the previous column // (Beware this doesn't build off the initial value of 5 in the first // column but the previously set values above) for($i = 0; $i < 10; $i++) { for($j = 0; $j < 20; $j++) { if($j == 0) { $alpha[$i][$j] = 5; } else { if($j - 1 >= 1) { $alpha[$i][$j] = $alpha[$i][$j-1] * 2; } } } } // Print the array alpha one row per line print "Printing the array alpha one row per line:<br/>"; for($i = 0; $i < 10; $i++) { for($j = 0; $j < 20; $j++) { print "[". $alpha[$i][$j] ."] "; } print "<br/>"; } print "<br/>"; // Print the array alpha one column per line print "Printing the array alpha one column per line:<br/>"; for($j = 0; $j < 20; $j++) { for($i = 0; $i < 10; $i++) { print "[". $alpha[$i][$j] ."] "; } print "<br/>"; } ?>