PHP-CLI issue

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

 

I am working on building a PHP-CLI 'game' for converting
binary/hex/decimal, to better your speed at converting between base
number systems. I started on the game class, and created a little menu
with user input for different game modes. If the user inputs invalid
data, the script is supposed to say 'invalid' and ask for the input
again. Then, as long as the data remains invalid, the user is
continuously prompted until the data is valid, whereas the correct
method is called.

 

My issue is that when invalid data is input, the 'invalid, try again'
message is displayed 3 times before the script stops for the user to try
again. So instead of:

 

Invalid data. Mode: (here, waits for the user to input)

 

it says:

 

Invalid data. Mode:

Invalid data. Mode:

Invalid data. Mode: (here, waits for the user to input)

 

I have tried altering my loop a few different ways, to no avail. This is
also my very first php-cli and so I am a bit of a newb when it comes to
php-cli. The code is below. I'm not worried about other portions of the
code at this point; can anyone see what I am doing incorrectly that is
looping this message three times instead of just displaying once?

 

class BaseGame {

            public function run() {

                        fwrite(STDOUT,$this->welcome());

                        $input = $this->getSelection();

                        $this->decideMode($input);

            }

            private function welcome() {

                        $menu = "Welcome to the Binary/Hexadecimal
conversion Practice Tool.\n";

                        $menu.= "This tool is used as a game for
learning binary/hexadecimal/decimal conversion.\n\n";

                        $menu.= "\tTo continue, please select your game
mode.\n\n";

                        $menu.= "\t[Mode]\t[Description]\n";

                        $menu.= "\tA.)\tGiven a random binary value,
resolve to decimal.\n";

                        $menu.= "\tB.)\tGiven a random binary value,
resolve to hexadecimal.\n";

                        $menu.= "\tC.)\tGiven a random hexadecimal
value, resolve to binary.\n";

                        $menu.= "\tD.)\tGiven a random hexadecimal
value, resolve to decimal.\n";

                        $menu.= "\tE.)\tGiven a random decimal value,
resolve to binary.\n";

                        $menu.= "\tF.)\tGiven a random decimal value,
resolve to hexadecimal.\n";

                        $menu.= "\tG.)\tGiven two random values, decide
which is larger.\n";

                        $menu.= "\tQ.)\tQuit.\n\n";

                        $menu.= "\tTo begin, simply type the mode
letter.\n\n";

                        return $menu;

            }

            private function getSelection() {

                        fwrite(STDOUT,"Mode: ");

                        $input = strtoupper(fgetc(STDIN));   

                        return $input;

            }

            private function decideMode($input) {

                        while($input) {

                                    if($input == "Q") { exit(0); }

                                    elseif($input == "A") {
$this->binary2decimal(); }    

                                    elseif($input == "B") {
$this->binary2hexadecimal(); }

                                    elseif($input == "C") {
$this->hexadecimal2binary(); }

                                    elseif($input == "D") {
$this->hexadecimal2decimal(); }

                                    elseif($input == "E") {
$this->decimal2binary(); }

                                    elseif($input == "F") {
$this->decimal2hexadecimal(); }

                                    elseif($input == "G") {
$this->comparison(); }

                                    else {

                                                fwrite(STDOUT,"\nInvalid
choice. Choose again.\n");          

                                                $input =
$this->getSelection();

                                    }

                        }

                        exit(0);

            }


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux