2048 Game HTML
Prompt
Create a complete, self-contained HTML page that implements the popular game "2048" with the following features: - A 4x4 game grid representing the game board. - Controls enabling players to move tiles up, down, left, and right using keyboard arrow keys. - Game logic that merges tiles with the same number when they collide during moves. - A live score counter that updates accurately as tiles merge throughout gameplay. - A restart button that resets the game board and score to start a new game. - CSS styling to make the game visually appealing and user-friendly. Requirements: - No external libraries or frameworks are allowed; all HTML, CSS, and JavaScript must be embedded within a single HTML file. - The JavaScript should handle all game mechanics internally, including tile spawning, merging, movement, and score management. - Use clear, concise comments throughout the code to explain your logic and the purpose of key sections to ensure maintainability and clarity. - The game should respond to arrow key inputs smoothly and update the UI dynamically. # Steps 1. Define the HTML structure with containers for the game grid, score, and controls. 2. Style the grid and tiles with CSS to visually distinguish tile values and create a pleasant layout. 3. Implement JavaScript to initialize the game state, manage the grid array, and spawn new tiles. 4. Add event listeners for keyboard input to handle tile movements and merges. 5. Implement the restart functionality to reset all game variables and UI elements. 6. Include comprehensive comments explaining the logic, data structures, and flow. # Output Format - A single HTML file containing all HTML, CSS, and JavaScript inline. - Well-indented, clean, and commented source code.
Related Coding Prompts
Write Code
As a seasoned programmer, your task is to write code in [programming language] to [perform action]. The code should be efficient, well-structured, and optimized for performance. Make sure to follow best practices and industry standards while implementing the necessary algorithms and logic to achieve the desired functionality. Test the code thoroughly to ensure it functions as intended and meets all requirements. Additionally, document the code properly for future reference and maintenance.
Debug Code
Act as a seasoned programmer with over 20 years of commercial experience. Analyze the provided [piece of code] that is causing a specific [error]. Your task involves diagnosing the root cause of the error, understanding the context and functionality intended by the code, and proposing a solution to fix the issue. Your analysis should include a step-by-step walkthrough of the code, identification of any bugs or logical mistakes, and a detailed explanation of how to resolve them. Additionally, suggest any improvements or optimizations to enhance the performance, readability, or maintainability of the code based on your extensive experience. Ensure that your solution adheres to best practices in software development and is compatible with the current development environment where the code is being executed.
Do Code Review
As a seasoned programmer with over 20 years of commercial experience, your task is to perform a comprehensive code review on the provided [piece of code]. Your review should meticulously evaluate the code's efficiency, readability, and maintainability. You are expected to identify any potential bugs, security vulnerabilities, or performance issues and suggest specific improvements or optimizations. Additionally, assess the code's adherence to industry standards and best practices. Your feedback should be constructive and detailed, offering clear explanations and recommendations for changes. Where applicable, provide examples or references to support your suggestions. Your goal is to ensure that the code not only functions as intended but also meets high standards of quality and can be easily managed and scaled in the future. This review is an opportunity to mentor and guide less experienced developers, so your insights should be both educational and actionable.