const choices = ['rock', 'paper', 'scissors'];
const playerScoreElement = document.getElementById('playerScore');
const computerScoreElement = document.getElementById('computerScore');
const resultElement = document.getElementById('result');
let playerScore = 0;
let computerScore = 0;
let playerHistory = [];
function getSmartComputerChoice() {
if (playerHistory.length < 3) {
return choices[Math.floor(Math.random() * choices.length)];
}
const recent = playerHistory.slice(-3);
const patterns = {
rock: 0, paper: 0, scissors: 0
};
recent.forEach(choice => patterns[choice]++);
const mostFrequent = Object.keys(patterns).reduce((a, b) =>
patterns[a] > patterns[b] ? a : b
);
const counters = {
rock: 'paper',
paper: 'scissors',
scissors: 'rock'
};
return Math.random() < 0.7 ? counters[mostFrequent] :
choices[Math.floor(Math.random() * choices.length)];
}
function determineWinner(playerChoice, computerChoice) {
if (playerChoice === computerChoice) return 'tie';
const winConditions = {
rock: 'scissors',
paper: 'rock',
scissors: 'paper'
};
return winConditions[playerChoice] === computerChoice ? 'player' : 'computer';
}
function playGame(playerChoice) {
playerHistory.push(playerChoice);
if (playerHistory.length > 10) playerHistory.shift();
const computerChoice = getSmartComputerChoice();
const winner = determineWinner(playerChoice, computerChoice);
document.getElementById('playerChoice').textContent = playerChoice;
document.getElementById('computerChoice').textContent = computerChoice;
if (winner === 'player') {
playerScore++;
resultElement.textContent = 'You Win!';
} else if (winner === 'computer') {
computerScore++;
resultElement.textContent = 'Computer Wins!';
} else {
resultElement.textContent = "It's a Tie!";
}
playerScoreElement.textContent = playerScore;
computerScoreElement.textContent = computerScore;
}
function resetGame() {
playerScore = 0;
computerScore = 0;
playerHistory = [];
playerScoreElement.textContent = playerScore;
computerScoreElement.textContent = computerScore;
resultElement.textContent = 'Make your choice!';
}This code demonstrates the core game logic. The actual implementation may include additional features and optimizations.