Home > Backend Development > PHP Tutorial > Web version of rock-paper-scissors game implemented in php

Web version of rock-paper-scissors game implemented in php

墨辰丷
Release: 2023-03-28 13:48:02
Original
2207 people have browsed it

This article mainly introduces the web version of the rock-paper-scissors game implemented in PHP, involving PHP array traversal, comparison and random array calling related operation skills. Friends in need can refer to the following

The details are as follows:

<?php
/*
 * Created on 2016-11-25
 *
 */
 if (isset($_POST[&#39;sub&#39;])) {
  $what = $_POST[&#39;what&#39;];
  //需要输入的数组
  $my_array = array("剪刀","石头","布");
  //获胜规则
  $guize = array(array("剪刀","布"),array("布","石头"),array("石头","剪刀"));
  //取数组中的随机值
  $rand_keys = array_rand($my_array);
  $computer = $my_array[$rand_keys];
  echo "你的".$what."VS ".$computer. "<br/>";
    $input = array($computer,$what);
    //将输入的what和电脑随机产生的值构造成一个数组,再判断在不在获胜规则中
    if (!(in_array($what,$my_array))) {
      echo "请输入 剪刀、石头、布";
      header("location:index.php");
    }
    if ($computer == $what) {
      echo "噢,平手";
    }elseif (in_array($input,$guize)) {
      echo "电脑胜";
    }else {
      echo "你赢咯~";
    }
 }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>剪刀石头布</title>
</head>
<body>
 <form action="" method="post">
 <p align="center">剪刀石头布,你出什么?<br/>
 <input type="radio" name="what" value="剪刀"/>剪刀<br/>
 <input type="radio" name="what" value="石头"/>石头<br/>
 <input type="radio" name="what" value="布"/>布<br/>
 </p>
 <p align="center">
 <input type="submit" name="sub" value="开始!" />
 <input type="reset" name="" value="重置" />
 </p>
 </form>
</body>
</html>
Copy after login

The operation effect diagram is as follows:

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

Detailed explanation of regular expression examples in PHP

phpSummary of examples of common methods for reading XML_phpTips

##PHP implements decimal, binary, octal and hexadecimal Methods of converting related functions

#

The above is the detailed content of Web version of rock-paper-scissors game implemented in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template