• Print

Author Topic: Code Help  (Read 3122 times)

Brent

  • Guest
Code Help
« on: September 08, 2011, 02:22:19 AM »
Rather than starting a new topic every single time that I need some help with code (trust me folks, that would be a LOT of new topics!), I am going to consolidate everything right here.

In addition to the gazillion other projects I am working on, I'm trying to get a card game going in QB64, except... I can't seem to get the deck to assemble!

Here's the code:

Code: [Select]
DIM SHARED Card(13) AS STRING
DIM SHARED Deck(4, 13, 13) AS STRING
DIM SHARED Rank(13) AS STRING
DIM SHARED Suit(4) AS STRING
DIM SHARED x
DIM SHARED y
DIM SHARED z

FOR x = 1 TO 4
    FOR y = 1 TO 13
        FOR z = 1 TO 13
            RESTORE Suits
            READ Suit(x)
            RESTORE Ranks
            READ Rank(y)
            RESTORE Cards
            READ Card(z)
        NEXT z
    NEXT y
NEXT x
FOR x = 1 TO 4
    FOR y = 1 TO 13
        FOR z = 1 TO 13
            Deck(x, y, z) = Suit(x) + Rank(y) + Card(z)
        NEXT z
    NEXT y
NEXT x

CLS
FOR x = 1 TO 4
    FOR y = 1 TO 13
        FOR z = 1 TO 13
            PRINT Deck(x, y, z);
        NEXT z
    NEXT y
NEXT x

END

Suits:
DATA "","","",""
Ranks:
DATA "2","3","4","5","6","7","8","9","10","J","Q","K","A"
Cards:
DATA "1","2","3","4","5","6","7","8","9","10","11","12","13"

EDIT:

Figured it out!  Apparently, I thought it was a good idea to put the RESTORE statements in the FOR/NEXT loops.  :-[
« Last Edit: September 08, 2011, 03:15:58 AM by Brent »

Cyperium

  • Hero Member
  • *****
  • Posts: 3285
  • Knowledge is good, but understanding is better
    • Cyperium
    • Email
Re: Code Help
« Reply #1 on: September 08, 2011, 05:01:23 AM »
Yes, RESTORE makes it read only the first element if you use it like that :)


I think I would've done it like this:

Code: [Select]
FOR x = 1 TO 4
    READ Suit(x)
NEXT x

FOR y = 1 TO 13
    READ Rank(y)
NEXT y

FOR z = 1 TO 13
    READ Card(z)
NEXT z
RESTORE
' you can use a single RESTORE in order to restore it to the very top if you want to read them again.    

When I don't know the number of elements in the DATA I use some identifier (like -1) as the last DATA value and test for that to see that it has come to the end of the DATA but this is obviously not needed here as we know the amount of DATA in each set.
Venture - New Prototype, QB64 Editor v1.95b (linux compatible, if you compile the source).

Brent

  • Guest
Re: Code Help
« Reply #2 on: September 08, 2011, 05:16:28 AM »
I was using the RESTORE command to set the starting point for the READ command... right now the issue is actually assigning a value to each card, such as "2"=1, "3"=2, "A"=13.

Cyperium

  • Hero Member
  • *****
  • Posts: 3285
  • Knowledge is good, but understanding is better
    • Cyperium
    • Email
Re: Code Help
« Reply #3 on: September 08, 2011, 05:30:55 AM »
I know, but when you have loops within the loop then that loop is called as many times as the higher loop (that only makes sense for the Deck variable that holds that much information).

It was also unnecessary in your program to RESTORE the DATA as it would correctly READ them anyhow.


The two arrays Rank and Card are parallell to eachother, so to know what value a card has you only need the array index;

The thirteenth card "A" has rank 13 because;

Code: [Select]
index = 13
PRINT "The Card:"; Card(index)
PRINT "The Rank:"; Rank(index)

You only need to shuffle the index and it will always reflect both the card and the rank at the same time. I'm sorry if I misunderstood you though.
« Last Edit: September 08, 2011, 05:40:22 AM by Cyperium »
Venture - New Prototype, QB64 Editor v1.95b (linux compatible, if you compile the source).

codeguy

  • Hero Member
  • *****
  • Posts: 3552
  • what the h3ll did i name that code?
    • stuff at dkm
    • Email
Re: Code Help
« Reply #4 on: September 08, 2011, 09:37:12 AM »
and here i present my card shuffling algo:
const NumCardsInDeck=52
for cardshuffled=1 to NumCardsInDeck
swap deck(cardshuffled),deck(cardshuffled+int(rnd*(NumCardsInDeck-cardshuffled)))
next
http://denteddisk.forums-free.com/make-an-appointment-with-the-resident-code-guru-f34.html

Cyperium

  • Hero Member
  • *****
  • Posts: 3285
  • Knowledge is good, but understanding is better
    • Cyperium
    • Email
Re: Code Help
« Reply #5 on: September 08, 2011, 11:49:34 AM »
You must have indexes of all the dimensions of the deck.

DIM SHARED Deck(4, 13, 13) AS STRING


swap deck(cardshuffled)   ,    deck(cardshuffled+int(rnd*(NumCardsInDeck-cardshuffled)))


If I would have done it I would have kept a single reference for the cards, and info on each reference:

Code: [Select]
DIM cards(1 TO 52)
COLOR 0, 7: CLS
FOR a = 1 TO 52
cards(a) = a
NEXT
 
FOR index = 1 TO 20
suit = ((cards(index) - 1) MOD 4) + 1
 
colo = ((cards(index) - 1) MOD 2) + 1
 
card = ((cards(index) - 1) MOD 13) + 1
 
IF suit = 1 THEN suit$ = ""
IF suit = 2 THEN suit$ = ""
IF suit = 3 THEN suit$ = ""
IF suit = 4 THEN suit$ = ""
 
IF colo = 1 THEN COLOR 0, 7
IF colo = 2 THEN COLOR 4, 7
PRINT suit$, card
NEXT
 
 


I know that it might seem more complicated, but I think it's easier than the method of storing everything in seperate arrays when you know each card is unique and can therefor be represented with a unique value. Also by this method you can use your card-shuffling technique without problems.
« Last Edit: September 08, 2011, 11:54:41 AM by Cyperium »
Venture - New Prototype, QB64 Editor v1.95b (linux compatible, if you compile the source).

Brent

  • Guest
Re: Code Help
« Reply #6 on: September 08, 2011, 04:57:27 PM »
I'm trying to make a scalable grid with differing sizes based on variables.  An example of what I'm talking about would be an 8x8 grid, 8x16 grid, 16x16 grid, etc. - but of varying sizes in pixels.  I'm wanting to be able to plug in the number of (a) rows (b) columns (c) pixels wide (d) pixels high.

This is my first foray into QB graphics using the LINE statement, so while this may (possibly) sound pretty easy or elementary to some, this is pretty darned complex to me.  Here's what I've been able to come up with so far, though not scalable:

Code: [Select]
SCREEN _NEWIMAGE(800, 600, 256)
LINE (0, 0)-(450, 450), 15, B
LINE (0, 112)-(450, 112), 15
LINE (0, 224)-(450, 224), 15
LINE (0, 336)-(450, 336), 15
LINE (112, 0)-(112, 450), 15
LINE (224, 0)-(224, 450), 15
LINE (336, 0)-(336, 450), 15

Thanks in advance for the influx of input I see coming...  ;D

EDIT:

I am getting closer, but I can't seem to get the rows and columns to multiply correctly... here's what I've got so far:

Code: [Select]
DIM SHARED row AS INTEGER
DIM SHARED col AS INTEGER
DIM SHARED pxh AS INTEGER
DIM SHARED pxw AS INTEGER

row = 8
col = 8
pxh = 200
pxw = 200

SCREEN _NEWIMAGE(800, 600, 256)
CLS
LINE (0, 0)-(pxh, pxw), 15, B
FOR i = 1 TO row - 1
    LINE (0, pxw / row)-(pxh, pxw / row), 15
NEXT i
pxh = 200
pxw = 200
FOR i = 1 TO col - 1
    LINE (pxh / col, 0)-(pxh / col, pxw)
NEXT i
« Last Edit: September 08, 2011, 06:03:35 PM by Brent »

Pete

  • Moderator
  • Hero Member
  • *****
  • Posts: 6240
  • Cuz I sez so varmint!
Re: Code Help
« Reply #7 on: September 08, 2011, 08:28:27 PM »
A couple of examples:

http://www.qb64.net/forum/index.php?topic=3294.msg38499#msg38499

Pete
It's only rocket science; it's not Linux!

Brent

  • Guest
Re: Code Help
« Reply #8 on: September 08, 2011, 09:47:15 PM »
First of all, if anyone can help me with the above Grid coding I'd be DEEPLY appreciative!

Secondly, amongst my trillion other projects, I want to try and create a chess program... does anyone see anything wrong with how I've chosen to dimension the board?  Are there any other better solutions?

Code: [Select]
DIM SHARED Board(8, 8) AS INTEGER
DIM SHARED CoordX(8) AS INTEGER
DIM SHARED CoordY(8) AS INTEGER
DIM x
DIM y

FOR x = 1 TO 8
    FOR y = 1 TO 8
        Board(x, y) = CoordX(x) + CoordY(y)
    NEXT y
NEXT x

Here's another portion of the code that I think would work in tandem with each other pretty well:

Code: [Select]
CONST Empty = 0
CONST Pawn = 1
CONST Rook = 2
CONST Knight = 3
CONST Bishop = 4
CONST Queen = 5
CONST King = 6

DATA 2,3,4,5,6,4,3,2
DATA 1,1,1,1,1,1,1,1
DATA 0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0
DATA 1,1,1,1,1,1,1,1
DATA 2,3,4,5,6,4,3,2

... or am I thinking too "primitively" in my approaches?  I'm trying to come up with something easy to code, easy to understand, and easy to implement.

OlDosLover

  • Hero Member
  • *****
  • Posts: 3859
  • OlDosLover
    • Email
Re: Code Help
« Reply #9 on: September 10, 2011, 04:33:09 AM »
Hi all,
    I've always wanted to make a Chess program also. There is one in the samples section included with QB64. I once made an addition to this program that recorded the game moves and saved to a file. I also made a replay game program with graphics to view the saved chess game. I'll look for them and post back.
    Your code seems fine to me for a beginning point and offer this mod as proof.
Code: [Select]
REM

CONST Empty = 0
CONST Pawn = 1
CONST Rook = 2
CONST Knight = 3
CONST Bishop = 4
CONST Queen = 5
CONST King = 6


DIM SHARED Board(8, 8) AS INTEGER
DIM SHARED CoordX(8) AS INTEGER
DIM SHARED CoordY(8) AS INTEGER
DIM x
DIM y

SCREEN _NEWIMAGE(800, 600, 32)
FOR x = 1 TO 8
  FOR y = 1 TO 8
    READ z%
    Board(x, y) = z%
  NEXT y
NEXT x

FOR x = 1 TO 8
  FOR y = 1 TO 8
    PRINT Board(x, y);
  NEXT y
  PRINT
NEXT x

SLEEP
SYSTEM



DATA 2,3,4,5,6,4,3,2
DATA 1,1,1,1,1,1,1,1
DATA 0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,0,0
DATA 1,1,1,1,1,1,1,1
DATA 2,3,4,5,6,4,3,2
    Of course all it does at the moment is to load the Board dim with the data values using READ and then displaying the contents. My suggestion is to get a simple form of graphics on screen by scanning the Board dim on call as a sub. Then consider how to "move" the content of an dim element into another element to simulate a "game move". By breaking down this program into separate sections that do specific things would make it easier in the long run. For example youd may want
.Move list
.Evaluation function
.Opening Book
.Save game option
.Time limits , etc
OlDosLover.

Brent

  • Guest
Re: Code Help
« Reply #10 on: September 11, 2011, 12:00:25 AM »
Sadly, I was completely unaware of the sample code in QB64... many thanks to you!  ;D

Brent

  • Guest
Re: Code Help
« Reply #11 on: September 15, 2011, 12:59:45 AM »
Can anyone tell me how I can optimize this code?  I've tried using FOR/NEXT loops but I just can't seem to get it down right...

Code: [Select]
SCREEN _NEWIMAGE(800, 600, 256)

LINE (0, 0)-(16, 16), 15, B
LINE (0, 0)-(32, 16), 15, B
LINE (0, 0)-(48, 16), 15, B
LINE (0, 0)-(64, 16), 15, B
LINE (0, 0)-(80, 16), 15, B
LINE (0, 0)-(96, 16), 15, B
LINE (0, 0)-(112, 16), 15, B
LINE (0, 0)-(128, 16), 15, B
LINE (0, 0)-(144, 16), 15, B
LINE (0, 0)-(160, 16), 15, B
LINE (0, 0)-(176, 16), 15, B
LINE (0, 0)-(192, 16), 15, B
LINE (0, 0)-(208, 16), 15, B
LINE (0, 0)-(224, 16), 15, B
LINE (0, 0)-(240, 16), 15, B
LINE (0, 0)-(256, 16), 15, B

LINE (0, 0)-(x, 16), 15, B -- x always increased by 16...

Brent

  • Guest
Re: Code Help
« Reply #12 on: September 15, 2011, 01:05:06 AM »
I figured it out... I was forgetting the x = x + 16 before the NEXT...

Code: [Select]
SCREEN _NEWIMAGE(800, 600, 256)

x = 16
FOR i = 1 TO 16
    LINE (0, 0)-(x, 16), 15, B
    x = x + 16
NEXT i

Brent

  • Guest
Re: Code Help
« Reply #13 on: September 15, 2011, 01:15:32 AM »
New issue... I got the FOR/NEXT loop down for a single line going horizontally across the screen, now what can I do to have the vertical go down every 32 pixels, say, 16 more times (16x16 grid)?

Code: [Select]
SCREEN _NEWIMAGE(800, 600, 256)

x = 16
FOR i = 1 TO 16
    LINE (0, 0)-(x, 16), 15, B
    x = x + 16
NEXT i

x = 16
FOR i = 1 TO 16
    LINE (0, 32)-(x, 16), 15, B
    x = x + 16
NEXT i
« Last Edit: September 15, 2011, 04:11:38 AM by Brent »

unseenmachine

  • Hero Member
  • *****
  • Posts: 3285
  • A fish, a fish, a fishy o!
Re: Code Help
« Reply #14 on: September 15, 2011, 06:40:40 AM »
Here's a simple method of drawing grids, try changing the StepVal% to change the number of cells you create.

8*8
Code: [Select]
SCREEN _NEWIMAGE(800, 600, 32)

StepVal% = 600 / 8
FOR X% = 0 TO 600 STEP StepVal%
   FOR y% = 0 TO 600 STEP StepVal%
      LINE (X%, y%)-(X% + StepVal%, y% + StepVal%), _RGB(200, 0, 0), B
   NEXT
NEXT

16*16
Code: [Select]
SCREEN _NEWIMAGE(800, 600, 32)

StepVal% = 600 / 16
FOR X% = 0 TO 600 STEP StepVal%
   FOR y% = 0 TO 600 STEP StepVal%
      LINE (X%, y%)-(X% + StepVal%, y% + StepVal%), _RGB(200, 0, 0), B
   NEXT
NEXT
UnseenGDK Download : http://dl.dropbox.com/u/8822351/UnseenGDK.bm
GDK Tutorial : http://dl.dropbox.com/u/8822351/UnseenGDK_Tutorial.doc
VQB02 : http://dl.dropbox.com/u/8822351/VQB02.zip

  • Print