Created
February 12, 2018 12:04
-
-
Save obelisk68/16551a11cd1e8d6967e28b6757391e23 to your computer and use it in GitHub Desktop.
Go言語でじゃんけんゲーム
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
import "math/rand" | |
import "time" | |
type Player struct { | |
name string | |
point byte | |
hand byte | |
} | |
func show_hand(hand byte) string { | |
hn := [...]string{"グー", "チョキ", "パー"} | |
return hn[hand - 1] | |
} | |
func judge(p1 *Player, p2 *Player) { | |
h1, h2 := p1.hand, p2.hand | |
fmt.Printf("%s 対 %s で ", show_hand(h2), show_hand(h1)) | |
var win *Player | |
if h1 == h2 { | |
fmt.Println("引き分けです。") | |
return | |
} else if (h2 - h1) % 3 == 2 { | |
win = p2 | |
} else { | |
win = p1 | |
} | |
fmt.Printf("%sの勝ちです。\n", win.name) | |
win.point++ | |
} | |
func game(p1 *Player, p2 *Player, n int) { | |
fmt.Printf("*** %d回戦 ***\n", n) | |
p1.hand = 0 | |
p2.hand = 0 | |
for p1.hand < 1 || 3 < p1.hand { | |
fmt.Printf("%sの手を入力して下さい(1:グー, 2:チョキ, 3:パー):", p1.name) | |
fmt.Scanf("%d", &p1.hand) | |
} | |
p2.hand = byte(rand.Intn(3) + 1) | |
judge(p1, p2) | |
} | |
func final_winner(p1 *Player, p2 *Player) { | |
po1, po2 := p1.point, p2.point | |
fmt.Println("\n*** 最終結果 ***") | |
fmt.Printf("%d 対 %d で ", po2, po1) | |
var win *Player | |
if po1 == po2 { | |
fmt.Println("引き分けです。") | |
return | |
} else if po1 > po2 { | |
win = p1 | |
} else { | |
win = p2 | |
} | |
fmt.Printf("%sの勝ちです。\n", win.name) | |
} | |
func main() { | |
rand.Seed(time.Now().UnixNano()) | |
man := &Player{point: 0} | |
cpu := &Player{name: "Comuputer", point: 0} | |
fmt.Print("あなたの名前を入力して下さい:") | |
fmt.Scanf("%s", &man.name) | |
fmt.Printf("%s 対 %s :じゃんけん開始\n\n", cpu.name, man.name) | |
for i := 1; i <= 5; i++ { | |
game(man, cpu, i) | |
} | |
final_winner(man, cpu) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment