Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

board_test.gno

1.66 Kb · 68 lines
 1package connect4_v4
 2
 3import "testing"
 4
 5func play(b *board, p byte, colsPlayed ...int) (int, int) {
 6	c, r := 0, 0
 7	for _, c = range colsPlayed {
 8		r = b.drop(c, p)
 9	}
10	return c, r
11}
12
13func TestWins(t *testing.T) {
14	cases := []struct {
15		name  string
16		setup func(b *board) (int, int)
17		want  bool
18	}{
19		{"horizontal", func(b *board) (int, int) { return play(b, 1, 0, 1, 2, 3) }, true},
20		{"vertical", func(b *board) (int, int) { return play(b, 1, 4, 4, 4, 4) }, true},
21		{"three only", func(b *board) (int, int) { return play(b, 1, 0, 1, 2) }, false},
22		{"diagonal up", func(b *board) (int, int) {
23			play(b, 2, 1, 2, 2, 3, 3, 3)
24			return play(b, 1, 0, 1, 2, 3)
25		}, true},
26		{"diagonal down", func(b *board) (int, int) {
27			play(b, 2, 3, 3, 3, 2, 2, 1)
28			return play(b, 1, 3, 2, 1, 0)
29		}, true},
30		{"gap breaks line", func(b *board) (int, int) { return play(b, 1, 0, 1, 3, 4) }, false},
31	}
32	for _, tc := range cases {
33		var b board
34		c, r := tc.setup(&b)
35		if got := b.wins(c, r); got != tc.want {
36			t.Errorf("%s: wins=%v, want %v", tc.name, got, tc.want)
37		}
38	}
39}
40
41func TestDropFullColumn(t *testing.T) {
42	var b board
43	for i := 0; i < rows; i++ {
44		if b.drop(0, 1) != i {
45			t.Fatalf("drop %d landed on wrong row", i)
46		}
47	}
48	if b.drop(0, 1) != -1 {
49		t.Fatal("drop into full column should return -1")
50	}
51}
52
53func TestWinLine(t *testing.T) {
54	var b board
55	if c, r := play(&b, 1, 0, 1, 2); b.winLine(c, r) != nil {
56		t.Fatal("three in a row is not a win")
57	}
58	c, r := play(&b, 1, 3)
59	line := b.winLine(c, r)
60	if len(line) != 4 {
61		t.Fatalf("want 4 winning cells, got %v", line)
62	}
63	for i, cell := range line {
64		if cell != [2]int{i, 0} {
65			t.Fatalf("cell %d = %v, want [%d 0]", i, cell, i)
66		}
67	}
68}