ゴーンで簡単な推測ゲームをビルドします.



完全初心者ガイド

コンセプト
プレイヤーは0と10の間の数を推測します.彼らの推測が正しいならば、彼らは勝ちます.それ以外の場合は、プログラムのいずれかを推測するか、または正しい番号に応じて低く推測するプレーヤーのヒントを与える.プレーヤーは、ゲームで3(3)ショットを持っています彼が誤って3回(3回)推測するならば、ゲームは終わります.

ゴラン
簡単に言えば、Goは、シンプルで信頼性の高い、効率的なソフトウェアを構築することを容易にするオープンソースプログラミング言語です.
この作品では、どのようにGolangで単純な推測ゲームをする方法を学びます.

Golangから始める
まず、Golangをお使いのコンピュータにインストールする必要があります.既にインストールしていない場合は、Golang websiteからこれを行うことができます.
インストールが完了したら、Windows上のCmd、Mac上の端末、Linuxを開きます.次に、あなたの推測ゲームを格納するフォルダに“CD”ディレクトリを変更します.
あなたの推測ゲームのディレクトリを作成します
mkdir Guess
新しい推測ディレクトリに移動する
cd Guess
プロジェクトを初期化します.modファイル.文字列' username 'をgithubのユーザ名に置き換えてください.
go mod init github.com/username/Guess

新しいファイルを作成します.試み
touch guess.go
あなたが今正常にインストール' GO 'ともあなたの推測ゲームファイルを設定している.
さて、推測を開きます.お使いのデバイス上のテキスト/コードエディタでファイルを移動します.私はVS Codeを使います.Windowsは、メモ帳があらかじめインストールされています.Mac OSにはtexteditが含まれています.LinuxユーザはVimを使うことができます.また、sublime textまたはatomのような他のテキストエディタをダウンロードすることができます.

Now let’s get started with coding



推測ゲームの作成
このチュートリアルの手順は、コメントとしてコード自体に含まれます.ゴランでは、ダブルフォワードスラッシュでコメントをする.
注:コメントせずにきれいなコードは、単にゲームでまっすぐにジャンプしたい場合に含まれています.
// The first statement in a go source file must start with a package “name”. All the functions declared in this program become part of the declared package. Go programs start running in the main package. This tells the Go compiler to compile the package as an executable program.
//An Executable simply means a file that contains a program that is capable of being executed or run as a program on your computer.
package main
import (
fmt // import the fmt package which allows you to use text 
      //formatting, reading input & printing output functions
math/rand //import the rand package which allows you to 
            //generate random numbers
time // import the package which will provide time functionality to measure time
)

func main() {
fmt.Println(Game: Guess a number between 0 and 10) 
// This informs the player about how to play the game.
fmt.Println(You have three(3) tries )

// generate a random number
source := rand.NewSource(time.Now().UnixNano()) 
//The default number generator is predictable, so it will produce the same sequence of numbers each time. To produce a varying range of numbers, give it a seed that changes (in this case: time would ensure it changes ). Note that this is not safe to use for random numbers you want to be secret; use crypto/rand for those.

randomizer := rand.New(source)
secretNumber := randomizer.Intn(10) 
// generate numbers between 0 and 10 only. If you want to change the range change the value 10 to a higher or lower value

var guess int 
// this is one form of declaration in go; you have to add the type of the variable been declared. “var guess” won't work

for try := 1; try <= 3; try++ { 
// declaring the conditions for the for loop ; the shorthand form of declaring a variable was used here. Declare and Initialize ‘ := ‘ you declare and assign a value upon declaration. Go will automatically infer the type of the variable since you already assigned a value to it.

fmt.Printf(TRIAL %d\n, try) 
// print out the number of times the player has made a guess

fmt.Println(Please enter your number) 
// the program will prompt the player to make a guess and enter a number

fmt.Scan(&guess) 
// this function makes it possible for the program to receive the input

if guess < secretNumber { 
// if the guessed number is less than or greater than the correct number; give the player a hint

fmt.Printf(Sorry, wrong guess ; number is too small\n )
} else if guess > secretNumber {
fmt.Printf(Sorry, wrong guess ; number is too large\n )
} else {
fmt.Printf(You win!\n) 
break
// Print out "you win" message when the player guesses the correct number
}

if try == 3 { 
// if the number of tries is equal to 3, print game over and also the correct number

fmt.Printf(Game over!!\n )
fmt.Printf(The correct number is %d\n, secretNumber)
break

}
}
}
編集が完了すると、プログラムを保存します.また、あなたのプログラムで何を変更することができます.たとえば、10から100までの数の推測範囲を増やすことができます.println ()関数のプレイヤーのアクションに対するプログラムの応答を変更することができます.あなたは何を行うことができます、ゲームはあなたの現在です🤗.

プログラムの実行
上記のテキストエディタの選択に応じて、VSコードまたはコマンドプロンプト(Windows/Linux)または端末(MAC)の端末を開きます.あなたがまだ上記のゲームディレクトリを推測していることを確認します.あなたがいない場合は、以下のコマンドを使用して推測ゲームディレクトリに移動します
cd
あなたの推測ゲームを実行するために使用できる2つの方法があります.ビルドまたは実行します.二つのどちらかを使ってください.
1ビルド
次のコマンドを入力します
go build guess.go
あなたの推測ディレクトリに新しいファイルが表示されます
その後、実行
./guess
2 -実行
次のコマンドを入力します
go run guess.go
方法にかかわらず、上に行きましたあなたのゲームは現在実行する必要があります.一度あなたのプログラムを実行して、それをテスト!それを数回プレイしてください.楽しい!
あなたがこのチュートリアルについての質問をするならば、コメントとしてそれをドロップするか、私にメッセージを送って、自由に感じてください、そして、私はあなたを助けるために最善を尽くします.
以下はコメントなしのコードのバージョンです.

package main

import (
fmt
math/rand
time
)

func main() {

fmt.Println(Game: Guess a number between 0 and 10)
fmt.Println(You have three(3) tries )

source := rand.NewSource(time.Now().UnixNano())
randomizer := rand.New(source)
secretNumber := randomizer.Intn(10)

var guess int
for try := 1; try <= 3; try++ {

fmt.Printf(TRIAL %d\n, try)
fmt.Println(Please enter your number)
fmt.Scan(&guess)

if guess < secretNumber {
fmt.Printf(Sorry, wrong guess ; number is too small\n )
} else if guess > secretNumber {
fmt.Printf(Sorry, wrong guess ; number is too large\n )
} else {
fmt.Printf(You win!\n)
break
}

if try == 3 {
fmt.Printf(Game over!!\n )
fmt.Printf(The correct number is %d\n, secretNumber)
break

}
}
}
ボーナス:あなたがVSコードを使用しているならば、あなたはこの単純なコマンドをタイプすることによってあなたの端末からIDEを開くことができます
code
注意:これをパスにインストールしていなければなりません.これを行うには、CMD + Shift + Pキーを押します.