0
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
        <style>
            #game{
                width: 200px;
                display: block;
                margin-left: auto;
                margin-right: auto;



            }

        </style>
    </head>
    <body>
        <div id="game">
            <?php

            function display() {
                $items = array(
                    '<a href="?item=rock">  <br/><img src= "img/rock.png" width="150" height="150" alt="rock"/></a>',
                    '<a href="?item=scissors">  <br/><img src="img/scissors.png" width="150" height="150" alt="scissors"></a>',
                    '<a href="?item=paper">  <br/><img src="img/paper.jpg" width="150" height="150" alt="paper"></a>'
                );
                foreach ($items as $item => $value):
                    echo $value;


                endforeach;
                if (isset($_GET['item']) == true):
                    //rock paper and scissors types
                    $item = array('rock', 'paper', 'scissors');
                    //users choice

                    $user_Choice = $_GET['item'];
                    if ($user_Choice == "rock"):
                        $user_Choice = $items[0];
                    endif;
                    if ($user_Choice == "scissors"):
                        $user_Choice = $items[1];
                    endif;
                    if ($user_Choice == "paper"):
                        $user_Choice = $items[2];
                    endif;

                    //random computer generated choice
                    $random_Num = rand(0, 2);
                    $computer_Choice = $items[$random_Num];
                    echo $user_Choice;
                    echo $computer_Choice;

                    if ($user_Choice == $items[0] && $computer_Choice == $items[0]):
                        echo "tie";
                    endif;
                    if ($user_Choice == $items[0] && $computer_Choice == $items[2]):
                        echo "you lose";
                    endif;
                    if ($user_Choice == $items[0] && $computer_Choice == $items[1]):
                        echo "you win";
                    endif;
                    if ($user_Choice == $items[2] && $computer_Choice == $items[0]):
                        echo "you win";
                    endif;
                    if ($user_Choice == $items[2] && $computer_Choice == $items[2]):
                        echo "you tie";
                    endif;
                    if ($user_Choice == $items[2] && $computer_Choice == $items[1]):
                        echo "you lose";
                    endif;
                    if ($user_Choice == $items[1] && $computer_Choice == $items[0]):
                        echo "you lose";
                    endif;
                    if ($user_Choice == $items[1] && $computer_Choice == $items[2]):
                        echo "you win";
                    endif;
                    if ($user_Choice == $items[1] && $computer_Choice == $items[1]):
                        echo "you tie";
                    endif;

                endif;
            }

            display();
            ?>
        </div>
    </body>
</html>

Hey guys! I was having a bit of a problem with this code im fooling around with, its a rock paper scissors im working on for fun and have run into a problem. Actually a couple issues. I was first off wondering how to incorporate css into php, i want to be able to position my pictures beside each other horizontally instead of vertically. The second problem im having with this code, is when I click rock paper or scissors, it displays the user choice the computer choice the "tie win loss statment" but the original pictures stay on screen. How do I change my code so that When I click rock paper or scissors, the original 3 pictures are hidden and the only images left are the computer_choice and the user_choice... Thanks for reading and if you help me thank you so much !

1 Answer 1

0

About css, you might not have notice but it's actually already in your code, it's the part enclosed by the style tag in your head section. That said, you don't need css to rearrange the pictures. Notice the <br/> tags in the picture array, they are basically a new line character in term of html. Simply remove the tag from the second and third picture and you're good.

            $items = array(
                '<a href="?item=rock">  <br/><img src= "img/rock.png" width="150" height="150" alt="rock"/></a>',
                '<a href="?item=scissors"> <img src="img/scissors.png" width="150" height="150" alt="scissors"></a>',
                '<a href="?item=paper"> <img src="img/paper.jpg" width="150" height="150" alt="paper"></a>'
            );

the second problem, hiding the pictures. There are many different way to do it, the most common way would be using javascript. However, considering your code structure, it seems like the simpliest way would be wrap your print code with a checking.

if (isset($_GET['item']) != true):
            foreach ($items as $item => $value):
                echo $value;
            endforeach;
endif;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.