-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaiConnectron.test.mjs
More file actions
118 lines (103 loc) · 3.5 KB
/
aiConnectron.test.mjs
File metadata and controls
118 lines (103 loc) · 3.5 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { checkWin, evaluateBoard, minimax, bestMove } from './aiConnectron.mjs';
describe('Connect4 AI', () => {
let board;
beforeEach(() => {
board = [
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]
];
});
describe('checkWin', () => {
it('should return true if player 1 wins horizontally', () => {
board[0][0] = 1;
board[0][1] = 1;
board[0][2] = 1;
board[0][3] = 1;
expect(checkWin(1)).toBe(true);
});
it('should return true if player 2 wins vertically', () => {
board[0][0] = 2;
board[1][0] = 2;
board[2][0] = 2;
board[3][0] = 2;
expect(checkWin(2)).toBe(true);
});
it('should return true if player 1 wins diagonally (top-left to bottom-right)', () => {
board[0][0] = 1;
board[1][1] = 1;
board[2][2] = 1;
board[3][3] = 1;
expect(checkWin(1)).toBe(true);
});
it('should return true if player 2 wins diagonally (bottom-left to top-right)', () => {
board[0][3] = 2;
board[1][2] = 2;
board[2][1] = 2;
board[3][0] = 2;
expect(checkWin(2)).toBe(true);
});
it('should return false if there is no winner', () => {
expect(checkWin(1)).toBe(false);
expect(checkWin(2)).toBe(false);
});
});
describe('evaluateBoard', () => {
it('should return 1 if player 1 wins', () => {
board[0][0] = 1;
board[0][1] = 1;
board[0][2] = 1;
board[0][3] = 1;
expect(evaluateBoard()).toBe(1);
});
it('should return -1 if player 2 wins', () => {
board[0][0] = 2;
board[1][0] = 2;
board[2][0] = 2;
board[3][0] = 2;
expect(evaluateBoard()).toBe(-1);
});
it('should return 0 if there is no winner', () => {
expect(evaluateBoard()).toBe(0);
});
});
describe('minimax', () => {
it('should return 10 if player 2 has a winning move', () => {
board[0][0] = 2;
board[1][0] = 2;
board[2][0] = 2;
board[3][0] = 2;
expect(minimax(0, -Infinity, Infinity, true)).toBe(10);
});
it('should return -10 if player 1 has a winning move', () => {
board[0][0] = 1;
board[0][1] = 1;
board[0][2] = 1;
board[0][3] = 1;
expect(minimax(0, -Infinity, Infinity, false)).toBe(-10);
});
it('should return 0 if there is no winner', () => {
expect(minimax(0, -Infinity, Infinity, true)).toBe(0);
expect(minimax(0, -Infinity, Infinity, false)).toBe(0);
});
});
describe('bestMove', () => {
it('should return the column with the best move for player 2', () => {
board[0][0] = 2;
board[1][0] = 2;
board[2][0] = 2;
board[3][0] = 2;
expect(bestMove()).toBe(0);
});
it('should return the column with the best move for player 1', () => {
board[0][0] = 1;
board[0][1] = 1;
board[0][2] = 1;
board[0][3] = 1;
expect(bestMove()).toBe(0);
});
});
});