Skip to main content
GameDev.net gamedev.net
🔒 Locked

New Game Programmer!

Started by Rayj00 Jul 14, 2015 at 10:41 PM 10 replies 3.8k views
Original Post
Rayj00
Rayj00

Actually, I am totally green! I know enough about programming to be dangeous.

I am an avid Hearts game player, I saw an online Hearts game the did not require any downoads or installs, It plays strictly through the browser.

It uses pixi.js for graphics. It's pretty awesome and fast.

Anyway, I'd like to program a Hearts game. I have a few ideas to improve what is generally available online.

It will be HTML5 compliant. Along with a lot of custom Javascript programming, I am thinking I will need either Websockets or sockit.io?

Right now I am looking at the easy stuff (graphics) and how to actually deal the cards out.

Is this the place to ask about client/server issues? I have no idea how to deal cards to 4 players so each player can only see his own cards?

That's just one issue I will be facing.

If you have ideas for me I would appreciate it.

Thanks,

Ray

frob
frob




Actually, I am totally green! I know enough about programming to be dangeous.

What do you want to do?

Is your goal to become a professional game developer? Or is you goal to be able to make your own simple card game for personal uses? Is your goal to build everything yourself?

For personal use, my recommendation is to pick an existing card game tool. (There are many out there, use your search engine.) It will take some effort, but you should be able to make most of those tools build the game you want.

If your goal is to build everything yourself, you've got a long journey ahead. If your goal is something professional, you've got an even longer road that usually includes a CS degree.

Assuming you know what you want to do, make a list of all the things you DO know how to do, and make a list of all the things you DO NOT know how to do. Then pick up a tool or library that can do it, or implement it yourself.




I have no idea how to deal cards to 4 players so each player can only see his own cards?

Sounds like those are good things to learn next.

There is the data side of figuring out what card should be in what pile. There is a network communications side of ensuring all clients have the data they need. There is the client/server or peer to peer side of coordinating control of the game, and processing the rules of what happens based on the game's flow. There are the graphics issues of deciding what to view and what not to view.

You don't really say what your skills are, other than knowing "enough to be dangerous". The skills assessment above about what you know how to do and what you don't would be useful here.

The usual advice is to start out with very simple games and slowly increase your skill. Start with games like guess the number and tic tac toe, grow to small text adventures, grow to pong, grow to falling blocks games, grow toward whatever you eventually want to make.

Just as you start a baby out with foods like mushed vegetables rather than steak, and you start a child reading books like "see spot run" rather than complex textbooks, when developing it is best to grow precept on precept until you have mastered all the pieces you need to build your game.




Is this the place to ask about client/server issues?

Networking specific issues go in the networking forum, but BE SURE TO READ THEIR FAQ FIRST.

Alpha_ProgDes
Alpha_ProgDes

The first question is this: Have you ever program a simple, plain Hearts game?

If the answer to this question is no, then I would suggest doing that before committing to any "improvements" on the game or the version of the game itself.

If this is the first game you've ever programmed and your goal is to write code and create games with that code (ie. game developer/programmer), then you should probably write the game from scratch.

Beginner in Game Development?  Read here. And read here.  
Rayj00
Rayj00

Ok I am pulling my hair out on this one. I am trying to intialize an array size 52 with random numbers that represent a deck of cards.

A loop should count from 0-51 but it stops at various random values. I am using document.write to trouble shoot. As of now, calling the page

will generate a numbered list of random numbers. It should show 52 numbers but never does.

Here is the code. Help a guy out please? Thanks.










LennyLen
LennyLen

The approach you're taking, while the most immediate obvious way to shuffle a deck, is very inefficient, and the code could theoretically run forever without finishing.

A better way is to start with a sorted deck, then swap the first card for a random card from the rest of the deck. Then you move to the second position, and swap it for a random card further in the deck. Then move to the third position and again swap this for a random card further in the deck. Just repeat until you get to the last position.

Rayj00
Rayj00

The approach you're taking, while the most immediate obvious way to shuffle a deck, is very inefficient, and the code could theoretically run forever without finishing.

A better way is to start with a sorted deck, then swap the first card for a random card from the rest of the deck. Then you move to the second position, and swap it for a random card further in the deck. Then move to the third position and again swap this for a random card further in the deck. Just repeat until you get to the last position.

I have a for loop. It should finish when it gets to 52. That's the problem, it NEVER gets to 52.. The fix should be simple. I can't figure it out. Can you?

Lactose
Lactose




I have a for loop.

Yes, but in that for loop, you have a piece of code that will essentially restart the current iteration of the for loop if the randomly chosen card has already been randomly chosen.

As you keep choosing unused cards, the chance of choosing an already chosen card will increase, which means constantly trying to select a new card, taking more and more time.

Other than the issue mentioned above, there's also an issue with how you randomly select a card. You currently choose a card between 1 and 52.

However, your arrays are only initialized to [51], so if card 52 is chosen (which will try to index into your arrays at [52]), you'll be comparing against uninitialized values, which might be what's causing your code to never finish.

Arrays are zero-indexed -- meaning that the first item in an array is at [0], not [1].

The fix for this issue is to simply remove the + 1 part of the random number generation.

Math.Rand() returns a number from 0 (0 is included) up to 1 (1 is not included).

Math.Floor(Math.Rand() * 52) -- this includeds the fix I mentioned -- yields a minimum of 0 and a maximum of 51. This is exactly what we want for indexing into our arrays.

Hello to all my stalkers.
LennyLen
LennyLen

in pseudocode, the algorithm for your approach is:





FOR n = 0 TO 51

    duplicate = TRUE
    WHILE !duplicate
        r = RANDOM(52)
        IF dup_array[r] != 1
            dup_array[r] = 1
            card[n] = r
            duplicate = FALSE
        END IF
    END WHILE
END FOR

edit:

I just realized you had a colon after getRndNmbr and not a semicolon as I first read, so never mind.

NathanRidley
NathanRidley
Here you go:

function range(start, end) {
    var arr = [];
    for(var i = start; i < end; i++) arr[arr.length] = i;
    return arr;
}
var sorted = range(0, 52).sort(function() { return Math.random(); });
Khatharr
Khatharr

If this is a deck for drawing from then instead of shuffling the deck you can just make a container of card IDs in any order, then select a random member from that container whenever you want to draw a card.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Brain
Brain

If you're shuffling a deck, you really shouldn't just use random swaps.

You should use something like this, as just using random swaps gives preference to certain card arrangements and is not statistically unbiased.

What you need is something called a "uniform random distribution".

You might get away with random swaps just to get you started but this is definitely something to bear in mind if you want your game to appeal to any real card game players...

frob
frob

Also, read this, comes with a 3-line implementation you can copy/paste and a list of bias sources if you aren't careful.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.