剪刀石頭布小習題三種語言python2、php、go代碼

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

剪刀石頭布小習題三種語言python2、php、go代碼

# coding:utf-8
"""
python核心編程6-14習題的解題思路
設計一個"石頭,剪子,布"遊戲,有時又叫"Rochambeau",你小時候可能玩過,下面是規則.
你和你的對手,在同一時間做出特定的手勢,必須是下面一種手勢:石頭,剪子,布.勝利者從
下面的規則中產生,這個規則本身是個悖論.
(a) 布包石頭.
(b)石頭砸剪子,
(c)剪子剪破布.在你的電腦版本中,使用者輸入她/他的選項,電腦找一個隨機選項,然後由你
的程式來決定一個勝利者或者平手.注意:最好的演算法是盡量少的使用 if 語句.
python培訓 黃哥所寫 python2
"""

import random
guess_list = ["石頭", "剪刀", "布"]
win_combination = [["布", "石頭"], ["石頭", "剪刀"], ["剪刀", "布"]]
while True:
    computer = random.choice(guess_list)
    people = raw_input('請輸入:石頭,剪刀,布\n').strip()
    if people not in guess_list:
        people = raw_input('重新請輸入:石頭,剪刀,布\n').strip()
        continue
    if computer == people:
        print "平手,再玩一次!"
    elif [computer, people] in win_combination:
        print "電腦獲勝!"
    else:
        print "人獲勝!"
        break



<?php
/*
本代碼由python視頻培訓班黃哥所寫。
python核心編程6-14習題,用php寫一遍。
在linux下終端運行 php test.php
本代碼在mac下測試回合無誤。
總計:這個代碼是根據本人所寫python代碼修改過來的
學會一種程式設計語言,再學第二種,就很容易,為啥?
編程思路是一樣的。
*/
$my_array = array("石頭","剪刀","布");
$guize = array(array("石頭","剪刀"),array("剪刀","布"),array("布","石頭"));
//上面2個變數定義一個需要輸入的數組,和一個獲勝規則的二維數組
// var_dump($guize);
$rand_keys = array_rand($my_array);
$computer = $my_array[$rand_keys];
//取數組中隨機值
 
echo $computer . "\n";
 
 
 
 
// echo $person;
while (True)
{
    echo "請輸入: 石頭  剪刀  布\n";
    $person = trim(fgets(STDIN)) ;
    $input = array($computer,$person);
    //將輸入的$person和電腦隨機產生的值構造一個數組
    //再判斷在不在獲勝規則數組中
 
    if (!(in_array($person,$my_array)))
    {
       echo "只能輸入'剪刀、石頭,布,請重新輸入'";
       continue;
 
    }
    
 
    if ($computer == $person )
    {
        echo "平手\n";
         
    }
    else if (in_array($input,$guize)) {
        echo "電腦勝\n";
         
    }
    else
    {
      echo "人獲勝\n";
      break;
    }
}
?>


package main

// 將python習題剪刀石頭布修改為go語言的代碼
// 黃哥寫於2014年3月19日北京

import (
    "fmt"
    "math/rand"
)

// 下面這個函數判斷一個一維slice在不在二維slice中,相當於python中in功能
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{"石頭", "剪刀", "布"}
    Win := [][]string{{"布", "石頭"}, {"石頭", "剪刀"}, {"剪刀", "布"}}

    for {
        num := rand.Intn(len(guess_list))
        computer := guess_list[num]
        fmt.Println(computer)
        fmt.Println("請輸入'石頭,剪刀,布'")
        fmt.Scanf("%s\n", &person)
        input := []string{computer, person} //構造一個一維slice
        if computer == person {
            fmt.Println("平手!")

        } else if exist_in(Win, input) > 0 {
            fmt.Println("電腦獲勝")

        } else {
            fmt.Println("人獲勝")
            break
        }
    }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.