mirror of
https://github.com/nippur72/apple1-videocard-lib.git
synced 2025-01-02 23:31:53 +00:00
add Anagram & Hangman
This commit is contained in:
parent
ab3d431795
commit
687832c5fc
167
demos/anagram/anagram.c
Normal file
167
demos/anagram/anagram.c
Normal file
@ -0,0 +1,167 @@
|
||||
#include <string.h>
|
||||
#include <utils.h>
|
||||
#include <apple1.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "words.h" // vocabulary of words
|
||||
|
||||
char *the_word; // the word to guess
|
||||
char scrambled[MAXWORDLEN+1]; // the word with scrambled letters
|
||||
char buf[16]; // string buffer containing the number of the score
|
||||
|
||||
unsigned int score; // keeps score (accumulated length of guessed words)
|
||||
__address(0x03) unsigned long int time = 0; // keeps track of time
|
||||
__address(0x04) unsigned int time_high; // high word of the time counter
|
||||
unsigned int timeout;
|
||||
byte len;
|
||||
|
||||
#define LETTERSCORE (150)
|
||||
|
||||
// gets a key, increment 32 bit timer and randomize seed
|
||||
byte getkey() {
|
||||
byte k = apple1_readkey();
|
||||
if(k!=0) return k;
|
||||
|
||||
// increment 32 bit timer
|
||||
asm {
|
||||
inc time
|
||||
bne inc_end
|
||||
inc time+1
|
||||
bne inc_end
|
||||
inc time+2
|
||||
bne inc_end
|
||||
inc time+3
|
||||
inc_end:
|
||||
}
|
||||
|
||||
// randomize random seed
|
||||
rand_state++;
|
||||
if(rand_state == 0) rand_state = 1; // avoid 0 state for the LFSR
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// pick the word to guess randomly from the vocabulary
|
||||
char *pick_random_word() {
|
||||
unsigned int index;
|
||||
do index = rand() & 0x7fff; while(index>=NUMWORDS);
|
||||
return words[index];
|
||||
}
|
||||
|
||||
// pick a random letter within the word
|
||||
byte pick_random_letter(byte l) {
|
||||
byte index;
|
||||
do index = rand() & 31; while(index>=l);
|
||||
return index;
|
||||
}
|
||||
|
||||
// scramble the word to guess
|
||||
void scramble_word() {
|
||||
strcpy(scrambled, the_word);
|
||||
|
||||
len = strlen(scrambled);
|
||||
|
||||
// permute each letter of the word with a random position
|
||||
for(byte x=0;x<len;x++) {
|
||||
byte y = pick_random_letter(len); // gets a random position
|
||||
|
||||
// do the letter swapping
|
||||
byte c = scrambled[x];
|
||||
scrambled[x] = scrambled[y];
|
||||
scrambled[y] = c;
|
||||
}
|
||||
}
|
||||
|
||||
byte game_loop() {
|
||||
|
||||
the_word = pick_random_word(); // picks a random word
|
||||
scramble_word(); // and scrambles it
|
||||
|
||||
// calculate timeout
|
||||
timeout = 0;
|
||||
for(byte t=0;t<len;t++) {
|
||||
timeout += LETTERSCORE;
|
||||
}
|
||||
|
||||
time = 0; // reset time
|
||||
byte x = 0; // position of letter guessed correctly
|
||||
|
||||
// display the scrambled word
|
||||
woz_puts("\r\r>>> ");
|
||||
woz_puts(scrambled);
|
||||
woz_puts(" <<< \r\r");
|
||||
|
||||
// loop that asks for a letter and counts time
|
||||
while(1) {
|
||||
byte k = getkey();
|
||||
if(k) {
|
||||
if(k==27) return 2; // ESC quits the game
|
||||
|
||||
// if letter is correct, display and advance
|
||||
if(k==the_word[x]) {
|
||||
woz_putc(k);
|
||||
x = x + 1;
|
||||
if(the_word[x] == 0) return 1; // whole word guessed correctly, game completed
|
||||
}
|
||||
else {
|
||||
// wrong letter, time penalty
|
||||
time_high += (LETTERSCORE/2);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// check if time elapsed
|
||||
if(time_high > timeout) break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void game() {
|
||||
//woz_puts("\r\rGUESS THE WORDS!");
|
||||
|
||||
score = 0;
|
||||
|
||||
while(1) {
|
||||
byte result = game_loop();
|
||||
if(result==1) {
|
||||
score += strlen(the_word);
|
||||
utoa(score, buf, 10);
|
||||
woz_puts("\r\rCORRECT! SCORE: ");
|
||||
woz_puts(buf);
|
||||
}
|
||||
else if(result==0) {
|
||||
woz_puts("\r\rOUT OF TIME!!! - YOU LOOSE\r\r");
|
||||
woz_puts("THE WORD WAS: ");
|
||||
woz_puts(the_word);
|
||||
break;
|
||||
}
|
||||
else break; // result == 2, ESC pressed
|
||||
}
|
||||
|
||||
utoa(score, buf, 10);
|
||||
woz_puts("\r\rFINAL SCORE: ");
|
||||
woz_puts(buf);
|
||||
woz_puts("\r\r");
|
||||
}
|
||||
|
||||
void main() {
|
||||
|
||||
woz_puts("\r\r*** THE ANAGRAM GAME ***");
|
||||
woz_puts("\r\rGUESS THE WORD BEFORE TIME RUNS OUT");
|
||||
woz_puts("\r\rTIME PENALTY FOR WRONG LETTERS");
|
||||
woz_puts("\r\rPRESS ANY KEY TO START ");
|
||||
|
||||
while(!getkey()); // randomize while waiting for a key
|
||||
|
||||
while(1) {
|
||||
game();
|
||||
woz_puts("PLAY AGAIN? ");
|
||||
byte k;
|
||||
do k = apple1_getkey(); while(k!='N' && k!='Y');
|
||||
if(k=='N') break;
|
||||
}
|
||||
woz_puts("\r\rGAME WRITTEN BY ANTONINO PORCINO\r\rBYE!\r\r");
|
||||
woz_mon();
|
||||
}
|
||||
|
208
demos/anagram/hangman.c
Normal file
208
demos/anagram/hangman.c
Normal file
@ -0,0 +1,208 @@
|
||||
#define APPLE1_USE_WOZ_MONITOR 1
|
||||
|
||||
#include <string.h>
|
||||
#include <utils.h>
|
||||
#include <apple1.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "words.h" // vocabulary of words
|
||||
|
||||
char *the_word; // the complete word to guess
|
||||
char partial[MAXWORDLEN+1]; // the partial word guessed by the user with "_"
|
||||
char keybuf[MAXWORDLEN+1]; // keyboard buffer
|
||||
|
||||
char len; // the length of the word
|
||||
char hang_state; // status progression of the hang
|
||||
|
||||
#define MAX_HANG_STATE 6 // 0=initial, 1=head, 2=body, 3=leftarm, 4=left leg, 5=right arm, 6=right leg
|
||||
|
||||
// gets a key and randomize seed
|
||||
byte getkey() {
|
||||
byte k = apple1_readkey();
|
||||
if(k!=0) return k;
|
||||
|
||||
// randomize random seed
|
||||
rand_state++;
|
||||
if(rand_state == 0) rand_state = 1; // avoid 0 state for the LFSR
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// pick the word to guess randomly from the vocabulary
|
||||
char *pick_random_word() {
|
||||
unsigned int index;
|
||||
do index = rand() & 0x7fff; while(index>=NUMWORDS);
|
||||
return words[index];
|
||||
}
|
||||
|
||||
// 012345678901234567
|
||||
//0 XXXXXXXXXXX
|
||||
//1 X / !
|
||||
//2 X / O
|
||||
//3 X/ /#\
|
||||
//4 X / \
|
||||
//5 X
|
||||
//6 X
|
||||
//7~~~~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
|
||||
char L0[20];
|
||||
char L1[20];
|
||||
char L2[20];
|
||||
char L3[20];
|
||||
char L4[20];
|
||||
char L5[20];
|
||||
char L6[20];
|
||||
char L7[20];
|
||||
|
||||
void print_hangman() {
|
||||
|
||||
strcpy(L0, " ___________");
|
||||
strcpy(L1, " ! / ");
|
||||
strcpy(L2, " ! / ");
|
||||
strcpy(L3, " !/ ");
|
||||
strcpy(L4, " ! ");
|
||||
strcpy(L5, " !");
|
||||
strcpy(L6, " !");
|
||||
strcpy(L7, "~~~+~~~~~~~~~~~~~~~");
|
||||
|
||||
if(hang_state >= 1) {
|
||||
L1[13] = '!';
|
||||
L2[13] = 'O';
|
||||
}
|
||||
if(hang_state >= 2) {
|
||||
L3[13] = 'X';
|
||||
}
|
||||
if(hang_state >= 3) {
|
||||
L3[12] = '/';
|
||||
}
|
||||
if(hang_state >= 4) {
|
||||
L4[12] = '/';
|
||||
}
|
||||
if(hang_state >= 5) {
|
||||
L3[14] = '\\';
|
||||
}
|
||||
if(hang_state >= 6) {
|
||||
L4[14] = '\\';
|
||||
}
|
||||
|
||||
/*
|
||||
woz_puts("\rhang_state:");
|
||||
woz_print_hex(hang_state);
|
||||
woz_putc('\r');
|
||||
*/
|
||||
woz_puts("\r\r");
|
||||
woz_puts(L0); woz_putc('\r');
|
||||
woz_puts(L1); woz_putc('\r');
|
||||
woz_puts(L2); woz_putc('\r');
|
||||
woz_puts(L3); woz_putc('\r');
|
||||
woz_puts(L4); woz_putc('\r');
|
||||
woz_puts(L5); woz_putc('\r');
|
||||
woz_puts(L6); woz_putc('\r');
|
||||
woz_puts(L6); woz_putc('\r');
|
||||
woz_puts(L7); woz_putc('\r');
|
||||
woz_putc('\r');
|
||||
}
|
||||
|
||||
void print_word() {
|
||||
woz_puts("\r\rWORD: ");
|
||||
woz_puts(partial);
|
||||
woz_puts("\r\r");
|
||||
}
|
||||
|
||||
byte game_loop() {
|
||||
byte t;
|
||||
|
||||
// picks a random word
|
||||
the_word = pick_random_word();
|
||||
len = strlen(the_word);
|
||||
|
||||
// reset partial word
|
||||
strcpy(partial, the_word);
|
||||
for(t=0;t<len;t++) partial[t] = '_';
|
||||
|
||||
// display the initial hangman
|
||||
hang_state = 0;
|
||||
print_hangman();
|
||||
|
||||
while(1) {
|
||||
print_word(); // display the partial word
|
||||
|
||||
apple1_input_line_prompt(keybuf, len);
|
||||
|
||||
t = strlen(keybuf);
|
||||
|
||||
if(t == 1) {
|
||||
// one letter guess
|
||||
byte found = 0;
|
||||
char c = keybuf[0];
|
||||
|
||||
for(t=0;t<len;t++) {
|
||||
char q = the_word[t];
|
||||
if(q==c && partial[t] == '_') {
|
||||
partial[t] = c;
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
if(found == 0) {
|
||||
woz_puts("\r\rWRONG !!!\r\r");
|
||||
hang_state++;
|
||||
print_hangman();
|
||||
if(hang_state == MAX_HANG_STATE) return 0; // game over: man hanged
|
||||
}
|
||||
else {
|
||||
if(strcmp(the_word,partial)==0) {
|
||||
print_word();
|
||||
return 1; // game over: word guessed
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(t == len) {
|
||||
// guess the whole word
|
||||
if(strcmp(the_word,keybuf)==0) {
|
||||
strcpy(partial, keybuf);
|
||||
print_word();
|
||||
return 1; // game over: word guessed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void game() {
|
||||
while(1) {
|
||||
byte result = game_loop();
|
||||
if(result==1) {
|
||||
woz_puts("\r\rCORRECT! YOU SAVED THE MAN!!");
|
||||
break;
|
||||
}
|
||||
else if(result==0) {
|
||||
woz_puts("\r\rNOOO, HE'S DEAD NOW!\r\r");
|
||||
woz_puts("THE WORD WAS: ");
|
||||
woz_puts(the_word);
|
||||
break;
|
||||
}
|
||||
else break; // result == 2, ESC pressed
|
||||
}
|
||||
woz_puts("\r\r");
|
||||
}
|
||||
|
||||
void main() {
|
||||
|
||||
woz_puts("\r\r*** HANGMAN ***");
|
||||
woz_puts("\r\rSAVE THE MAN BY GUESSING THE WORD\r\r");
|
||||
|
||||
while(!getkey()); // randomize while waiting for a key
|
||||
|
||||
while(1) {
|
||||
game();
|
||||
woz_puts("PLAY AGAIN? ");
|
||||
byte k;
|
||||
do k = apple1_getkey(); while(k!='N' && k!='Y');
|
||||
if(k=='N') break;
|
||||
}
|
||||
woz_puts("\r\rGAME WRITTEN BY ANTONINO PORCINO\r\rBYE!\r\r");
|
||||
woz_mon();
|
||||
}
|
||||
|
10
demos/anagram/m.bat
Normal file
10
demos/anagram/m.bat
Normal file
@ -0,0 +1,10 @@
|
||||
@set TARGET=NOJUKEBOX
|
||||
@call ..\..\tools\build anagram
|
||||
copy out\anagram.prg ..\..\..\apple1-emu\software\anagram.prg /y
|
||||
copy out\anagram.bin ..\..\..\apple1-emu\software\sdcard_image\PLAB\ANAGRAM#060280 /y
|
||||
|
||||
@set TARGET=NOJUKEBOX
|
||||
@call ..\..\tools\build hangman
|
||||
copy out\hangman.prg ..\..\..\apple1-emu\software\hangman.prg /y
|
||||
copy out\hangman.bin ..\..\..\apple1-emu\software\sdcard_image\PLAB\HANGMAN#060280 /y
|
||||
|
3020
demos/anagram/mkwords.js
Normal file
3020
demos/anagram/mkwords.js
Normal file
File diff suppressed because it is too large
Load Diff
6
demos/anagram/words.h
Normal file
6
demos/anagram/words.h
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user