-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
85 lines (75 loc) · 2.27 KB
/
Copy pathmain.go
File metadata and controls
85 lines (75 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"fmt"
"math/rand"
"github.com/Techbert08/ChessProblem/internal"
)
// coin is an interface for a random (or not) coin
type coin interface {
// Toss returns true for heads and false for tails.
Toss() bool
}
// realCoin is a coin that is actually random, returning true or false
// with equal probability.
type realCoin struct{}
func (r realCoin) Toss() bool {
return rand.Intn(2) == 1
}
// twoDice is an interface for a random (or not) set of dice.
type twoDice interface {
// Roll returns the sum of two six sided die rolls
Roll() int
}
// realDice is an implementation that returns random results.
type realDice struct{}
func (d realDice) Roll() int {
// Intn returns numbers from zero to 5, so add one per die
return rand.Intn(6) + rand.Intn(6) + 2
}
// evaluateProblem runs the stated problem, emitting log statements to the
// returned slice of strings. On error the program terminates, but logs emitted
// so far are in the output slice.
func evaluateProblem(c coin, d twoDice, numMoves int) ([]string, error) {
out := make([]string, 0)
board := internal.NewBoard()
rook := internal.NewRook(internal.BLACK)
if err := board.PlacePiece(rook, "h1"); err != nil {
return out, err
}
bishop := internal.NewBishop(internal.WHITE)
if err := board.PlacePiece(bishop, "c3"); err != nil {
return out, err
}
for i := 0; i < numMoves; i++ {
roll := d.Roll()
if c.Toss() {
out = append(out, fmt.Sprintf("Heads, rolled %v", roll))
if err := board.MovePiece(rook, rook.GetPosition().Move(0, roll)); err != nil {
return out, err
}
} else {
out = append(out, fmt.Sprintf("Tails, rolled %v", roll))
if err := board.MovePiece(rook, rook.GetPosition().Move(roll, 0)); err != nil {
return out, err
}
}
out = append(out, fmt.Sprint(rook))
if bishop.GetPosition() == nil {
return append(out, "Rook takes bishop, Black wins"), nil
}
// Rook should never be nil, panic is fine if it is.
if bishop.IsLegalMove(*rook.GetPosition()) {
return append(out, "Bishop can take rook, White wins"), nil
}
}
return append(out, "Rook escapes, Black wins"), nil
}
func main() {
logs, err := evaluateProblem(&realCoin{}, &realDice{}, 15)
for _, l := range logs {
fmt.Println(l)
}
if err != nil {
fmt.Println("Terminated with error: ", err)
}
}