Beispiel #1
0
func main() {
	fmt.Println("Connect4!")
	onePlayer := promptOnePlayer()
	if onePlayer {
		onePlayerMode()
		return
	}
	p1 := promptToken(1)
	p2 := promptToken(2)

	g := connect4.NewGame()
	won := false
	for !won {
		printBoard(g, p1, p2)
		columnIndex := 0
		fmt.Scanf("%d", &columnIndex)
		w, err := g.Play(columnIndex)
		if err != nil {
			fmt.Println("Invalid move!")
		}
		won = w
	}
	printBoard(g, p1, p2)
	winner := g.Turn()%2 + 1
	fmt.Printf("Congratulations player %v!\n", winner)
}
Beispiel #2
0
func onePlayerMode() {
	p1 := promptToken(1)
	p2 := promptToken(2)
	depth := promptDepth()
	g := connect4.NewGame()
	won := false
	for !won {
		printBoard(g, p1, p2)
		if g.Turn() == byte(1) {
			columnIndex := 0
			fmt.Scanf("%d", &columnIndex)
			w, err := g.Play(columnIndex)
			if err != nil {
				fmt.Println("Invalid move!")
			}
			won = w
		} else {
			move := connect4.Solve(g, depth)
			w, err := g.Play(move)
			if err != nil {
				fmt.Println("Invalid move!")
			}
			won = w
		}
	}
	printBoard(g, p1, p2)
	winner := g.Turn()%2 + 1
	fmt.Printf("Congratulations player %v!\n", winner)
}