Sure! Below is a simple HTML and JavaScript game where you can type in a crime, and an "AI judge" will assign a sentence based on predefined rules. This example uses basic keyword matching to determine the sentence length. You can expand the list of crimes and corresponding sentences as desired.
HTML and JavaScript Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AI Judge Game</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
padding: 20px;
}
.container {
max-width: 500px;
margin: auto;
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
input[type="text"] {
width: 100%;
padding: 12px 20px;
margin: 8px 0 20px 0;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
}
button {
background-color: #4CAF50;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
font-size: 16px;
}
button:hover {
background-color: #45a049;
}
.result {
margin-top: 20px;
padding: 15px;
background-color: #e7f3fe;
border-left: 6px solid #2196F3;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="container">
<h2>AI Judge Simulator</h2>
<p>Type in the crime you committed, and let the AI judge decide your sentence.</p>
<input type="text" id="crimeInput" placeholder="Enter the crime here...">
<button onclick="judgeSentence()">Submit</button>
<div id="result" class="result" style="display:none;"></div>
</div>
<script>
// Predefined crimes and their corresponding sentence ranges (in years)
const crimeDatabase = {
"theft": { min: 1, max: 5 },
"assault": { min: 2, max: 10 },
"fraud": { min: 3, max: 8 },
"murder": { min: 15, max: 30 },
"vandalism": { min: 0, max: 2 },
"embezzlement": { min: 5, max: 15 },
"arson": { min: 10, max: 20 },
"drug trafficking": { min: 10, max: 25 },
"kidnapping": { min: 12, max: 25 },
"cybercrime": { min: 3, max: 12 }
};
function judgeSentence() {
const input = document.getElementById('crimeInput').value.toLowerCase().trim();
const resultDiv = document.getElementById('result');
if (input === "") {
alert("Please enter a crime.");
return;
}
// Initialize variables to determine the sentence
let sentence = "";
let years = 0;
let matched = false;
// Check if input matches any predefined crime
for (const crime in crimeDatabase) {
if (input.includes(crime)) {
const range = crimeDatabase[crime];
years = getRandomInt(range.min, range.max);
sentence = `For ${crime}, you are sentenced to ${years} year(s) in prison.`;
matched = true;
break;
}
}
// If no predefined crime matched, assign a default sentence
if (!matched) {
years = getRandomInt(1, 5);
sentence = `The crime "${input}" is recognized but not categorized. You are sentenced to ${years} year(s) in prison.`;
}
// Display the result
resultDiv.style.display = "block";
resultDiv.innerHTML = `<strong>AI Judge's Decision:</strong> ${sentence}`;
}
// Helper function to get a random integer between min and max (inclusive)
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
</script>
</body>
</html>
How It Works:
-
HTML Structure:
- A simple form where the user can input a crime.
- A button to submit the crime.
- A result area to display the AI judge's decision.
-
Styling:
- Basic CSS for a clean and centered layout.
-
JavaScript Logic:
crimeDatabase: An object that maps predefined crimes to their respective sentence ranges.
judgeSentence(): This function is triggered when the user clicks the "Submit" button.
- It reads the user input, converts it to lowercase, and trims any extra spaces.
- It checks if the input matches any predefined crimes. If a match is found, it assigns a sentence within the specified range.
- If there's no match, it assigns a default sentence.
- The result is then displayed to the user.
getRandomInt(min, max): A helper function to generate a random integer between the specified min and max values, inclusive.
How to Use:
-
Save the Code:
- Copy the above code into a text editor and save it with a
.html extension, for example, ai_judge_game.html.
-
Open in Browser:
- Double-click the saved file or open it in your preferred web browser.
-
Play the Game:
- Enter any crime into the input field and click "Submit".
- The AI judge will respond with a sentence based on your input.
Example:
-
Input: Theft
-
Output: For theft, you are sentenced to 3 year(s) in prison.
-
Input: Cybercrime
-
Output: For cybercrime, you are sentenced to 7 year(s) in prison.
-
Input: Piracy
-
Output: The crime "piracy" is recognized but not categorized. You are sentenced to 4 year(s) in prison.
Customization:
-
Adding More Crimes:
- To add more predefined crimes, simply add new entries to the
crimeDatabase object with appropriate sentence ranges.
const crimeDatabase = {
// Existing crimes...
"piracy": { min: 5, max: 15 },
"bribery": { min: 2, max: 7 },
// Add more as needed
};
-
Enhancing Matching Logic:
- For more sophisticated matching (e.g., handling synonyms or partial matches), consider implementing more advanced text processing or integrating with a backend service.
-
Improving User Experience:
- Add images, sounds, or animations to make the game more engaging.
- Implement multiple stages or levels with different scenarios.
Disclaimer:
This game is intended for entertainment purposes only. It does not represent actual legal judgments or advice. The sentences assigned by the "AI judge" are fictional and should not be taken seriously.
Feel free to modify and expand upon this basic framework to create a more comprehensive and engaging game!