-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexamples_test.go
More file actions
79 lines (73 loc) · 2.21 KB
/
examples_test.go
File metadata and controls
79 lines (73 loc) · 2.21 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
// Package chess contains examples for the other packages in this repo.
package chess
/*
import (
"bufio"
"fmt"
"github.com/andrewbackes/chess/fen"
"github.com/andrewbackes/chess/game"
"github.com/andrewbackes/chess/pgn"
"github.com/andrewbackes/chess/position/move"
"github.com/andrewbackes/chess/position/square"
"os"
"time"
)
func ExampleFoolsMate() {
// Create a new game:
g := game.New()
// Moves can be created based on source and destination squares:
f3 := move.Move{Source: square.F2, Destination: square.F3}
g.MakeMove(f3)
// They can also be created by parsing algebraic notation:
e5, _ := g.Position.ParseMove("e5")
g.MakeMove(e5)
// Or by using piece coordinate notation:
g4 := move.Parse("g2g4")
g.MakeMove(g4)
// Another example of SAN:
foolsmate, _ := g.Position.ParseMove("Qh4#")
// Making a move also returns the game status:
gamestatus := g.MakeMove(foolsmate)
fmt.Println(gamestatus == game.WhiteCheckmated)
// Output: true
}
func ExampleTimedGame() {
forWhite := game.NewTimeControl(5*time.Minute, 40, 0, true)
forBlack := game.NewTimeControl(1*time.Minute, 40, 0, true)
tc := [2]game.TimeControl{forWhite, forBlack}
g := game.NewTimedGame(tc)
g.MakeTimedMove(move.Parse("e2e4"), 1*time.Minute)
}
func ExamplePlayTimedGame() {
tc := game.NewTimeControl(40*time.Minute, 40, 0, true)
g := game.NewTimedGame([2]game.TimeControl{tc, tc})
console := bufio.NewReader(os.Stdin)
for g.Status() == game.InProgress {
fmt.Print(g, "\nMove: ")
start := time.Now()
input, _ := console.ReadString('\n')
if move, err := g.Position.ParseMove(input); err == nil {
g.MakeTimedMove(move, time.Since(start))
} else {
fmt.Println("Couldn't understand your move.")
}
}
}
func ExamplePrintGMGames() {
f, _ := os.Open("myfile.pgn")
defer f.Close()
unfiltered, _ := pgn.Read(f)
filtered := pgn.Filter(unfiltered, pgn.NewTagFilter("WhiteElo>2600"), pgn.NewTagFilter("BlackElo>2600"))
for _, game := range filtered {
fmt.Println(game)
}
}
func ExampleSaavedraPositionFEN() {
decoded, _ := fen.Decode("8/8/1KP5/3r4/8/8/8/k7 w - - 0 1")
fmt.Println(decoded)
// Will Output: chess board of the position.
encoded, _ := fen.Encode(decoded)
fmt.Println(encoded)
// Will Output: the inputted FEN
}
*/