Rock
Paper
Scissors
The game of Rock, Paper,
Scissors . . . using
a micro.
to
Page 1
Page 2
GOING FURTHER
The program can be extended in many ways.
The first addition is a recognition of the players choice. With only a single
push button, it can be pressed one, two or three times for Rock, Paper,
Scissors.
The flag file (file 1F) is incremented (by setting bit 5, then 6, then 7) each time the button is pressed. The
button-press file is in the delay routine as this is the where the micro is
looping most of the time.
A couple of things we have to consider are: How to debounce the switch and
how to recognise the "end of play" for a player.
The switch debounce is contained within the delay routine.
Bit0 in file 1F is set when the button is pushed.
Each time the button is pressed, bit 5, then 6 then 7 is set. This indicates
"Rock," "Paper," "Scissors."
Del100
Del101
Del102
Del103
Del104
Del105
Del106
|
MOVLW 64h
MOVWF 1B
NOP
DECFSZ 1A,1
GOTO Del101
BTFSC 05,0
GOTO Del103
BCF 1F,0
DECFSZ 1B,1
GOTO Del101
RETURN
BTFSC 1F,0
GOTO Del102
BTFSC 1F,5
GOTO Del104
BSF 1F,5
GOTO Del106
BTFSC 1F,6
GOTO Del105
BSF 1F,6
GOTO Del106
BSF 1F,7
BSF 1F,0
GOTO Del102
|
;100 loops = 100mS
;Is button pressed?
;First pass?
;Set bit for Rock
;Set bit for Paper
;Set bit for
Scissors
;Set "button pressed" flag
|
|
The next sub-routine to develop is called "Score." It takes
the Player value and Computer value and works out the winner. It
then increments the Player or Computer counter. The
player value is contained in file 1F, bits 5, 6, 7. The computer value
will 01, 02 or 04 in file 12h.
Player Score will be placed in file 17h and Computer Score will
be placed in file 18h. The number of games is held in file 16h. When file 16h
equals three games, "I Lose," "You Lose"
or "TIE" will be produced.
The value of the bits in files 1F and 12h are as follows:
File 1F,5
File 1F,6
File 1F,7 |
Player Rock
Player Paper
Player Scissors |
File
12h,0
File 12h,1
File 12h,2 |
Computer Rock
Computer Paper
Computer Scissors |
Score
Score1
Score2
Score3
Score4
Score5
Score6
Score7
|
BTFSS 1Fh,7
GOTO Score2
BTFSS 12h,0
GOTO Score1
INCF 18h,1
GOTO Score7
BTFSS 12h,1
GOTO Score6
INCF 17h,1
GOTO Score7
BTFSS 1Fh,6
GOTO Score4
BTFSS 12h,0
GOTO Score3
INCF 17h,1
GOTO Score7
BTFSC 12h,1
GOTO Score6
INCF 18h,1
GOTO Score7
BTFSS 1Fh,5
GOTO Score7
BTFSC 12h,0
GOTO Score6
BTFSS 12h,1
GOTO Score5
INCF 18h,1
GOTO Score7
INCF 17h,1
GOTO Score7
INCF 17h,1
INCF 18h,1
INCF 16h,1
CLRF 1F
RETURN
|
;Player Scissors?
;Not player scissors
;Computer Rock?
;Computer wins
;Computer Paper?
;Player Scissors, Computer Paper
;Player Paper?
;Not player paper
;Computer Rock?
;Player wins
;Computer Paper?
;Player Paper, Computer Scissors. Computer wins
;Player Rock?
;Player not playing
;Player Rock, Computer Rock?
;Player Rock, Computer Paper?
;Computer wins
;Player wins
;Draw
;Draw
;INCrement the number of games.
|
|
The next sub-routine produces "I Lose," "You Lose:"
Table2
Mess1
Mess1A
Mess2
Show
...
|
ADDWF 02h,1
RETLW 6Eh
RETLW 3Fh
RETLW 3Eh
RETLW 00h
RETLW 38h
RETLW 3Fh
RETLW 6Dh
RETLW 79h
RETLW 00h
RETLW 0FFh
MOVLW 06
MOVWF 06
CALL Show
CALL Show
MOVLW 0Ch
MOVWF 12h
CLRF 06
CALL Show
MOVF 12h,0
CALL Table2
MOVWF 06
MOVLW 0FFh
XORWF 06,0
BTFSC 03,2
RETURN
INCF 12h,1
CALL Show
CALL Show
GOTO Mess1A
MOVLW 0Ah
MOVWF 12h
GOTO Mess1A
NOP
DECFSZ 1A,1
GOTO Show
DECFSZ 1B,1
GOTO Show
RETURN
|
;Add W to the Program Counter to create a jump.
;y format= gfedcba
;O
;U
;
;L
;O
;S
;E
;
;End of message
;"Mess1" shows on 7-segment display: "I Lose"
;Show the letter "I"
;Jumper for table
;Output to display
;Detect end of Table
;End of Table detected
;Jump to next letter
;"Mess2" shows on 7-segment display: "You Lose"
;Jumper for table
;Create 250mS delay
|
|
All the sub-routines are now ready for insertion into a program. The only
feature we have to create is the "end of play" for one or two presses
of the button.
This will be a simple "time-out" feature. The button-presses must be completed during the time when the random effect is showing, otherwise a
wrong result will be entered. The random effects have been lengthened to give
the player sufficient time to register his decision.
The Main routine must be altered to take the new sub-routines, but
now we have a program that has feedback from the player.
Two "hidden" points we need to mention are:
1. The pointer for the random "RPS" is file 0Dh. It is incremented
in "Attract" and masked to produce a value up to 0F for Table.
This gives a random start to the game.
2. Bits 5, 6, and 7 of file 1F are tested to decide the winner of each game.
Bit 7 is tested first as all results contain bit 5. This is a
very important point worth noting.
The table "jump-value" is incremented twice for each game to make
the outcome more unknown. Anyone copying the table will have to locate each second
value. PLAYING
THE GAME
To play the game, switch the project on. The Attract mode will show on
the 8 LEDs.
Press the button and the first effect will appear.
Press the button one, two or three times to represent Rock, Paper or Scissors,
before the effect disappears.
The computer choice will then appear. The program will then work out the
winner in "Score" and place the result in
file 17h for the player or 18h for the computer. The game counter file
(file 16h) will also be incremented.
After 3 games the results will appear.
The program will then go back to the Attract mode.
;RPS-2.asm
;Project: Rock Paper Scissors
List P = 16F84
#include <p16F84.inc>
__CONFIG 1Bh ;_CP_OFF & _PWRTE_ON
& _WDT_OFF & _RC_OSC |
SetUp
Table
Table2
Attract
Del100
Del101
Del102
Del103
Del104
Del105
Del106
Delay1
DelA
Del2
Del2A
Del2B
Del300
DelB
Del4
Del4A
Display
Flash
Flash1
Mess1
Mess1A
Mess2
Mess3
Run
RunA
Run1
Run2
Run3
Score
Score1
Score2
Score3
Score4
Score5
Score6
Score7
Show
Toggle
Toggle1
Main
MainA
MainB
Main2
Main3
Main4
MainC
MainD
Main5
Main6
Main7
MainE
Main8
Main9
...
|
ORG 0
BSF 03,5
MOVLW 01
MOVWF 05
CLRF 06
BCF 03,5
CLRF 16h
CLRF 17h
CLRF 18h
CLRF 1Ch
CLRF 1Fh
GOTO Main
ADDWF 02h,1
RETLW 04h
RETLW 01h
RETLW 02h
RETLW 02h
RETLW 01h
RETLW 04h
RETLW 02h
RETLW 04h
RETLW 02h
RETLW 01h
RETLW 04h
RETLW 02h
RETLW 04h
RETLW 02h
RETLW 01h
RETLW 04h
RETLW 04h
RETLW 01h
RETLW 01h
RETLW 04h
RETLW 02h
RETLW 01h
RETLW 02h
RETLW 01h
RETLW 04h
RETLW 02h
RETLW 04h
RETLW 02h
RETLW 02h
RETLW 01h
RETLW 04h
RETLW 02h
RETLW 04h
RETLW 02h
RETLW 01h
RETLW 04h
RETLW 04h
RETLW 01h
RETLW 01h
RETLW 04h
RETLW 02h
RETLW 01h
RETLW 0FFh
RETLW 0FFh
RETLW 0FFh
RETLW 0FFh
ADDWF 02h,1 .
RETLW 6Eh
RETLW 3Fh
RETLW 3Eh
RETLW 00h
RETLW 38h
RETLW 3Fh
RETLW 6Dh
RETLW 79h
RETLW 00h
RETLW 0FFh
RETLW 78h
RETLW 30h
RETLW 79h
RETLW 00h
RETLW 0FFh
CLRF 06
DECF 06,1
CALL Del2
MOVLW 0E7h
MOVWF 06
CALL Del2
MOVLW 0C3h
MOVWF 06
CALL Del2
MOVLW 81h
MOVWF 06
CALL Del2
CLRF 06
CALL Del2
MOVLW 81
MOVWF 06
CALL Del2
MOVLW 0C3h
MOVWF 06
CALL Del2
MOVLW 0E7h
MOVWF 06
CALL Del2
CLRF 06
DECF 06,1
CALL Del2
CLRF 06
INCF 1D
RETURN
MOVLW 64h
MOVWF 1B
NOP
DECFSZ 1A,1
GOTO Del101
BTFSC 05,0
GOTO Del103
BCF 1F,0
DECFSZ 1B,1
GOTO Del101
RETURN
BTFSC 1F,0
GOTO Del102
BTFSC 1F,5
GOTO Del104
BSF 1F,5
GOTO Del106
BTFSC 1F,6
GOTO Del105
BSF 1F,6
GOTO Del106
BSF 1F,7
BSF 1F,0
GOTO Del102
MOVLW 0A
MOVWF 10h
CALL Del100
DECFSZ 10h,1
GOTO DelA
RETURN
MOVLW 34h
MOVWF 1B
NOP
BTFSC 05,0
GOTO Del2B
DECFSZ 1A,1
GOTO Del2A
DECFSZ 1B,1
GOTO Del2A
RETURN
BSF 1F,0
RETURN
MOVLW 03
MOVWF 10h
CALL Del100
DECFSZ 10h,1
GOTO DelB
RETURN
MOVLW 04
MOVWF 1C
NOP
DECFSZ 1A,1
GOTO Del4A
DECFSZ 1B,1
GOTO Del4A
DECFSZ 1C,1
GOTO Del4A
RETURN
MOVF 0Eh,0
MOVWF 06
CALL Del100
CLRF 06
CALL Del100
MOVF 0Eh,0
MOVWF 06
CALL Del300
CLRF 06
CALL Del300
MOVF 0Eh,0
MOVWF 06
CALL Delay1
CALL Delay1
CALL Delay1
CALL Delay1
CLRF 06
CALL Delay1
INCF 1C,1
INCF 1D,1
INCF 1D,1
RETURN
MOVLW 08
MOVWF 1E
CLRF 06h
CALL Del100
CALL Del100
DECF 06,1
CALL Del100
CALL Del100
DECFSZ 1E,1
GOTO Flash1
CLRF 06
RETURN
MOVLW 30h
MOVWF 06h
CALL Show
CALL Show
MOVLW 03h
MOVWF 12h
CLRF 06
CALL Show
MOVF 12h,0
CALL Table2
MOVWF 06h
MOVLW 0FFh
XORWF 06,0
BTFSC 03,2
RETURN
INCF 12h,1
CALL Show
CALL Show
GOTO Mess1A
CLRF 12h
GOTO Mess1A
MOVLW 0Ah
MOVWF 12h
GOTO Mess1A
MOVLW 04
MOVWF 1E
CLRF 06
INCF 06,1
RLF 06,1
BTFSC 03,0
GOTO Run2
CALL Del100
GOTO Run1
RRF 06,1
CALL Del100
RRF 06,1
BTFSS 03,0
GOTO Run3
DECFSZ 1E,1
GOTO RunA
CLRF 06
RETURN
BTFSS 1Fh,7
GOTO Score2
BTFSS 12h,0
GOTO Score1
INCF 18h,1
GOTO Score7
BTFSS 12h,1
GOTO Score6
INCF 17h,1
GOTO Score7
BTFSS 1Fh,6
GOTO Score4
BTFSS 12h,0
GOTO Score3
INCF 17h,1
GOTO Score7
BTFSC 12h,1
GOTO Score6
INCF 18h,1
GOTO Score7
BTFSS 1Fh,5
GOTO Score7
BTFSC 12h,0
GOTO Score6
BTFSS 12h,1
GOTO Score5
INCF 18h,1
GOTO Score7
INCF 17h,1
GOTO Score7
INCF 17h,1
INCF 18h,1
INCF 16h,1
CLRF 1F
RETURN
SHOW NOP
DECFSZ 1A,1
GOTO Show
DECFSZ 1B,1
GOTO Show
RETURN
MOVLW 06
MOVWF 1E
MOVLW 0F0h
MOVWF 06
CALL Del300
MOVLW 0Fh
MOVWF 06
CALL Del300
DECFSZ 1E,1
GOTO Toggle1
CLRF 06
RETURN
CALL Attract
BTFSS 1F,0
GOTO Main
CLRF 06
CALL Del4
MOVLW 0Fh
ANDWF 1Dh
MOVF 1C,0
CALL Table
MOVWF 11h
BTFSS 11h,7
GOTO MainB
CLRF 1Ch
GOTO MainA
BTFSS 11h,0
GOTO Main2
CALL Flash
GOTO Main4
BTFSS 11h,1
GOTO Main3
CALL Run
GOTO Main4
CALL Toggle
CALL Delay1
MOVF 1D,0
CALL Table
MOVWF 12h
BTFSS 12h,7
GOTO MainD
CLRF 1D
GOTO Main4
BTFSS 12h,0
GOTO Main5
MOVLW 33h
MOVWF 0E
CALL Display
GOTO Main7
BTFSS 12h,1
GOTO Main6
MOVLW 73h
MOVWF 0E
CALL Display
GOTO Main7
MOVLW 6Dh
MOVWF 0E
CALL Display
MOVF 1F,0
MOVWF 06
CALL Score
MOVLW 03h
XORWF 16h,0
BTFSS 03,2
GOTO MainA
CLRF 16h
MOVF 17,1
BTFSS 03,2
GOTO MainE
MOVF 18,1
BTFSC 03,2
GOTO SetUp
MOVF 17h,0
SUBWF 18h,0
BTFSS 03,2
GOTO Main8
GOTO Mess3
GOTO SetUp
BTFSS 03,0
GOTO Main9
GOTO Mess2
GOTO SetUp
GOTO Mess1
GOTO SetUp
END
|
;Start of memory for the program.
;Go to Bank 1
;Load W with 0000 0001
;Make RA0 input
;Make all port B output
;Go to Bank 0 - the program memory area.
;Clear the number-of-games counter
;Clear the Player counter
;Clear the Computer counter
;Clear the jump counter
;Clear the button-press file
;Add W to Program Counter to create a jump.
;4
;1
;2
;2
;1 - third selection
;4
;2
;4
;2
;1
;4
;2
;4
;2
;1
;4
;4
;1
;1
;4
;2
;1
;2
;1
;4
;2
;4
;2
;2
;1
;4
;2
;4
;2
;1
;4
;4
;1
;1
;4
;2
;1
;End of table
;End of table
;End of table
;End of table
;Add W to the Program Counter to create a jump.
;y format= gfedcba
;O
;U
;
;L
;O
;S
;E
;
;End of message
;t
;I
;E
;
;End of message
;"Attract"
Attract removes the LEDs from the
middle of
;the display and continues until all LEDs are removed,
;then reverses.
;Illuminate all LEDs
;Illuminate all LEDs
;100 loops = 100mS
;Create 1 second delay
;Create 100mS delay
;1 sec delay not using Del100
;"Flash" Flashes all 8 LEDs on the display 8 times.
;then RETURNs to Main
;Zero file 6
;Make file 06 = FF Turn on all LEDs
;Show the letter "I"
;Start at "Lose" on table
;Jumper for table
;Output to display
;Detect end of Table
;End of Table detected
;Jump to next letter
;"Mess2" shows on 7-segment display: "You Lose"
;Jumper for table
;"Mess3" shows on 7-segment display: "tie"
;Start at "tIE" on table
;Jumper for table
;"Run" produces a Running LEDs effect across the
;display. The Running bit is stored in file 0D. It moves
;across and back 4 times
then RETURNs to Main
;4 runs
;Create "bit"
;Test the carry bit
;100mS delay
;Move the bit back onto the display
;100mS delay
;Test the carry bit
;Number of games in file 16h
;Player Score in file 17h
;Computer Score in file 18h.
;Player Scissors?
;Not player scissors
;Computer Rock?
;Computer wins
;Computer Paper?
;Player Scissors, Computer Paper
;Player Paper?
;Not player paper
;Computer Rock?
;Player wins
;Computer Paper?
;Player Paper, Computer Scissors. Computer wins
;Player Rock?
;Player not playing
;Player Rock, Computer Rock?
;Player Rock, Computer Paper?
;Computer wins
;Player wins
;Draw
;Draw
;INCrement the number of games.
;Create 250mS delay
;"Toggle" Flashes the top 4 LEDs then the bottom 4
;LEDs. This is repeated times
;Turn on 4 top LEDs
;Turn on 4 bottom LEDs
;File 1C = jump value for
"Attract" sub-routine
before
;displaying RPS. File 1D = jump value for RPS value
;Blank screen for 1 sec
;Produce random number up to F
;Copy 1C to W
;File 11h holds 1, 2, 4 or FF
;Test for End of Table
;Start at top of table
;Call 1 second delay
;Copy 1D to W
;File 12h holds 1, 2 or 4
;Test for End of Table
;Produce "R"
;Put display value into file 0E
;Produce "P"
;Put display value into file 0E
;Produce "S"
;Put display value into file 0E
;Increment three counters
;Sample at 3 games. 14h for twenty games
;Match?
;Not 3 games
;Reset the games counter
;Test for zero in file
;Test for zero in file - no games played!
;Copy Player Score into W
;Subtract Player from Computer Score
;Check for "tie"
;Not a tie
;Display "tie"
;Test carry flag
;Display "You Lose"
;Display "I Lose"
;Tells assembler end of program
|
|
FINAL NOTES
This program uses about one-third of the memory of the chip and has not been
optimised. Some of the delays could be combined and the two tables could be
combined. Some of the sub-routines could also be reduced by very clever
programming, but why make things more-complex than needed?
A program such as RPS shows how to make the most of limited features. The two
displays operate at the same time, so it is difficult to know which display
will be operational, and the switch has to perform a number of
functions.
You can increase the size of the display with illuminated pictures and add
three buttons, to increase the impact as a promotional display. This would be
ideal as an advertising display at a meeting/drinking venue or billboard
etc.
It could even be made into a credit card game and given away FREE with a
purchase of a promotional product etc. It could even be made into a drink
coaster for a bar/cocktail venue, night-club.
Some-one will run with the idea and make millions.
It could be added to a FREE box of matches or attached to to any number of
products.
All you need to do is combine it with a number of other games/ideas and you
have the basis of a product/project worth marketing.
Once you understand each of the sub-routines you will able to add new features
or design a totally different game.
The possibilities are almost limitless.
RPS has been created by hand and the only diagnostic tool was a sub-routine
that displayed the value in File 1F onto the 8 LEDs to help get the
button-press to work.
The same routine was used to see the contents in files 17h and 18h to get
the "I Lose" etc to display correctly.
Of course it's a lot of long, slow, work, but the requirements of the program
rate fairly high in complexity and no other program or approach I have seen,
can deliver any form of simplification. If you can show me a simpler approach
than that described above, there is $10,000 in the offering.
Once you get to this level of programming, you will be ready for our next range
of projects. These will include modules for the PIC16F84 as well as the
PIC16F628. The index will contain all the latest information, so keep up with
our developments and you will in the running for designing simple
projects.
There are a number of discussion groups on the web concerning the dismissal of electronics
staff who are not up-to-date in the digital/micro area.
All their training was in the analogue/RF area, and they now find themselves out of
work.
One of the important points to understand is the constant change in electronics
and if you don't keep up with this advancement, you will be overtaken.
The enquirer on the web asked if anyone knew of a company employing personnel
in the purely analogue/RF field.
My answer is simple. If a prospective employee has analogue/RF skills as well
as micro/programming skills, the answer is clear-cut - he is readily
employable.
I cannot see any new product being developed without the inclusion of digital
concepts.
It may be an LCD screen, digital volume or digital on/off. Without a digital
understanding your chances for long-term employment are virtually nil.
The addition of digital/micro to almost any product will bring it up-to-date
and quite often reduce its cost. Every manufacturer is aiming for this and if
he sees a staff member "falling behind" in concepts, he will be
looking for new talent.
There is only one thing between you and the boss. Money.
You must be able to
pay your way two-fold.
All the projects we are presenting on our site are designed to start you at the
beginning. This is the only site ANYWHERE to have this aim and to prove its
success with tens of thousands of readers and sales of hundreds of thousands of
kits.
As we have always said, take a few of our projects to an interview and you are
absolutely GUARANTEED to leap-frog over all the other prospective applicants.
But you must be able to back-up the image you present at the interview with
knowledge. And the only way to get this knowledge is by carrying out the practical
side of electronics by building the projects and experimenting and
experimenting and experimenting.
We have provided it all for you. It's just a matter of fulfilling your side of
the bargain. Start with the PIC LAB-1. It can show you so much.
To Top
|