QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Pete on September 16, 2018, 08:49:11 pm

Title: New and BETTER Challenge. Make a game 20-lines or less.
Post by: Pete on September 16, 2018, 08:49:11 pm
It just has to be stand alone code, that means no $INCLUDE files, library calls, etc. and no colons. No tricks like using REM or a long variable that the program parses and feeds to another program, and then runs that much larger (More program lines) program.

2-Line Card Game Example:

Code: QB64: [Select]
  1.     a$ = LTRIM$(STR$(INT(RND * 13 + 2))) + "." + LTRIM$(STR$(INT(RND * 4 + 1)))
  2.     IF INT(VAL(a$)) > 10 THEN b$ = MID$("JQKA", VAL(a$) - 10, 1) + MID$(CHR$(3) + CHR$(4) + CHR$(5) + CHR$(6), VAL(MID$(a$, INSTR(a$, ".") + 1)), 1) ELSE b$ = LTRIM$(STR$(INT(VAL(a$)))) + MID$(CHR$(3) + CHR$(4) + CHR$(5) + CHR$(6), VAL(MID$(a$, INSTR(a$, ".") + 1)), 1)
  3.     IF INSTR(mystring$, b$) = 0 THEN mystring$ = b$ + "=" + MID$(a$, 1, INSTR(a$, ".") - 1) + "|" + mystring$
  4. LOOP WHILE LEN(mystring$) < 284
  5.     msg$ = MID$(mystring$, 1, INSTR(mystring$, "=") - 1) + " "
  6.     x = VAL(MID$(mystring$, INSTR(mystring$, "=") + 1))
  7.     DO
  8.         IF LEN(msg$) < 5 THEN PRINT msg$;
  9.         LINE INPUT "Will the next card be [h]igher or [l]ower? "; hl$
  10.         mystring$ = MID$(mystring$, INSTR(mystring$, "|") + 1)
  11.         IF UCASE$(hl$) = "H" THEN IF VAL(MID$(mystring$, INSTR(mystring$, "=") + 1)) > x THEN msg$ = "1|Correct! Plus 1-Point." ELSE IF VAL(MID$(mystring$, INSTR(mystring$, "=") + 1)) = x THEN msg$ = "0|It's a push, try again!" ELSE msg$ = "-1|Wrong. Minus 1-Point."
  12.         IF UCASE$(hl$) = "L" THEN IF VAL(MID$(mystring$, INSTR(mystring$, "=") + 1)) < x THEN msg$ = "1|Correct! Plus 1-Point." ELSE IF VAL(MID$(mystring$, INSTR(mystring$, "=") + 1)) = x THEN msg$ = "0|It's a push, try again!" ELSE msg$ = "-1|Wrong. Minus 1-Point."
  13.     LOOP WHILE LEN(msg$) < 5
  14.     score = score + VAL(msg$)
  15.     PRINT " ... "; MID$(msg$, INSTR(msg$, "|") + 1) + " Score = "; LTRIM$(STR$(score)); CHR$(13)
  16.     IF mystring$ = "" THEN EXIT DO



Title: Re: New and BETTER Challenge. Make a game 20-lines or less.
Post by: Cobalt on September 17, 2018, 12:21:23 pm
Here you go 20 lines of code for a game!(ish) padded it out to 20 lines, the original was only 16
Simple card guessing game with 1 hint.
Code: QB64: [Select]
  1. 'Card Guessing Game, Kobolt 9/17/2018-12:15pm Ver 1.0, 20 line Challenge!
  2. DIM hints(8) AS STRING
  3. FOR i% = 1 TO 8
  4.  READ hints(i%)
  5. NEXT i%
  6.  c% = INT(RND * 4) + 1
  7.  n% = INT(RND * 13) + 1
  8.  PRINT hints(c%)
  9.  DO
  10.   INPUT "What card is it (S,C,H,D:A-1,J-11,Q-12,K-13|ex-D8=8 of Diamonds)"; a$
  11.   Try~% = Try~% + 1
  12.  LOOP UNTIL UCASE$(a$) = (hints(4 + c%) + LTRIM$(RTRIM$(STR$(n%))))
  13.  Good~% = Good~% + 1
  14.  INPUT "Try again?"; a$
  15. LOOP UNTIL LCASE$(a$) = "n"
  16. PRINT "Total trys:"; Try~%; " for "; Good~%; " Correct!"
  17. DATA "a garden tool","used when somebody is bad","gotta love with this","Ladies best friend","S","C","H","D"
  18.  
Title: Re: New and BETTER Challenge. Make a game 20-lines or less.
Post by: Pete on September 17, 2018, 01:59:04 pm
FUN! But I had difficulty because I thought a "Ladies best friend." started with a "V" and I couldn't find that as one of the selection choices. Other than that, nice job! It really gave me a good vibe!

Pete :)
Title: Re: New and BETTER Challenge. Make a game 20-lines or less.
Post by: Cobalt on September 17, 2018, 04:08:35 pm
FUN! But I had difficulty because I thought a "Ladies best friend." started with a "V" and I couldn't find that as one of the selection choices. Other than that, nice job! It really gave me a good vibe!

Pete :)

You know somebody I used to work with brought back a deck of cards like that from Vegas! Instead of the normal spade,clubs,hearts,and diamonds it was right up the "V" alley!(or gutter if you like)
One thing a program of only 20 lines comes up short on is instructions!(and pictures like the ones on the deck of cards ;)
Glad you found it interesting, never done one of these challenges before.
Title: Re: New and BETTER Challenge. Make a game 20-lines or less.
Post by: Pete on September 17, 2018, 04:40:47 pm
Glad you enjoyed it. We had a whole forum dedicated to QB coding challenges at the N54 QBasic forum. We had a couple of coders who made it their home. I hardly ever got involved but I the ones I did participate in were fun. My friend Mac, who ran the forum at the time and who was a career programmer for IBM couldn't believe I just programmed as a hobby. I often thought if I had been somehow introduced to BASIC programming just a year earlier, instead of the BS FORTRAN cards I had to punch to take chemistry tests for an idiot chem teacher, I might have chosen this as my field instead of healthcare.

Pete
Title: Re: New and BETTER Challenge. Make a game 20-lines or less.
Post by: bplus on September 17, 2018, 08:20:53 pm
Code: QB64: [Select]
  1. RANDOMIZE TIMER ' digit thief.bas for QB64 2018-09-16 B+
  2.     gCount% = 0
  3.     thief% = INT(RND * 10)
  4.     robbed$ = "0123456789"
  5.     PRINT
  6.     WHILE _KEYDOWN(27) = 0 AND robbed$ <> STRING$(10, " ")
  7.         INPUT "Enter Guess (0 to 9) where the digit thief is now "; guess%
  8.         gCount% = gCount% + 1
  9.         IF guess% = thief% THEN
  10.             PRINT "You caught the thief in" + STR$(gCount%) + " guesses."
  11.             EXIT WHILE
  12.         ELSE
  13.             MID$(robbed$, thief% + 1, 1) = " "
  14.             PRINT "Game Update: " + robbed$
  15.             thief% = ABS(INT(RND * 3) - 1 + thief%) MOD 10
  16.             IF robbed$ = STRING$(10, " ") THEN PRINT "Dang! too late! Gone in" + STR$(gCount%) + " guesses."
  17.         END IF
  18.     WEND
  19.  

It is really hard to loose all your digits. ;)
Title: Re: New and BETTER Challenge. Make a game 20-lines or less.
Post by: Pete on September 17, 2018, 09:39:52 pm
It took me 13 guesses to catch the Democrat. Is that a lot?

Nice job! I just guessed at random, not sure if there is a strategy.

Pete
Title: Re: New and BETTER Challenge. Make a game 20-lines or less.
Post by: bplus on September 17, 2018, 10:51:08 pm
I think I have the essence of it:
Code: QB64: [Select]
  1. RANDOMIZE TIMER '21.bas for QB64 2018-09-17 B+
  2. DIM deck%(1 TO 52)
  3. FOR i% = 1 TO 52
  4.     IF i% MOD 13 + 1 > 10 THEN deck%(i%) = 10 ELSE deck%(i%) = i% MOD 13 + 1
  5.     IF deck%(i%) = 1 THEN deck%(i%) = 11 'aces always 11
  6. FOR i% = 52 TO 2 STEP -1 'shuffle
  7.     r% = INT(RND * i%) + 1
  8.     SWAP deck%(r%), deck%(i%)
  9. WHILE playerT% < 15
  10.     di% = di% + 1
  11.     playerT% = playerT% + deck%(di%)
  12.     _PRINTSTRING (27, di% + 5), "Player card: " + RIGHT$(SPACE$(2) + STR$(deck%(di%)), 2) + " total is " + RIGHT$(SPACE$(2) + STR$(playerT%), 2)
  13. WHILE playerT% < 22 AND dealerT% < playerT%
  14.     di% = di% + 1
  15.     dealerT% = dealerT% + deck%(di%)
  16.     _PRINTSTRING (27, di% + 7), "Dealer card: " + RIGHT$(SPACE$(2) + STR$(deck%(di%)), 2) + " total is " + RIGHT$(SPACE$(2) + STR$(dealerT%), 2)
  17.  
Title: Re: New and BETTER Challenge. Make a game 20-lines or less.
Post by: Pete on September 18, 2018, 01:58:15 am
Ah Black Jack, nice! I wrote a much larger version in the 1980s to evaluate different strategies. I actually found one that if you played enough hands and bet enough money, you might want to quit your day job and consider moving to Vegas. The problem was that would be putting in a 16 hour day and 100-500 bets. The odds also went down if you were playing from a shoe. Now what kept me from it is the days that you played a lot of hands, but were still down. Also, a book I read. It stated that if you think the people who put Vegas together with drugs, booze, and broads wouldn't cheat you at cards, well, you deserve what's coming to you. I actually saw a dealer wipe a player out in Reno by dealing seconds. I had a great view of him flipping the deck from a balcony. The guy was doing pretty well, so he started to bet heavy. He got wiped out in 7 straight losses.

Pete
Title: Re: New and BETTER Challenge. Make a game 20-lines or less.
Post by: SMcNeill on September 18, 2018, 10:52:47 pm
My Graphical Game:  CHASE IT!!

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. GOTO movebox
  3. DO UNTIL loss = 3
  4.     CLS
  5.     IF TIMER > timelimit## THEN
  6.         loss = loss + 1
  7.         movebox:
  8.         timelimit## = TIMER + 10 * 0.9 ^ (loss + tag)
  9.         x = RND * (_WIDTH - 50)
  10.         y = RND * (_HEIGHT - 25)
  11.     END IF
  12.     LINE (x, y)-STEP(50, 25), &HFFFF0000, BF
  13.     'Two lines from combined WEND statement above
  14.     IF _MOUSEX >= x AND _MOUSEX <= x + 50 AND _MOUSEY >= y AND _MOUSEY <= y + 25 THEN tag = tag + 1: GOTO movebox 'clicked in the box
  15.     'Two lines from combined IF statement above
  16.     PRINT "You've tagged the box"; STR$(tag); " times."; CHR$(13); "You've missed the box"; loss; "times."; CHR$(13); "Time Limit this turn"; timelimit## - TIMER
  17.     _DISPLAY

The challenge here is to see how many times you can chase and tag the red box with your mouse.   

GASP!!

That's RIGHT!!   A 20 line game which even includes mouse support to play it!   HURRAH!!

/STRUT  /PRANCE  /GIGGLE

It's nothing too fancy, but I think it's nice for the short number of lines which comprises it.   We have mouse support, increasing time constraints, a counter so we can compete against ourselves or others...   Basically, we have a cheesy little "game" in 20 lines. 

And I didn't even have to break Pete's rules to post an entry,  or change them to make a new challenge.  :P
Title: Re: New and BETTER Challenge. Make a game 20-lines or less.
Post by: bplus on September 19, 2018, 01:07:31 am
Nice to see graphics in 20 lines and a real game but weird jumping into middle of loop and this:
timelimit## = TIMER + 10 * 0.9 ^ (loss + tag)

that one needs some study. :)
Title: Re: New and BETTER Challenge. Make a game 20-lines or less.
Post by: SMcNeill on September 19, 2018, 01:14:22 am
Nice to see graphics in 20 lines and a real game but weird jumping into middle of loop and this:
timelimit## = TIMER + 10 * 0.9 ^ (loss + tag)

that one needs some study. :)

TIMER plus ( 10 seconds...  reduced by 90% each time you tag the box or "lose")

10
9
8.1
7.21

And down it counts, giving less response time as it goes.
Title: Re: New and BETTER Challenge. Make a game 20-lines or less.
Post by: Pete on September 19, 2018, 01:21:04 am
BZZZZZZZZZ

The rules clearly state no colons. ICE has been notified and you will be de-ported to FREEBASIC, immediately! :D

OK, just kidding about the Idiot Code Enforcement people. They've been under staffed ever since FB came on the seen, anyway.

It works! Actually it would be pretty darn challenging if you had to click the mouse on the box before it moved. That's especially true using just one finger on a touch pad. Now is getting that in 20-lines possible?

Pete
Title: Re: New and BETTER Challenge. Make a game 20-lines or less.
Post by: SMcNeill on September 19, 2018, 01:30:12 am
BZZZZZZZZZ

The rules clearly state no colons. ICE has been notified and you will be de-ported to FREEBASIC, immediately! :D

OK, just kidding about the Idiot Code Enforcement people. They've been under staffed ever since FB came on the seen, anyway.

It works! Actually it would be pretty darn challenging if you had to click the mouse on the box before it moved. That's especially true using just one finger on a touch pad. Now is getting that in 20-lines possible?

Pete

You could if you took out the RANDOMIZE TIMER.  Or perhaps actually label the lines 1,2,3...  Then you could remove the movebox: label, freeing up a line of code.

Just add a OldButton = _MOUSEBUTTON(1) to track old mouse button state before the _DISPLAY, and then add 2 conditions to the IF check....

IF (existing junk)  AND _MOUSEBUTTON(1) AND NOT OldButton THEN.....

Then the user would have to UP the mouse each "click", before the next DOWN would register for the button.

Close enough to "mouse click" behavior for such a line limitation program.   
Title: Re: New and BETTER Challenge. Make a game 20-lines or less.
Post by: TempodiBasic on September 30, 2018, 04:10:18 pm
Hi guys
in this weekend I got some time to code my little idea
here the game
Pick Highest card
you start the game and pick a card, after AI does the same
you can replay while the program shows the scores of the two player (user vs AI)

Code: QB64: [Select]
  1. PRINT "WELCOME TO HIGH CARD GAME:"
  2. PRINT "Pick a card and see if you have more fortune than AI"
  3. 6 CLS , 4
  4. PRINT "To play press Enter, another key to quit       Player "; scoreP%; " vs AI "; scoreA%
  5. IF (INPUT$(1) <> CHR$(13)) THEN END
  6. PLAYER(1) = (INT(RND * 12) + 1) + ((INT(RND * 4) + 1) * 100)
  7.     PLAYER(0) = (INT(RND * 12) + 1) + ((INT(RND * 4) + 1) * 100)
  8. LOOP UNTIL PLAYER(1) <> PLAYER(0)
  9. PRINT "Player card: "; RIGHT$(STR$(PLAYER(1)), 2) + MID$("", (PLAYER(1) \ 100), 1)
  10. PRINT "AI card: "; RIGHT$(STR$(PLAYER(0)), 2) + MID$("", (PLAYER(0) \ 100), 1)
  11. IF (PLAYER(1) - PLAYER(0) > 0) THEN PRINT "Player wins. AI looses"
  12. IF (PLAYER(1) - PLAYER(0) > 0) THEN scoreP% = scoreP% + 1
  13. IF (PLAYER(1) - PLAYER(0) < 0) THEN PRINT "Player looses. AI wins"
  14. IF (PLAYER(1) - PLAYER(0) < 0) THEN scoreA% = scoreA% + 1
  15.  

Thanks to try
Title: Re: New and BETTER Challenge. Make a game 20-lines or less.
Post by: bplus on September 30, 2018, 08:55:27 pm
Hey TempodiBasic!

Yeah! Your code reminded me of the first post I ever made on a forum, I called it "War and Peace". It ran a little longer than this:
Code: QB64: [Select]
  1. _TITLE "War for QB64 B+ 2018-09-30"
  2. DIM deck$(51)
  3. FOR i% = 0 TO 51
  4.     deck$(i%) = MID$(" 2 3 4 5 6 7 8 910 J Q K A", 2 * ((i% MOD 13)) + 1, 2) + MID$("", INT(i% / 13) + 1, 1)
  5. FOR i% = 51 TO 1 STEP -1 'shuffle
  6.     r% = INT(RND * i%) + 1
  7.     SWAP deck$(r%), deck$(i%)
  8. FOR i% = 0 TO 50 STEP 2
  9.     IF INSTR(" 2 3 4 5 6 7 8 910 J Q K A", LEFT$(deck$(i%), 2)) - INSTR(" 2 3 4 5 6 7 8 910 J Q K A", LEFT$(deck$(i% + 1), 2)) > 0 THEN p = p + t + 2
  10.     IF INSTR(" 2 3 4 5 6 7 8 910 J Q K A", LEFT$(deck$(i%), 2)) - INSTR(" 2 3 4 5 6 7 8 910 J Q K A", LEFT$(deck$(i% + 1), 2)) < 0 THEN ai = ai + t + 2
  11.     IF INSTR(" 2 3 4 5 6 7 8 910 J Q K A", LEFT$(deck$(i%), 2)) - INSTR(" 2 3 4 5 6 7 8 910 J Q K A", LEFT$(deck$(i% + 1), 2)) = 0 THEN t = t + 2 ELSE t = 0
  12.     PRINT deck$(i%); " vrs "; deck$(i% + 1); "  Player:"; p; " Opponent:"; ai; " Ties:"; t;
  13.     INPUT "   Enter to continue...", wate$
  14.  
  15.  
Title: Re: New and BETTER Challenge. Make a game 20-lines or less.
Post by: TempodiBasic on October 01, 2018, 11:30:27 am
Hi Bplus,
fine your War and Peace, the differences between pick highest card and War and Peace , IMHO, are
1. you project a tie option so the seed make no difference, while in my project the rule "Come Quando Fuori Piove = Cuori Quadri Fiori Picche" used also in the game of poker decides who wins when the value of the card is the same. In English Hearts Diamonds Clubs Spades from the higher to the lower.
2. in the code you have preferred to use the data as string while I have preferred to use data as integer number
3. you have choosen to create a deck of french cards of 52 cards, to shuffle it and to pick sequentially the cards, one for each player, until the deck is finished. To replay you must add a loop like DO..LOOP or a GOTO to the beginning of the code.
In the while I have choosen to create at moment one card for each player at each cycle of the game. This emulates that the cards used are put in the deck and this deck is shuffled at each cycle of the game.

Two version of the same game or if you prefer two similar games...
Thanks to read
Title: Re: New and BETTER Challenge. Make a game 20-lines or less.
Post by: bplus on October 01, 2018, 01:17:21 pm
Hi TempodiBasic,

In my actual game of War and Peace, I did use the suit to decide between ties in card value ie in times of Peace Hearts trump all (Hearts > Spades > Diamonds > Clubs) whereas in times of War, Clubs trumped all in reverse direction Clubs > Diamonds > Spades > Hearts. But this is not that game.

Now in the card game of WAR, which I depicted a shortened version here, there are no trump or suit favoritism, so when there is a tie, say Queen against Queen WAR is declared and the next person who gets highest card value gets all the cards that had tied. Here in this shortened version, I use points instead of cards and only go through deck once. It would take me more than 20 lines to simulate the actual card game of WAR fully (to which there is usually no end).

You call 52 card decks "french cards" ?
Title: Re: New and BETTER Challenge. Make a game 20-lines or less.
Post by: TempodiBasic on October 01, 2018, 04:24:40 pm
Hi Bplus
about
Quote
You call 52 card decks "french cards" ?
yes
https://en.wikipedia.org/wiki/French_playing_cards (https://en.wikipedia.org/wiki/French_playing_cards) French cards in English
https://en.wikipedia.org/wiki/List_of_traditional_card_and_tile_packs (https://en.wikipedia.org/wiki/List_of_traditional_card_and_tile_packs) all kind of deck in the world
https://it.wikipedia.org/wiki/Carte_da_gioco#Semi (https://it.wikipedia.org/wiki/Carte_da_gioco#Semi) seeds of French cards in different variant of name and images 13 for seed, 4 seeds and plus 2 jolly/joker
https://it.wikipedia.org/wiki/Carte_da_gioco#Semi_italiani (https://it.wikipedia.org/wiki/Carte_da_gioco#Semi_italiani) seeds used in italian variants 10 cards for seed, 4 seeds
     http://www.cavallore.it/index.php/mazzi-di-carte/regionali-italiane/66-carte-triestine?jjj=1538424330056 (http://www.cavallore.it/index.php/mazzi-di-carte/regionali-italiane/66-carte-triestine?jjj=1538424330056) triestine
     http://www.portaledelleosterie.it/osterie_dettaglio.php?Main=20&Sub=&Ter=158 (http://www.portaledelleosterie.it/osterie_dettaglio.php?Main=20&Sub=&Ter=158) bolognesi
      http://www.portaledelleosterie.it/osterie_dettaglio.php?Main=20&Sub=&Ter=148 (http://www.portaledelleosterie.it/osterie_dettaglio.php?Main=20&Sub=&Ter=148) piacentine
     http://www.pagliuccone.it/curiosita/origine-delle-carte-napoletane/ (http://www.pagliuccone.it/curiosita/origine-delle-carte-napoletane/)napoletane
     http://www.cavallore.it/index.php/mazzi-di-carte/regionali-italiane/45-carte-siciliane (http://www.cavallore.it/index.php/mazzi-di-carte/regionali-italiane/45-carte-siciliane)siciliane
     http://www.cavallore.it/index.php/mazzi-di-carte/regionali-italiane/67-carte-sarde (http://www.cavallore.it/index.php/mazzi-di-carte/regionali-italiane/67-carte-sarde)sarde
 http://giocareacarte.altervista.org/mazzi-tedeschi-mazzi-svizzeri/ (http://giocareacarte.altervista.org/mazzi-tedeschi-mazzi-svizzeri/) german (36 cards) and swiss cards (40 cards)
     http://www.portaledelleosterie.it/osterie_dettaglio.php?Main=20&Sub=&Ter=157 (http://www.portaledelleosterie.it/osterie_dettaglio.php?Main=20&Sub=&Ter=157) salisburgh cards (36 cards) and AltoAtesine (36 or 40 cards)

How much hystory is shown in the play cards of the world!


Title: Re: New and BETTER Challenge. Make a game 20-lines or less.
Post by: bplus on October 01, 2018, 05:03:27 pm
Cool card history!

Turns out I did not get the rules to "War" quite right. According to what I read here:
https://www.bicyclecards.com/how-to-play/war/

with a tie, 2 cards are put face down and then 2 more cards face up, highest card takes all 6 cards!

If another tie and winner on next turn, they would get 10 cards, 4 face down + 6 battle cards.
Title: Re: New and BETTER Challenge. Make a game 20-lines or less.
Post by: TempodiBasic on October 03, 2018, 04:57:51 am
Thank Bplus
you let me to know  War and Peace that I thought it was an your game.
It is like a my local card game called Stoppa, yes War and Peace is not hazard game and it is simpler, but the logic behind is the same.
https://it.wikipedia.org/wiki/Stoppa_(gioco_di_carte) (https://it.wikipedia.org/wiki/Stoppa_(gioco_di_carte)) It needs deck of 40 cards to play.