Thanks to visit codestin.com
Credit goes to github.com

Skip to content

concutato/ak47bmt

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

15 Commits
ย 
ย 

Repository files navigation

import React, { useState, useEffect } from 'react'; import './App.css';

const WINNING_COMBINATIONS = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], // rows [0, 3, 6], [1, 4, 7], [2, 5, 8], // columns [0, 4, 8], [2, 4, 6] // diagonals ];

const App = () => { const [board, setBoard] = useState(Array(9).fill(null)); const [isXNext, setIsXNext] = useState(true); const [winner, setWinner] = useState(null); const [gameOver, setGameOver] = useState(false); const [moveHistory, setMoveHistory] = useState([]); const [botThinking, setBotThinking] = useState(false);

useEffect(() => { if (!isXNext && !gameOver) { setBotThinking(true); setTimeout(() => { botMove(); setBotThinking(false); }, 1000); } }, [isXNext, board, gameOver]);

const handleClick = (index) => { if (board[index] || gameOver) return;

const newBoard = [...board];
newBoard[index] = 'X';
setBoard(newBoard);
setMoveHistory([...moveHistory, index]);
setIsXNext(false);

const winner = calculateWinner(newBoard);
if (winner) {
  setWinner(winner);
  setGameOver(true);
}

};

const botMove = () => { const bestMove = getBestMove(board); const newBoard = [...board]; newBoard[bestMove] = 'O'; setBoard(newBoard); setMoveHistory([...moveHistory, bestMove]); setIsXNext(true);

const winner = calculateWinner(newBoard);
if (winner) {
  setWinner(winner);
  setGameOver(true);
}

};

const getBestMove = (board) => { const availableSpaces = board .map((val, idx) => (val === null ? idx : null)) .filter((idx) => idx !== null);

return availableSpaces[Math.floor(Math.random() * availableSpaces.length)];

};

const calculateWinner = (board) => { for (let [a, b, c] of WINNING_COMBINATIONS) { if (board[a] && board[a] === board[b] && board[a] === board[c]) { return board[a]; } } return null; };

const resetGame = () => { setBoard(Array(9).fill(null)); setIsXNext(true); setWinner(null); setGameOver(false); setMoveHistory([]); };

return (

Tic-Tac-Toe

{winner ? ๐ŸŽ‰ ${winner} wins! : gameOver ? '๐Ÿ˜ž It's a draw!' : Next: ${isXNext ? 'You (X)' : 'Bot (O)'}}
{board.map((cell, index) => ( <div key={index} className={square ${cell} ${gameOver && winner === cell ? 'winner' : ''}} onClick={() => handleClick(index)} > {cell}
))}
Start New Game {botThinking &&
Bot is thinking...
} ); };

export default App;

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published