Creating a basic Rock Paper Scissors game in PHP

This tutorial will outline the creation of a very basic game I created a long time ago in PHP for the purpose of learning some basic concepts around inputs/outputs from the command-line, decision trees (nested “if-elseif-else” type statements) and variability in PHP’s presentation layer. I thought I’d dust this one off in particular to emphasize how easy it is to create a basic game and encourage readers to tinker with their own simple game creation.
This is the first part of a four part series. The first part is reading from the command-line if the program is run from a console/terminal (and how to determine this). The next part will show how to convert the same program to work via a browser as well if run from there (and how to detect this). The third part shows how you can extend the game to allow different formats for displaying it (depending on where it was being integrated) via SOAP Web Services. The fourth part shows how to create a basic Java-based desktop application that consumes the SOAP Web Service. Yes you heard right, SOAP not REST, because I created this example over a decade ago just before AJAX, REST and later JSON started taking the development world by storm.
These experiments would shine an important light on the diverse capabilities of PHP for me in my early days of learning the language, some 11-12 years ago, and also demonstrated some of its limitations (i.e. did not realize at the outset that PHP had no GUI system on its own like Java’s AWT or Swing, and that without somewhat obscure 3rd-party C-based frameworks like GTK+, Qt-php, wxWidgets, etc your only choice for interaction is command-line, browser or exposing an API that can be consumed by another programming language).
Inputs
You will be given the selection of any of the following values:
- Rock (also accept “R” or the representative number “0” which looks kinda like a rock)
- Paper (also accept “P” or the representative number “1” which kind of looks like a flat piece of paper)
- Scissors (also accept “S” or the representative number “2” which might require a bit more imagination, but just use your fingers to say “2” and it seems like a good fit as well)
This can be presented to the player and read in to the game via a variety of possible entry methods (i.e. toggling a Radio box, selecting from a single-value dropdown, typing in command-line, or in our case, clicking on an image).
Outputs
The game will randomize three possible outcomes on the competitor/server (aka “computer”) side:
- 0 = Rock
- 1 = Paper
- 2 = Scissors
Based on your selection you will be told whether you won, lost or had a draw with (picked the same value as) the computer player.
Rules
The rules of the game are simple, so evaluating a win or loss couldn’t be easier:
- Rock (0) smashes Scissors (2)
- Paper (1) covers Rock (0)
- Scissors (2) cut Paper (1)
- The same selection is always a draw (i.e. 0=0, 1=1, 2=2)
Why bother with the numbers? It’s slightly faster than comparing strings each time, and allows us to do more expensive operations like “equalsIgnoreCase” just once, at the onset of determining the user’s input. In general, machines excel in crunching numbers, its just what they do, not necessarily true for text.
Game Logic
With some added bits of logic for creating a User Session to persist wins/losses/draws data, the game code looks roughly as follows:
<?php // ?>
To run the game, type the following from a terminal/console and hit enter:
> php RPS.php
It will ask you for your choice of Rock (R or 0), Paper (P or 1) or Scissors (S or 2) and tell you whether you won or lost.
-or-
Conclusion
So if you made it through this part, you’ve learned to take your baby steps with server-side game development by doing some basic but interesting things in PHP with the command-line to create an old school DOS/UNIX command-line style game. Can you believe entire MMORPGs and even visual games (see “bsdgames“) used to be played this way? They even still enjoy a cult following to this day. Speaking of “old school”; nowadays, I’ve seen so many similar tutorial series centered around creating basic command-line game engines (is there some reason its almost always TicTacToe?) as a tool for learning more modern stacks like Ruby/Rails, NodeJS & Google’s Go language, which is what gave me the idea to dust off this old PHP game I had put together back in the day. Despite the fact that we now have high definition 3D/VR/AR real-time image rendering engines like Unreal, there are so many parallels between what was new and innovative back then, and what is being called new and innovative today. At the end of the day, you need to as quickly as possible express some business logic, drop your code on a server or device knowing it can run there without requiring any changes, and expose it to the required platforms for end-user consumption; whether that’s a Computer command-line, Web Browser, Mobile device, connected Car Dashboard, VR headset, or just about anything else. The implementation really doesn’t matter, speed (both in terms of time-to-market for developer and application performance experienced by the user) along with convenience (in terms of maintainability for the developer and ease-of-use for the user) should always be the top priority.
See the other parts here:
- Part 1: Creating a basic Rock Paper Scissors game in PHP <– you are here
- Part 2: Moving a command-line PHP Rock Paper Scissors game to the Browser
- Part 3: Exposing your PHP Rock Paper Scissors game via SOAP Web Services
- Part 4: Integrate PHP based SOAP RPS game server in another language, JAVA desktop GUI