forked from victorKariuki/tictactoe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecommend.js
134 lines (116 loc) · 3.75 KB
/
recommend.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
const fs = require("fs/promises");
const path = require("path");
const gameStateFilePath = path.join(__dirname, "../assets/gameState.json");
const recommendedMoveFilePath = path.join(
__dirname,
"../assets/recommendedMove.json"
);
// Function to read the game state from a file
const readJsonFile = async (filePath) => {
try {
const data = await fs.readFile(filePath, "utf8");
return JSON.parse(data);
} catch (err) {
console.error(`Error reading file at ${filePath}:`, err);
throw err;
}
};
// Function to write JSON data to a file
const writeJsonFile = async (filePath, data) => {
try {
await fs.writeFile(filePath, JSON.stringify(data, null, 2));
console.log("Data saved successfully:", data);
} catch (err) {
console.error(`Error saving data to ${filePath}:`, err);
}
};
// Function to validate the game state
const validateGameState = (gameState) => {
if (!Array.isArray(gameState.gameBoard) || gameState.gameBoard.length !== 9) {
throw new Error("Invalid game board.");
}
if (!["X", "O"].includes(gameState.currentPlayer)) {
throw new Error("Invalid current player.");
}
return gameState;
};
// Function to check for a win
const checkWin = (gameBoard, currentPlayer) => {
const winningCombinations = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
return winningCombinations.some((combination) => {
const [a, b, c] = combination;
return (
gameBoard[a] === currentPlayer &&
gameBoard[b] === currentPlayer &&
gameBoard[c] === currentPlayer
);
});
};
// Function to recommend a move
const recommendMove = (gameBoard, currentPlayer) => {
const opponent = currentPlayer === "X" ? "O" : "X";
// Check if the current player can win
const winningMove = gameBoard
.map((cell, index) => {
if (cell === " ") {
const hypotheticalBoard = [...gameBoard];
hypotheticalBoard[index] = currentPlayer;
return checkWin(hypotheticalBoard, currentPlayer) ? index : null;
}
return null;
})
.find((move) => move !== null);
if (winningMove !== undefined) return winningMove;
// Check if the opponent can win and we need to block
const blockingMove = gameBoard
.map((cell, index) => {
if (cell === " ") {
const hypotheticalBoard = [...gameBoard];
hypotheticalBoard[index] = opponent;
return checkWin(hypotheticalBoard, opponent) ? index : null;
}
return null;
})
.find((move) => move !== null);
if (blockingMove !== undefined) return blockingMove;
// If no immediate win/block, recommend center or corners
const preferredMoves = [4, 0, 2, 6, 8]; // Center and corners first
const availablePreferredMove = preferredMoves.find(
(move) => gameBoard[move] === " "
);
if (availablePreferredMove !== undefined) return availablePreferredMove;
// If no preferred move is available, take the first available
const firstAvailableMove = gameBoard.findIndex((cell) => cell === " ");
return firstAvailableMove !== -1 ? firstAvailableMove : null; // Return null if no moves available
};
// Main function to execute the recommendation and saving
const main = async () => {
try {
const gameState = await readJsonFile(gameStateFilePath);
validateGameState(gameState);
const { gameBoard, currentPlayer } = gameState;
const move = recommendMove(gameBoard, currentPlayer);
if (move !== null) {
console.log(`Recommended move: ${move}`);
await writeJsonFile(recommendedMoveFilePath, {
move,
timestamp: new Date().toISOString(),
});
} else {
console.log("No available moves.");
}
} catch (error) {
console.error("Error:", error);
}
};
// Execute the main function
main();