0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Go] 定番(?)のじゃんけんゲームを作ってみた

Last updated at Posted at 2022-11-14

Go言語を使ってじゃんけんゲームを作ってみた!

目的 : Go言語の基本文法を確認

INFORMATION

インポートするライブラリ

  • fmt : ターミナルへの出力用フォーマットライブラリ
  • math/rand : ランダム値の生成
  • strconv.Atoi : ターミナルからのstring入力(Ascii)をint typeに変換
  • time : ランダム値の生成時に利用

環境

  • macOSX : Montrey 12.6.1
  • Goland : go1.19.3 darwin/amd64
  • Visual Studio Code : 1.73.1

利用した文法

  • if 〜 else if 〜 else 文
  • ランダム値の生成
  • ポインタによる、値の更新
  • mapを使った連想配列(Pythonの辞書相当)
  • スライス機能

ゲームの内容

  • コンピュータの手は、ランダム値から生成&決定
  • 5回実行して、結果を「○勝 ○敗 ○引き分け」で表示する

はじめの一歩なので、ベストプラクティスに基づく書き方になっているのか??です。

実行コード

rockPaperScissors.go
package main

import (
	"fmt"
	"math/rand"
	"strconv"
	"time"
)

func main() {
	fmt.Println("じゃんけんをしよう!")
	gameWin := 0
	gameEqual := 0
	gameLose := 0
	game(1, &gameWin, &gameEqual, &gameLose)
	game(2, &gameWin, &gameEqual, &gameLose)
	game(3, &gameWin, &gameEqual, &gameLose)
	game(4, &gameWin, &gameEqual, &gameLose)
	game(5, &gameWin, &gameEqual, &gameLose)

	fmt.Printf("あなたの成績は%d勝 %d敗 %d引分けです。\n", gameWin, gameLose, gameEqual)
}

func game(number int, winPtr *int, losePtr *int, equalPtr *int) {
	hands := map[int]string{0: "グー", 1: "チョキ", 2: "パー"}
	result := map[int]string{0: " => 惜しい!引き分けです。", 1: " => やった!あなたの勝ちです。",
		2: " => 残念!あなたの負けです。"}

	rand.Seed(time.Now().Unix())
	computer := rand.Intn(3)

	var input string
	fmt.Printf("[%d回戦目] 手の番号を入力してください! 0:グー 1:チョキ 2:パー : ", number)
	fmt.Scan(&input)
	yinput, _ := strconv.Atoi(input)

	if yinput >= 3 {
		fmt.Println(" => 入力番号を間違えていますので、あたたの負けです")
		fmt.Println("")
		*losePtr += 1

	} else if computer == 0 && yinput == 1 {
		fmt.Printf("コンピュータ(%s) vs あなた(%s)\n", hands[computer], hands[yinput])
		fmt.Println(result[2])
		fmt.Println("")
		*losePtr += 1

	} else if computer == 0 && yinput == 2 {
		fmt.Printf("コンピュータ(%s) vs あなた(%s)\n", hands[computer], hands[yinput])
		fmt.Println(result[1])
		fmt.Println("")
		*winPtr += 1

	} else if computer == 1 && yinput == 0 {
		fmt.Printf("コンピュータ(%s) vs あなた(%s)\n", hands[computer], hands[yinput])
		fmt.Println(result[1])
		fmt.Println("")
		*winPtr += 1

	} else if computer == 1 && yinput == 2 {
		fmt.Printf("コンピュータ(%s) vs あなた(%s)\n", hands[computer], hands[yinput])
		fmt.Println(result[2])
		fmt.Println("")
		*losePtr += 1

	} else if computer == 2 && yinput == 0 {
		fmt.Printf("コンピュータ(%s) vs あなた(%s)\n", hands[computer], hands[yinput])
		fmt.Println(result[2])
		fmt.Println("")
		*losePtr += 1

	} else if computer == 2 && yinput == 1 {
		fmt.Printf("コンピュータ(%s) vs あなた(%s)\n", hands[computer], hands[yinput])
		fmt.Println(result[1])
		fmt.Println("")
		*winPrt += 1

	} else {
		fmt.Printf("コンピュータ(%s) vs あなた(%s)\n", hands[computer], hands[yinput])
		fmt.Println(result[0])
		fmt.Println("")
		*equalPtr += 1
	}
}

実行結果

mac$ go run ./rockPaperScissors.go 

じゃんけんをしよう!
[1回戦目] 手の番号を入力してください! 0:グー 1:チョキ 2:パー : 0
コンピュータ(グー) vs あなた(グー)
 => 惜しい!引き分けです。

[2回戦目] 手の番号を入力してください! 0:グー 1:チョキ 2:パー : 1
コンピュータ(グー) vs あなた(チョキ)
 => 残念!あなたの負けです。

[3回戦目] 手の番号を入力してください! 0:グー 1:チョキ 2:パー : 2
コンピュータ(グー) vs あなた(パー)
 => やった!あなたの勝ちです。

[4回戦目] 手の番号を入力してください! 0:グー 1:チョキ 2:パー : 3
 => 入力番号を間違えていますので、あたたの負けです

[5回戦目] 手の番号を入力してください! 0:グー 1:チョキ 2:パー : 1
コンピュータ(チョキ) vs あなた(チョキ)
 => 惜しい!引き分けです。

あなたの成績は1勝 2敗 2引分けです。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?