Golang Learning Example-scissors games

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

Learned a golang, think should practice, wrote this scissors small game, go with the study went testing.


Main program source code:

Package Mainimport ("Bufio"//input/output "FMT" "Math" "Math/rand" "OS" "StrConv" "Time")//rock, Paper, scissorsconst (Rock int = 1scissors int = 2paper int = 3rockName string = "Stone" scissorsname string = "Scissors" papername string = "cloth" Win int = 1 lose int = -1draw int = 0)//fist structure type Finger struct {value intname string}func createfinger (n int) (Finger Finger) {SWITC H N {case rock:finger.value = Rockfinger.name = Rocknamecase Scissors:finger.value = Scissorsfinger.name = Scissorsnamecas E Paper:finger.value = Paperfinger.name = Papername}return}func Isaiwin (ai finger, user finger) int {result: = Ai.value- user.value//if it is a comparison between a stone and a cloth, the comparison value is reversed if int (math. Abs (float64 (result)) = = Paper-rock {result =-(result)}if Result < 0 {return win} else if result > 0 {return lose}r Eturn Draw}func Randfinger () (finger finger) {rand: = rand. New (Rand. Newsource (time. Now (). Unixnano ())) switch Rand. INTN (3) {case 0:finger.value = Rockfinger.name = Rocknamecase 1:finger.value = Scissorsfinger.name = ScissorsnamEcase 2:finger.value = Paperfinger.name = Papername}return}func Main () {var aifinger, Userfinger FingerReader: = Bufio. Newreader (OS. Stdin) fmt. Printf ("Please input you want to punch:%d-> stone,%d-> scissors,%d-> cloth, 9-> exit \ n", rock, Scissors, paper) for {data, _, Err: = Reader. ReadLine () if err! = Nil {fmt. PRINTLN ("program error") Break}input, err: = StrConv. Atoi (String (data)) if err! = Nil {fmt. PRINTLN ("format wrong, enter Number:", input) continue}if input = = 9 {break}switch Input {case rock, scissors, Paper:aifinger = Randfinger ( ) FMT. PRINTLN ("Computer Punch for:", aifinger.name) Userfinger = Createfinger (input) fmt. Println ("You Punch for:", userfinger.name) Aiwin: = Isaiwin (Aifinger, userfinger) if Aiwin = = win {fmt. Println ("You lose, continue typing:")} else if Aiwin = = lose {fmt. Println ("You win, continue typing:")} else {fmt. Println ("Draw, continue to enter:")}default:fmt. PRINTLN ("Input not required, continue input:")}}}


Testing source

package mainimport ("testing"//Load Test Package) func Testcreatefinger (t *testing. T) {//the following struct as the initialization of map value cases: = Map[int]finger{rock:finger{rock, Rockname},scissors:finger{scissors, Scis Sorsname},paper:finger{paper, papername},6:finger{},//OTHER}FOR cs, except: = Range Cases {result: = Createf Inger (CS) if result! = except {T.errorf ("Case%v, except%v, result%v", CS, except, result)}}}func Testisaiwin (t *testing. T) {//the following struct as the initialization of the array element cases: = [...] struct {cs [2]intexcept int}{{[2]int{paper, Rock}, Win},{[2]int{rock, paper}, Lose},{[2]int{rock, scissors}, Win},{[2] Int{scissors, Rock}, Lose},{[2]int{scissors, paper}, Win},{[2]int{paper, scissors}, lose},{[2]int{scissors, scissors} , draw},}for I: = 0; I < Len (cases); i++ {cs: = cases[i].csexcept: = Cases[i].exceptresult: = Isaiwin (Createfinger (Cs[0]), Createfinger (Cs[1])) if result! = EX cept {T.errorf ("Case%v, except%v, result%v", CS, except, result)}}} 

Perform a preview

Please enter the punch you want out of the:1-> stone,2-> scissors,3-> cloth, 9-> exit 3 computer punch for:  Stone you punch for:  cloth You won, continue to enter: 2 computer out fist:  cloth you punch for:  Scissors you win, continue to enter : 1 Computer punch for:  Stone you punch for: Stone  Draw, continue to enter: 1 computer punch for:  scissors you punch for:  Stone you win, continue to enter: 1 Computer out fist:  cloth you punch for:  Stone You lose, continue to enter: 9exit Code 0, process exited normally.

In practice, there are a number of problems that are found, such as the long-appearing problem of missing types, such as
Cases: = [...] struct {cs     [2]intexcept int}{{[2]int{paper, Rock}, Win},{[2]int{rock, paper}, Lose},{[2]int{rock, scissors}, win} , {[2]int{scissors, rock}, Lose},{[2]int{scissors, paper}, Win},{[2]int{paper, scissors}, Lose},{{scissors, scissors}, Draw},//originally conceived [2]int can be omitted, but in this case the semantic analysis fails, thus losing CS data }

Some questions

If the import is separated (that is, there is a blank line in the middle), the FMT will not be swapped in order, and will not be affected by import.
Import ("Bufio"//input/output "OS" "FMT" "StrConv" "Math" "Math/rand" "Time")

If you don't separate, it will be.
Import ("Bufio"//input/output "FMT" "Math" "Math/rand" "OS" "StrConv" "Time")







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.