• Ever thought it'd be cool to have your art, writing, or challenge runs featured on PokéCommunity? Click here for info - we'd love to spotlight your work!
  • Our weekly protagonist poll is now up! Vote for your favorite Trading Card Game 2 protagonist in the poll by clicking here.
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

Card Game For Sphere

Hero of Time Link³

KFC's 33rd Employee
  • 336
    Posts
    18
    Years
    I found this tutorial off the internet for card games.

    So you want to create a card game...
    It's as easy as you want it to be.

    You have a deck of cards in the middle of the table.

    var card_deck = new Array();

    Wow, now you have a deck (albeit empty) on the game table.

    Now you need some cards.
    This can be as simple or as difficult as you want it to be, for example:-

    var card = 0; // the first card

    Or:

    function Card(suite, number) {
    this.suite = suite;
    this.number = number;
    }

    var card = new Card("Hearts", 0); // ace of hearts

    The first method, you have to remember what card it actually is.
    The second method, is probably easier, but we'll go for the first method for now.

    So before we start the game, we need to place all the cards down in the deck...

    function RandomSort(temp)
    {
    var sortedArray = new Array();

    for(var i = 0; i < temp.length; ++i)
    {
    var n = Math.floor(Math.random() * temp.length);
    sortedArray.push(temp[n]);
    temp.splice(n, 1);
    }

    return sortedArray;
    }

    var card_deck = new Array();

    function init_card_game() {
    card_deck = new Array();
    for (var i = 0; i < 52; ++i) {
    card_deck.push(i);
    }
    card_deck = RandomSort(card_deck);
    }

    So now, how many players do we have in the game, how many cards do they get each?

    var num_players = 2;
    var num_cards_each = 7;

    var players_hand = new Array();

    function DealDeck() {
    players_hand = new Array(num_players);

    for (var i = 0; i < players_hand.length; ++i)
    players_hand = new Array();

    for (var i = 0; i < num_players * num_cards_each; ++i) {
    players_hand[i % num_players] = card_deck;
    card_deck.splice(i, 1);
    }
    }

    So the deck has been delt.
    Start the game, or the while loop, whichever you prefer to think of it as.

    It'll look like this:

    var done = false;
    // var background = LoadImage("background.png");
    var player_index = 0;

    // player status values
    var OK = 0;
    var BUST = 1;

    function GetPlayersHandValue(player_index) {
    var total_cards = players_hand[player_index].length;
    var total_card_value = 0;

    for (var i = 0; i < total_cards; ++i)
    total_card_value += (players_hand[player_index] + 1) % 12;

    return total_card_value;
    }

    function GetPlayerStatus(player_index) {
    var total_cards = players_hand[player_index].length;
    if (total_cards > 5)
    return BUST;

    if (GetPlayersHandValue(player_index) <= 21)
    return OK;

    return BUST;
    }

    while (!done) {
    var turn_over = false;
    while (!turn_over) {
    // draw a background
    // background.blit(0,0);

    // draw the cards
    // we skip this step since we don't have images anyway

    // give some kind of option
    if (player_index == 0) // if it's your turn
    {
    // menu here, like "stick" or "twist"
    }
    else
    {
    // computer A.I. here
    }

    // check the status
    switch(GetPlayerStatus(player_index)) {
    case (BUST): turn_over = true; break;
    case (OK): break;
    }

    FlipScreen();
    }

    // next player
    player_index += 1;
    if (player_index > num_players)
    player_index = 0;

    if (player_index == 0) // everyone has had a turn
    {
    var possible_winners = new Array();
    for (var i = 0; i < num_players; ++i)
    if (GetPlayerStatus(i) == OK)
    possible_winners.push(i);

    var winners = new Array();
    var highest_value = 0;
    for (var i = 0; i < possible_winners.length; ++i)
    if (GetPlayersHandValue(possible_winners) > highest_value)
    highest_value = GetPlayersHandValue(possible_winners);

    for (var i = 0; i < possible_winners.length; ++i)
    if (GetPlayersHandValue(possible_winners) == highest_value)
    winners.push(possible_winners);

    // winners is now an array of the winners

    }
    }
     
    Back
    Top