ownagesbot
ownagesbot

Reputation: 101

PHP - Rearranging a string into order

sorry the title might not be quite correct but i'm a little tired.

I was wondering if someone could help me figure something out, I have a string containing this (or similar i.e. follows same pattern) text.

Text:

Player 1 ($630) Player 2 ($578) CLICKK ($507) Player 5 ($600) Player 6 ($621)

Player 1 posts (SB) $3 Player 2 posts (BB) $6

From the first text that declares stacksize, the only information you can gather is which player has which stack size.

The information underneath tells us that Player 1 is SB (Small Blind) and Player 2 is BB (Big Blind), from this information, I can now deduce that CLICKK is UTG, Player 4 (non-existant in this scenario) is MP, Player 5 is CO and Player 6 is BTN.

It always follows the trend that UTG is after BB, MP is after UTG, CO is after MP and so on.

What I would like to do, is replace the player names, i.e. Player 1, Player 2 etc. with their corresponding positions, so Player 1 ($630) will become SB ($630) and so on..

then finally I can just remove the bottom part as it'll become redundant.

I'm not asking you to do this for me, but if you could point me in the right direction, or give me some logical steps to take i'd greatly appreciate it, thanks.

Upvotes: 1

Views: 474

Answers (1)

MackieeE
MackieeE

Reputation: 11862

Supposedly, you'll have to identify all the players at the start of the hand. Probably regex is the best method for string searching, but I'll try with PHP Explode:

 <?php

    /** 
     *  Input Strings
    **/
    $GameString = "Player 1 ($630) Player 2 ($578) CLICKK 
                   ($507) Player 5 ($600) Player 6 ($621)";

    $PostBlindString = "Player 1 posts (SB) $3 Player 2 posts (BB) $6";

    $data = explode( ")", $GameString );
    $players = array();

    /** 
     *  Get the Small Blind Player Name
    **/
    $SmallBlindPlayer = trim( 
                          substr( $PostBlindString, 0, 
                              strrpos( $PostBlindString, " posts (SB)" 
                                )
                            )
                        );

    echo $SmallBlindPlayer;
    // (Echos 'Player 1' )


    /** 
     *  Go through each exploded string
     *  find it's name before the bracket
    **/
    foreach ( $data as $p ) {
        if ( $p !== '' )
            $players[] = trim(substr( $p, 0, strrpos( $p, "(" )));
    }

    /** 
     * Our Resulting players
    **/
    print_r( $players );

    Array
    (
        [0] => Player 1
        [1] => Player 2
        [2] => CLICKK
        [3] => Player 5
        [4] => Player 6
    )

    /** 
     *  Game states array
     *  when we know someone's 
     *  position, we can assign it
     *  through some loop
    **/
    $gameStates = array ( 
                    "SB",
                    "BB",
                    "UTG",
                    "MP",
                    "CO",
                    "BTN"
                );

    /**
     *  Find the small button player
    **/
    for ( $x = 0; $x < count($players); $x++ ) {
        if ( $players[$x] == $SmallBlindPlayer )
            echo $players[$x] . " This player is the small blind!";
    }

    /**
     *  Go through them, as assign it
     *  loop back to start if the player
     *  is late in the array
    **/
    $PlayerPositions = array(); 
    $LoopedThrough = false;
    $Found = false; 
    $FoundAt = 0;
    $c = 0;

    for ( $x = 0; $x < count($players); $x++ ) {

        if ( $players[$x] == $SmallBlindPlayer && !$Found ) {
            $PlayerPositions[$players[$x]] = $gameStates[$c];
            $Found = true; 
            $FoundAt = $x;

        } else { 

            if ( $Found ) {
                if ( $x != $FoundAt )
                    $PlayerPositions[$players[$x]] = $gameStates[$c++];
            }

            if ( $Found && !$LoopedThrough ) {
                    $x = -1; $LoopedThrough = true;
            }
        }
    }


    /**
     *  Print the "merged" arrays
    **/
    print_r( $PlayerPositions );

    Array
    (
        [Player 1] => SB
        [Player 2] => BB
        [CLICKK] => UTG
        [Player 5] => MP
        [Player 6] => CO
    )

 ?>

My thinking would be that from here, you can iterate through the players list, knowing that your $Gamestates would state at where your player 'found' was, and append to a new string, or replace the original $Gamestring with something like substr_replace

Furthermore, you could collect the stacksizes at the same point when you collect the player names to simply create a new $GameString - regardless, now that you have them in arrays, there's alot more you can do with them.

Upvotes: 2

Related Questions