Lab 3

Introduction:

This is a simple number guessing game written for the 6502 processor. The goal of the game is to guess a random number between 1 and 100. The player is prompted to input their guess, and the game provides feedback on whether the guess is too high, too low, or correct.

Game Initialization

The game starts by initializing important variables and generating a random target number. The INIT_GAME subroutine is responsible for setting things up, including clearing the screen and generating the target number.


The CLEAR_SCREEN clears both the character display and bitmap display, while GENERATE_RANDOM produces a random target number between 1 and 100. The random number is stored in the memory location $10.


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

Once the prompt is displayed, the program waits for the user to enter a guess. The GET_USER_INPUT subroutine checks the keyboard buffer, ensuring that the input is a valid number (0-9). It then converts the ASCII value of the input (which is between 0x30 and 0x39) to an integer value and stores it in the USER_GUESS memory location.


Checking the Guess

After receiving the input, the program compares the user's guess with the target number. If the guess is correct, it goes to the WINNER subroutine to display "You win!" and restart the game. If the guess is too low, it jumps to the TOO_LOW subroutine to prompt the user to guess higher, and similarly, if it's too high, it jumps to the TOO_HIGH subroutine to prompt the user to guess lower.

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 gives the user the opportunity 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

I am encountering an issue with the number guessing game I’m working on. Regardless of the number I enter as a guess, the game always displays "You win!" and then quickly resets. I’ve checked the logic of the comparison between the player's guess and the target number, but it seems that the game is not properly recognizing the difference between the two. Instead, it always triggers the "win" condition.

I suspect the issue might lie in how the random target number is being generated or how the guess comparison is being executed. I would appreciate any guidance on how to troubleshoot this further or if there’s something specific, I may have missed in the logic.

Comments

Popular posts from this blog

Lab 2 - Lab Results

Project Stage 1

Lab 5 - Part 1 - Aarch 64