Login / Register
const firebaseConfig = {
apiKey: "AIzaSyB76cVSDBVpGfKeCbGjri4qWMYl9XDyHKA",
authDomain: "coinbrowser.firebaseapp.com",
projectId: "coinbrowser",
storageBucket: "coinbrowser.firebasestorage.app",
messagingSenderId: "692275980462",
appId: "1:692275980462:web:4ec649f4529894d088253f"
};
firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
const db = firebase.firestore();
let currentUser = null;
let coins = 0;
let gameOver = false;
let board = [ '', '', '', '', '', '', '', '', '' ];
const boardElement = document.getElementById('board');
const coinsElement = document.getElementById('coins');
const difficultyElement = document.getElementById('difficulty');
auth.onAuthStateChanged((user)=>{
const authSection = document.getElementById('authSection');
const appSection = document.getElementById('appSection');
if(user){
currentUser = user;
authSection.style.display = "none";
appSection.style.display = "block";
loadUserData();
}
else{
authSection.style.display = "block";
appSection.style.display = "none";
}
});
function register(){
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
auth.createUserWithEmailAndPassword(email,password)
.then((userCredential)=>{
currentUser = userCredential.user;
createUserData(currentUser.uid);
alert("✅ Registered Successfully");
})
.catch((error)=>{
alert(error.message);
});
}
function login(){
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
auth.signInWithEmailAndPassword(email,password)
.then(()=>{
alert("✅ Login Successful");
})
.catch((error)=>{
alert(error.message);
});
}
function logout(){
auth.signOut();
currentUser = null;
coins = 0;
coinsElement.innerText = 0;
alert("👋 Logged Out");
}
function createUserData(uid){
db.collection("users").doc(uid).set({
wallet:{ coins:0 },
stats:{ wins:0, losses:0, draws:0 }
});
}
function loadUserData(){
db.collection("users") .doc(currentUser.uid) .get()
.then((doc)=>{
if(doc.exists){
coins = doc.data().wallet.coins;
coinsElement.innerText = coins;
updateDifficulty();
}
});
}
function saveCoins(){
if(currentUser){
db.collection("users") .doc(currentUser.uid) .update({
"wallet.coins": coins
});
}
}
function updateCoins(amount){
coins += amount;
coinsElement.innerText = coins;
saveCoins();
updateDifficulty();
if(coins >= 5000){
alert("🎉 Minimum withdrawal reached!");
}
}
function getDifficulty(){
let cycleCoins = coins % 5000;
if(cycleCoins < 2000){ return "easy"; } else if(cycleCoins < 4000){ return "medium"; } else{ return "hard"; } } function updateDifficulty(){ let difficulty = getDifficulty(); if(difficulty === "easy"){ difficultyElement.innerText = "AI Level: Easy 😄"; } else if(difficulty === "medium"){ difficultyElement.innerText = "AI Level: Medium 😬"; } else{ difficultyElement.innerText = "AI Level: Hard 💀"; } } function watchAd(){ alert("📺 Monetag ad goes here"); updateCoins(50); } function createBoard(){ boardElement.innerHTML = ''; board.forEach((cell,index)=>{
const cellElement = document.createElement('div');
cellElement.classList.add('cell');
cellElement.innerText = cell;
cellElement.addEventListener('click',()=>{
makeMove(index);
});
boardElement.appendChild(cellElement);
});
}
function makeMove(index){
if(board[index] !== '' || gameOver){ return; }
board[index] = 'X';
createBoard();
if(checkWinner('X')){
gameOver = true;
alert("🎉 You Won +10 Coins");
updateCoins(10);
setTimeout(resetGame,1000);
return;
}
if(checkDraw()){
gameOver = true;
alert("🤝 Draw");
setTimeout(resetGame,1000);
return;
}
setTimeout(()=>{
aiMove();
createBoard();
if(checkWinner('O')){
gameOver = true;
alert("😅 AI Wins");
setTimeout(resetGame,1000);
return;
}
if(checkDraw()){
gameOver = true;
alert("🤝 Draw");
setTimeout(resetGame,1000);
return;
}
},300);
}
function aiMove(){
let emptyCells = board .map((cell,index)=> cell === '' ? index : null) .filter(cell => cell !== null);
if(emptyCells.length === 0){ return; }
let difficulty = getDifficulty();
if(difficulty === "easy"){
randomMove(emptyCells);
}
else if(difficulty === "medium"){
if(!smartMove('O')){
randomMove(emptyCells);
}
}
else{
if(!smartMove('O')){
if(!smartMove('X')){
randomMove(emptyCells);
}
}
}
}
function randomMove(emptyCells){
let randomIndex = emptyCells[Math.floor(Math.random()*emptyCells.length)];
board[randomIndex] = 'O';
}
function smartMove(player){
for(let i = 0; i < board.length; i++){ if(board[i] === ''){ board[i] = player; if(checkWinner(player)){ board[i] = 'O'; return true; } board[i] = ''; } } return false; } function checkWinner(player){ const winningCombos = [ [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 winningCombos.some(combo=>{
return combo.every(index=>{
return board[index] === player;
});
});
}
function checkDraw(){
return board.every(cell => cell !== '');
}
function resetGame(){
board = [ '', '', '', '', '', '', '', '', '' ];
gameOver = false;
createBoard();
}
function withdrawCoins(){
const email = document.getElementById('paypal').value;
if(!currentUser){
alert("Login First");
return;
}
if(coins < 5000){ alert("❌ Minimum withdrawal is 5000 coins"); return; } db.collection("withdrawals").add({ uid: currentUser.uid, paypal_email: email, amount_coins: 5000, status: "pending", requested_at: new Date() }); alert("💸 Withdrawal Request Sent"); coins = 0; coinsElement.innerText = coins; saveCoins(); updateDifficulty(); } createBoard();