Spaces:
Sleeping
Sleeping
Delete game.js
Browse files
game.js
DELETED
|
@@ -1,275 +0,0 @@
|
|
| 1 |
-
class TicTacToeGame {
|
| 2 |
-
constructor() {
|
| 3 |
-
this.board = Array(9).fill('');
|
| 4 |
-
this.currentPlayer = 'X'; // Human is X, AI is O
|
| 5 |
-
this.gameActive = true;
|
| 6 |
-
this.cells = document.querySelectorAll('.cell');
|
| 7 |
-
this.gameStatus = document.getElementById('gameStatus');
|
| 8 |
-
|
| 9 |
-
this.initGame();
|
| 10 |
-
}
|
| 11 |
-
|
| 12 |
-
initGame() {
|
| 13 |
-
this.cells.forEach((cell, index) => {
|
| 14 |
-
cell.addEventListener('click', () => this.handleCellClick(index));
|
| 15 |
-
});
|
| 16 |
-
}
|
| 17 |
-
|
| 18 |
-
async handleCellClick(index) {
|
| 19 |
-
if (!this.gameActive || this.board[index] !== '' || this.currentPlayer !== 'X') {
|
| 20 |
-
return;
|
| 21 |
-
}
|
| 22 |
-
|
| 23 |
-
// Make human move
|
| 24 |
-
this.makeMove(index, 'X');
|
| 25 |
-
|
| 26 |
-
// Check for win/draw
|
| 27 |
-
if (this.checkGameEnd()) {
|
| 28 |
-
return;
|
| 29 |
-
}
|
| 30 |
-
|
| 31 |
-
// AI's turn
|
| 32 |
-
this.gameStatus.textContent = "Mistral is thinking...";
|
| 33 |
-
await this.makeAIMove();
|
| 34 |
-
}
|
| 35 |
-
|
| 36 |
-
makeMove(index, player) {
|
| 37 |
-
this.board[index] = player;
|
| 38 |
-
this.cells[index].textContent = player;
|
| 39 |
-
this.cells[index].classList.add(player.toLowerCase());
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
-
async makeAIMove() {
|
| 43 |
-
try {
|
| 44 |
-
const aiMove = await this.getAIMoveAndChat();
|
| 45 |
-
console.log('AI Move received:', aiMove);
|
| 46 |
-
console.log('Current board:', this.board);
|
| 47 |
-
console.log('Move valid?', aiMove.move >= 0 && aiMove.move < 9);
|
| 48 |
-
console.log('Square empty?', this.board[aiMove.move] === '');
|
| 49 |
-
|
| 50 |
-
if (aiMove && typeof aiMove.move === 'number' && aiMove.move >= 0 && aiMove.move < 9 && this.board[aiMove.move] === '') {
|
| 51 |
-
this.makeMove(aiMove.move, 'O');
|
| 52 |
-
|
| 53 |
-
// Add AI's chat message
|
| 54 |
-
if (aiMove.message) {
|
| 55 |
-
this.addChatMessage(aiMove.message, 'ai');
|
| 56 |
-
}
|
| 57 |
-
|
| 58 |
-
if (!this.checkGameEnd()) {
|
| 59 |
-
this.gameStatus.textContent = "Your turn! Click a square.";
|
| 60 |
-
}
|
| 61 |
-
} else {
|
| 62 |
-
// Fallback to random move
|
| 63 |
-
console.warn('AI move invalid, using fallback. Move:', aiMove.move, 'Board:', this.board);
|
| 64 |
-
const emptySpots = this.board.map((cell, index) => cell === '' ? index : null).filter(val => val !== null);
|
| 65 |
-
if (emptySpots.length > 0) {
|
| 66 |
-
const randomMove = emptySpots[Math.floor(Math.random() * emptySpots.length)];
|
| 67 |
-
this.makeMove(randomMove, 'O');
|
| 68 |
-
this.addChatMessage("Technical hiccup! But I'm still playing! 🤖", 'ai');
|
| 69 |
-
|
| 70 |
-
if (!this.checkGameEnd()) {
|
| 71 |
-
this.gameStatus.textContent = "Your turn! Click a square.";
|
| 72 |
-
}
|
| 73 |
-
} else {
|
| 74 |
-
this.gameStatus.textContent = "Game error - no valid moves!";
|
| 75 |
-
}
|
| 76 |
-
}
|
| 77 |
-
} catch (error) {
|
| 78 |
-
console.error('AI move failed:', error);
|
| 79 |
-
// Fallback to random move instead of error
|
| 80 |
-
const emptySpots = this.board.map((cell, index) => cell === '' ? index : null).filter(val => val !== null);
|
| 81 |
-
if (emptySpots.length > 0) {
|
| 82 |
-
const randomMove = emptySpots[Math.floor(Math.random() * emptySpots.length)];
|
| 83 |
-
this.makeMove(randomMove, 'O');
|
| 84 |
-
this.addChatMessage("Connection issues, but I'm improvising! 😅", 'ai');
|
| 85 |
-
|
| 86 |
-
if (!this.checkGameEnd()) {
|
| 87 |
-
this.gameStatus.textContent = "Your turn! Click a square.";
|
| 88 |
-
}
|
| 89 |
-
}
|
| 90 |
-
}
|
| 91 |
-
}
|
| 92 |
-
|
| 93 |
-
async getAIMoveAndChat() {
|
| 94 |
-
const response = await fetch("/ai-move", {
|
| 95 |
-
method: "POST",
|
| 96 |
-
headers: {
|
| 97 |
-
"Content-Type": "application/json",
|
| 98 |
-
},
|
| 99 |
-
body: JSON.stringify({
|
| 100 |
-
board: this.board
|
| 101 |
-
})
|
| 102 |
-
});
|
| 103 |
-
|
| 104 |
-
if (!response.ok) {
|
| 105 |
-
throw new Error(`HTTP error! status: ${response.status}`);
|
| 106 |
-
}
|
| 107 |
-
|
| 108 |
-
const data = await response.json();
|
| 109 |
-
return data;
|
| 110 |
-
}
|
| 111 |
-
|
| 112 |
-
async callMistralAPI(messages) {
|
| 113 |
-
const response = await fetch("/mistral-proxy", {
|
| 114 |
-
method: "POST",
|
| 115 |
-
headers: {
|
| 116 |
-
"Content-Type": "application/json",
|
| 117 |
-
},
|
| 118 |
-
body: JSON.stringify({
|
| 119 |
-
model: "mistral-large-latest",
|
| 120 |
-
messages: messages,
|
| 121 |
-
temperature: 0.7,
|
| 122 |
-
response_format: { type: "json_object" }
|
| 123 |
-
})
|
| 124 |
-
});
|
| 125 |
-
|
| 126 |
-
if (!response.ok) {
|
| 127 |
-
throw new Error(`HTTP error! status: ${response.status}`);
|
| 128 |
-
}
|
| 129 |
-
|
| 130 |
-
const data = await response.json();
|
| 131 |
-
return data.choices[0].message.content;
|
| 132 |
-
}
|
| 133 |
-
|
| 134 |
-
boardToString() {
|
| 135 |
-
let result = "";
|
| 136 |
-
for (let i = 0; i < 9; i += 3) {
|
| 137 |
-
result += `${this.board[i] || ' '} | ${this.board[i+1] || ' '} | ${this.board[i+2] || ' '}\n`;
|
| 138 |
-
if (i < 6) result += "---------\n";
|
| 139 |
-
}
|
| 140 |
-
return result;
|
| 141 |
-
}
|
| 142 |
-
|
| 143 |
-
checkGameEnd() {
|
| 144 |
-
const winner = this.checkWinner();
|
| 145 |
-
|
| 146 |
-
if (winner) {
|
| 147 |
-
this.gameActive = false;
|
| 148 |
-
if (winner === 'X') {
|
| 149 |
-
this.gameStatus.textContent = "🎉 You won! Impressive!";
|
| 150 |
-
this.addChatMessage("Wow! You actually beat me! 😱 Well played, human! 🏆", 'ai');
|
| 151 |
-
} else {
|
| 152 |
-
this.gameStatus.textContent = "💀 Mistral AI wins!";
|
| 153 |
-
this.addChatMessage("Victory is mine! 😈 Better luck next time! 🤖", 'ai');
|
| 154 |
-
}
|
| 155 |
-
return true;
|
| 156 |
-
}
|
| 157 |
-
|
| 158 |
-
if (this.board.every(cell => cell !== '')) {
|
| 159 |
-
this.gameActive = false;
|
| 160 |
-
this.gameStatus.textContent = "🤝 It's a draw!";
|
| 161 |
-
this.addChatMessage("A respectable draw! You're better than I expected! 🤝", 'ai');
|
| 162 |
-
return true;
|
| 163 |
-
}
|
| 164 |
-
|
| 165 |
-
return false;
|
| 166 |
-
}
|
| 167 |
-
|
| 168 |
-
checkWinner() {
|
| 169 |
-
const winPatterns = [
|
| 170 |
-
[0, 1, 2], [3, 4, 5], [6, 7, 8], // rows
|
| 171 |
-
[0, 3, 6], [1, 4, 7], [2, 5, 8], // columns
|
| 172 |
-
[0, 4, 8], [2, 4, 6] // diagonals
|
| 173 |
-
];
|
| 174 |
-
|
| 175 |
-
for (const pattern of winPatterns) {
|
| 176 |
-
const [a, b, c] = pattern;
|
| 177 |
-
if (this.board[a] && this.board[a] === this.board[b] && this.board[a] === this.board[c]) {
|
| 178 |
-
return this.board[a];
|
| 179 |
-
}
|
| 180 |
-
}
|
| 181 |
-
return null;
|
| 182 |
-
}
|
| 183 |
-
|
| 184 |
-
addChatMessage(message, sender) {
|
| 185 |
-
const chatMessages = document.getElementById('chatMessages');
|
| 186 |
-
const messageDiv = document.createElement('div');
|
| 187 |
-
messageDiv.className = `message ${sender}`;
|
| 188 |
-
|
| 189 |
-
const senderDiv = document.createElement('div');
|
| 190 |
-
senderDiv.className = 'message-sender';
|
| 191 |
-
senderDiv.textContent = sender === 'user' ? 'You:' : 'Mistral AI:';
|
| 192 |
-
|
| 193 |
-
const contentDiv = document.createElement('div');
|
| 194 |
-
contentDiv.textContent = message;
|
| 195 |
-
|
| 196 |
-
messageDiv.appendChild(senderDiv);
|
| 197 |
-
messageDiv.appendChild(contentDiv);
|
| 198 |
-
chatMessages.appendChild(messageDiv);
|
| 199 |
-
|
| 200 |
-
// Scroll to bottom
|
| 201 |
-
chatMessages.scrollTop = chatMessages.scrollHeight;
|
| 202 |
-
}
|
| 203 |
-
|
| 204 |
-
async sendChatMessage(message) {
|
| 205 |
-
if (!message.trim()) return;
|
| 206 |
-
|
| 207 |
-
// Add user message
|
| 208 |
-
this.addChatMessage(message, 'user');
|
| 209 |
-
|
| 210 |
-
try {
|
| 211 |
-
const aiResponse = await this.getAIChatResponse(message);
|
| 212 |
-
this.addChatMessage(aiResponse, 'ai');
|
| 213 |
-
} catch (error) {
|
| 214 |
-
console.error('Chat failed:', error);
|
| 215 |
-
this.addChatMessage("Sorry, I'm having trouble responding right now! 🤖", 'ai');
|
| 216 |
-
}
|
| 217 |
-
}
|
| 218 |
-
|
| 219 |
-
async getAIChatResponse(userMessage) {
|
| 220 |
-
const response = await fetch("/ai-chat", {
|
| 221 |
-
method: "POST",
|
| 222 |
-
headers: {
|
| 223 |
-
"Content-Type": "application/json",
|
| 224 |
-
},
|
| 225 |
-
body: JSON.stringify({
|
| 226 |
-
message: userMessage,
|
| 227 |
-
board: this.board
|
| 228 |
-
})
|
| 229 |
-
});
|
| 230 |
-
|
| 231 |
-
if (!response.ok) {
|
| 232 |
-
throw new Error(`HTTP error! status: ${response.status}`);
|
| 233 |
-
}
|
| 234 |
-
|
| 235 |
-
const data = await response.json();
|
| 236 |
-
return data.message;
|
| 237 |
-
}
|
| 238 |
-
}
|
| 239 |
-
|
| 240 |
-
// Initialize game when page loads
|
| 241 |
-
let game;
|
| 242 |
-
document.addEventListener('DOMContentLoaded', () => {
|
| 243 |
-
game = new TicTacToeGame();
|
| 244 |
-
});
|
| 245 |
-
|
| 246 |
-
// Chat functions
|
| 247 |
-
function sendChatMessage() {
|
| 248 |
-
const chatInput = document.getElementById('chatInput');
|
| 249 |
-
const message = chatInput.value.trim();
|
| 250 |
-
|
| 251 |
-
if (message) {
|
| 252 |
-
game.sendChatMessage(message);
|
| 253 |
-
chatInput.value = '';
|
| 254 |
-
}
|
| 255 |
-
}
|
| 256 |
-
|
| 257 |
-
function handleEnter(event) {
|
| 258 |
-
if (event.key === 'Enter') {
|
| 259 |
-
sendChatMessage();
|
| 260 |
-
}
|
| 261 |
-
}
|
| 262 |
-
|
| 263 |
-
// Reset game
|
| 264 |
-
function resetGame() {
|
| 265 |
-
game = new TicTacToeGame();
|
| 266 |
-
|
| 267 |
-
// Clear board visually
|
| 268 |
-
game.cells.forEach(cell => {
|
| 269 |
-
cell.textContent = '';
|
| 270 |
-
cell.classList.remove('x', 'o');
|
| 271 |
-
});
|
| 272 |
-
|
| 273 |
-
game.gameStatus.textContent = "Your turn! Click a square to play X";
|
| 274 |
-
game.addChatMessage("New game! Let's see if you can do better this time! 😏", 'ai');
|
| 275 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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