Scissors stone cloth Small exercise three languages python2, PHP, go code

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Scissors stone cloth Small exercise three languages python2, PHP, go code

# Coding:utf-8
"""
Python core programming 6-14 problem solving ideas
Design a "stone, scissors, cloth" game, sometimes called "Rochambeau", you may have played as a child, the following is the rule.
You and your opponent, at the same time making specific gestures, must be one of the following gestures: stone, scissors, cloth. Victor from
In the following rules, the rule itself is a paradox.
(a) cloth-clad stones.
(b) The stone hit the scissors,
(c) Scissors cut the cloth. In your computer version, the user enters her/his options, the computer is looking for a random option, and then by you
Program to determine a winner or a tie. Note: The best algorithm is to use the IF statement as little as possible.
Python training Huanggo wrote Python2
"""

Import Random
Guess_list = ["Stone", "scissors", "cloth"]
Win_combination = [["Cloth", "stone"], ["Stone", "scissors"], ["Scissors", "cloth"]]
While True:
Computer = Random.choice (guess_list)
People = raw_input (' Please input: stone, scissors, cloth \ n '). Strip ()
If people not in Guess_list:
People = Raw_input (' re-enter: stone, scissors, cloth \ n '). Strip ()
Continue
if computer = = People:
Print "Tie, play again!" "
elif [Computer, people] in win_combination:
Print "Computer wins!" "
Else
Print "Man wins!" "
Break



<?php
/*
This code is written by the Python video training course Huanggo.
Python core programming 6-14 exercises, write it again in PHP.
Running PHP test.php on Linux terminal
This code is tested and run correctly under the Mac.
Total: This code is modified according to the Python code I wrote.
Learning a programming language and learning the second one is easy, why?
The programming idea is the same.
*/
$my _array = Array ("Stone", "scissors", "cloth");
$guize = Array ("Stone", "scissors"), Array ("scissors", "cloth"), Array ("cloth", "stone");
The above 2 variables define an array to be entered, and a two-dimensional array of winning rules
Var_dump ($guize);
$rand _keys = Array_rand ($my _array);
$computer = $my _array[$rand _keys];
Take random values from an array

Echo $computer. "\ n";




Echo $person;
while (True)
{
echo "Please enter: stone scissors cloth \ n";
$person = Trim (fgets (STDIN));
$input = Array ($computer, $person);
Constructs an array of input $person and randomly generated values from the computer
Re-judge in the winning rule array

if (! ( In_array ($person, $my _array)))
{
echo "can only input ' scissors, stone, cloth, please re-enter '";
Continue

}


if ($computer = = $person)
{
echo "Tie \ n";

}
else if (In_array ($input, $guize)) {
echo "Computer wins \ n";

}
Else
{
echo "man wins \ n";
Break
}
}
?>


Package Main

Change the python exercise scissors stone cloth to the code of the Go language
Huanggo written on March 19, 2014 in Beijing

Import (
"FMT"
"Math/rand"
)

The following function determines that a one-dimensional slice is not in the two-dimensional slice, which is equivalent to the in function in Python
Func exist_in (str1 [][]string, str2 []string) int {
For _, Item: = Range STR1 {
If item[0] = = Str2[0] && item[1] = = Str2[1] {
Return 1
}
}
return 0

}
Func Main () {
var person string
Guess_list: = []string{"Stone", "scissors", "cloth"}
Win: = [][]string{{"cloth", "stone"}, {"Stone", "scissors"}, {"Scissors", "Cloth"}}

for {
Num: = Rand. INTN (Len (guess_list))
Computer: = Guess_list[num]
Fmt. PRINTLN (computer)
Fmt. PRINTLN ("Please input ' stone, scissors, cloth '")
Fmt. SCANF ("%s\n", &person)
Input: = []string{computer, person}//constructs a one-dimensional slice
if computer = = Person {
Fmt. PRINTLN ("Tie! ")

} else if exist_in (Win, input) > 0 {
Fmt. PRINTLN ("Computer wins")

} else {
Fmt. PRINTLN ("Man wins")
Break
}
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.