-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtic-tac-toe-ml.tsx
More file actions
300 lines (273 loc) Β· 11.1 KB
/
tic-tac-toe-ml.tsx
File metadata and controls
300 lines (273 loc) Β· 11.1 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
"use client"
import { useState, useEffect, useCallback } from "react"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { Badge } from "@/components/ui/badge"
import { Board } from "./components/Board"
import { AIStats } from "./components/AIStats"
import { qLearningAI } from "./ai/qlearning-ai"
import { createEmptyBoard, checkWinner, isDraw, WINNING_COMBINATIONS } from "./utils/game-utils"
import type { Board as BoardType, Cell, GameMode, GameStats } from "./types/game"
export default function TicTacToeML() {
const [board, setBoard] = useState<BoardType>(createEmptyBoard())
const [currentPlayer, setCurrentPlayer] = useState<Cell>("X")
const [gameMode, setGameMode] = useState<GameMode | null>(null)
const [gameState, setGameState] = useState<"playing" | "won" | "draw">("playing")
const [winner, setWinner] = useState<Cell>(null)
const [isAIThinking, setIsAIThinking] = useState(false)
const [winningCells, setWinningCells] = useState<number[]>([])
const [gameStats, setGameStats] = useState<GameStats>({
playerWins: 0,
aiWins: 0,
draws: 0,
totalGames: 0,
})
const [prevBoard, setPrevBoard] = useState<BoardType | null>(null)
const [lastAIMove, setLastAIMove] = useState<number | null>(null)
const findWinningCells = useCallback((board: BoardType): number[] => {
for (const combination of WINNING_COMBINATIONS) {
const [a, b, c] = combination
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
return combination
}
}
return []
}, [])
const updateGameState = useCallback(() => {
const gameWinner = checkWinner(board)
const gameDraw = isDraw(board)
if (gameWinner) {
setWinner(gameWinner)
setGameState("won")
setWinningCells(findWinningCells(board))
// Update stats
setGameStats((prev) => ({
...prev,
totalGames: prev.totalGames + 1,
playerWins: gameWinner === "X" ? prev.playerWins + 1 : prev.playerWins,
aiWins: gameWinner === "O" ? prev.aiWins + 1 : prev.aiWins,
}))
// AI learning update
if (gameMode === "ai" && prevBoard && lastAIMove !== null) {
const reward = gameWinner === "O" ? 10 : gameWinner === "X" ? -10 : 0
qLearningAI.updateQValue(prevBoard, lastAIMove, board, reward)
qLearningAI.gameFinished(board, gameWinner)
}
} else if (gameDraw) {
setGameState("draw")
setGameStats((prev) => ({
...prev,
totalGames: prev.totalGames + 1,
draws: prev.draws + 1,
}))
// AI learning update for draw
if (gameMode === "ai" && prevBoard && lastAIMove !== null) {
qLearningAI.updateQValue(prevBoard, lastAIMove, board, 0)
qLearningAI.gameFinished(board, null)
}
}
}, [board, gameMode, prevBoard, lastAIMove, findWinningCells])
useEffect(() => {
updateGameState()
}, [updateGameState])
// AI move logic
useEffect(() => {
if (gameMode === "ai" && currentPlayer === "O" && gameState === "playing") {
setIsAIThinking(true)
const timer = setTimeout(() => {
const aiMove = qLearningAI.getBestMove(board)
if (aiMove !== -1) {
setPrevBoard([...board])
setLastAIMove(aiMove)
makeMove(aiMove, "O")
}
setIsAIThinking(false)
}, 800) // Longer delay to show AI thinking
return () => clearTimeout(timer)
}
}, [currentPlayer, gameMode, gameState, board])
const makeMove = (index: number, player: Cell) => {
if (board[index] !== null || gameState !== "playing") return
const newBoard = [...board]
newBoard[index] = player
setBoard(newBoard)
setCurrentPlayer(player === "X" ? "O" : "X")
// Update Q-values for intermediate moves
if (gameMode === "ai" && player === "O" && prevBoard && lastAIMove !== null) {
const reward = 0 // Intermediate reward
qLearningAI.updateQValue(prevBoard, lastAIMove, newBoard, reward)
}
}
const handleCellClick = (index: number) => {
if (gameMode === "friend") {
makeMove(index, currentPlayer)
} else if (gameMode === "ai" && currentPlayer === "X" && !isAIThinking) {
makeMove(index, "X")
}
}
const resetGame = () => {
setBoard(createEmptyBoard())
setCurrentPlayer("X")
setWinner(null)
setGameState("playing")
setWinningCells([])
setIsAIThinking(false)
setPrevBoard(null)
setLastAIMove(null)
}
const startNewGame = (mode: GameMode) => {
setGameMode(mode)
resetGame()
}
const resetAI = () => {
qLearningAI.reset()
setGameStats({
playerWins: 0,
aiWins: 0,
draws: 0,
totalGames: 0,
})
}
const getStatusMessage = () => {
if (isAIThinking) return "π€ AI is thinking..."
if (winner) return `π ${winner === "X" ? "You win!" : winner === "O" ? "AI wins!" : "Winner!"}`
if (gameState === "draw") return "π€ It's a draw!"
if (gameMode === "friend") return `Player ${currentPlayer}'s turn`
if (gameMode === "ai") return currentPlayer === "X" ? "Your turn" : "AI's turn"
return ""
}
if (!gameMode) {
return (
<div className="min-h-screen bg-gradient-to-br from-indigo-100 via-purple-50 to-pink-100 flex items-center justify-center p-4">
<Card className="w-full max-w-lg shadow-2xl">
<CardHeader className="text-center pb-2">
<CardTitle className="text-4xl font-bold bg-gradient-to-r from-indigo-600 to-purple-600 bg-clip-text text-transparent">
Tic-Tac-Toe ML
</CardTitle>
<p className="text-gray-600 mt-2">Experience AI that learns as you play</p>
</CardHeader>
<CardContent className="space-y-6 pt-4">
<Button
onClick={() => startNewGame("ai")}
className="w-full h-16 text-lg bg-gradient-to-r from-purple-600 to-indigo-600 hover:from-purple-700 hover:to-indigo-700"
>
π€ Challenge the AI
<Badge variant="secondary" className="ml-2">
Q-Learning
</Badge>
</Button>
<Button onClick={() => startNewGame("friend")} className="w-full h-16 text-lg" variant="outline">
π₯ Play with Friend
</Button>
</CardContent>
</Card>
</div>
)
}
return (
<div className="min-h-screen bg-gradient-to-br from-indigo-100 via-purple-50 to-pink-100 p-4">
<div className="max-w-6xl mx-auto">
{/* Header */}
<div className="text-center mb-8">
<h1 className="text-5xl font-bold bg-gradient-to-r from-indigo-600 to-purple-600 bg-clip-text text-transparent mb-4">
Tic-Tac-Toe ML
</h1>
<div className="flex items-center justify-center gap-4 mb-4">
<Badge variant={gameMode === "ai" ? "default" : "outline"} className="text-lg px-4 py-2">
{gameMode === "ai" ? "π€ vs Q-Learning AI" : "π₯ vs Friend"}
</Badge>
</div>
<p className="text-2xl font-semibold text-gray-700">{getStatusMessage()}</p>
</div>
<div className="grid lg:grid-cols-3 gap-8">
{/* Game Board */}
<div className="lg:col-span-2">
<Card className="shadow-xl">
<CardContent className="p-8">
<Board
board={board}
onCellClick={handleCellClick}
disabled={gameState !== "playing" || isAIThinking || (gameMode === "ai" && currentPlayer === "O")}
winningCells={winningCells}
/>
<div className="flex gap-4 mt-8 justify-center">
<Button onClick={resetGame} variant="outline" size="lg">
π New Game
</Button>
<Button onClick={() => setGameMode(null)} variant="outline" size="lg">
π Main Menu
</Button>
{gameMode === "ai" && (
<Button onClick={resetAI} variant="destructive" size="lg">
π§ Reset AI
</Button>
)}
</div>
</CardContent>
</Card>
</div>
{/* Stats Panel */}
<div className="space-y-6">
{/* Game Stats */}
<Card className="shadow-xl">
<CardHeader>
<CardTitle>π Game Statistics</CardTitle>
</CardHeader>
<CardContent>
<div className="space-y-4">
<div className="grid grid-cols-3 gap-2 text-center">
<div className="bg-blue-50 p-3 rounded-lg">
<div className="text-2xl font-bold text-blue-600">{gameStats.playerWins}</div>
<div className="text-sm text-gray-600">Your Wins</div>
</div>
<div className="bg-gray-50 p-3 rounded-lg">
<div className="text-2xl font-bold text-gray-600">{gameStats.draws}</div>
<div className="text-sm text-gray-600">Draws</div>
</div>
<div className="bg-red-50 p-3 rounded-lg">
<div className="text-2xl font-bold text-red-600">{gameStats.aiWins}</div>
<div className="text-sm text-gray-600">AI Wins</div>
</div>
</div>
<div className="text-center">
<div className="text-lg font-semibold">Total Games: {gameStats.totalGames}</div>
{gameStats.totalGames > 0 && (
<div className="text-sm text-gray-600">
Win Rate: {((gameStats.playerWins / gameStats.totalGames) * 100).toFixed(1)}%
</div>
)}
</div>
</div>
</CardContent>
</Card>
{/* AI Stats */}
{gameMode === "ai" && <AIStats stats={qLearningAI.getStats()} />}
{/* How it Works */}
{gameMode === "ai" && (
<Card className="shadow-xl">
<CardHeader>
<CardTitle>π§ How Q-Learning Works</CardTitle>
</CardHeader>
<CardContent className="text-sm text-gray-600 space-y-2">
<p>
<strong>Exploration:</strong> AI tries random moves to discover new strategies
</p>
<p>
<strong>Learning:</strong> AI updates its knowledge based on game outcomes
</p>
<p>
<strong>Exploitation:</strong> AI uses learned knowledge to make optimal moves
</p>
<p className="text-xs mt-4 p-3 bg-blue-50 rounded">
The AI starts by exploring randomly, then gradually becomes more strategic as it learns from wins
and losses.
</p>
</CardContent>
</Card>
)}
</div>
</div>
</div>
</div>
)
}