Archive for the 'State' Category

Page 2 of 2

PHP Game Making Part I: Algorithms and Design Patterns

cornerpicGeek Game

The Rice University MOOC course introducing Python owed part of its popularity to the fact that all of the projects involved game development. One of the first games was based on an old one called “Rock-paper-scissors” that has been revised and updated to Rock-paper-scissors-lizard-Spock. (For those of you who are Big Bang Theory fans, you may have seen it demonstrated on that show.) The game itself is pretty simple. Two players chant “Rock-paper-scissors-lizard-Spock” and as soon as “Spock” is spoken, they throw one of five hand gestures (see image in upper left corner for the five gestures.)

  • Rock – a Fist
  • Spock – Vulcan salute (hand with fingers separated at ring and middle fingers)
  • Paper – Flat hand
  • Lizard – Hand puppet shape with thumb and index finger forming ‘mouth’
  • Scissors – Two fingers in ‘cutting’ position

The winning outcome is determined by the following rules:

  • Scissors cut paper
  • Paper covers rock
  • Rock crushes lizard
  • Lizard poisons Spock
  • Spock dissenigrates scissors
  • Scissors decapitate lizard
  • Lizard eats paper
  • Paper disproves Spock
  • Spock vaporizes rock
  • Rock breaks scissors

Each gesture has two other gestures it can beat and two other gestures that beat it. For example, if a player throws a lizard gesture, it can beat paper and Spock, and it can be beaten by scissors and rock.

The Game in PHP

One of the main things that I came away with from the Python course was a more advanced and simpler way of calculating outcomes; primarily by the use of modular arithmetic. In PHP, some of you may have used the modulus (or modulo) operator (%) in setting up table lines for alternating colors something like the following algorithm:

$alternate = $counter % 2;
if($alternate)
{
  $shade = $dark;
}
else
{
  $shade = $light;
}
$alternate ++;

When a program employs modulus 2 (% 2) all outcomes are either 0 or 1, which translate to Boolean False and True, respectively. However, more can be done with modular arithmetic than alternate background shading in output.

To get started, take a look at Figure 1. Each of the gestures can defeat the two gestures counter clock-wise and is defeated by the two gestures clockwise of the gesture. For example, paper can defeat Spock and rock, but it is defeated by lizard and scissors.

Figure 1: Circle of wins and losses

Figure 1: Circle of wins and losses

Notice that each gesture has a number associated with it. That number will be used in determining the outcome. In this post I’d like to get started with the game playing against the computer. However, this being PHP, I’d like to develop a game two players can play against one another in future posts. (By the way, more than two players can play this game at the same time, but it gets cumbersome, and so throughout this series, we’ll just be using two players. Before going further, though, try out your luck against the computer:
Play

The moves of each player (you and the computer) is expressed in numbers associated with the circle. (Click below to continue.)
Continue reading ‘PHP Game Making Part I: Algorithms and Design Patterns’