gofmt-ify 4s, iterable

R=rsc
http://go/go-review/1016055
This commit is contained in:
Robert Griesemer 2009-11-05 14:43:03 -08:00
parent 2a89915485
commit 9786b3f1bd
5 changed files with 352 additions and 341 deletions

View file

@ -32,7 +32,7 @@ func main() {
p = pieces5; p = pieces5;
} }
dx, dy := 500, 500; dx, dy := 500, 500;
w, err := av.Init(av.SubsystemVideo|av.SubsystemAudio, dx, dy); w, err := av.Init(av.SubsystemVideo | av.SubsystemAudio, dx, dy);
if err != nil { if err != nil {
log.Exit(err); log.Exit(err);
} }
@ -55,7 +55,7 @@ func audioServer() {
b := <-sndc; b := <-sndc;
for len(b)*2 >= n { for len(b)*2 >= n {
var a []uint16; var a []uint16;
a, b = b[0:n/2], b[n/2:len(b)]; a, b = b[0 : n/2], b[n/2 : len(b)];
n, err = av.AudioStream(a); n, err = av.AudioStream(a);
if err != nil { if err != nil {
log.Exit(err); log.Exit(err);
@ -74,6 +74,6 @@ func PlaySound(b []uint16) {
sndc <- b; sndc <- b;
} }
var whoosh = []uint16 { var whoosh = []uint16{
// Insert your favorite sound samples here. // Insert your favorite sound samples here.
} }

View file

@ -13,130 +13,130 @@ package main
import . "exp/draw" import . "exp/draw"
var pieces4 = []Piece{ var pieces4 = []Piece{
Piece{ 0, 0, Point{4,1}, []Point{ Point{0,0}, Point{1,0}, Point{1,0}, Point{1,0} }, nil, nil}, Piece{0, 0, Point{4, 1}, []Point{Point{0, 0}, Point{1, 0}, Point{1, 0}, Point{1, 0}}, nil, nil},
Piece{ 1, 0, Point{1,4}, []Point{ Point{0,0}, Point{0,1}, Point{0,1}, Point{0,1} }, nil, nil}, Piece{1, 0, Point{1, 4}, []Point{Point{0, 0}, Point{0, 1}, Point{0, 1}, Point{0, 1}}, nil, nil},
Piece{ 2, 0, Point{4,1}, []Point{ Point{0,0}, Point{1,0}, Point{1,0}, Point{1,0} }, nil, nil}, Piece{2, 0, Point{4, 1}, []Point{Point{0, 0}, Point{1, 0}, Point{1, 0}, Point{1, 0}}, nil, nil},
Piece{ 3, 0, Point{1,4}, []Point{ Point{0,0}, Point{0,1}, Point{0,1}, Point{0,1} }, nil, nil}, Piece{3, 0, Point{1, 4}, []Point{Point{0, 0}, Point{0, 1}, Point{0, 1}, Point{0, 1}}, nil, nil},
Piece{ 0, 3, Point{2,2}, []Point{ Point{0,1}, Point{1,0}, Point{0,-1}, Point{-1,0}}, nil, nil}, Piece{0, 3, Point{2, 2}, []Point{Point{0, 1}, Point{1, 0}, Point{0, -1}, Point{-1, 0}}, nil, nil},
Piece{ 1, 3, Point{2,2}, []Point{ Point{0,1}, Point{1,0}, Point{0,-1}, Point{-1,0}}, nil, nil}, Piece{1, 3, Point{2, 2}, []Point{Point{0, 1}, Point{1, 0}, Point{0, -1}, Point{-1, 0}}, nil, nil},
Piece{ 2, 3, Point{2,2}, []Point{ Point{0,1}, Point{1,0}, Point{0,-1}, Point{-1,0}}, nil, nil}, Piece{2, 3, Point{2, 2}, []Point{Point{0, 1}, Point{1, 0}, Point{0, -1}, Point{-1, 0}}, nil, nil},
Piece{ 3, 3, Point{2,2}, []Point{ Point{0,1}, Point{1,0}, Point{0,-1}, Point{-1,0}}, nil, nil}, Piece{3, 3, Point{2, 2}, []Point{Point{0, 1}, Point{1, 0}, Point{0, -1}, Point{-1, 0}}, nil, nil},
Piece{ 0, 1, Point{3,2}, []Point{ Point{0,0}, Point{1,0}, Point{1,0}, Point{0,1} }, nil, nil}, Piece{0, 1, Point{3, 2}, []Point{Point{0, 0}, Point{1, 0}, Point{1, 0}, Point{0, 1}}, nil, nil},
Piece{ 1, 1, Point{2,3}, []Point{ Point{1,0}, Point{0,1}, Point{0,1}, Point{-1,0} }, nil, nil}, Piece{1, 1, Point{2, 3}, []Point{Point{1, 0}, Point{0, 1}, Point{0, 1}, Point{-1, 0}}, nil, nil},
Piece{ 2, 1, Point{3,2}, []Point{ Point{0,0}, Point{0,1}, Point{1,0}, Point{1,0} }, nil, nil}, Piece{2, 1, Point{3, 2}, []Point{Point{0, 0}, Point{0, 1}, Point{1, 0}, Point{1, 0}}, nil, nil},
Piece{ 3, 1, Point{2,3}, []Point{ Point{0,0}, Point{1,0}, Point{-1,1}, Point{0,1} }, nil, nil}, Piece{3, 1, Point{2, 3}, []Point{Point{0, 0}, Point{1, 0}, Point{-1, 1}, Point{0, 1}}, nil, nil},
Piece{ 0, 2, Point{3,2}, []Point{ Point{0,1}, Point{1,0}, Point{1,0}, Point{0,-1}}, nil, nil}, Piece{0, 2, Point{3, 2}, []Point{Point{0, 1}, Point{1, 0}, Point{1, 0}, Point{0, -1}}, nil, nil},
Piece{ 1, 2, Point{2,3}, []Point{ Point{0,0}, Point{0,1}, Point{0,1}, Point{1,0} }, nil, nil}, Piece{1, 2, Point{2, 3}, []Point{Point{0, 0}, Point{0, 1}, Point{0, 1}, Point{1, 0}}, nil, nil},
Piece{ 2, 2, Point{3,2}, []Point{ Point{0,0}, Point{1,0}, Point{1,0}, Point{-2,1} }, nil, nil}, Piece{2, 2, Point{3, 2}, []Point{Point{0, 0}, Point{1, 0}, Point{1, 0}, Point{-2, 1}}, nil, nil},
Piece{ 3, 2, Point{2,3}, []Point{ Point{0,0}, Point{1,0}, Point{0,1}, Point{0,1} }, nil, nil}, Piece{3, 2, Point{2, 3}, []Point{Point{0, 0}, Point{1, 0}, Point{0, 1}, Point{0, 1}}, nil, nil},
Piece{ 0, 4, Point{3,2}, []Point{ Point{0,0}, Point{1,0}, Point{1,0}, Point{-1,1} }, nil, nil}, Piece{0, 4, Point{3, 2}, []Point{Point{0, 0}, Point{1, 0}, Point{1, 0}, Point{-1, 1}}, nil, nil},
Piece{ 1, 4, Point{2,3}, []Point{ Point{1,0}, Point{-1,1}, Point{1,0}, Point{0,1} }, nil, nil}, Piece{1, 4, Point{2, 3}, []Point{Point{1, 0}, Point{-1, 1}, Point{1, 0}, Point{0, 1}}, nil, nil},
Piece{ 2, 4, Point{3,2}, []Point{ Point{1,0}, Point{-1,1}, Point{1,0}, Point{1,0} }, nil, nil}, Piece{2, 4, Point{3, 2}, []Point{Point{1, 0}, Point{-1, 1}, Point{1, 0}, Point{1, 0}}, nil, nil},
Piece{ 3, 4, Point{2,3}, []Point{ Point{0,0}, Point{0,1}, Point{0,1}, Point{1,-1}}, nil, nil}, Piece{3, 4, Point{2, 3}, []Point{Point{0, 0}, Point{0, 1}, Point{0, 1}, Point{1, -1}}, nil, nil},
Piece{ 0, 5, Point{3,2}, []Point{ Point{0,0}, Point{1,0}, Point{0,1}, Point{1,0} }, nil, nil}, Piece{0, 5, Point{3, 2}, []Point{Point{0, 0}, Point{1, 0}, Point{0, 1}, Point{1, 0}}, nil, nil},
Piece{ 1, 5, Point{2,3}, []Point{ Point{1,0}, Point{0,1}, Point{-1,0}, Point{0,1} }, nil, nil}, Piece{1, 5, Point{2, 3}, []Point{Point{1, 0}, Point{0, 1}, Point{-1, 0}, Point{0, 1}}, nil, nil},
Piece{ 2, 5, Point{3,2}, []Point{ Point{0,0}, Point{1,0}, Point{0,1}, Point{1,0} }, nil, nil}, Piece{2, 5, Point{3, 2}, []Point{Point{0, 0}, Point{1, 0}, Point{0, 1}, Point{1, 0}}, nil, nil},
Piece{ 3, 5, Point{2,3}, []Point{ Point{1,0}, Point{0,1}, Point{-1,0}, Point{0,1} }, nil, nil}, Piece{3, 5, Point{2, 3}, []Point{Point{1, 0}, Point{0, 1}, Point{-1, 0}, Point{0, 1}}, nil, nil},
Piece{ 0, 6, Point{3,2}, []Point{ Point{0,1}, Point{1,0}, Point{0,-1}, Point{1,0} }, nil, nil}, Piece{0, 6, Point{3, 2}, []Point{Point{0, 1}, Point{1, 0}, Point{0, -1}, Point{1, 0}}, nil, nil},
Piece{ 1, 6, Point{2,3}, []Point{ Point{0,0}, Point{0,1}, Point{1,0}, Point{0,1} }, nil, nil}, Piece{1, 6, Point{2, 3}, []Point{Point{0, 0}, Point{0, 1}, Point{1, 0}, Point{0, 1}}, nil, nil},
Piece{ 2, 6, Point{3,2}, []Point{ Point{0,1}, Point{1,0}, Point{0,-1}, Point{1,0} }, nil, nil}, Piece{2, 6, Point{3, 2}, []Point{Point{0, 1}, Point{1, 0}, Point{0, -1}, Point{1, 0}}, nil, nil},
Piece{ 3, 6, Point{2,3}, []Point{ Point{0,0}, Point{0,1}, Point{1,0}, Point{0,1} }, nil, nil}, Piece{3, 6, Point{2, 3}, []Point{Point{0, 0}, Point{0, 1}, Point{1, 0}, Point{0, 1}}, nil, nil},
} }
var pieces5 = []Piece{ var pieces5 = []Piece{
Piece{ 0, 1, Point{5,1}, []Point{ Point{0,0}, Point{1,0}, Point{1,0}, Point{1,0}, Point{1,0} }, nil, nil}, Piece{0, 1, Point{5, 1}, []Point{Point{0, 0}, Point{1, 0}, Point{1, 0}, Point{1, 0}, Point{1, 0}}, nil, nil},
Piece{ 1, 1, Point{1,5}, []Point{ Point{0,0}, Point{0,1}, Point{0,1}, Point{0,1}, Point{0,1} }, nil, nil}, Piece{1, 1, Point{1, 5}, []Point{Point{0, 0}, Point{0, 1}, Point{0, 1}, Point{0, 1}, Point{0, 1}}, nil, nil},
Piece{ 2, 1, Point{5,1}, []Point{ Point{0,0}, Point{1,0}, Point{1,0}, Point{1,0}, Point{1,0} }, nil, nil}, Piece{2, 1, Point{5, 1}, []Point{Point{0, 0}, Point{1, 0}, Point{1, 0}, Point{1, 0}, Point{1, 0}}, nil, nil},
Piece{ 3, 1, Point{1,5}, []Point{ Point{0,0}, Point{0,1}, Point{0,1}, Point{0,1}, Point{0,1} }, nil, nil}, Piece{3, 1, Point{1, 5}, []Point{Point{0, 0}, Point{0, 1}, Point{0, 1}, Point{0, 1}, Point{0, 1}}, nil, nil},
Piece{ 0, 0, Point{4,2}, []Point{ Point{0,0}, Point{1,0}, Point{1,0}, Point{1,0}, Point{0,1} }, nil, nil}, Piece{0, 0, Point{4, 2}, []Point{Point{0, 0}, Point{1, 0}, Point{1, 0}, Point{1, 0}, Point{0, 1}}, nil, nil},
Piece{ 1, 0, Point{2,4}, []Point{ Point{1,0}, Point{0,1}, Point{0,1}, Point{0,1}, Point{-1,0} }, nil, nil}, Piece{1, 0, Point{2, 4}, []Point{Point{1, 0}, Point{0, 1}, Point{0, 1}, Point{0, 1}, Point{-1, 0}}, nil, nil},
Piece{ 2, 0, Point{4,2}, []Point{ Point{0,0}, Point{0,1}, Point{1,0}, Point{1,0}, Point{1,0} }, nil, nil}, Piece{2, 0, Point{4, 2}, []Point{Point{0, 0}, Point{0, 1}, Point{1, 0}, Point{1, 0}, Point{1, 0}}, nil, nil},
Piece{ 3, 0, Point{2,4}, []Point{ Point{0,0}, Point{1,0}, Point{-1,1}, Point{0,1}, Point{0,1} }, nil, nil}, Piece{3, 0, Point{2, 4}, []Point{Point{0, 0}, Point{1, 0}, Point{-1, 1}, Point{0, 1}, Point{0, 1}}, nil, nil},
Piece{ 0, 2, Point{4,2}, []Point{ Point{0,0}, Point{0,1}, Point{1,-1}, Point{1,0}, Point{1,0} }, nil, nil}, Piece{0, 2, Point{4, 2}, []Point{Point{0, 0}, Point{0, 1}, Point{1, -1}, Point{1, 0}, Point{1, 0}}, nil, nil},
Piece{ 1, 2, Point{2,4}, []Point{ Point{0,0}, Point{1,0}, Point{0,1}, Point{0,1}, Point{0,1} }, nil, nil}, Piece{1, 2, Point{2, 4}, []Point{Point{0, 0}, Point{1, 0}, Point{0, 1}, Point{0, 1}, Point{0, 1}}, nil, nil},
Piece{ 2, 2, Point{4,2}, []Point{ Point{0,1}, Point{1,0}, Point{1,0}, Point{1,0}, Point{0,-1}}, nil, nil}, Piece{2, 2, Point{4, 2}, []Point{Point{0, 1}, Point{1, 0}, Point{1, 0}, Point{1, 0}, Point{0, -1}}, nil, nil},
Piece{ 3, 2, Point{2,4}, []Point{ Point{0,0}, Point{0,1}, Point{0,1}, Point{0,1}, Point{1,0} }, nil, nil}, Piece{3, 2, Point{2, 4}, []Point{Point{0, 0}, Point{0, 1}, Point{0, 1}, Point{0, 1}, Point{1, 0}}, nil, nil},
Piece{ 0, 7, Point{3,3}, []Point{ Point{0,0}, Point{1,0}, Point{1,0}, Point{0,1}, Point{0,1} }, nil, nil}, Piece{0, 7, Point{3, 3}, []Point{Point{0, 0}, Point{1, 0}, Point{1, 0}, Point{0, 1}, Point{0, 1}}, nil, nil},
Piece{ 1, 7, Point{3,3}, []Point{ Point{0,2}, Point{1,0}, Point{1,0}, Point{0,-1}, Point{0,-1}}, nil, nil}, Piece{1, 7, Point{3, 3}, []Point{Point{0, 2}, Point{1, 0}, Point{1, 0}, Point{0, -1}, Point{0, -1}}, nil, nil},
Piece{ 2, 7, Point{3,3}, []Point{ Point{0,0}, Point{0,1}, Point{0,1}, Point{1,0}, Point{1,0} }, nil, nil}, Piece{2, 7, Point{3, 3}, []Point{Point{0, 0}, Point{0, 1}, Point{0, 1}, Point{1, 0}, Point{1, 0}}, nil, nil},
Piece{ 3, 7, Point{3,3}, []Point{ Point{0,2}, Point{0,-1}, Point{0,-1}, Point{1,0}, Point{1,0} }, nil, nil}, Piece{3, 7, Point{3, 3}, []Point{Point{0, 2}, Point{0, -1}, Point{0, -1}, Point{1, 0}, Point{1, 0}}, nil, nil},
Piece{ 0, 3, Point{3,2}, []Point{ Point{0,0}, Point{1,0}, Point{1,0}, Point{-2,1}, Point{1,0} }, nil, nil}, Piece{0, 3, Point{3, 2}, []Point{Point{0, 0}, Point{1, 0}, Point{1, 0}, Point{-2, 1}, Point{1, 0}}, nil, nil},
Piece{ 1, 3, Point{2,3}, []Point{ Point{0,0}, Point{1,0}, Point{-1,1}, Point{1,0}, Point{0,1} }, nil, nil}, Piece{1, 3, Point{2, 3}, []Point{Point{0, 0}, Point{1, 0}, Point{-1, 1}, Point{1, 0}, Point{0, 1}}, nil, nil},
Piece{ 2, 3, Point{3,2}, []Point{ Point{1,0}, Point{1,0}, Point{-2,1}, Point{1,0}, Point{1,0} }, nil, nil}, Piece{2, 3, Point{3, 2}, []Point{Point{1, 0}, Point{1, 0}, Point{-2, 1}, Point{1, 0}, Point{1, 0}}, nil, nil},
Piece{ 3, 3, Point{2,3}, []Point{ Point{0,0}, Point{0,1}, Point{1,0}, Point{-1,1}, Point{1,0} }, nil, nil}, Piece{3, 3, Point{2, 3}, []Point{Point{0, 0}, Point{0, 1}, Point{1, 0}, Point{-1, 1}, Point{1, 0}}, nil, nil},
Piece{ 0, 4, Point{3,2}, []Point{ Point{0,0}, Point{1,0}, Point{1,0}, Point{-1,1}, Point{1,0} }, nil, nil}, Piece{0, 4, Point{3, 2}, []Point{Point{0, 0}, Point{1, 0}, Point{1, 0}, Point{-1, 1}, Point{1, 0}}, nil, nil},
Piece{ 1, 4, Point{2,3}, []Point{ Point{1,0}, Point{-1,1}, Point{1,0}, Point{-1,1}, Point{1,0} }, nil, nil}, Piece{1, 4, Point{2, 3}, []Point{Point{1, 0}, Point{-1, 1}, Point{1, 0}, Point{-1, 1}, Point{1, 0}}, nil, nil},
Piece{ 2, 4, Point{3,2}, []Point{ Point{0,0}, Point{1,0}, Point{-1,1}, Point{1,0}, Point{1,0} }, nil, nil}, Piece{2, 4, Point{3, 2}, []Point{Point{0, 0}, Point{1, 0}, Point{-1, 1}, Point{1, 0}, Point{1, 0}}, nil, nil},
Piece{ 3, 4, Point{2,3}, []Point{ Point{0,0}, Point{1,0}, Point{-1,1}, Point{1,0}, Point{-1,1} }, nil, nil}, Piece{3, 4, Point{2, 3}, []Point{Point{0, 0}, Point{1, 0}, Point{-1, 1}, Point{1, 0}, Point{-1, 1}}, nil, nil},
Piece{ 0, 7, Point{3,2}, []Point{ Point{0,0}, Point{2,0}, Point{-2,1}, Point{1,0}, Point{1,0} }, nil, nil}, Piece{0, 7, Point{3, 2}, []Point{Point{0, 0}, Point{2, 0}, Point{-2, 1}, Point{1, 0}, Point{1, 0}}, nil, nil},
Piece{ 1, 7, Point{2,3}, []Point{ Point{0,0}, Point{1,0}, Point{-1,1}, Point{0,1}, Point{1,0} }, nil, nil}, Piece{1, 7, Point{2, 3}, []Point{Point{0, 0}, Point{1, 0}, Point{-1, 1}, Point{0, 1}, Point{1, 0}}, nil, nil},
Piece{ 2, 7, Point{3,2}, []Point{ Point{0,0}, Point{1,0}, Point{1,0}, Point{-2,1}, Point{2,0} }, nil, nil}, Piece{2, 7, Point{3, 2}, []Point{Point{0, 0}, Point{1, 0}, Point{1, 0}, Point{-2, 1}, Point{2, 0}}, nil, nil},
Piece{ 3, 7, Point{2,3}, []Point{ Point{0,0}, Point{1,0}, Point{0,1}, Point{-1,1}, Point{1,0} }, nil, nil}, Piece{3, 7, Point{2, 3}, []Point{Point{0, 0}, Point{1, 0}, Point{0, 1}, Point{-1, 1}, Point{1, 0}}, nil, nil},
Piece{ 0, 5, Point{3,3}, []Point{ Point{0,0}, Point{1,0}, Point{0,1}, Point{1,0}, Point{-1,1} }, nil, nil}, Piece{0, 5, Point{3, 3}, []Point{Point{0, 0}, Point{1, 0}, Point{0, 1}, Point{1, 0}, Point{-1, 1}}, nil, nil},
Piece{ 1, 5, Point{3,3}, []Point{ Point{2,0}, Point{-2,1}, Point{1,0}, Point{1,0}, Point{-1,1} }, nil, nil}, Piece{1, 5, Point{3, 3}, []Point{Point{2, 0}, Point{-2, 1}, Point{1, 0}, Point{1, 0}, Point{-1, 1}}, nil, nil},
Piece{ 2, 5, Point{3,3}, []Point{ Point{1,0}, Point{-1,1}, Point{1,0}, Point{0,1}, Point{1,0} }, nil, nil}, Piece{2, 5, Point{3, 3}, []Point{Point{1, 0}, Point{-1, 1}, Point{1, 0}, Point{0, 1}, Point{1, 0}}, nil, nil},
Piece{ 3, 5, Point{3,3}, []Point{ Point{1,0}, Point{-1,1}, Point{1,0}, Point{1,0}, Point{-2,1} }, nil, nil}, Piece{3, 5, Point{3, 3}, []Point{Point{1, 0}, Point{-1, 1}, Point{1, 0}, Point{1, 0}, Point{-2, 1}}, nil, nil},
Piece{ 0, 6, Point{3,3}, []Point{ Point{1,0}, Point{1,0}, Point{-2,1}, Point{1,0}, Point{0,1} }, nil, nil}, Piece{0, 6, Point{3, 3}, []Point{Point{1, 0}, Point{1, 0}, Point{-2, 1}, Point{1, 0}, Point{0, 1}}, nil, nil},
Piece{ 1, 6, Point{3,3}, []Point{ Point{1,0}, Point{-1,1}, Point{1,0}, Point{1,0}, Point{0,1} }, nil, nil}, Piece{1, 6, Point{3, 3}, []Point{Point{1, 0}, Point{-1, 1}, Point{1, 0}, Point{1, 0}, Point{0, 1}}, nil, nil},
Piece{ 2, 6, Point{3,3}, []Point{ Point{1,0}, Point{0,1}, Point{1,0}, Point{-2,1}, Point{1,0} }, nil, nil}, Piece{2, 6, Point{3, 3}, []Point{Point{1, 0}, Point{0, 1}, Point{1, 0}, Point{-2, 1}, Point{1, 0}}, nil, nil},
Piece{ 3, 6, Point{3,3}, []Point{ Point{0,0}, Point{0,1}, Point{1,0}, Point{1,0}, Point{-1,1} }, nil, nil}, Piece{3, 6, Point{3, 3}, []Point{Point{0, 0}, Point{0, 1}, Point{1, 0}, Point{1, 0}, Point{-1, 1}}, nil, nil},
Piece{ 0, 0, Point{4,2}, []Point{ Point{0,0}, Point{1,0}, Point{1,0}, Point{1,0}, Point{-2,1} }, nil, nil}, Piece{0, 0, Point{4, 2}, []Point{Point{0, 0}, Point{1, 0}, Point{1, 0}, Point{1, 0}, Point{-2, 1}}, nil, nil},
Piece{ 1, 0, Point{2,4}, []Point{ Point{1,0}, Point{-1,1}, Point{1,0}, Point{0,1}, Point{0,1} }, nil, nil}, Piece{1, 0, Point{2, 4}, []Point{Point{1, 0}, Point{-1, 1}, Point{1, 0}, Point{0, 1}, Point{0, 1}}, nil, nil},
Piece{ 2, 0, Point{4,2}, []Point{ Point{2,0}, Point{-2,1}, Point{1,0}, Point{1,0}, Point{1,0} }, nil, nil}, Piece{2, 0, Point{4, 2}, []Point{Point{2, 0}, Point{-2, 1}, Point{1, 0}, Point{1, 0}, Point{1, 0}}, nil, nil},
Piece{ 3, 0, Point{2,4}, []Point{ Point{0,0}, Point{0,1}, Point{0,1}, Point{1,0}, Point{-1,1} }, nil, nil}, Piece{3, 0, Point{2, 4}, []Point{Point{0, 0}, Point{0, 1}, Point{0, 1}, Point{1, 0}, Point{-1, 1}}, nil, nil},
Piece{ 0, 2, Point{4,2}, []Point{ Point{0,0}, Point{1,0}, Point{1,0}, Point{1,0}, Point{-1,1} }, nil, nil}, Piece{0, 2, Point{4, 2}, []Point{Point{0, 0}, Point{1, 0}, Point{1, 0}, Point{1, 0}, Point{-1, 1}}, nil, nil},
Piece{ 1, 2, Point{2,4}, []Point{ Point{1,0}, Point{0,1}, Point{-1,1}, Point{1,0}, Point{0,1} }, nil, nil}, Piece{1, 2, Point{2, 4}, []Point{Point{1, 0}, Point{0, 1}, Point{-1, 1}, Point{1, 0}, Point{0, 1}}, nil, nil},
Piece{ 2, 2, Point{4,2}, []Point{ Point{1,0}, Point{-1,1}, Point{1,0}, Point{1,0}, Point{1,0} }, nil, nil}, Piece{2, 2, Point{4, 2}, []Point{Point{1, 0}, Point{-1, 1}, Point{1, 0}, Point{1, 0}, Point{1, 0}}, nil, nil},
Piece{ 3, 2, Point{2,4}, []Point{ Point{0,0}, Point{0,1}, Point{1,0}, Point{-1,1}, Point{0,1} }, nil, nil}, Piece{3, 2, Point{2, 4}, []Point{Point{0, 0}, Point{0, 1}, Point{1, 0}, Point{-1, 1}, Point{0, 1}}, nil, nil},
Piece{ 0, 1, Point{3,3}, []Point{ Point{0,0}, Point{1,0}, Point{0,1}, Point{1,0}, Point{0,1} }, nil, nil}, Piece{0, 1, Point{3, 3}, []Point{Point{0, 0}, Point{1, 0}, Point{0, 1}, Point{1, 0}, Point{0, 1}}, nil, nil},
Piece{ 1, 1, Point{3,3}, []Point{ Point{2,0}, Point{-1,1}, Point{1,0}, Point{-2,1}, Point{1,0} }, nil, nil}, Piece{1, 1, Point{3, 3}, []Point{Point{2, 0}, Point{-1, 1}, Point{1, 0}, Point{-2, 1}, Point{1, 0}}, nil, nil},
Piece{ 2, 1, Point{3,3}, []Point{ Point{0,0}, Point{0,1}, Point{1,0}, Point{0,1}, Point{1,0} }, nil, nil}, Piece{2, 1, Point{3, 3}, []Point{Point{0, 0}, Point{0, 1}, Point{1, 0}, Point{0, 1}, Point{1, 0}}, nil, nil},
Piece{ 3, 1, Point{3,3}, []Point{ Point{1,0}, Point{1,0}, Point{-2,1}, Point{1,0}, Point{-1,1} }, nil, nil}, Piece{3, 1, Point{3, 3}, []Point{Point{1, 0}, Point{1, 0}, Point{-2, 1}, Point{1, 0}, Point{-1, 1}}, nil, nil},
Piece{ 0, 3, Point{3,3}, []Point{ Point{0,0}, Point{1,0}, Point{1,0}, Point{-1,1}, Point{0,1} }, nil, nil}, Piece{0, 3, Point{3, 3}, []Point{Point{0, 0}, Point{1, 0}, Point{1, 0}, Point{-1, 1}, Point{0, 1}}, nil, nil},
Piece{ 1, 3, Point{3,3}, []Point{ Point{2,0}, Point{-2,1}, Point{1,0}, Point{1,0}, Point{0,1} }, nil, nil}, Piece{1, 3, Point{3, 3}, []Point{Point{2, 0}, Point{-2, 1}, Point{1, 0}, Point{1, 0}, Point{0, 1}}, nil, nil},
Piece{ 2, 3, Point{3,3}, []Point{ Point{1,0}, Point{0,1}, Point{-1,1}, Point{1,0}, Point{1,0} }, nil, nil}, Piece{2, 3, Point{3, 3}, []Point{Point{1, 0}, Point{0, 1}, Point{-1, 1}, Point{1, 0}, Point{1, 0}}, nil, nil},
Piece{ 3, 3, Point{3,3}, []Point{ Point{0,0}, Point{0,1}, Point{1,0}, Point{1,0}, Point{-2,1} }, nil, nil}, Piece{3, 3, Point{3, 3}, []Point{Point{0, 0}, Point{0, 1}, Point{1, 0}, Point{1, 0}, Point{-2, 1}}, nil, nil},
Piece{ 0, 4, Point{3,3}, []Point{ Point{1,0}, Point{-1,1}, Point{1,0}, Point{1,0}, Point{-1,1} }, nil, nil}, Piece{0, 4, Point{3, 3}, []Point{Point{1, 0}, Point{-1, 1}, Point{1, 0}, Point{1, 0}, Point{-1, 1}}, nil, nil},
Piece{ 1, 4, Point{3,3}, []Point{ Point{1,0}, Point{-1,1}, Point{1,0}, Point{1,0}, Point{-1,1} }, nil, nil}, Piece{1, 4, Point{3, 3}, []Point{Point{1, 0}, Point{-1, 1}, Point{1, 0}, Point{1, 0}, Point{-1, 1}}, nil, nil},
Piece{ 2, 4, Point{3,3}, []Point{ Point{1,0}, Point{-1,1}, Point{1,0}, Point{1,0}, Point{-1,1} }, nil, nil}, Piece{2, 4, Point{3, 3}, []Point{Point{1, 0}, Point{-1, 1}, Point{1, 0}, Point{1, 0}, Point{-1, 1}}, nil, nil},
Piece{ 3, 4, Point{3,3}, []Point{ Point{1,0}, Point{-1,1}, Point{1,0}, Point{1,0}, Point{-1,1} }, nil, nil}, Piece{3, 4, Point{3, 3}, []Point{Point{1, 0}, Point{-1, 1}, Point{1, 0}, Point{1, 0}, Point{-1, 1}}, nil, nil},
Piece{ 0, 8, Point{4,2}, []Point{ Point{0,0}, Point{1,0}, Point{0,1}, Point{1,0}, Point{1,0} }, nil, nil}, Piece{0, 8, Point{4, 2}, []Point{Point{0, 0}, Point{1, 0}, Point{0, 1}, Point{1, 0}, Point{1, 0}}, nil, nil},
Piece{ 1, 8, Point{2,4}, []Point{ Point{1,0}, Point{-1,1}, Point{1,0}, Point{-1,1}, Point{0,1} }, nil, nil}, Piece{1, 8, Point{2, 4}, []Point{Point{1, 0}, Point{-1, 1}, Point{1, 0}, Point{-1, 1}, Point{0, 1}}, nil, nil},
Piece{ 2, 8, Point{4,2}, []Point{ Point{0,0}, Point{1,0}, Point{1,0}, Point{0,1}, Point{1,0} }, nil, nil}, Piece{2, 8, Point{4, 2}, []Point{Point{0, 0}, Point{1, 0}, Point{1, 0}, Point{0, 1}, Point{1, 0}}, nil, nil},
Piece{ 3, 8, Point{2,4}, []Point{ Point{1,0}, Point{0,1}, Point{-1,1}, Point{1,0}, Point{-1,1} }, nil, nil}, Piece{3, 8, Point{2, 4}, []Point{Point{1, 0}, Point{0, 1}, Point{-1, 1}, Point{1, 0}, Point{-1, 1}}, nil, nil},
Piece{ 0, 9, Point{4,2}, []Point{ Point{2,0}, Point{1,0}, Point{-3,1}, Point{1,0}, Point{1,0} }, nil, nil}, Piece{0, 9, Point{4, 2}, []Point{Point{2, 0}, Point{1, 0}, Point{-3, 1}, Point{1, 0}, Point{1, 0}}, nil, nil},
Piece{ 1, 9, Point{2,4}, []Point{ Point{0,0}, Point{0,1}, Point{0,1}, Point{1,0}, Point{0,1} }, nil, nil}, Piece{1, 9, Point{2, 4}, []Point{Point{0, 0}, Point{0, 1}, Point{0, 1}, Point{1, 0}, Point{0, 1}}, nil, nil},
Piece{ 2, 9, Point{4,2}, []Point{ Point{1,0}, Point{1,0}, Point{1,0}, Point{-3,1}, Point{1,0} }, nil, nil}, Piece{2, 9, Point{4, 2}, []Point{Point{1, 0}, Point{1, 0}, Point{1, 0}, Point{-3, 1}, Point{1, 0}}, nil, nil},
Piece{ 3, 9, Point{2,4}, []Point{ Point{0,0}, Point{0,1}, Point{1,0}, Point{0,1}, Point{0,1} }, nil, nil}, Piece{3, 9, Point{2, 4}, []Point{Point{0, 0}, Point{0, 1}, Point{1, 0}, Point{0, 1}, Point{0, 1}}, nil, nil},
Piece{ 0, 5, Point{3,3}, []Point{ Point{0,0}, Point{0,1}, Point{1,0}, Point{1,0}, Point{0,1} }, nil, nil}, Piece{0, 5, Point{3, 3}, []Point{Point{0, 0}, Point{0, 1}, Point{1, 0}, Point{1, 0}, Point{0, 1}}, nil, nil},
Piece{ 1, 5, Point{3,3}, []Point{ Point{1,0}, Point{1,0}, Point{-1,1}, Point{-1,1}, Point{1,0} }, nil, nil}, Piece{1, 5, Point{3, 3}, []Point{Point{1, 0}, Point{1, 0}, Point{-1, 1}, Point{-1, 1}, Point{1, 0}}, nil, nil},
Piece{ 2, 5, Point{3,3}, []Point{ Point{0,0}, Point{0,1}, Point{1,0}, Point{1,0}, Point{0,1} }, nil, nil}, Piece{2, 5, Point{3, 3}, []Point{Point{0, 0}, Point{0, 1}, Point{1, 0}, Point{1, 0}, Point{0, 1}}, nil, nil},
Piece{ 3, 5, Point{3,3}, []Point{ Point{1,0}, Point{1,0}, Point{-1,1}, Point{-1,1}, Point{1,0} }, nil, nil}, Piece{3, 5, Point{3, 3}, []Point{Point{1, 0}, Point{1, 0}, Point{-1, 1}, Point{-1, 1}, Point{1, 0}}, nil, nil},
Piece{ 0, 6, Point{3,3}, []Point{ Point{2,0}, Point{-2,1}, Point{1,0}, Point{1,0}, Point{-2,1} }, nil, nil}, Piece{0, 6, Point{3, 3}, []Point{Point{2, 0}, Point{-2, 1}, Point{1, 0}, Point{1, 0}, Point{-2, 1}}, nil, nil},
Piece{ 1, 6, Point{3,3}, []Point{ Point{0,0}, Point{1,0}, Point{0,1}, Point{0,1}, Point{1,0} }, nil, nil}, Piece{1, 6, Point{3, 3}, []Point{Point{0, 0}, Point{1, 0}, Point{0, 1}, Point{0, 1}, Point{1, 0}}, nil, nil},
Piece{ 2, 6, Point{3,3}, []Point{ Point{2,0}, Point{-2,1}, Point{1,0}, Point{1,0}, Point{-2,1} }, nil, nil}, Piece{2, 6, Point{3, 3}, []Point{Point{2, 0}, Point{-2, 1}, Point{1, 0}, Point{1, 0}, Point{-2, 1}}, nil, nil},
Piece{ 3, 6, Point{3,3}, []Point{ Point{0,0}, Point{1,0}, Point{0,1}, Point{0,1}, Point{1,0} }, nil, nil}, Piece{3, 6, Point{3, 3}, []Point{Point{0, 0}, Point{1, 0}, Point{0, 1}, Point{0, 1}, Point{1, 0}}, nil, nil},
}; }

View file

@ -50,36 +50,36 @@ const (
) )
var ( var (
N int; N int;
display draw.Context; display draw.Context;
screen draw.Image; screen draw.Image;
screenr draw.Rectangle; screenr draw.Rectangle;
board [NY][NX]byte; board [NY][NX]byte;
rboard draw.Rectangle; rboard draw.Rectangle;
pscore draw.Point; pscore draw.Point;
scoresz draw.Point; scoresz draw.Point;
pcsz = 32; pcsz = 32;
pos draw.Point; pos draw.Point;
bbr, bb2r draw.Rectangle; bbr, bb2r draw.Rectangle;
bb, bbmask, bb2, bb2mask *image.RGBA; bb, bbmask, bb2, bb2mask *image.RGBA;
whitemask image.Image; whitemask image.Image;
br, br2 draw.Rectangle; br, br2 draw.Rectangle;
points int; points int;
dt int; dt int;
DY int; DY int;
DMOUSE int; DMOUSE int;
lastmx int; lastmx int;
mouse draw.Mouse; mouse draw.Mouse;
newscreen bool; newscreen bool;
timerc <-chan int64; timerc <-chan int64;
suspc chan bool; suspc chan bool;
mousec chan draw.Mouse; mousec chan draw.Mouse;
resizec <-chan bool; resizec <-chan bool;
kbdc chan int; kbdc chan int;
suspended bool; suspended bool;
tsleep int; tsleep int;
piece *Piece; piece *Piece;
pieces []Piece; pieces []Piece;
) )
type Piece struct { type Piece struct {
@ -92,56 +92,66 @@ type Piece struct {
} }
var txbits = [NCOL][32]byte{ var txbits = [NCOL][32]byte{
[32]byte{0xDD,0xDD,0xFF,0xFF,0x77,0x77,0xFF,0xFF, [32]byte{0xDD, 0xDD, 0xFF, 0xFF, 0x77, 0x77, 0xFF, 0xFF,
0xDD,0xDD,0xFF,0xFF,0x77,0x77,0xFF,0xFF, 0xDD, 0xDD, 0xFF, 0xFF, 0x77, 0x77, 0xFF, 0xFF,
0xDD,0xDD,0xFF,0xFF,0x77,0x77,0xFF,0xFF, 0xDD, 0xDD, 0xFF, 0xFF, 0x77, 0x77, 0xFF, 0xFF,
0xDD,0xDD,0xFF,0xFF,0x77,0x77,0xFF,0xFF}, 0xDD, 0xDD, 0xFF, 0xFF, 0x77, 0x77, 0xFF, 0xFF,
[32]byte{0xDD,0xDD,0x77,0x77,0xDD,0xDD,0x77,0x77, },
0xDD,0xDD,0x77,0x77,0xDD,0xDD,0x77,0x77, [32]byte{0xDD, 0xDD, 0x77, 0x77, 0xDD, 0xDD, 0x77, 0x77,
0xDD,0xDD,0x77,0x77,0xDD,0xDD,0x77,0x77, 0xDD, 0xDD, 0x77, 0x77, 0xDD, 0xDD, 0x77, 0x77,
0xDD,0xDD,0x77,0x77,0xDD,0xDD,0x77,0x77}, 0xDD, 0xDD, 0x77, 0x77, 0xDD, 0xDD, 0x77, 0x77,
[32]byte{0xAA,0xAA,0x55,0x55,0xAA,0xAA,0x55,0x55, 0xDD, 0xDD, 0x77, 0x77, 0xDD, 0xDD, 0x77, 0x77,
0xAA,0xAA,0x55,0x55,0xAA,0xAA,0x55,0x55, },
0xAA,0xAA,0x55,0x55,0xAA,0xAA,0x55,0x55, [32]byte{0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55,
0xAA,0xAA,0x55,0x55,0xAA,0xAA,0x55,0x55}, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55,
[32]byte{0xAA,0xAA,0x55,0x55,0xAA,0xAA,0x55,0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55,
0xAA,0xAA,0x55,0x55,0xAA,0xAA,0x55,0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55,
0xAA,0xAA,0x55,0x55,0xAA,0xAA,0x55,0x55, },
0xAA,0xAA,0x55,0x55,0xAA,0xAA,0x55,0x55}, [32]byte{0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55,
[32]byte{0x22,0x22,0x88,0x88,0x22,0x22,0x88,0x88, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55,
0x22,0x22,0x88,0x88,0x22,0x22,0x88,0x88, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55,
0x22,0x22,0x88,0x88,0x22,0x22,0x88,0x88, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55,
0x22,0x22,0x88,0x88,0x22,0x22,0x88,0x88}, },
[32]byte{0x22,0x22,0x00,0x00,0x88,0x88,0x00,0x00, [32]byte{0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88,
0x22,0x22,0x00,0x00,0x88,0x88,0x00,0x00, 0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88,
0x22,0x22,0x00,0x00,0x88,0x88,0x00,0x00, 0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88,
0x22,0x22,0x00,0x00,0x88,0x88,0x00,0x00}, 0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88,
[32]byte{0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00, },
0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00, [32]byte{0x22, 0x22, 0x00, 0x00, 0x88, 0x88, 0x00, 0x00,
0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00, 0x22, 0x22, 0x00, 0x00, 0x88, 0x88, 0x00, 0x00,
0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00}, 0x22, 0x22, 0x00, 0x00, 0x88, 0x88, 0x00, 0x00,
[32]byte{0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00, 0x22, 0x22, 0x00, 0x00, 0x88, 0x88, 0x00, 0x00,
0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00, },
0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00, [32]byte{0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00}, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
[32]byte{0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC, },
0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC}, [32]byte{0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
[32]byte{0xCC,0xCC,0xCC,0xCC,0x33,0x33,0x33,0x33, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xCC,0xCC,0xCC,0xCC,0x33,0x33,0x33,0x33, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xCC,0xCC,0xCC,0xCC,0x33,0x33,0x33,0x33, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xCC,0xCC,0xCC,0xCC,0x33,0x33,0x33,0x33}, },
[32]byte{0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
},
[32]byte{0xCC, 0xCC, 0xCC, 0xCC, 0x33, 0x33, 0x33, 0x33,
0xCC, 0xCC, 0xCC, 0xCC, 0x33, 0x33, 0x33, 0x33,
0xCC, 0xCC, 0xCC, 0xCC, 0x33, 0x33, 0x33, 0x33,
0xCC, 0xCC, 0xCC, 0xCC, 0x33, 0x33, 0x33, 0x33,
},
} }
var txpix = [NCOL]draw.Color{ var txpix = [NCOL]draw.Color{
draw.Yellow, /* yellow */ draw.Yellow, /* yellow */
draw.Cyan, /* cyan */ draw.Cyan, /* cyan */
draw.Green, /* lime green */ draw.Green, /* lime green */
draw.GreyBlue, /* slate */ draw.GreyBlue, /* slate */
draw.Red, /* red */ draw.Red, /* red */
draw.GreyGreen, /* olive green */ draw.GreyGreen, /* olive green */
draw.Blue, /* blue */ draw.Blue, /* blue */
draw.Color(0xFF55AAFF), /* pink */ draw.Color(0xFF55AAFF), /* pink */
draw.Color(0xFFAAFFFF), /* lavender */ draw.Color(0xFFAAFFFF), /* lavender */
draw.Color(0xBB005DFF), /* maroon */ draw.Color(0xBB005DFF), /* maroon */
@ -155,7 +165,7 @@ func movemouse() int {
func warp(p draw.Point, x int) int { func warp(p draw.Point, x int) int {
if !suspended && piece != nil { if !suspended && piece != nil {
x = pos.X + piece.sz.X*pcsz/2; x = pos.X + piece.sz.X * pcsz / 2;
if p.Y < rboard.Min.Y { if p.Y < rboard.Min.Y {
p.Y = rboard.Min.Y; p.Y = rboard.Min.Y;
} }
@ -184,12 +194,12 @@ func initPieces() {
} }
func collide(pt draw.Point, p *Piece) bool { func collide(pt draw.Point, p *Piece) bool {
pt.X = (pt.X - rboard.Min.X) / pcsz; pt.X = (pt.X - rboard.Min.X)/pcsz;
pt.Y = (pt.Y - rboard.Min.Y) / pcsz; pt.Y = (pt.Y - rboard.Min.Y)/pcsz;
for _, q := range p.d { for _, q := range p.d {
pt.X += q.X; pt.X += q.X;
pt.Y += q.Y; pt.Y += q.Y;
if pt.X<0 || pt.X>=NX || pt.Y<0 || pt.Y>=NY { if pt.X < 0 || pt.X >= NX || pt.Y < 0 || pt.Y >= NY {
return true; return true;
continue; continue;
} }
@ -201,8 +211,8 @@ func collide(pt draw.Point, p *Piece) bool {
} }
func collider(pt, pmax draw.Point) bool { func collider(pt, pmax draw.Point) bool {
pi := (pt.X - rboard.Min.X) / pcsz; pi := (pt.X - rboard.Min.X)/pcsz;
pj := (pt.Y - rboard.Min.Y) / pcsz; pj := (pt.Y - rboard.Min.Y)/pcsz;
n := pmax.X / pcsz; n := pmax.X / pcsz;
m := pmax.Y / pcsz + 1; m := pmax.Y / pcsz + 1;
for i := pi; i < pi+n && i < NX; i++ { for i := pi; i < pi+n && i < NX; i++ {
@ -228,8 +238,8 @@ func setpiece(p *Piece) {
var r draw.Rectangle; var r draw.Rectangle;
r.Min = bbr.Min; r.Min = bbr.Min;
for i, pt := range p.d { for i, pt := range p.d {
r.Min.X += pt.X*pcsz; r.Min.X += pt.X * pcsz;
r.Min.Y += pt.Y*pcsz; r.Min.Y += pt.Y * pcsz;
r.Max.X = r.Min.X + pcsz; r.Max.X = r.Min.X + pcsz;
r.Max.Y = r.Min.Y + pcsz; r.Max.Y = r.Min.Y + pcsz;
if i == 0 { if i == 0 {
@ -237,7 +247,7 @@ func setpiece(p *Piece) {
draw.Draw(bb, r.Inset(1), txpix[piece.tx], nil, draw.ZP); draw.Draw(bb, r.Inset(1), txpix[piece.tx], nil, draw.ZP);
draw.Draw(bbmask, r, draw.Opaque, nil, draw.ZP); draw.Draw(bbmask, r, draw.Opaque, nil, draw.ZP);
op = r.Min; op = r.Min;
}else{ } else {
draw.Draw(bb, r, bb, nil, op); draw.Draw(bb, r, bb, nil, op);
draw.Draw(bbmask, r, bbmask, nil, op); draw.Draw(bbmask, r, bbmask, nil, op);
} }
@ -249,7 +259,7 @@ func setpiece(p *Piece) {
} }
} }
br.Max = br.Max.Sub(bbr.Min); br.Max = br.Max.Sub(bbr.Min);
delta := draw.Pt(0,DY); delta := draw.Pt(0, DY);
br2.Max = br.Max.Add(delta); br2.Max = br.Max.Add(delta);
r = br.Add(bb2r.Min); r = br.Add(bb2r.Min);
r2 := br2.Add(bb2r.Min); r2 := br2.Add(bb2r.Min);
@ -280,26 +290,26 @@ func rest() {
for _, p := range piece.d { for _, p := range piece.d {
pt.X += p.X; pt.X += p.X;
pt.Y += p.Y; pt.Y += p.Y;
board[pt.Y][pt.X] = byte(piece.tx+16); board[pt.Y][pt.X] = byte(piece.tx + 16);
} }
} }
func canfit(p *Piece) bool { func canfit(p *Piece) bool {
var dx = [...]int{0, -1, 1, -2, 2, -3, 3, 4, -4}; var dx = [...]int{0, -1, 1, -2, 2, -3, 3, 4, -4};
j := N + 1; j := N+1;
if j >= 4 { if j >= 4 {
j = p.sz.X; j = p.sz.X;
if j<p.sz.Y { if j < p.sz.Y {
j = p.sz.Y; j = p.sz.Y;
} }
j = 2*j-1; j = 2*j - 1;
} }
for i:=0; i<j; i++ { for i := 0; i < j; i++ {
var z draw.Point; var z draw.Point;
z.X = pos.X + dx[i]*pcsz; z.X = pos.X + dx[i]*pcsz;
z.Y = pos.Y; z.Y = pos.Y;
if !collide(z, p) { if !collide(z, p) {
z.Y = pos.Y + pcsz-1; z.Y = pos.Y + pcsz - 1;
if !collide(z, p) { if !collide(z, p) {
undrawpiece(); undrawpiece();
pos.X = z.X; pos.X = z.X;
@ -312,28 +322,28 @@ func canfit(p *Piece) bool {
func score(p int) { func score(p int) {
points += p; points += p;
// snprint(buf, sizeof(buf), "%.6ld", points); // snprint(buf, sizeof(buf), "%.6ld", points);
// draw.Draw(screen, draw.Rpt(pscore, pscore.Add(scoresz)), draw.White, nil, draw.ZP); // draw.Draw(screen, draw.Rpt(pscore, pscore.Add(scoresz)), draw.White, nil, draw.ZP);
// string(screen, pscore, draw.Black, draw.ZP, font, buf); // string(screen, pscore, draw.Black, draw.ZP, font, buf);
} }
func drawsq(b draw.Image, p draw.Point, ptx int) { func drawsq(b draw.Image, p draw.Point, ptx int) {
var r draw.Rectangle; var r draw.Rectangle;
r.Min = p; r.Min = p;
r.Max.X = r.Min.X+pcsz; r.Max.X = r.Min.X + pcsz;
r.Max.Y = r.Min.Y+pcsz; r.Max.Y = r.Min.Y + pcsz;
draw.Draw(b, r, draw.Black, nil, draw.ZP); draw.Draw(b, r, draw.Black, nil, draw.ZP);
draw.Draw(b, r.Inset(1), txpix[ptx], nil, draw.ZP); draw.Draw(b, r.Inset(1), txpix[ptx], nil, draw.ZP);
} }
func drawboard() { func drawboard() {
draw.Border(screen, rboard.Inset(-2), 2, draw.Black, draw.ZP); draw.Border(screen, rboard.Inset(-2), 2, draw.Black, draw.ZP);
draw.Draw(screen, draw.Rect(rboard.Min.X, rboard.Min.Y-2, rboard.Max.X, rboard.Min.Y), draw.Draw(screen, draw.Rect(rboard.Min.X, rboard.Min.Y - 2, rboard.Max.X, rboard.Min.Y),
draw.White, nil, draw.ZP); draw.White, nil, draw.ZP);
for i:=0; i<NY; i++ { for i := 0; i < NY; i++ {
for j:=0; j<NX; j++ { for j := 0; j < NX; j++ {
if board[i][j] != 0 { if board[i][j] != 0 {
drawsq(screen, draw.Pt(rboard.Min.X+j*pcsz, rboard.Min.Y+i*pcsz), int(board[i][j]-16)); drawsq(screen, draw.Pt(rboard.Min.X + j*pcsz, rboard.Min.Y + i*pcsz), int(board[i][j]-16));
} }
} }
} }
@ -348,8 +358,8 @@ func choosepiece() {
i := rand.Intn(len(pieces)); i := rand.Intn(len(pieces));
setpiece(&pieces[i]); setpiece(&pieces[i]);
pos = rboard.Min; pos = rboard.Min;
pos.X += rand.Intn(NX)*pcsz; pos.X += rand.Intn(NX) * pcsz;
if !collide(draw.Pt(pos.X, pos.Y+pcsz-DY), piece) { if !collide(draw.Pt(pos.X, pos.Y + pcsz - DY), piece) {
break; break;
} }
} }
@ -359,7 +369,7 @@ func choosepiece() {
func movepiece() bool { func movepiece() bool {
var mask image.Image; var mask image.Image;
if collide(draw.Pt(pos.X, pos.Y+pcsz), piece) { if collide(draw.Pt(pos.X, pos.Y + pcsz), piece) {
return false; return false;
} }
if collider(pos, br2.Max) { if collider(pos, br2.Max) {
@ -374,11 +384,11 @@ func movepiece() bool {
func suspend(s bool) { func suspend(s bool) {
suspended = s; suspended = s;
/* /*
if suspended { if suspended {
setcursor(mousectl, &whitearrow); setcursor(mousectl, &whitearrow);
} else { } else {
setcursor(mousectl, nil); setcursor(mousectl, nil);
} }
*/ */
if !suspended { if !suspended {
drawpiece(); drawpiece();
@ -417,8 +427,8 @@ func pause(t int) {
func horiz() bool { func horiz() bool {
var lev [MAXN]int; var lev [MAXN]int;
h := 0; h := 0;
for i:=0; i<NY; i++ { for i := 0; i < NY; i++ {
for j:=0; board[i][j] != 0; j++ { for j := 0; board[i][j] != 0; j++ {
if j == NX-1 { if j == NX-1 {
lev[h] = i; lev[h] = i;
h++; h++;
@ -431,20 +441,20 @@ func horiz() bool {
} }
r := rboard; r := rboard;
newscreen = false; newscreen = false;
for j:=0; j<h; j++ { for j := 0; j < h; j++ {
r.Min.Y = rboard.Min.Y + lev[j]*pcsz; r.Min.Y = rboard.Min.Y + lev[j]*pcsz;
r.Max.Y = r.Min.Y + pcsz; r.Max.Y = r.Min.Y + pcsz;
draw.Draw(screen, r, draw.White, whitemask, draw.ZP); draw.Draw(screen, r, draw.White, whitemask, draw.ZP);
display.FlushImage(); display.FlushImage();
} }
PlaySound(whoosh); PlaySound(whoosh);
for i:=0; i<3; i++ { for i := 0; i < 3; i++ {
pause(250); pause(250);
if newscreen { if newscreen {
drawboard(); drawboard();
break; break;
} }
for j:=0; j<h; j++ { for j := 0; j < h; j++ {
r.Min.Y = rboard.Min.Y + lev[j]*pcsz; r.Min.Y = rboard.Min.Y + lev[j]*pcsz;
r.Max.Y = r.Min.Y + pcsz; r.Max.Y = r.Min.Y + pcsz;
draw.Draw(screen, r, draw.White, whitemask, draw.ZP); draw.Draw(screen, r, draw.White, whitemask, draw.ZP);
@ -452,13 +462,13 @@ func horiz() bool {
display.FlushImage(); display.FlushImage();
} }
r = rboard; r = rboard;
for j:=0; j<h; j++ { for j := 0; j < h; j++ {
i := NY - lev[j] - 1; i := NY-lev[j]-1;
score(250+10*i*i); score(250 + 10*i*i);
r.Min.Y = rboard.Min.Y; r.Min.Y = rboard.Min.Y;
r.Max.Y = rboard.Min.Y+lev[j]*pcsz; r.Max.Y = rboard.Min.Y + lev[j]*pcsz;
draw.Draw(screen, r.Add(draw.Pt(0,pcsz)), screen, nil, r.Min); draw.Draw(screen, r.Add(draw.Pt(0, pcsz)), screen, nil, r.Min);
r.Max.Y = rboard.Min.Y+pcsz; r.Max.Y = rboard.Min.Y + pcsz;
draw.Draw(screen, r, draw.White, nil, draw.ZP); draw.Draw(screen, r, draw.White, nil, draw.ZP);
for k := lev[j]-1; k >= 0; k-- { for k := lev[j]-1; k >= 0; k-- {
board[k+1] = board[k]; board[k+1] = board[k];
@ -470,8 +480,8 @@ func horiz() bool {
} }
func mright() { func mright() {
if !collide(draw.Pt(pos.X+pcsz, pos.Y), piece) if !collide(draw.Pt(pos.X + pcsz, pos.Y), piece) &&
&& !collide(draw.Pt(pos.X+pcsz, pos.Y+pcsz-DY), piece) { !collide(draw.Pt(pos.X + pcsz, pos.Y + pcsz - DY), piece) {
undrawpiece(); undrawpiece();
pos.X += pcsz; pos.X += pcsz;
drawpiece(); drawpiece();
@ -480,8 +490,8 @@ func mright() {
} }
func mleft() { func mleft() {
if !collide(draw.Pt(pos.X-pcsz, pos.Y), piece) if !collide(draw.Pt(pos.X - pcsz, pos.Y), piece) &&
&& !collide(draw.Pt(pos.X-pcsz, pos.Y+pcsz-DY), piece) { !collide(draw.Pt(pos.X - pcsz, pos.Y + pcsz - DY), piece) {
undrawpiece(); undrawpiece();
pos.X -= pcsz; pos.X -= pcsz;
drawpiece(); drawpiece();
@ -509,13 +519,13 @@ var fusst = 0
func drop(f bool) bool { func drop(f bool) bool {
if f { if f {
score(5*(rboard.Max.Y-pos.Y)/pcsz); score(5*(rboard.Max.Y - pos.Y)/pcsz);
for movepiece() { for movepiece() {
} }
} }
fusst = 0; fusst = 0;
rest(); rest();
if pos.Y==rboard.Min.Y && !horiz() { if pos.Y == rboard.Min.Y && !horiz() {
return true; return true;
} }
horiz(); horiz();
@ -536,7 +546,7 @@ func play() {
for { for {
select { select {
case mouse = <-mousec: case mouse = <-mousec:
if suspended { if suspended {
om = mouse; om = mouse;
break; break;
} }
@ -551,15 +561,15 @@ func play() {
mleft(); mleft();
lastmx = mouse.X; lastmx = mouse.X;
} }
if mouse.Buttons&^om.Buttons&1 == 1 { if mouse.Buttons &^ om.Buttons & 1 == 1 {
rleft(); rleft();
} }
if mouse.Buttons&^om.Buttons&2 == 2 { if mouse.Buttons &^ om.Buttons & 2 == 2 {
if drop(true) { if drop(true) {
return; return;
} }
} }
if mouse.Buttons&^om.Buttons&4 == 4 { if mouse.Buttons &^ om.Buttons & 4 == 4 {
rright(); rright();
} }
om = mouse; om = mouse;
@ -579,7 +589,7 @@ func play() {
if suspended { if suspended {
break; break;
} }
switch(r){ switch r {
case 'f', ';': case 'f', ';':
mright(); mright();
case 'a', 'j': case 'a', 'j':
@ -601,7 +611,7 @@ func play() {
dt -= tsleep; dt -= tsleep;
if dt < 0 { if dt < 0 {
i := 1; i := 1;
dt = 16 * (points+rand.Intn(10000)-5000) / 10000; dt = 16*(points + rand.Intn(10000) - 5000)/10000;
if dt >= 32 { if dt >= 32 {
i += (dt-32)/16; i += (dt-32)/16;
dt = 32; dt = 32;
@ -656,17 +666,17 @@ func suspproc() {
} }
func redraw(new bool) { func redraw(new bool) {
// if new && getwindow(display, Refmesg) < 0 { // if new && getwindow(display, Refmesg) < 0 {
// sysfatal("can't reattach to window"); // sysfatal("can't reattach to window");
// } // }
r := draw.Rect(0, 0, screen.Width(), screen.Height()); r := draw.Rect(0, 0, screen.Width(), screen.Height());
pos.X = (pos.X - rboard.Min.X) / pcsz; pos.X = (pos.X - rboard.Min.X)/pcsz;
pos.Y = (pos.Y - rboard.Min.Y) / pcsz; pos.Y = (pos.Y - rboard.Min.Y)/pcsz;
dx := r.Max.X - r.Min.X; dx := r.Max.X - r.Min.X;
dy := r.Max.Y - r.Min.Y - 2*32; dy := r.Max.Y - r.Min.Y - 2*32;
DY = dx / NX; DY = dx/NX;
if DY > dy / NY { if DY > dy/NY {
DY = dy / NY; DY = dy/NY;
} }
DY /= 8; DY /= 8;
if DY > 4 { if DY > 4 {
@ -678,19 +688,19 @@ func redraw(new bool) {
log.Exitf("screen too small: %d", pcsz); log.Exitf("screen too small: %d", pcsz);
} }
rboard = screenr; rboard = screenr;
rboard.Min.X += (dx-pcsz*NX)/2; rboard.Min.X += (dx - pcsz*NX)/2;
rboard.Min.Y += (dy-pcsz*NY)/2+32; rboard.Min.Y += (dy - pcsz*NY)/2 + 32;
rboard.Max.X = rboard.Min.X+NX*pcsz; rboard.Max.X = rboard.Min.X + NX*pcsz;
rboard.Max.Y = rboard.Min.Y+NY*pcsz; rboard.Max.Y = rboard.Min.Y + NY*pcsz;
pscore.X = rboard.Min.X+8; pscore.X = rboard.Min.X + 8;
pscore.Y = rboard.Min.Y-32; pscore.Y = rboard.Min.Y - 32;
// scoresz = stringsize(font, "000000"); // scoresz = stringsize(font, "000000");
pos.X = pos.X*pcsz + rboard.Min.X; pos.X = pos.X * pcsz + rboard.Min.X;
pos.Y = pos.Y*pcsz + rboard.Min.Y; pos.Y = pos.Y * pcsz + rboard.Min.Y;
bbr = draw.Rect(0, 0, N*pcsz, N*pcsz); bbr = draw.Rect(0, 0, N*pcsz, N*pcsz);
bb = image.NewRGBA(bbr.Max.X, bbr.Max.Y); bb = image.NewRGBA(bbr.Max.X, bbr.Max.Y);
bbmask = image.NewRGBA(bbr.Max.X, bbr.Max.Y); // actually just a bitmap bbmask = image.NewRGBA(bbr.Max.X, bbr.Max.Y); // actually just a bitmap
bb2r = draw.Rect(0, 0, N*pcsz, N*pcsz+DY); bb2r = draw.Rect(0, 0, N*pcsz, N*pcsz + DY);
bb2 = image.NewRGBA(bb2r.Dx(), bb2r.Dy()); bb2 = image.NewRGBA(bb2r.Dx(), bb2r.Dy());
bb2mask = image.NewRGBA(bb2r.Dx(), bb2r.Dy()); // actually just a bitmap bb2mask = image.NewRGBA(bb2r.Dx(), bb2r.Dy()); // actually just a bitmap
draw.Draw(screen, screenr, draw.White, nil, draw.ZP); draw.Draw(screen, screenr, draw.White, nil, draw.ZP);
@ -716,10 +726,10 @@ func Play(pp []Piece, ctxt draw.Context) {
pieces = pp; pieces = pp;
N = len(pieces[0].d); N = len(pieces[0].d);
initPieces(); initPieces();
rand.Seed(int32(time.Nanoseconds()%(1e9-1))); rand.Seed(int32(time.Nanoseconds() % (1e9-1)));
whitemask = draw.White.SetAlpha(0x7F); whitemask = draw.White.SetAlpha(0x7F);
tsleep = 50; tsleep = 50;
timerc = time.Tick(int64(tsleep/2) * 1e6); timerc = time.Tick(int64(tsleep/2)*1e6);
suspc = make(chan bool); suspc = make(chan bool);
mousec = make(chan draw.Mouse); mousec = make(chan draw.Mouse);
resizec = ctxt.ResizeChan(); resizec = ctxt.ResizeChan();

View file

@ -12,77 +12,77 @@ import "container/vector"
type Iterable interface { type Iterable interface {
// Iter should return a fresh channel each time it is called. // Iter should return a fresh channel each time it is called.
Iter() <-chan interface {} Iter() <-chan interface{};
} }
func not(f func(interface {}) bool) (func(interface {}) bool) { func not(f func(interface{}) bool) (func(interface{}) bool) {
return func(e interface {}) bool { return !f(e) } return func(e interface{}) bool { return !f(e) };
} }
// All tests whether f is true for every element of iter. // All tests whether f is true for every element of iter.
func All(iter Iterable, f func(interface {}) bool) bool { func All(iter Iterable, f func(interface{}) bool) bool {
for e := range iter.Iter() { for e := range iter.Iter() {
if !f(e) { if !f(e) {
return false return false;
} }
} }
return true return true;
} }
// Any tests whether f is true for at least one element of iter. // Any tests whether f is true for at least one element of iter.
func Any(iter Iterable, f func(interface {}) bool) bool { func Any(iter Iterable, f func(interface{}) bool) bool {
return !All(iter, not(f)) return !All(iter, not(f));
} }
// Data returns a slice containing the elements of iter. // Data returns a slice containing the elements of iter.
func Data(iter Iterable) []interface {} { func Data(iter Iterable) []interface{} {
vec := vector.New(0); vec := vector.New(0);
for e := range iter.Iter() { for e := range iter.Iter() {
vec.Push(e) vec.Push(e);
} }
return vec.Data() return vec.Data();
} }
// filteredIterable is a struct that implements Iterable with each element // filteredIterable is a struct that implements Iterable with each element
// passed through a filter. // passed through a filter.
type filteredIterable struct { type filteredIterable struct {
it Iterable; it Iterable;
f func(interface {}) bool; f func(interface{}) bool;
} }
func (f *filteredIterable) iterate(out chan<- interface {}) { func (f *filteredIterable) iterate(out chan<- interface{}) {
for e := range f.it.Iter() { for e := range f.it.Iter() {
if f.f(e) { if f.f(e) {
out <- e out <- e;
} }
} }
close(out) close(out);
} }
func (f *filteredIterable) Iter() <-chan interface {} { func (f *filteredIterable) Iter() <-chan interface{} {
ch := make(chan interface {}); ch := make(chan interface{});
go f.iterate(ch); go f.iterate(ch);
return ch; return ch;
} }
// Filter returns an Iterable that returns the elements of iter that satisfy f. // Filter returns an Iterable that returns the elements of iter that satisfy f.
func Filter(iter Iterable, f func(interface {}) bool) Iterable { func Filter(iter Iterable, f func(interface{}) bool) Iterable {
return &filteredIterable{ iter, f } return &filteredIterable{iter, f};
} }
// Find returns the first element of iter that satisfies f. // Find returns the first element of iter that satisfies f.
// Returns nil if no such element is found. // Returns nil if no such element is found.
func Find(iter Iterable, f func(interface {}) bool) interface {} { func Find(iter Iterable, f func(interface{}) bool) interface{} {
for e := range Filter(iter, f).Iter() { for e := range Filter(iter, f).Iter() {
return e return e;
} }
return nil return nil;
} }
// Injector is a type representing a function that takes two arguments, // Injector is a type representing a function that takes two arguments,
// an accumulated value and an element, and returns the next accumulated value. // an accumulated value and an element, and returns the next accumulated value.
// See the Inject function. // See the Inject function.
type Injector func(interface {}, interface {}) interface{}; type Injector func(interface{}, interface{}) interface{}
// Inject combines the elements of iter by repeatedly calling f with an // Inject combines the elements of iter by repeatedly calling f with an
// accumulated value and each element in order. The starting accumulated value // accumulated value and each element in order. The starting accumulated value
@ -92,42 +92,42 @@ type Injector func(interface {}, interface {}) interface{};
// sum := iterable.Inject(arr, 0, // sum := iterable.Inject(arr, 0,
// func(ax interface {}, x interface {}) interface {} { // func(ax interface {}, x interface {}) interface {} {
// return ax.(int) + x.(int) }).(int) // return ax.(int) + x.(int) }).(int)
func Inject(iter Iterable, initial interface {}, f Injector) interface {} { func Inject(iter Iterable, initial interface{}, f Injector) interface{} {
acc := initial; acc := initial;
for e := range iter.Iter() { for e := range iter.Iter() {
acc = f(acc, e) acc = f(acc, e);
} }
return acc return acc;
} }
// mappedIterable is a helper struct that implements Iterable, returned by Map. // mappedIterable is a helper struct that implements Iterable, returned by Map.
type mappedIterable struct { type mappedIterable struct {
it Iterable; it Iterable;
f func(interface {}) interface {}; f func(interface{}) interface{};
} }
func (m *mappedIterable) iterate(out chan<- interface {}) { func (m *mappedIterable) iterate(out chan<- interface{}) {
for e := range m.it.Iter() { for e := range m.it.Iter() {
out <- m.f(e) out <- m.f(e);
} }
close(out) close(out);
} }
func (m *mappedIterable) Iter() <-chan interface {} { func (m *mappedIterable) Iter() <-chan interface{} {
ch := make(chan interface {}); ch := make(chan interface{});
go m.iterate(ch); go m.iterate(ch);
return ch return ch;
} }
// Map returns an Iterable that returns the result of applying f to each // Map returns an Iterable that returns the result of applying f to each
// element of iter. // element of iter.
func Map(iter Iterable, f func(interface {}) interface {}) Iterable { func Map(iter Iterable, f func(interface{}) interface{}) Iterable {
return &mappedIterable{ iter, f } return &mappedIterable{iter, f};
} }
// Partition(iter, f) returns Filter(iter, f) and Filter(iter, !f). // Partition(iter, f) returns Filter(iter, f) and Filter(iter, !f).
func Partition(iter Iterable, f func(interface {}) bool) (Iterable, Iterable) { func Partition(iter Iterable, f func(interface{}) bool) (Iterable, Iterable) {
return Filter(iter, f), Filter(iter, not(f)) return Filter(iter, f), Filter(iter, not(f));
} }
// TODO: // TODO:

View file

@ -8,82 +8,83 @@ import (
"testing"; "testing";
) )
type IntArray []int; type IntArray []int
func (arr IntArray) Iter() <-chan interface {} { func (arr IntArray) Iter() <-chan interface{} {
ch := make(chan interface {}); ch := make(chan interface{});
go func() { go func() {
for _, x := range arr { for _, x := range arr {
ch <- x ch <- x;
} }
close(ch) close(ch);
}(); }();
return ch return ch;
} }
var oneToFive = IntArray{ 1, 2, 3, 4, 5 }; var oneToFive = IntArray{1, 2, 3, 4, 5}
func isNegative(n interface {}) bool { func isNegative(n interface{}) bool {
return n.(int) < 0 return n.(int) < 0;
} }
func isPositive(n interface {}) bool { func isPositive(n interface{}) bool {
return n.(int) > 0 return n.(int) > 0;
} }
func isAbove3(n interface {}) bool { func isAbove3(n interface{}) bool {
return n.(int) > 3 return n.(int) > 3;
} }
func isEven(n interface {}) bool { func isEven(n interface{}) bool {
return n.(int) % 2 == 0 return n.(int) % 2 == 0;
} }
func doubler(n interface {}) interface {} { func doubler(n interface{}) interface{} {
return n.(int) * 2 return n.(int) * 2;
} }
func addOne(n interface {}) interface {} { func addOne(n interface{}) interface{} {
return n.(int) + 1 return n.(int) + 1;
} }
func adder(acc interface {}, n interface {}) interface {} { func adder(acc interface{}, n interface{}) interface{} {
return acc.(int) + n.(int) return acc.(int) + n.(int);
} }
// A stream of the natural numbers: 0, 1, 2, 3, ... // A stream of the natural numbers: 0, 1, 2, 3, ...
type integerStream struct {} type integerStream struct{}
func (i integerStream) Iter() <-chan interface {} {
ch := make(chan interface {}); func (i integerStream) Iter() <-chan interface{} {
go func() { ch := make(chan interface{});
for i := 0; ; i++ { go func() {
ch <- i for i := 0; ; i++ {
} ch <- i;
}(); }
return ch }();
return ch;
} }
func TestAll(t *testing.T) { func TestAll(t *testing.T) {
if !All(oneToFive, isPositive) { if !All(oneToFive, isPositive) {
t.Error("All(oneToFive, isPositive) == false") t.Error("All(oneToFive, isPositive) == false");
} }
if All(oneToFive, isAbove3) { if All(oneToFive, isAbove3) {
t.Error("All(oneToFive, isAbove3) == true") t.Error("All(oneToFive, isAbove3) == true");
} }
} }
func TestAny(t *testing.T) { func TestAny(t *testing.T) {
if Any(oneToFive, isNegative) { if Any(oneToFive, isNegative) {
t.Error("Any(oneToFive, isNegative) == true") t.Error("Any(oneToFive, isNegative) == true");
} }
if !Any(oneToFive, isEven) { if !Any(oneToFive, isEven) {
t.Error("Any(oneToFive, isEven) == false") t.Error("Any(oneToFive, isEven) == false");
} }
} }
func assertArraysAreEqual(t *testing.T, res []interface {}, expected []int) { func assertArraysAreEqual(t *testing.T, res []interface{}, expected []int) {
if len(res) != len(expected) { if len(res) != len(expected) {
t.Errorf("len(res) = %v, want %v", len(res), len(expected)); t.Errorf("len(res) = %v, want %v", len(res), len(expected));
goto missing goto missing;
} }
for i := range res { for i := range res {
if v := res[i].(int); v != expected[i] { if v := res[i].(int); v != expected[i] {
t.Errorf("res[%v] = %v, want %v", i, v, expected[i]); t.Errorf("res[%v] = %v, want %v", i, v, expected[i]);
goto missing goto missing;
} }
} }
return; return;
@ -94,35 +95,35 @@ missing:
func TestFilter(t *testing.T) { func TestFilter(t *testing.T) {
ints := integerStream{}; ints := integerStream{};
moreInts := Filter(ints, isAbove3).Iter(); moreInts := Filter(ints, isAbove3).Iter();
res := make([]interface {}, 3); res := make([]interface{}, 3);
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
res[i] = <-moreInts; res[i] = <-moreInts;
} }
assertArraysAreEqual(t, res, []int{ 4, 5, 6 }) assertArraysAreEqual(t, res, []int{4, 5, 6});
} }
func TestFind(t *testing.T) { func TestFind(t *testing.T) {
ints := integerStream{}; ints := integerStream{};
first := Find(ints, isAbove3); first := Find(ints, isAbove3);
if first.(int) != 4 { if first.(int) != 4 {
t.Errorf("Find(ints, isAbove3) = %v, want 4", first) t.Errorf("Find(ints, isAbove3) = %v, want 4", first);
} }
} }
func TestInject(t *testing.T) { func TestInject(t *testing.T) {
res := Inject(oneToFive, 0, adder); res := Inject(oneToFive, 0, adder);
if res.(int) != 15 { if res.(int) != 15 {
t.Errorf("Inject(oneToFive, 0, adder) = %v, want 15", res) t.Errorf("Inject(oneToFive, 0, adder) = %v, want 15", res);
} }
} }
func TestMap(t *testing.T) { func TestMap(t *testing.T) {
res := Data(Map(Map(oneToFive, doubler), addOne)); res := Data(Map(Map(oneToFive, doubler), addOne));
assertArraysAreEqual(t, res, []int{ 3, 5, 7, 9, 11 }) assertArraysAreEqual(t, res, []int{3, 5, 7, 9, 11});
} }
func TestPartition(t *testing.T) { func TestPartition(t *testing.T) {
ti, fi := Partition(oneToFive, isEven); ti, fi := Partition(oneToFive, isEven);
assertArraysAreEqual(t, Data(ti), []int{ 2, 4 }); assertArraysAreEqual(t, Data(ti), []int{2, 4});
assertArraysAreEqual(t, Data(fi), []int{ 1, 3, 5 }) assertArraysAreEqual(t, Data(fi), []int{1, 3, 5});
} }