Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    New Coder
    Join Date
    Jan 2018
    Posts
    29
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Help required please creating what seems to me a complicated while loop

    Hi I have completted the code for the combat part of my game. I want to test it by using 100 different id's (each have different stats) and I want them each to do 50 duels against all opponents. I'm having trouble getting my head round how to do a while loop to acheive this. I figure that 25 duels each would amount to 50 when roles are reversed ie id:1 vs id:2 = 25 duels and later id:2 vs id:1 = an additional 25 duels = 50. The result will show (25 duels * 99 opponents = one player's total) * (100) = 247,500 duels.

    Question: How do I create a loop that will do this and also include in that loop sending all the results to a database, so that I will have the info to display later in a table?
    Note: I have exactly 100 combat id's in the datebase.

    Thank you

  2. #2
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,285
    Thanks
    10
    Thanked 1,176 Times in 1,166 Posts
    Your scripting uses PHP SESSIONS? You have 100 IDs, which would be 100 people, each on their own browser. I don't think you can simulate that. Don't you need to test the PHP/AJAX/PDO database all at the same time with 100 users all logged in at the same time? That's going to be your bandwidth bottleneck, not the 250K lines in your database. The SQL database can easily handle that many rows. Maybe someone else knows of a possible way you can simulate the users?

  3. #3
    New Coder
    Join Date
    Jan 2018
    Posts
    29
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    Your scripting uses PHP SESSIONS? You have 100 IDs, which would be 100 people, each on their own browser. I don't think you can simulate that. Don't you need to test the PHP/AJAX/PDO database all at the same time with 100 users all logged in at the same time? That's going to be your bandwidth bottleneck, not the 250K lines in your database. The SQL database can easily handle that many rows. Maybe someone else knows of a possible way you can simulate the users?
    Thanks mlseim, I haven't included sessions yet, so atm it's just testing using phpMyAdmin data.

  4. #4
    New Coder
    Join Date
    Jan 2018
    Posts
    29
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi, My problem remains unresolved. I have experimented for two days and come up with the code at the bottom, but I want to remove the last 5 rows, as copy pasted here :-

    duel = ALPHA 6 vs BETA 4 uel number 1
    duel = ALPHA 6 vs BETA 4 uel number 2
    duel = ALPHA 6 vs BETA 4 uel number 3
    duel = ALPHA 6 vs BETA 4 uel number 4
    duel = ALPHA 6 vs BETA 4 uel number 5

    ncp dueler ALPHA 6 doesn't exist and if I use this code by adjusting it for 100 players in my database then ALPHA 101 will cause it to crash. Note: no sessions have been added to the code so it jusy my ncp players atm.

    Could someone give me a tip on what to change below, to remove these last 5 rows? Thanks

    <?php
    $duel=1;
    $b=1;
    $c=1;
    $ALPHA = 1; // is the player id:1
    $BETA = 1; // is the oppontents id:1
    /* I'm not concerned that my ncp players will duel against themself, it's
    the other matches which I need. I'll display the results into a Table
    using 100 duelers after adjusting this code and wrapping it round my
    duelling code.
    */
    echo "duel = $duel". "<br>";
    echo "b = $b ". "<br>";
    echo "c = $c". "<br>";

    while($duel <= 5)
    {
    echo "duel = ALPHA $ALPHA vs BETA $BETA uel number $duel". "<br>";
    $duel++;

    while($duel == 6 AND $b <= 5) // b causes the number of duel vs same opponent
    {
    echo"b = $b";
    echo"<br>";
    $b++;
    $ALPHA++; //

    while($b == 6 AND $c <= 3) // b causes the number of duel vs same opponent
    {
    $BETA++;
    $c++;
    $b = 1;
    $ALPHA = 1;
    echo"c = $c";
    echo"<br>";

    }
    $duel = 1;

    }

    }
    ?>

  5. #5
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,013
    Thanks
    3
    Thanked 430 Times in 419 Posts
    I honestly can't figure out what it is any of this is even supposed to do, but I suspect a lot of that are the vague/meaningless single letter variables giving ZERO idea their purpose. "Beta", "Alpha", "c", "b", "a" -- meaningless. Give them names that describe their purpose!

    Though some advice, STOP using double quotes on echo, comma delimit the variables vs. strings in your echo, and stop using multiple echo to do one echo's job.

    ... and really your relying on "AND" for dual comparison to force truthiness just makes the loops confusing AND introduces overhead since both checks are run every loop. Same goes for that setting "B" and "ALPHA" to 1 which should probably be moved OUT of the loop.

    though the way I read this code I'm wondering why it's not creating an infinite loop.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  6. #6
    New Coder
    Join Date
    Jan 2018
    Posts
    29
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by deathshadow View Post
    I honestly can't figure out what it is any of this is even supposed to do, but I suspect a lot of that are the vague/meaningless single letter variables giving ZERO idea their purpose. "Beta", "Alpha", "c", "b", "a" -- meaningless. Give them names that describe their purpose!

    Though some advice, STOP using double quotes on echo, comma delimit the variables vs. strings in your echo, and stop using multiple echo to do one echo's job.

    ... and really your relying on "AND" for dual comparison to force truthiness just makes the loops confusing AND introduces overhead since both checks are run every loop. Same goes for that setting "B" and "ALPHA" to 1 which should probably be moved OUT of the loop.

    though the way I read this code I'm wondering why it's not creating an infinite loop.
    Hi deathshadow, thanks for your reply. I have created the duelling part of my game, I wish to test the funtionality of the combat skills. To do this I want to put my combat script into a loop to perform 50 duels vs every opponent. The code is written so that $ALPHA duels $BETA, the id of any of my 100 duelers can be set to the value of $ALPHA and $BETA. I desire to get these 100 opponents to duel the same opponent 50 times and do that against all the others, that's about 250,000 duels. Then i plan to dislay the results in a table showing 1 point for a win and 0.5 point for a Tie. Then i can alter the combat settings to create game balance. My example script uses smaller numbers for ease of reading but once I can get it working I can adjust it for the 100 duelers.

    If you would be so kind as to copy paste my script, I believe my explanation of it's purpose will become clear with visual result of running my script.

    Thank you for the advise, I will use it and I welome it, any futher help is greatly appreciated!

  7. #7
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,013
    Thanks
    3
    Thanked 430 Times in 419 Posts
    Then call them 'player1' and 'player2' so people know that you're working with players; "alpha" and "beta" could mean almost anything! What are $a, $b, and $c? Those are vague/cryptic/meaningless as well.

    The output from your program means nothing to me... and even once I formatted it I'm not entirely clear how/why it isn't landing itself in an infiinite loop... though your use of "AND" causes some serious code confusion as does the possible repeated loading of variable values inside the loop, slowing the loops for nothing.

    Much less why the 5 loops and the bizarre increment to 'not loop but loop'?!? What you are describing of player vs. player for a certain number of rounds seems to have NOTHING to do with the code you posted.

    I've been programming for four decades, what you are asking and the code you presented seem completely unrelated... and even just the code on its own is incomprehensible gibberish.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  8. #8
    New Coder
    Join Date
    Jan 2018
    Posts
    29
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thanks deathshadow, i'm sorry I am upsetting you. I've changed ALPHA and BETA to player.

    player1 vs player1 ten times.
    player1 vs player2 ten times.
    player1 vs player3 ten times.
    player1 vs player4 ten times.
    player1 vs player5 ten times.

    player2 vs player1 ten times.
    player2 vs player2 ten times.
    player2 vs player3 ten times.
    player2 vs player4 ten times.
    player2 vs player5 ten times.

    player3 vs player1 ten times.
    player3 vs player2 ten times.
    player3 vs player3 ten times.
    player3 vs player4 ten times.
    player3 vs player5 ten times.

    player4 vs player1 ten times.
    player4 vs player2 ten times.
    player4 vs player3 ten times.
    player4 vs player4 ten times.
    player4 vs player5 ten times.

    player5 vs player1 ten times.
    player5 vs player2 ten times.
    player5 vs player3 ten times.
    player5 vs player4 ten times.
    player5 vs player5 ten times.


    Please could you show me how to write a while loop to output the 250 duel matches above? Thanks

  9. #9
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,013
    Thanks
    3
    Thanked 430 Times in 419 Posts
    Not annoying, just wasn't grasping what you're asking for. Now I get it -- that text description was far more useful.

    First thing I'd do is make an array of objects for each player. This way you can store the name, number of wins, number of losses, and other relevant information AND have a single source over how many players there are.

    Next I would eliminate 'redundant' matches from the check. No reason to run "Player 2 vs. Player 1" if you just ran "player 1 vs. Player 2" -- a progressive reduction can easily handle this in 1 on 1 situations -- since do the logic. Once you've done player 1 against everyone, you never need to do player 1 again. Once you've done player 2 against everyone remaining, you never need to do player 2 again... hence if you have five players the desired sequence should actually be:

    Player 1 vs. Player 2
    Player 1 vs. Player 3
    Player 1 vs. Player 4
    Player 1 vs. Player 5
    Player 2 vs. Player 3
    Player 2 vs. Player 4
    Player 2 vs. Player 5
    Player 3 vs. Player 4
    Player 3 vs. Player 5
    Player 4 vs. Player 5

    DONE! That's all the possible combinations. Notice that if you do all the other players you do NOT have to actually do the last one! This gives you an accurate sampling with no redundancy -- for 100 matches total assuming ten per vs.

    Code:
    <?php
    
    define('MATCHES_PER_LOOP', 10);
    
    class Player {
    
    	public
    		$name,
    		$wins = 0,
    		$losses = 0;
    		
    	public function __construct($name) {
    		$this->name = $name;
    	}
    	
    }
    
    // let's set up five players.
    
    $players = [];
    for ($i = 0; $i < 5; $i++) $players[] = new Player('Player ' . ($i + 1));
    
    /*
    	dummy "game" routine. Since they are objects they are passed by
    	reference meaning we can operate on their values directly.
    */
    
    function match($player1, $player2) {
    	if (rand(0, 1)) {
    		echo $player1->name, ' (winner) vs. ', $player2->name, ' (loser)<br>';
    		$player1->wins++;
    		$player2->losses++;
    	} else {
    		echo $player1->name, ' (loser) vs. ', $player2->name, ' (winnner)<br>';
    		$player1->losses++;
    		$player2->wins++;
    	}
    }
    
    // store our loop calculations first, speeds up 'for'
    $playersCount = count($players);
    $playersStop = $playersCount - 1;
    
    /*
    	NOW actually run all the possible matches.
    	of course here I am berating you for vague single letter variables, but
    	i, j, k are industry standard for counters on loops.
    */
    for ($i = 0; $i < $playersStop; $i++) {
    	for ($j = $i + 1; $j < $playersCount; $j++) {
    		for ($k = 0; $k < MATCHES_PER_LOOP; $k++) {
    			match($players[$i], $players[$j]);
    		}
    	}
    }
    
    // then some pretty formatted output
    
    echo '
    	<table>
    		<caption>
    			Results out of ', MATCHES_PER_LOOP, ' matches per player
    		</caption>
    		<thead>
    			<tr>
    				<th scope="col">Name</th>
    				<th scope="col">Wins</th>
    				<th scope="col">Losses</th>
    			</tr>
    		</thead><tbody>';
    
    for ($i = 0; $i < $playersCount; $i++) {
    	$player = $players[$i];
    	echo '
    			<tr>
    				<th scope="row">', $player->name, '</th>
    				<td>', $player->wins, '</td>
    				<td>', $player->losses, '</td>
    			</tr>';
    }
    
    echo '
    		</tbody>
    	</table>';
    Since it's keeping track of the totals on the fly, and given the way the matchup pairings are set, with 10 samples per loop the combination per player of wins and losses should never exceed 40, since with 5 players each player would/should only face the other four opponents ONCE.

    That about what you're aiming for?
    Last edited by deathshadow; Aug 10th, 2018 at 08:10 PM.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  10. Users who have thanked deathshadow for this post:

    JoeGr (Aug 11th, 2018)

  11. #10
    New Coder
    Join Date
    Jan 2018
    Posts
    29
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thank you so much deathshadow!!! This is going to take some time for me to study and understand, because I'm such a novice. My head is spinning already.

    I do have a question regarding how to impliment this into my dueling code. I have a file which contains all of the skill values for each of the 9 combat skills, also included in that file is $ALPHA and $BETA, these are the variables used to read the two player id's together with their own associated skill values for my dueling formula, these values are taken from phpMyAdmin. So when I want to test two players against each other lets say William Cody who is id:1 verses Doc Holiday who is id:5 what I currently do is change the value so $ALPHA = 1 and $BETA= 5, then refresh my page and my Table displays the duel results as shown here



    QUESTION 1) I need to assign the numbers/values, identifing who plays who, of your code to $ALPHA and to $BETA, so that this changes with each new loop.

    For example where: Player 1 (loser) vs. Player 5 (winnner)

    1 needs assigning to $ALPHA, so
    $ALPHA = 1;

    5 needs assigning to $BETA, so
    $BETA = 5;

    How do I do that?

    QUESTION 2) I assume that I can remove the dummy "game" routine (which you so kindly used to demonstate results)
    ie
    function match($player1, $player2) {
    etc

    Can that be replaced by my duelling code without any need to alter your code ?

    Btw I am blown away by what you have done to help me and so quickly, thank you!

  12. #11
    Master Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    5,267
    Thanks
    120
    Thanked 620 Times in 606 Posts
    Quote Originally Posted by mlseim View Post
    Your scripting uses PHP SESSIONS? You have 100 IDs, which would be 100 people, each on their own browser. I don't think you can simulate that.
    Oh that's pretty easy to be honest. Just have a script call (via cURL) another script in a loop 100 times and having a tiny timeout value so that it moves on to the next cycle.

    In the called script, ignore_user_abort() so that it keeps running and then change the session identifier before calling session_start().

    The real challenge is getting all scripts to work at the same time but you could have them check the system clock in a loop for the nth second of the minute or something.

  13. #12
    New Coder
    Join Date
    Jan 2018
    Posts
    29
    Thanks
    12
    Thanked 0 Times in 0 Posts
    While I am very grateful for the help given, I'm still unable to move forward without a little more help please, re-my last responce, thank you.

  14. #13
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,013
    Thanks
    3
    Thanked 430 Times in 419 Posts
    Well that new post is again confusing as I'm still unclear what you're even asking -- again the whole alpha/beta meaningless nonsense confusing matters... but I think how you're storing results could also be a contributing factor as you've not really said WHERE you're storing this or how.

    When I said move users to an array of associative arrays, that was so that you could pass the ENTIRE user regardless of the data-source. Ideally this looks like stuff that should be stored in a database so just:

    pull the first user into $player1 (what you're calling $alpha)
    pull the second user into $player2 (what you're calling $beta)

    Pass both to the fight. get the result back, store the win/loss on each $player# as appropriate, then write them back to the database for long-term storage.

    ... and for the love of christmas don't spam people's inboxes like that; we're not all on the same time schedule!
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  15. #14
    New Coder
    Join Date
    Jan 2018
    Posts
    29
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by deathshadow View Post
    Well that new post is again confusing as I'm still unclear what you're even asking -- again the whole alpha/beta meaningless nonsense confusing matters... but I think how you're storing results could also be a contributing factor as you've not really said WHERE you're storing this or how.

    When I said move users to an array of associative arrays, that was so that you could pass the ENTIRE user regardless of the data-source. Ideally this looks like stuff that should be stored in a database so just:

    pull the first user into $player1 (what you're calling $alpha)
    pull the second user into $player2 (what you're calling $beta)

    Pass both to the fight. get the result back, store the win/loss on each $player# as appropriate, then write them back to the database for long-term storage.

    ... and for the love of christmas don't spam people's inboxes like that; we're not all on the same time schedule!
    Thank you deathshadow for responding. i deeply apologize for spamming you inbox. You are the first person I have messaged and when I sent it, I was booted out and had to log back in then I checked my sent messages and it was empty so i tried a number of things to try and get a message sent. I had no idea they all were sent. I would never spam anyone and thank you for not ignoring me, because on first impresions I desirved that. I had seen that you are based in USA while I'm in England, so i have been patient, being aware of your time zone and also considered your real life situation regarding your availability.

    1) I have not set up any storing of results I planned to do that when the loop was working and yes it has been my intention to do that in my database (phpMyAdmin)

    2) I do not understand "pull the first user into $player1 (what you're calling $alpha)"
    Very sorry, are you refering to the .. function match($player1, $player2) .. ? or the triple for loop ie match($players[$i], $players[$j]); ? and are you saying replace $player1 with $ALPHA and replace $player2 with $BETA?

    Thank you for your help it means a great deal to me.

  16. #15
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,013
    Thanks
    3
    Thanked 430 Times in 419 Posts
    Quote Originally Posted by JoeGr View Post
    I was booted out and had to log back in then I checked my sent messages and it was empty so i tried a number of things to try and get a message sent. I had no idea they all were sent.
    Ooh, that doesn't sound good -- though I had that problem at one point when I was blocking only some JavaScripts instead of all of them. vBull is a bit temperamental as forum software goes since it goes full Pakled on the use of JavaScript.

    You never go full Pakled.


    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);1) I have not set up any storing of results I planned to do that when the loop was working and yes it has been my intention to do that in my database (phpMyAdmin)[/quote]
    Then where/how are you tracking any of these players to even get as far as this? I think that's the disconnect here is you keep talking about "alpha" and "beta" (whatever the blazes those are) without saying WHERE their information is coming from or where you plan on storing it when done with it.

    Data structures FIRST, otherwise where are you even getting these players FROM to compare.

    Where are your users -- their names, their id's, their grand totals, their stats -- STORED?!?

    I also have no idea what phpMyAdmin -- a debugging tool -- has to do with any of this. It exists for playing with existing databases for debugging/testing/restructuring and is NOT where you should be creating tables, creating data (other than perhaps for testing), or pulling data from. Hell I don't even trust it for backups since most of my tables are too big for the connection timeout!
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com


 
Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •