This is my first ever created game. I been slowly but continuously learning PHP for the past 2 months and just last night I thought that I might be able to create a simple and very basic game.
Due to my lack of coding experience I am aware that my code could be pretty bad to the point of making anyone to cringe.
But because I am totally aware that my code could be a shame when it comes to write simple and easy to understand code, that's why I Google "code review" and I found this site.
I want to improve the code and game in 3 ways.
1- Change the code from procedural to object oriented. (But I need to learn object oriented PHP first.)
2- Create a scoreboard that shows player and computer score. (I assume I can get this done with $_SESSION.)
3- Create a highest player scoreboard. (I already know how to create prepared PDO statements to insert data into MariaDB. I also learned procedural MySQLi but I already decided to focus on learning PDO just because of scalability. Then later I can continue learning MySQLi.)
So, my problem is that I just want to know what experienced PHP programmers can tell me to improve my code. Maybe a way to simplify what I've done into an easier to read code or less lines of code etc. Any comment is very appreciated, positive or negative. I don't take anything personal.
The code consist of just a single index.php file with everything in there. Nothing more, nothing less. Anyone can literally copy paste the code and get it running with XAMPP etc.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<style>
.game_form {
text-align: center;
margin: 100px;
font-size: 150%;
}
</style>
<body>
<?php
// First shuffle/randomizer algorithm.
// There are multiple duplicated values to alter the options because otherwise
// the computer will almost always choose the same values
// and it is easy to win by learning/guessing the pattern.
$array = ['rock', 'rock', 'rock', 'rock', 'paper', 'paper', 'paper', 'paper', 'scissor', 'scissor', 'scissor', 'scissor'];
shuffle($array);
$computerSelection = $array[3];
// Second shuffle/randomizer algorithm.
// $input = ["rock"=>"rock", "paper"=>"paper", "scissor"=>"scissor",
// "rock"=>"rock", "paper"=>"paper", "scissor"=>"scissor",
// "rock"=>"rock", "paper"=>"paper", "scissor"=>"scissor",
// "rock"=>"rock", "paper"=>"paper", "scissor"=>"scissor",
// "rock"=>"rock", "paper"=>"paper", "scissor"=>"scissor"];
// $computerRandomSelection = array_rand($input, 1);
?>
<form class="game_form" action="index.php" method="POST">
<input type="radio" name="playerSelection" value="rock">Rock<br>
<input type="radio" name="playerSelection" value="paper">Paper<br>
<input type="radio" name="playerSelection" value="scissor">Scissor<br>
<button name="submit">SUBMIT</button><br><br>
<?php
if (isset($_POST['playerSelection']) == NULL) {
echo "Select an option.";
}
if (isset($_POST['playerSelection'])) {
$playerSelection = $_POST['playerSelection'];
echo "You selected: " . $playerSelection . "<br>";
echo "Computer selected: " . $computerSelection . "<br>";
if ($playerSelection == 'rock') {
if ($computerSelection == 'rock')
echo "<h1>Draw...</h1>";
if ($computerSelection == 'paper')
echo "<h1>You lose.</h1>";
if ($computerSelection == 'scissor')
echo "<h1>Winner!</h1>";
}
if ($playerSelection == 'paper') {
if ($computerSelection == 'paper')
echo "<h1>Draw...</h1>";
if ($computerSelection == 'rock')
echo "<h1>Winner!</h1>";
if ($computerSelection == 'scissor')
echo "<h1>You lose.</h1>";
}
if ($playerSelection == 'scissor') {
if ($computerSelection == 'scissor')
echo "<h1>Draw...</h1>";
if ($computerSelection == 'rock')
echo "<h1>You lose.</h1>";
if ($computerSelection == 'paper')
echo "<h1>Winner!</h1>";
}
}
?>
</form>
<!--
Just a reference to build the winner vs the looser.
Rock & Rock ===== Draw
Rock & Paper ==== Paper wins
Rock & Scissor == Rock wins
Paper & Paper === Draw
Paper & Rock ==== Paper wins
Paper & Scissor = Scissor wins
Scissor & Scissor = Draw
Scissor & Rock == Rock wins
Scissor & Paper = Scissor wins
-->
</body>
</html>