Sure! Below is a simple implementation of a game called "Gryptogram" using JavaScript and HTML in an Object-Oriented Programming (OOP) style.
🧩 Game Concept:
Gryptogram is a word puzzle game where a phrase is encrypted using a simple substitution cipher (like a Caesar cipher), and the player must decrypt it by guessing the correct letters.
📁 File Structure:
📄 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Gryptogram</title>
<style>
body {
font-family: Arial, sans-serif;
background: #f4f4f4;
padding: 20px;
text-align: center;
}
#encrypted, #decrypted {
font-size: 24px;
margin: 20px 0;
word-wrap: break-word;
}
.letter-box {
display: inline-block;
width: 30px;
height: 40px;
margin: 2px;
border-bottom: 2px solid #000;
text-transform: uppercase;
font-size: 24px;
}
.input-box {
width: 30px;
text-align: center;
font-size: 18px;
}
#message {
margin-top: 20px;
font-weight: bold;
}
</style>
</head>
<body>
<h1>Gryptogram</h1>
<div id="encrypted"></div>
<div id="decrypted"></div>
<div id="message"></div>
<button onclick="game.checkSolution()">Check Solution</button>
<script src="script.js"></script>
</body>
</html>
📜 script.js
class Cipher {
constructor(shift = 3) {
this.shift = shift;
}
encrypt(text) {
return text
.toUpperCase()
.split('')
.map(char => {
if (/[A-Z]/.test(char)) {
return String.fromCharCode(((char.charCodeAt(0) - 65 + this.shift) % 26) + 65);
}
return char;
})
.join('');
}
decrypt(text) {
return text
.toUpperCase()
.split('')
.map(char => {
if (/[A-Z]/.test(char)) {
return String.fromCharCode(((char.charCodeAt(0) - 65 - this.shift + 26) % 26) + 65);
}
return char;
})
.join('');
}
}
class GryptogramGame {
constructor(phrase) {
this.originalPhrase = phrase.toUpperCase();
this.cipher = new Cipher(Math.floor(Math.random() * 25) + 1); // Random shift
this.encryptedPhrase = this.cipher.encrypt(this.originalPhrase);
this.userGuesses = Array(this.originalPhrase.length).fill('');
this.renderEncrypted();
this.renderDecrypted();
}
renderEncrypted() {
const container = document.getElementById('encrypted');
container.innerHTML = '';
for (let char of this.encryptedPhrase) {
const span = document.createElement('span');
span.className = 'letter-box';
span.textContent = /[A-Z]/.test(char) ? char : ' ';
container.appendChild(span);
}
}
renderDecrypted() {
const container = document.getElementById('decrypted');
container.innerHTML = '';
for (let i = 0; i < this.encryptedPhrase.length; i++) {
const char = this.encryptedPhrase[i];
if (/[A-Z]/.test(char)) {
const input = document.createElement('input');
input.className = 'input-box';
input.maxLength = 1;
input.dataset.index = i;
input.addEventListener('input', (e) => this.handleInput(e));
container.appendChild(input);
} else {
const span = document.createElement('span');
span.className = 'letter-box';
span.textContent = char;
container.appendChild(span);
}
}
}
handleInput(e) {
const index = parseInt(e.target.dataset.index);
const value = e.target.value.toUpperCase();
if (/^[A-Z]$/.test(value)) {
this.userGuesses[index] = value;
} else {
this.userGuesses[index] = '';
e.target.value = '';
}
}
checkSolution() {
const guess = this.userGuesses.map((char, i) => {
return /[A-Z]/.test(this.encryptedPhrase[i]) ? char : this.encryptedPhrase[i];
}).join('');
const decrypted = this.cipher.decrypt(this.encryptedPhrase);
const message = document.getElementById('message');
if (guess === decrypted) {
message.textContent = '🎉 Correct! You solved the Gryptogram!';
message.style.color = 'green';
} else {
message.textContent = '❌ Not quite. Keep trying!';
message.style.color = 'red';
}
}
}
// Example phrase
const game = new GryptogramGame("HELLO WORLD");
✅ Features:
- Random Caesar cipher shift each time.
- User inputs guesses for each encrypted letter.
- Checks if the decrypted phrase matches the original.
- OOP structure with Cipher and GryptogramGame classes.
🧠 You can expand this game by:
- Adding a dictionary of phrases.
- Allowing letter mapping instead of per-letter input.
- Adding hints or a scoring system.
Let me know if you'd like to add more features!