TheNix Posted March 5, 2009 Share Posted March 5, 2009 So I'm trying to make a program that seats people randomly at tables for a poker game. I am getting an array of names through SESSION and based on how big the array is I'll have a variable number of tables (10 people at a table most) I was going to have a random number between 1-number of people in the array. That person would be put at the first table and then i move all the names after the player chosen down the list, making it smaller. I continues this placing 1 person at each table, then placing a 2nd person at each table and keep going till i run out of people. The problem I'm having is getting people into a two dimensional array $table[table number][table seat number] while($size!=0){ for($y=1;$y<=$tablenum;$y=$y+1){ if($size!=0){ srand ((double) microtime( )*1000000); $random_number = rand(0,$size); $table[$y][$z]=$names[$random_number]; $i=$random_number; while($i<$size){ $names[$i] = $names[$i+1]; $i=$i+1; } $size=$size-1; } } $z=$z+1; } $size is the number of people to be seated $tablenum is the number of tables there will be $table is the 2 dimensional array that holds the name of a person for a seat at a table What am I doing wrong with $table? or am i doing something wrong somewhere else? Quote Link to comment https://forums.phpfreaks.com/topic/148027-seating-people-at-a-poker-tourney/ Share on other sites More sharing options...
laffin Posted March 5, 2009 Share Posted March 5, 2009 <?php $size=89; $tables=10; $peoplepertable=10; $curtable=0; $table=array(); for($person=0;$person<$size && $persion <= ($tables*$peoplepertable;;$person++) { $table[$curtable++][]=$person; $curtable*=(($curtable<$tables)?1:0); } I think thats right, if I understood ya correctly. we dun need to assign $seat since [] will allocate the next element in the array. so trick is keeping the table count and reset it once ya reach max number of tables; the trick here is knowing how to use the ++ operator $curtable++, adds one to the table count after the operation ++$curtable, adds one to the table count before the operation $curtable++ wud look like this in code $table[$cirtable][]=4; $curtable=$curtable+1; while ++$curtable would look like $curtable=$curtable+1; $table[$curtable][]=4; the other trick is resetting our table count when we reached the max several ways of doing that a simple if wudda worked as well if($curtable>=$tables) $curtable=0; Anyways have fun wif yer code Quote Link to comment https://forums.phpfreaks.com/topic/148027-seating-people-at-a-poker-tourney/#findComment-776990 Share on other sites More sharing options...
TheNix Posted March 5, 2009 Author Share Posted March 5, 2009 This would work if I had a number of people that was a multiple of 10. if I don't though I could only have 8 at each table instead of 10, then I can't fill each table with 10 or I'll end up with 1 table that is really short on people. I need to make all the tables even, so i need to put one person at each table at a time. Quote Link to comment https://forums.phpfreaks.com/topic/148027-seating-people-at-a-poker-tourney/#findComment-776993 Share on other sites More sharing options...
Stephen68 Posted March 5, 2009 Share Posted March 5, 2009 This would work if I had a number of people that was a multiple of 10. if I don't though I could only have 8 at each table instead of 10, then I can't fill each table with 10 or I'll end up with 1 table that is really short on people. I need to make all the tables even, so i need to put one person at each table at a time. But how can you place one person at a table when you don't know how many tables you are going to have? I thinking you going to have to do some math to figure out how many tables you need and then seat them. Maybe Divide the number of people by 10 and see what you have for a remainder if you have like 1 as a remainder you bump your tables down to one person. Something like that, I'm tired hehe.. Quote Link to comment https://forums.phpfreaks.com/topic/148027-seating-people-at-a-poker-tourney/#findComment-776996 Share on other sites More sharing options...
TheNix Posted March 5, 2009 Author Share Posted March 5, 2009 I do a little math, I determine how many tables there will be by dividing the number of people by 10. Quote Link to comment https://forums.phpfreaks.com/topic/148027-seating-people-at-a-poker-tourney/#findComment-777003 Share on other sites More sharing options...
Philip Posted March 5, 2009 Share Posted March 5, 2009 <?php $totalPlayers = 83; // # of people $seatsPerTable = 10; // # of seats at each table $tableNum = ceil($totalPlayers/$seatsPerTable); // # of tables $maxTables = ($totalPlayers%$seatsPerTable); // # of tables that are maxed out $table = array(); // setup table array for($currentTable=1, $currentPlayer=1; $currentTable<=$tableNum; $currentTable++) { // Loop through each table if($currentTable==$maxTables) { // If the current table is equal to the number of tables with max players, // we need to subtract 1 from the seat count on the rest of the tables $seatsPerTable--; } for($currentSeat=1; $currentSeat<=$seatsPerTable; $currentSeat++,$currentPlayer++) { // Loop through each seat $table[$currentTable][$currentSeat] = 'player #'.$currentPlayer; } } $totalSeats = $currentPlayer-1; // We have to adjust for the last loop (still adds 1) // Show results: echo 'Seats used: ',$totalSeats,'<pre>'; print_r($table); echo '</pre>'; ?> Tested. However, not sure if it's what you want though. Just to show output here: Seats used: 83 Array ( [1] => Array ( [1] => player #1 [2] => player #2 [3] => player #3 [4] => player #4 [5] => player #5 [6] => player #6 [7] => player #7 [8] => player #8 [9] => player #9 [10] => player #10 ) [2] => Array ( [1] => player #11 [2] => player #12 [3] => player #13 [4] => player #14 [5] => player #15 [6] => player #16 [7] => player #17 [8] => player #18 [9] => player #19 [10] => player #20 ) [3] => Array ( [1] => player #21 [2] => player #22 [3] => player #23 [4] => player #24 [5] => player #25 [6] => player #26 [7] => player #27 [8] => player #28 [9] => player #29 ) [4] => Array ( [1] => player #30 [2] => player #31 [3] => player #32 [4] => player #33 [5] => player #34 [6] => player #35 [7] => player #36 [8] => player #37 [9] => player #38 ) [5] => Array ( [1] => player #39 [2] => player #40 [3] => player #41 [4] => player #42 [5] => player #43 [6] => player #44 [7] => player #45 [8] => player #46 [9] => player #47 ) [6] => Array ( [1] => player #48 [2] => player #49 [3] => player #50 [4] => player #51 [5] => player #52 [6] => player #53 [7] => player #54 [8] => player #55 [9] => player #56 ) [7] => Array ( [1] => player #57 [2] => player #58 [3] => player #59 [4] => player #60 [5] => player #61 [6] => player #62 [7] => player #63 [8] => player #64 [9] => player #65 ) [8] => Array ( [1] => player #66 [2] => player #67 [3] => player #68 [4] => player #69 [5] => player #70 [6] => player #71 [7] => player #72 [8] => player #73 [9] => player #74 ) [9] => Array ( [1] => player #75 [2] => player #76 [3] => player #77 [4] => player #78 [5] => player #79 [6] => player #80 [7] => player #81 [8] => player #82 [9] => player #83 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/148027-seating-people-at-a-poker-tourney/#findComment-777019 Share on other sites More sharing options...
Psycho Posted March 5, 2009 Share Posted March 5, 2009 There's a much easier way. Assuming the list of player names is $_SESSION['players'] it is a simple operation //Randomize the player names shuffle($_SESSION['players']); $maxTableSize = 10; //Change to any value you want $tableCount = ceil(count($_SESSION['players']) / $maxTableSize); //Assign each plaer to a table foreach ($_SESSION['players'] as $pnum => $player) { $tables[$pnum % $tableCount][] = $player; } That will result in a multidimensional array like this Array ( [0] => Array ( [0] => Player Name 25 [1] => Player Name 12 [2] => Player Name 5 [3] => Player Name 6 [4] => Player Name 9 [5] => Player Name 13 [6] => Player Name 4 [7] => Player Name 23 [8] => Player Name 2 ) [1] => Array ( [0] => Player Name 16 [1] => Player Name 10 [2] => Player Name 18 [3] => Player Name 11 [4] => Player Name 17 [5] => Player Name 8 [6] => Player Name 14 [7] => Player Name 15 ) [2] => Array ( [0] => Player Name 24 [1] => Player Name 7 [2] => Player Name 19 [3] => Player Name 1 [4] => Player Name 20 [5] => Player Name 22 [6] => Player Name 21 [7] => Player Name 3 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/148027-seating-people-at-a-poker-tourney/#findComment-777025 Share on other sites More sharing options...
Philip Posted March 5, 2009 Share Posted March 5, 2009 Why didn't I think of it like that Quote Link to comment https://forums.phpfreaks.com/topic/148027-seating-people-at-a-poker-tourney/#findComment-777049 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.