~bigbes/game-prototype-ftl

ref: c3461b001847b1677f79a3876fde63902df8726e game-prototype-ftl/tiles_test.go -rw-r--r-- 2.4 KiB
c3461b00 — Eugene Blikh docs(crew-movement): record review conclusion 30 days ago
                                                                                
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main

import (
	"reflect"
	"testing"
)

func TestWalkableTiles_coversAllRooms(t *testing.T) {
	s := NewPlayerShip()
	walk := walkableTiles(s)
	if len(walk) != 54 {
		t.Fatalf("expected 54 walkable tiles, got %d", len(walk))
	}
	// Spot-check one tile from each room
	checks := [][2]int{
		{4, 10}, // Pilot center
		{7, 7},  // Weapons
		{7, 10}, // Shields
		{7, 13}, // MedBay
		{11, 10}, // Engines center
	}
	for _, tc := range checks {
		if !walk[tc] {
			t.Errorf("expected tile %v to be walkable", tc)
		}
	}
	// Off-ship tile must be absent
	if walk[[2]int{0, 0}] {
		t.Error("tile (0,0) must not be walkable")
	}
}

func TestWalkableTiles_ignoresOutside(t *testing.T) {
	s := NewPlayerShip()
	walk := walkableTiles(s)
	if walk[[2]int{0, 0}] {
		t.Error("tile (0,0) must not be walkable")
	}
	if walk[[2]int{20, 20}] {
		t.Error("tile (20,20) must not be walkable")
	}
}

func TestBFSPath_sameTile(t *testing.T) {
	s := NewPlayerShip()
	walk := walkableTiles(s)
	path := bfsPath(walk, [2]int{4, 10}, [2]int{4, 10})
	if len(path) != 0 {
		t.Errorf("expected empty path for same-tile, got %v", path)
	}
}

func TestBFSPath_adjacentTile(t *testing.T) {
	s := NewPlayerShip()
	walk := walkableTiles(s)
	path := bfsPath(walk, [2]int{4, 10}, [2]int{5, 10})
	if len(path) != 1 {
		t.Fatalf("expected path of length 1, got %d: %v", len(path), path)
	}
	if path[0] != ([2]int{5, 10}) {
		t.Errorf("expected path[0] == {5,10}, got %v", path[0])
	}
}

func TestBFSPath_unreachable(t *testing.T) {
	s := NewPlayerShip()
	walk := walkableTiles(s)
	path := bfsPath(walk, [2]int{4, 10}, [2]int{0, 0})
	if len(path) != 0 {
		t.Errorf("expected empty path for unreachable target, got %v", path)
	}
}

func TestBFSPath_throughShields(t *testing.T) {
	s := NewPlayerShip()
	walk := walkableTiles(s)
	from := [2]int{4, 10}
	to := [2]int{11, 10}
	path := bfsPath(walk, from, to)
	if len(path) != 7 {
		t.Fatalf("expected path length 7, got %d: %v", len(path), path)
	}
	if path[len(path)-1] != to {
		t.Errorf("expected last tile %v, got %v", to, path[len(path)-1])
	}
	for _, tile := range path {
		if !walk[tile] {
			t.Errorf("path contains non-walkable tile %v", tile)
		}
	}
}

func TestBFSPath_deterministic(t *testing.T) {
	s := NewPlayerShip()
	walk := walkableTiles(s)
	from := [2]int{4, 10}
	to := [2]int{11, 10}
	p1 := bfsPath(walk, from, to)
	p2 := bfsPath(walk, from, to)
	if !reflect.DeepEqual(p1, p2) {
		t.Errorf("non-deterministic: first=%v second=%v", p1, p2)
	}
}