Qbasicnews.com

Full Version: need stack help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am working on a program that randomly picks one card, if the card is a diamond or heart it goes in stack1. If the card is a club or spade it goes in stack 2. Each stack can only hold 10 cards. Once one of the stack reaches 10 cards then it will not be able to deal anymore and it will print each stack to a text file. (stack1 to file1, stack2 to file2).

I have tried a lot of things and i just cant figure it out, I have recieved help and examples from the instructor but it hasnt helped.

Here is what i have so far:

Code:
DIM numbs$(12)
numbs$(1) = "Ace of "
numbs$(2) = "2 of "
numbs$(3) = "3 of "
numbs$(4) = "4 of "
numbs$(5) = "5 of "
numbs$(6) = "6 of "
numbs$(7) = "7 of "
numbs$(8) = "8 of "
numbs$(9) = "9 of "
numbs$(10) = "Jack of "
numbs$(11) = "Queen of "
numbs$(12) = "King of "

DIM shape$(4)
shape$(1) = "Spades"
shape$(2) = "Diamonds"
shape$(3) = "Hearts"
shape$(4) = "Clubs"


RANDOMIZE TIMER
CLS

redo:
i% = INT(RND * 12) + 1
t% = INT(RND * 4) + 1
n$ = LTRIM$(RTRIM$(STR$(i%)))
IF INSTR(used$, n$) THEN GOTO redo
used$ = used$ + n$

IF t% = 2 THEN
COLOR 12
END IF
IF t% = 3 THEN
COLOR 12
END IF
IF t% = 1 THEN
COLOR 13
END IF
IF t% = 4 THEN
COLOR 13
END IF

PRINT numbs$(i%); shape$(t%)
COLOR 7

OPEN "rfile.txt" FOR OUTPUT AS #1
PRINT #1, numbs$(i%); shape$(t%)
CLOSE
Code:
DIM numbs$(12)
numbs$(1) = "Ace of "
numbs$(2) = "2 of "
numbs$(3) = "3 of "
numbs$(4) = "4 of "
numbs$(5) = "5 of "
numbs$(6) = "6 of "
numbs$(7) = "7 of "
numbs$(8) = "8 of "
numbs$(9) = "9 of "
numbs$(10) = "Jack of "
numbs$(11) = "Queen of "
numbs$(12) = "King of "

DIM shape$(4)
shape$(1) = "Spades"
shape$(2) = "Diamonds"
shape$(3) = "Hearts"
shape$(4) = "Clubs"

OPEN "rfile.txt" FOR OUTPUT AS #1
OPEN "bfile.txt" FOR OUTPUT AS #2

RANDOMIZE TIMER
CLS
DO
redo:
  i% = INT(RND * 12) + 1
  t% = INT(RND * 4) + 1
  n$ = numbs$(i%) + shape$(t%)
  
  SELECT CASE t%
    CASE 2, 3
      IF INSTR(Stack1$, n$) = 0 then
        Stack1$ = Stack1$ + n$
        PRINT #1, n$
        Red = Red + 1
        COLOR 12
      END IF
    CASE 1, 4
      IF INSTR(Stack2$, n$) = 0 then
        Stack2$ = Stack2$ + n$
        PRINT #2, n$
        Black = Black + 1
        COLOR 13
      END IF
  END SELECT
  PRINT n$
  COLOR 7
  
LOOP WHILE Red < 10 AND Black < 10

CLOSE #1
CLOSE #2
This should work, but I have edited since I tested it. You can still make some improvements.
Code:
DEFINT A-Z
DECLARE FUNCTION used%(suit, rank)
TYPE CardType
  suit AS INTEGER
  rank AS INTEGER
END TYPE

DIM stack(1 TO 2, 1 TO 10) AS CardType
DIM SHARED usedsuits$, usedranks$

RANDOMIZE TIMER

DO
  suit = INT(RND * 4) + 1
  rank = INT(RND * 13) + 1
  IF suit < 3 AND NOT(used%(suit, rank)) THEN
    I = I + 1
    stack(1, I).suit = suit
    stack(1, I).rank = rank
  ELSEIF NOT(used%(suit, rank)) THEN
    K = K + 1
    stack(2, K).suit = suit
    stack(2, K).rank = rank
  END IF
LOOP UNTIL I = 10 OR K = 10

OPEN "file1.txt" FOR OUTPUT AS #1
FOR count = 1 TO I
  PRINT #1, stack(1, count).suit + " " + stack(1, count).rank
NEXT count
CLOSE #1

OPEN "file2.txt" FOR OUTPUT AS #1
FOR count = 1 TO I
  PRINT #1, stack(2, count).suit + " " + stack(2, count).rank
NEXT count
CLOSE #1


FUNCTION used%(suit, rank)
suittocheck$ = LTRIM$(STR$(suit))
ranktocheck$ = LTRIM$(STR$(rank))
IF LEN(suittocheck$) = 1 THEN suittocheck$ = "0" + suittocheck$
IF LEN(ranktocheck$) = 1 THEN ranktocheck$ = "0" + ranktocheck$

IF INSTR(usedsuits$, suittocheck$) <> 0 THEN
  IF INSTR(usedranks$, ranktocheck$) <> 0 THEN
    used% = 0
    EXIT FUNCTION
  END IF
END IF

usedsuits$ = usedsuits$ + suittocheck$
usedranks$ = usedranks$ + ranktocheck$

used% = -1
END FUNCTION

Alpha, untested, made up at school so I don't have access to a compiler, and it doesn't beautify the output (you can do that yourself), but you can see the sort of algorithm I'm using Smile
Thanks guys, this helps alot Big Grin