Skip to content

Latest commit

 

History

History
12 lines (9 loc) · 1.04 KB

README.md

File metadata and controls

12 lines (9 loc) · 1.04 KB

Last Pencil ✏️

A terminal-based Java game built as part of the Hyperskill Java Backend Developer track. The game pits a human player against an automated bot in a classic game of Nim (taking turns removing 1-3 pencils until the last one is drawn).

What I Learned

This project was a deep dive into core Java mechanics, specifically:

  • Scanner Buffer Management: Transitioned from nextInt() to nextLine() combined with regex (\\d+) to prevent the program from crashing when a user inputs empty lines or text.
  • Game State Management: Consolidated duplicated player logic into a single while(pencils > 0) loop using a currentPlayer tracker to alternate turns cleanly.
  • Algorithmic Bot Logic: Built custom ArrayList sequences tracking intervals of 4 to dynamically calculate the bot's winning and losing positions.

How to Run

Compile and run the Main.java file. Enter the initial number of pencils, choose who goes first (Human or Steve the Bot), and enter 1, 2, or 3 on your turn. The goal is to not to take the last pencil.