test/ai.test.js   A
last analyzed

Complexity

Total Complexity 16
Complexity/F 1

Size

Lines of Code 149
Function Count 16

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 0
loc 149
rs 10
wmc 16
mnd 0
bc 14
fnc 16
bpm 0.875
cpm 1
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A ai.test.js ➔ ??? 0 1 1
1
"use strict";
2
3
/* global describe it expect */
4
const { bestMove, randomMove } = require("../src/ai.js");
5
6
const empty10x10 = Array.from({ length: 10 * 10 }, () => 0);
7
const full10x10 = Array.from({ length: 10 * 10 }, (_, i) => i % 2 + 1);
8
9
const allButFirstFull10x10 = (() => {
10
    let t = [...full10x10];
11
12
    t[0] = 0;
13
    return t;
14
})();
15
16
const allButLastFull10x10 = (() => {
17
    let t = [...full10x10];
18
19
    t[99] = 0;
20
    return t;
21
})();
22
23
describe("randomMove", () => {
24
    it("should generate position", () => {
25
        const pos = randomMove(empty10x10, 10);
26
27
        expect(pos).toHaveProperty("x");
28
        expect(pos).toHaveProperty("y");
29
    });
30
31
    it("should return null if board is full", () => {
32
        const pos = randomMove(full10x10, 10);
33
34
        expect(pos).toBe(null);
35
    });
36
37
    it("should return first pos on all but first", () => {
38
        const pos = bestMove(allButFirstFull10x10, 10);
39
40
        expect(pos).toEqual({ x: 0, y: 0 });
41
    });
42
43
    it("should return last pos on all but last", () => {
44
        const pos = bestMove(allButLastFull10x10, 10);
45
46
        expect(pos).toEqual({ x: 9, y: 9 });
47
    });
48
});
49
50
describe("bestMove", () => {
51
    it("should generate position", () => {
52
        const pos = bestMove(empty10x10, 10);
53
54
        expect(pos).toHaveProperty("x");
55
        expect(pos).toHaveProperty("y");
56
    });
57
58
    it("should return null if board is full", () => {
59
        const pos = bestMove(full10x10, 10);
60
61
        expect(pos).toBe(null);
62
    });
63
64
    it("should place winning move", () => {
65
        const size = 10;
66
        // prettier-ignore
67
        const board = [
68
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72
            0, 0, 0, 0, 1, 2, 0, 0, 0, 0,
73
            0, 0, 0, 0, 1, 2, 0, 0, 0, 0,
74
            0, 0, 0, 0, 1, 2, 0, 0, 0, 0,
75
            0, 0, 0, 0, 1, 2, 0, 0, 0, 0,
76
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78
        ];
79
80
        // prettier-ignore
81
        const winningBoard = [
82
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85
            0, 0, 0, 0, 1, 0, 0, 0, 0, 0, //<-- First winning pos
86
            0, 0, 0, 0, 1, 2, 0, 0, 0, 0,
87
            0, 0, 0, 0, 1, 2, 0, 0, 0, 0,
88
            0, 0, 0, 0, 1, 2, 0, 0, 0, 0,
89
            0, 0, 0, 0, 1, 2, 0, 0, 0, 0,
90
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
92
        ];
93
94
        const pos = bestMove(board, size);
95
96
        board[pos.x + pos.y * size] = 1;
97
98
        expect(board).toEqual(winningBoard);
99
    });
100
101
    it("should place blocking move", () => {
102
        const size = 10;
103
        // prettier-ignore
104
        const board = [
105
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
107
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
108
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
109
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
110
            0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
111
            0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
112
            0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
113
            0, 0, 0, 0, 2, 2, 2, 0, 0, 0,
114
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
115
        ];
116
117
        // prettier-ignore
118
        const blockingBoard = [
119
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
120
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
121
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
122
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
124
            0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
125
            0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
126
            0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
127
            0, 0, 0, 1, 2, 2, 2, 0, 0, 0, //<-- First blocking pos
128
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
129
        ];
130
131
        const pos = bestMove(board, size);
132
133
        board[pos.x + pos.y * size] = 1;
134
135
        expect(board).toEqual(blockingBoard);
136
    });
137
138
    it("should return first pos on all but first", () => {
139
        const pos = bestMove(allButFirstFull10x10, 10);
140
141
        expect(pos).toEqual({ x: 0, y: 0 });
142
    });
143
144
    it("should return last pos on all but last", () => {
145
        const pos = bestMove(allButLastFull10x10, 10);
146
147
        expect(pos).toEqual({ x: 9, y: 9 });
148
    });
149
});
150