-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtic-tac-toe-game.tsx
More file actions
194 lines (174 loc) · 6.57 KB
/
tic-tac-toe-game.tsx
File metadata and controls
194 lines (174 loc) · 6.57 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
"use client"
import { useState, useEffect } from "react"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { Badge } from "@/components/ui/badge"
import { GameBoard } from "./components/GameBoard"
import { GameStats } from "./components/GameStats"
import { type Board, type Player, type GameMode, createEmptyBoard, checkWinner, isDraw } from "./utils/gameLogic"
import { aiPlayer } from "./utils/aiPlayer"
export default function TicTacToeGame() {
const [board, setBoard] = useState<Board>(createEmptyBoard())
const [currentPlayer, setCurrentPlayer] = useState<Player>("X")
const [gameMode, setGameMode] = useState<GameMode | null>(null)
const [winner, setWinner] = useState<Player>(null)
const [isGameOver, setIsGameOver] = useState(false)
const [isAiThinking, setIsAiThinking] = useState(false)
const [playerStats, setPlayerStats] = useState({
wins: 0,
losses: 0,
draws: 0,
})
// Check for game end conditions
useEffect(() => {
const gameWinner = checkWinner(board)
const gameDraw = isDraw(board)
if (gameWinner || gameDraw) {
setWinner(gameWinner)
setIsGameOver(true)
// Update stats
if (gameMode === "ai") {
if (gameWinner === "X") {
setPlayerStats((prev) => ({ ...prev, wins: prev.wins + 1 }))
} else if (gameWinner === "O") {
setPlayerStats((prev) => ({ ...prev, losses: prev.losses + 1 }))
} else {
setPlayerStats((prev) => ({ ...prev, draws: prev.draws + 1 }))
}
}
}
}, [board, gameMode])
// AI move logic
useEffect(() => {
if (gameMode === "ai" && currentPlayer === "O" && !isGameOver) {
setIsAiThinking(true)
// Add delay to make AI thinking visible
const timer = setTimeout(() => {
const aiMove = aiPlayer.getBestMove(board)
if (aiMove !== -1) {
makeMove(aiMove, "O")
}
setIsAiThinking(false)
}, 500)
return () => clearTimeout(timer)
}
}, [currentPlayer, gameMode, board, isGameOver])
const makeMove = (index: number, player: Player) => {
if (board[index] !== null || isGameOver) return
const newBoard = [...board]
newBoard[index] = player
setBoard(newBoard)
setCurrentPlayer(player === "X" ? "O" : "X")
// Let AI learn from the move
if (gameMode === "ai" && player === "O") {
const gameWinner = checkWinner(newBoard)
if (gameWinner || isDraw(newBoard)) {
aiPlayer.learnFromGame(newBoard, gameWinner, index)
}
}
}
const handleCellClick = (index: number) => {
if (gameMode === "friend") {
makeMove(index, currentPlayer)
} else if (gameMode === "ai" && currentPlayer === "X") {
makeMove(index, "X")
}
}
const resetGame = () => {
setBoard(createEmptyBoard())
setCurrentPlayer("X")
setWinner(null)
setIsGameOver(false)
setIsAiThinking(false)
}
const startNewGame = (mode: GameMode) => {
setGameMode(mode)
resetGame()
}
const getGameStatus = () => {
if (isAiThinking) return "AI is thinking..."
if (winner) return `${winner} wins!`
if (isDraw(board)) 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-blue-50 to-purple-50 flex items-center justify-center p-4">
<Card className="w-full max-w-md">
<CardHeader className="text-center">
<CardTitle className="text-3xl font-bold bg-gradient-to-r from-blue-600 to-purple-600 bg-clip-text text-transparent">
Tic-Tac-Toe ML
</CardTitle>
<p className="text-gray-600">Choose your game mode</p>
</CardHeader>
<CardContent className="space-y-4">
<Button onClick={() => startNewGame("ai")} className="w-full h-16 text-lg" variant="default">
🤖 Play vs AI
<Badge variant="secondary" className="ml-2">
Machine Learning
</Badge>
</Button>
<Button onClick={() => startNewGame("friend")} className="w-full h-16 text-lg" variant="outline">
👥 Play vs Friend
</Button>
</CardContent>
</Card>
</div>
)
}
return (
<div className="min-h-screen bg-gradient-to-br from-blue-50 to-purple-50 p-4">
<div className="max-w-4xl mx-auto">
<div className="text-center mb-6">
<h1 className="text-4xl font-bold bg-gradient-to-r from-blue-600 to-purple-600 bg-clip-text text-transparent mb-2">
Tic-Tac-Toe ML
</h1>
<div className="flex items-center justify-center gap-4 mb-4">
<Badge variant={gameMode === "ai" ? "default" : "outline"}>
{gameMode === "ai" ? "🤖 vs AI" : "👥 vs Friend"}
</Badge>
<p className="text-lg font-medium">{getGameStatus()}</p>
</div>
</div>
<div className="grid lg:grid-cols-2 gap-8">
<Card>
<CardContent className="p-6">
<GameBoard
board={board}
onCellClick={handleCellClick}
disabled={isGameOver || isAiThinking || (gameMode === "ai" && currentPlayer === "O")}
/>
<div className="flex gap-4 mt-6 justify-center">
<Button onClick={resetGame} variant="outline">
Reset Game
</Button>
<Button onClick={() => setGameMode(null)} variant="outline">
Change Mode
</Button>
</div>
</CardContent>
</Card>
{gameMode === "ai" && (
<Card>
<CardHeader>
<CardTitle>AI Learning Progress</CardTitle>
</CardHeader>
<CardContent>
<GameStats aiStats={aiPlayer.getStats()} playerStats={playerStats} />
<div className="mt-4 p-3 bg-blue-50 rounded-lg">
<p className="text-sm text-blue-800">
<strong>How it works:</strong> The AI uses a minimax algorithm combined with machine learning. It
starts with some randomness to explore different strategies, then gradually becomes more strategic
as it learns from each game.
</p>
</div>
</CardContent>
</Card>
)}
</div>
</div>
</div>
)
}