Page 1 of 1
Deck to Array()
Posted: 23 July 2020, 02:46
by Fusionpro
Greetings,
At the end of the game, cards must be scored in the "stRoundEnd" function.
Each player has their own hand from
Code: Select all
$this->cards->getCardsInLocation($player_id);
I've written out pseudo-code for how I want to handle the scoring logic, but it relies on the type_arg values of all the cards from players' hands being stuffed into individual arrays.
Foreach player, Foreach card in hand, I want to push that 'type_arg' value into an array so that I can handle the scoring logic on the array instead of a player hand with card objects.
Anybody have an example project I could reference or some guidance on
converting cards in hand to an array of values? Much obliged friends, cheers.
Re: Deck to Array()
Posted: 23 July 2020, 03:47
by paramesis
not knowing what array functions you'd need, I would suggest you might not need to go through the trouble and could probably use the objects, but this would do strictly what you're asking.
Code: Select all
$scoring_array = array();
foreach( $players as $player_id => $player ) {
$cards = $this->cards->getCardsInLocation( $player_id );
foreach( $cards as $card ) {
$scoring_array[$player_id] [] = $card['type_arg'];
}
}
Note that 'location' is a string and 'location_arg' is an integer, so you're implicitly converting the assumed integer $player_id to a string. Unless the order of the cards matters and is stored in 'location_arg', I would have have made the location be something like 'hand' and the location_arg be the player_id. If it helps, here is an excerpt from my scoring function in Off the Rails in which the score is based on the 'type_arg' of jewels, which are stored as cards, and don't require nested loops since I loop over the 'stockpile' of all players.
Code: Select all
$jewels_in_stockpile = $this->jewels->getCardsInLocation( 'stockpile' );
$player_jewel_counts = array();
$player_cart_status = array();
foreach ( $players as $player ) {
$player_jewel_counts[$player['id']] = array(0,0,0,0,0);
$player_cart_status[$player['id']] = array(0,0);
}
foreach( $jewels_in_stockpile as $jewel ) {
$player_id = $jewel['location_arg'];
$score = $jewel['type_arg'];
switch( $score ) {
case 1: $this->incStat( 1, 'points_from_rubies', $player_id ); break;
case 2: $this->incStat( 2, 'points_from_emeralds', $player_id ); break;
case 3: $this->incStat( 3, 'points_from_gold', $player_id ); break;
case 4: $this->incStat( 4, 'points_from_diamonds', $player_id ); break;
default: break;
}
self::DbQuery( "UPDATE player SET player_score=player_score+".$score." WHERE player_id='".$player_id."'" );
// add tiebreaker
$tb_add = ( $score > 0 ? 101 : 100 );
self::DbQuery( "UPDATE player SET player_score_aux=player_score_aux+".$tb_add." WHERE player_id='".$player_id."'" );
$player_jewel_counts[$player_id][$score]++;
}
Re: Deck to Array()
Posted: 23 July 2020, 10:03
by Tisaac
Fusionpro wrote: ↑23 July 2020, 02:46
Greetings,
At the end of the game, cards must be scored in the "stRoundEnd" function.
Each player has their own hand from
Code: Select all
$this->cards->getCardsInLocation($player_id);
I've written out pseudo-code for how I want to handle the scoring logic, but it relies on the type_arg values of all the cards from players' hands being stuffed into individual arrays.
Foreach player, Foreach card in hand, I want to push that 'type_arg' value into an array so that I can handle the scoring logic on the array instead of a player hand with card objects.
Anybody have an example project I could reference or some guidance on
converting cards in hand to an array of values? Much obliged friends, cheers.
You could use array_values, or array_map for instance.
Array functions are really the best, I'm using them everywhere on both server and client side.
Re: Deck to Array()
Posted: 23 July 2020, 15:35
by Fusionpro
Many thanks Tisaac, Paramesis,
Great call-out on the implicit casting to string from "location" to string. Definitely deviated from best-practice on that one - but the deck/card setup for this one is very simple. I noticed I goofed this up too when planning out a different game's deck structure.
It helps to see how you can avoid additional nested loops by using 'location' and 'location_arg' effectively.
I don't think I sort cards in the deck construct - only in the local JS Stock view. If I had leveraged 'location_arg' differently, perhaps I could handle the scoring calculations without an array. I'm wanting to iterate through the card values to identify runs/straights of values. (33,32,31,29 would score 31 and 29 for a total of 60)
Going to try the sorted array of values route next. Have a good one y'all!