Lab 3: Number Guessing Game

 

Introduction:

Designed for the 6502 CPU, this is a straightforward number-guessing game. You have to predict a random number between 1 and 100 to win the game. The game gives the user feedback on whether their guess was accurate or too high or low once they are asked to enter it.

Game Initialization

Important variables are initialized and a random target number is generated at the beginning of the game. Setting everything up, including cleaning the screen and producing the goal number, is the responsibility of the INIT_GAME function.



The GENERATE_RANDOM command generates a random target number between 1 and 100, whereas the CLEAR_SCREEN command clears the bitmap and character displays. Memory location $10 is where the random number is kept.


Displaying the Prompt

The game asks the user to guess a number between 1 and 100. This is done by the DISPLAY_PROMPT subroutine, which outputs the string "Guess a number (1-100):" to the screen.


This subroutine breaks down the string into individual characters and sends them to the output screen using the CHROUT routine.


Getting User Input

The application waits for the user to input a guess after the question is presented. The keyboard buffer is checked by the GET_USER_INPUT function to make sure the input is a valid number (0–9). The input's ASCII value, which ranges from 0 to 39, is then converted to an integer value and stored in the USER_GUESS memory address.


Checking the Guess

Once the input has been received, the software compares the target number with the user's estimation. The game is restarted and "You win!" is shown in the WINNER subroutine if the guess is accurate. It jumps to the TOO_LOW function to ask the user to guess higher if the guess is too low, and to the TOO_HIGH subroutine to ask the user to guess lower if the guess is too high.


In the TOO_LOW and TOO_HIGH subroutines, the game gives feedback to the player. If the guess is too low, it prints "Too low," and if the guess is too high, it prints "Too high."


Winning the Game

If the user guesses correctly, the program displays "You win!" and restarts the game by calling INIT_GAME again. This allows the user to play multiple rounds.



Game code:

; 6502 Number Guessing Game

; A simple number guessing game for the 6502 Emulator

;

; Features:

; - Uses the character screen for user interaction

; - Uses the bitmapped screen for visual feedback

; - Takes keyboard input for number guesses

; - Uses arithmetic operations (addition, subtraction, bitwise operations)

; Define memory locations for important variables

define TARGET_NUMBER $10 ; Random number to guess

define USER_GUESS $11 ; User's current guess

define SCREEN $f000 ; Character display start

define POINTER $0200 ; Bitmap display start

; Initialize the game

JSR INIT_GAME

; Main loop

MAIN_LOOP:

JSR DISPLAY_PROMPT ; Ask user for a guess

JSR GET_USER_INPUT ; Get user input

JSR CHECK_GUESS ; Check if guess is correct

JMP MAIN_LOOP ; Repeat

; --- Game Initialization ---

INIT_GAME:

JSR CLEAR_SCREEN ; Clear text and bitmap screen

JSR GENERATE_RANDOM ; Generate a random target number

RTS

; --- Display Prompt to User ---

DISPLAY_PROMPT:

LDA #$47 ; Print "Guess a number (1-100): "

JSR CHROUT

LDA #$75

JSR CHROUT

LDA #$65

JSR CHROUT

LDA #$73

JSR CHROUT

LDA #$73

JSR CHROUT

LDA #$3A

JSR CHROUT

RTS

; --- Get User Input ---

GET_USER_INPUT:

LDA $FF ; Read keyboard buffer

BEQ GET_USER_INPUT ; Wait until a key is pressed

CMP #$30 ; Ensure input is at least '0'

BCC GET_USER_INPUT ; Ignore invalid input

CMP #$39 ; Ensure input is at most '9'

BCS GET_USER_INPUT ; Ignore invalid input

SEC

SBC #$30 ; Convert ASCII to integer (0-9)

STA USER_GUESS

RTS

; --- Check Guess Against Target ---

CHECK_GUESS:

LDA USER_GUESS ; Load user guess

STA SCREEN ; Debug: Print user guess

LDA TARGET_NUMBER

STA SCREEN,X ; Debug: Print target number

CMP TARGET_NUMBER

BEQ WINNER ; If equal, user wins

BCC TOO_LOW ; If less, show too low

JMP TOO_HIGH ; If greater, show too high

RTS ; Else, too high

TOO_LOW:

LDA #$4C

JSR CHROUT

LDA #$6F

JSR CHROUT

LDA #$77

JSR CHROUT

JSR GET_USER_INPUT ; Get new guess

JMP CHECK_GUESS

TOO_HIGH:

LDA #$48

JSR CHROUT

LDA #$69

JSR CHROUT

LDA #$67

JSR CHROUT

LDA #$68

JSR CHROUT

JSR GET_USER_INPUT ; Get new guess

JMP CHECK_GUESS

WINNER:

LDA #$59

JSR CHROUT

LDA #$6F

JSR CHROUT

LDA #$75

JSR CHROUT

LDA #$20

JSR CHROUT

LDA #$57

JSR CHROUT

LDA #$69

JSR CHROUT

LDA #$6E

JSR CHROUT

JSR WAIT_FOR_KEYPRESS ; Wait for a keypress before restarting

JSR INIT_GAME ; Restart game

RTS

; Wait for user to press a key before restarting

WAIT_FOR_KEYPRESS:

LDA $FF ; Read keyboard buffer

BEQ WAIT_FOR_KEYPRESS ; Wait until a key is pressed

RTS

END_CHECK:

RTS

; --- Generate Random Number ---

GENERATE_RANDOM:

LDA $FE ; Read PRNG value from emulator

AND #$63 ; Keep it within 0-99 range

CLC

ADC #1 ; Ensure it's 1-100

STA TARGET_NUMBER

RTS

; --- Clear Screen ---

CLEAR_SCREEN:

JSR SCINIT ; Clear character screen

LDX #$00

CLEAR_LOOP:

STA POINTER, X ; Clear bitmap display

INX

BNE CLEAR_LOOP

RTS

; ROM Routines

define SCINIT $ff81 ; Clear screen

define CHROUT $ffd2 ; Output character

define CHRIN $ffcf ; Input character



Issue with Number Guessing Game Always Triggering 'You Win!' Condition

The number-guessing game I'm working on is giving me trouble.  The game constantly says "You win!" and then promptly resets, regardless of the amount I guess.  It appears that the game is not correctly identifying the difference between the player's guess and the goal number, based on my examination of the logic of the comparison.  Rather, it consistently sets off the "win" condition.

I have a suspicion that the problem may be with the way the guess comparison is carried out or the random target number is created.  Any advice on how to debug this further or if there is something special that I could have overlooked in the logic would be greatly appreciated.


Comments

Popular posts from this blog

Project Stage 3: Enhancing GCC Pass for Multiple Function Clone Handling: Progress Update

Project Stage 1: Troubleshooting a bit - Coming soon

Lab - 1: Exploring 6502 Assembly