-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectron.js
More file actions
190 lines (170 loc) · 7.52 KB
/
connectron.js
File metadata and controls
190 lines (170 loc) · 7.52 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import readlineSync from 'readline-sync';
export default class Connectron {
static EMPTY_CELL_CONTENT = "O";
constructor(rows, columns, playerCount, lineLength) {
this.gridRows = rows; // 6 - 100
this.gridColumns = columns; // 6 - 100
this.grid = this.#createGrid(this.gridRows, this.gridColumns);
this.numberOfPlayers = playerCount; // 0 - 10
this.desiredLengthOfLine = lineLength; // 4 - 10
this.numberOfTurns = (this.gridRows * this.gridColumns) / (this.numberOfPlayers === 1 ? 2 : this.numberOfPlayers);
this.pointsTable = {};
}
//################## Private methods ##################
#createGrid = (rows, columns) => {
const outerArray = new Array(rows);
for (let i = 0; i < rows; i++) {
outerArray[i] = new Array(columns).fill(Connectron.EMPTY_CELL_CONTENT);
}
return outerArray;
};
#strategize = () => {
// Implement a strategy to place the counter
// For now, just place the counter in the first available column
for (let rowIndex = this.gridRows - 1; rowIndex >= 0; rowIndex--) {
for (let columnIndex = 0; columnIndex < this.gridColumns; columnIndex++) {
if (this.grid[rowIndex][columnIndex] === Connectron.EMPTY_CELL_CONTENT) {
return columnIndex;
}
}
}
return -1;
}
#findWinner = () => {
let winningPoints = 0;
let winners = [];
Object.keys(this.pointsTable). //
forEach( (player) => {
if(this.pointsTable[player] > winningPoints) {
winningPoints = this.pointsTable[player];
// Clear all winners
winners.splice(0);
winners[0] = player;
} else if(this.pointsTable[player] === winningPoints) {
winners.push(player);
}
});
if(winners.length === 1) {
console.log(`Player ${winners[0]} wins with ${winningPoints} point(s)`);
} else if(winners.length > 1) {
console.log(`It's a tie between players ${winners.join(', ')} with ${winningPoints} point(s) each`);
} else {
console.log(`No winner`);
}
};
#addCounterToColumn = (zeroBasedColumnIndex, playerCounterValue) => {
const columnIndex = zeroBasedColumnIndex + 1;
if (zeroBasedColumnIndex < 0 || zeroBasedColumnIndex >= this.gridColumns) {
console.log(`Column with index ${columnIndex} does not exist`);
return;
}
const filledZeroBasedRowIndex = this.grid.findIndex((element) => element[zeroBasedColumnIndex] !== Connectron.EMPTY_CELL_CONTENT);
const zeroBasedRowIndex = filledZeroBasedRowIndex === -1 ? this.gridRows - 1 : filledZeroBasedRowIndex - 1;
if (zeroBasedRowIndex < 0) {
console.log(`Column ${columnIndex} is full`);
return;
}
this.grid[zeroBasedRowIndex][zeroBasedColumnIndex] = playerCounterValue;
const rowIndex = zeroBasedRowIndex + 1;
console.log(`Counter '${playerCounterValue}' placed at row ${rowIndex}, column ${columnIndex}`);
console.log(`***************`);
this.calculatePoints();
console.log(`***************`);
this.printGrid();
};
//################## Public methods ##################
printGrid = () => {
let gridString = '';
for (let i = 0; i < this.grid.length; i++) {
gridString += `${this.grid[i].join(' ')}\n`;
}
console.log(`Grid State\n${gridString}`);
};
calculatePoints = () => {
console.log(`Calculating points`);
this.pointsTable = {};
const winningPoints = this.numberOfPlayers; // 0 - 10
for(let rowIndex = 0; rowIndex < this.gridRows; rowIndex++) {
for(let columnIndex = 0; columnIndex < this.gridColumns; columnIndex++) {
const cellValue = this.grid[rowIndex][columnIndex];
if (cellValue !== Connectron.EMPTY_CELL_CONTENT) {
// Check horizontally forward
if (columnIndex + this.desiredLengthOfLine <= this.gridColumns) {
let counterCount = 1;
for (let i = 1; i < this.desiredLengthOfLine; i++) {
if (this.grid[rowIndex][columnIndex + i] === cellValue) {
counterCount++;
}
}
if (counterCount === this.desiredLengthOfLine) {
this.pointsTable[cellValue] = this.pointsTable[cellValue] ? this.pointsTable[cellValue] + winningPoints : winningPoints;
console.log(`Player ${cellValue} won ${winningPoints} points horizontally from row ${rowIndex+1}, column ${columnIndex+1}`);
}
}
// Check vertically downward
if (rowIndex + this.desiredLengthOfLine <= this.gridRows) {
let counterCount = 1;
for (let i = 1; i < this.desiredLengthOfLine; i++) {
if (this.grid[rowIndex + i][columnIndex] === cellValue) {
counterCount++;
}
}
if (counterCount === this.desiredLengthOfLine) {
this.pointsTable[cellValue] = this.pointsTable[cellValue] ? this.pointsTable[cellValue] + winningPoints : winningPoints;
console.log(`Player ${cellValue} won ${winningPoints} points vertically from row ${rowIndex+1}, column ${columnIndex+1}`);
}
}
// Check diagonally downward
if(rowIndex + this.desiredLengthOfLine <= this.gridRows) {
// and forward
if (columnIndex + this.desiredLengthOfLine <= this.gridColumns) {
let counterCount = 1;
for (let i = 1; i < this.desiredLengthOfLine; i++) {
if (this.grid[rowIndex + i][columnIndex + i] === cellValue) {
counterCount++;
}
}
if (counterCount === this.desiredLengthOfLine) {
this.pointsTable[cellValue] = this.pointsTable[cellValue] ? this.pointsTable[cellValue] + winningPoints : winningPoints;
console.log(`Player ${cellValue} won ${winningPoints} points diagonally downward and forward from row ${rowIndex+1}, column ${columnIndex+1}`);
}
}
// and backward
if (columnIndex + 1 - this.desiredLengthOfLine >= 0) {
let counterCount = 1;
for (let i = 1; i < this.desiredLengthOfLine; i++) {
if (this.grid[rowIndex + i][columnIndex - i] === cellValue) {
counterCount++;
}
}
if (counterCount === this.desiredLengthOfLine) {
this.pointsTable[cellValue] = this.pointsTable[cellValue] ? this.pointsTable[cellValue] + winningPoints : winningPoints;
console.log(`Player ${cellValue} won ${winningPoints} points diagonally downward and backward from row ${rowIndex+1}, column ${columnIndex+1}`);
}
}
}
}
}
}
console.log(`Total points so far : ${JSON.stringify(this.pointsTable)}`);
};
play = () => {
this.printGrid();
for (let currentTurn = 1; currentTurn <= this.numberOfTurns; currentTurn++) {
console.log(`Turn #${currentTurn}`);
for (let currentPlayer = 1; currentPlayer <= this.numberOfPlayers; currentPlayer++) {
console.log(`Player${currentPlayer}'s turn`);
const column = readlineSync.question(`Enter column to place counter : `);
const zeroBasedColumnIndex = column - 1;
this.#addCounterToColumn( zeroBasedColumnIndex, currentPlayer);
if (this.numberOfPlayers === 1) {
// Computer should play against human
console.log(`Computer's turn`);
const zeroBasedColumnIndex = this.#strategize();
this.#addCounterToColumn( zeroBasedColumnIndex, "C");
}
}
}
this.#findWinner();
};
}