-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnect4_code.js
151 lines (140 loc) · 4.9 KB
/
connect4_code.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// BUSINESS LOGIC
// -----------------------------PURE LAYER-----------------------------
// Declare gamestate
const gameState = {
player: 'red',
maxTurn: 42,
turn: 0,
winnerPlayer: '',
winnerPlayerColour: '',
player1Name: '',
player2Name: '',
gameOver: false,
highscore: 0,
win: null,
grid: [
[null, null, null, null, null, null, null],
[null, null, null, null, null, null, null],
[null, null, null, null, null, null, null],
[null, null, null, null, null, null, null],
[null, null, null, null, null, null, null],
[null, null, null, null, null, null, null]
]
}
// Take the row and column number and update the game state and alternates player turn
function takeTurn (row, column) {
if (!gameState.gameOver && row !== null) {
gameState.turn++
gameState.highscore = gameState.maxTurn - gameState.turn
gameState.grid[row][column] = gameState.player
if (gameState.player === 'red') {
gameState.player = 'yellow'
} else if (gameState.player === 'yellow') {
gameState.player = 'red'
}
}
}
// find the lowest available empty slot in a column and row
function getLowestAvailableRowInColumn(columnNumber, myGrid) {
for (let i = 5; i >= 0; i--) {
if (myGrid[i][columnNumber] === null) {
return i
}
}
return null
}
// check 4 slots in a column = winner
function verticalWinner(grid){
for(let r = 0; r < 3; r++) {
for (let c = 0; c < 7; c++) {
if(grid[r][c] === 'red' && grid[r+1][c] === 'red' && grid[r+2][c] === 'red' && grid[r+3][c] === 'red') {
return 'red'
} if (grid[r][c] === 'yellow' && grid[r+1][c] === 'yellow' && grid[r+2][c] === 'yellow' && grid[r+3][c] === 'yellow') {
return 'yellow'
}
}
} return null
}
// check 4 slots in a row = winner
function horizontalWinner(grid){
for(let r = 0; r < 6; r++) {
for (let c = 0; c < 4; c++) {
if(grid[r][c] === 'red' && grid[r][c+1] === 'red' && grid[r][c+2] === 'red' && grid[r][c+3] === 'red') {
return 'red'
} if(grid[r][c] === 'yellow' && grid[r][c+1] === 'yellow' && grid[r][c+2] === 'yellow' && grid[r][c+3] === 'yellow') {
return 'yellow'
}
}
} return null
}
// check 4 slots upward diagonal = winner
function diagonalUpWinner(grid){
for(let r = 0; r < 3; r++) {
for (let c = 0; c < 7; c++) {
if(grid[r][c] === 'red' && grid[r+1][c+1] === 'red' && grid[r+2][c+2] === 'red' && grid[r+3][c+3] === 'red') {
return 'red'
} if(grid[r][c] === 'yellow' && grid[r+1][c+1] === 'yellow' && grid[r+2][c+2] === 'yellow' && grid[r+3][c+3] === 'yellow') {
return 'yellow'
}
}
}
return null
}
// check 4 slots downward diagonal = winner
function diagonalDownWinner(grid){
for(let r = 0; r < 3; r++) {
for (let c = 7; c > 2; c--) {
if(grid[r][c] === 'red' && grid[r+1][c-1] === 'red' && grid[r+2][c-2] === 'red' && grid[r+3][c-3] === 'red') {
return 'red'
} if(grid[r][c] === 'yellow' && grid[r+1][c-1] === 'yellow' && grid[r+2][c-2] === 'yellow' && grid[r+3][c-3] === 'yellow') {
return 'yellow'
}
}
}
return null
}
// check for no winners (when whole board is full ie turn = 42)
function nobodyWinner() {
if (gameState.turn === gameState.maxTurn) {
gameState.gameOver = true
gameState.win = 'nobody'
gameState.winnerPlayer = 'nobody'
gameState.winnerPlayerColour = 'blue'
return gameState.win
}
gameState.gameOver = false
return null
}
// resets gameState keys for a new game (with same player names though as last game)
function resetGame () {
gameState.grid = [
[null, null, null, null, null, null, null],
[null, null, null, null, null, null, null],
[null, null, null, null, null, null, null],
[null, null, null, null, null, null, null],
[null, null, null, null, null, null, null],
[null, null, null, null, null, null, null]
],
gameState.player = 'red',
gameState.turn = 0,
gameState.winnerPlayer = '',
gameState.winnerPlayerColour = '',
gameState.gameOver = false,
gameState.highscore = 0,
gameState.win = null,
console.log('resetGame was called')
}
// Pure functions/objects to be exported for testing in separate test file
if (typeof module !== 'undefined') {
module.exports = {
//gameState,
takeTurn,
getLowestAvailableRowInColumn,
verticalWinner,
horizontalWinner,
diagonalDownWinner,
diagonalUpWinner,
nobodyWinner,
resetGame
}
}