A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: AS3 Flash Top Trumps Game

  1. #1
    Member
    Join Date
    Apr 2011
    Posts
    42

    AS3 Flash Top Trumps Game

    Hey Guys,

    I am trying to create a top trumps game in Flash AS3 and so far haven't got much working.

    The game is about supercars and there is 58 of these cards.

    Each card has 5 values: speed, 0-60, power, weight and cost (Some of these values were not easy to find out.

    Ideally I want the player to click the play button and be taken to a page that's already been built. Next I want AS3 to select 30 of these 58 cards and split them evenly between the AI and player. Possibly using a variable?

    I'm thinking that I'll need a continue button for after the player has selected a value and have already wrote the code to compare the values. As you can see there's a lot more code to this game than you'd think and would be helpful if I could speak to someone with a more experience with AS3 than me.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Never heard of top trumps.

    No one wants to start from scratch building your game for you. I suggest getting as far as you can, then asking for help on particular issues when you get stuck.

    Search for existing card game tutorials. They will have concepts and code you can use such as modeling your cards and your deck, and how to shuffle and create a hand.

    You casually mentioned an AI element. AI is not easy. Do you have any idea how that's going to work?

  3. #3
    Member
    Join Date
    Apr 2011
    Posts
    42
    Thanks for the reply

    Firstly you've never heard of top trumps? Really? Google the phrase quickly lol

    It's not building it from scratch I've already found a way to draw 30 random cards out of the 58 and don't want anyone to write the code for me I just am a bit stuck now on splitting the cards between the player and AI.

    There's already well over 2000 lines of code in this game including the code used to create a page for the player to view the cards.

    So far I need to find a way to split the 30 randomly selected cards between the player and AI and store them away so they can be called after each hand. I haven the code to compare the cards and see who's won and I know I can use code I wrote to check which of the cards are visible to see who won.

  4. #4
    Member
    Join Date
    Apr 2011
    Posts
    42
    Posted a reply to this thread yesterday afternoon and it still hasn't been allowed.

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I hadn't realised the post was under moderation. I've approved it

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    What sort of data structure are you using to represent the players hands? At the simplest, you could have an array for each player's cards, and just divvy up the 30 cards into the two arrays. If the cards were randomly selected in the first place, you could just alternate which hand the card goes to. Or you could have a way of randomly selecting (and removing) cards from the pool of 30 and putting them in the hand.

    Where are you getting stuck? Post code.

  7. #7
    Member
    Join Date
    Apr 2011
    Posts
    42
    Thanks for the reply.

    I'm currently stuck on trying to work out how to get AS3 pick 15 cards for the player and 15 for the AI. I also hasn't worked out how the cards will be dished out and how exactly AS3 will be able to work out which card has won. For working out which card has won I have given each card 5 values e.g.

    Ascari.Speed = 220;
    ...

    There is a button at the top of the in game scene called SpeedButton and then same sort of thing is done for the other 4 values Power,-60,Weight and Cost.

    I just can't work out how to relate when a button is pressed to only compare the card in the players hand to the AI's.

    Sorry about the length comment and I appreciate your help.

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Here's a very generic example of dividing an array into two arrays. This first method just takes the first half for one and the second for the other.
    Code:
    var chosenCards:Array = [/** pretend I put 30 cards here **/];
    var playerCards:Array = chosenCards.slice(0, chosenCards.length/2);
    var aiCards:Array = chosenCards.slice(chosenCards.length/2);
    The following method interleaves the cards between the arrays, one-for-me-one-for-you style.
    Code:
    var chosenCards:Array = [/** pretend I put 30 cards here **/];
    var playerCards:Array =[];
    var aiCards:Array = [];
    for (var i:int = 0; i < chosenCards.length; i++){
      var targetHand:Array;
      if (i % 2 == 0){
        targetHand = playerCards;
      }else{
        targetHand = aiCards;
      }
      targetHand.push(chosenCards[i]);
    }
    And lastly, here's a function to randomly select a number of elements from an array without replacement. This MODIFIES the array sent in.
    Code:
    function chooseElementsFrom(num:int, inArray:Array):Array{
      var toreturn:Array = [];
      if (num > inArray.length){
        throw new Error("Array not big enough");
      }
      for (var i: int = 0; i < num; i++){
        toreturn.push(inArray.splice(Math.floor(Math.random()*inArray.length), 1)[0]);
      }
      return toreturn;
    }

  9. #9
    Member
    Join Date
    Apr 2011
    Posts
    42
    Thanks for that and the quick reply.

    The second code was similar to the one I had started to write except I used

    for (var p: int=p; p<30; p++)

    to select 30 of the cards.

    Thanks for your help I'm now going to attempt to work out how get AS3 to display half the cards at different positions.

    Quick question ... If I trace PlayerCards or AICards in the second code if codes back with ,,,, only ... do you know why?
    Last edited by AntBirch; 04-20-2011 at 01:29 PM.

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    That looks like you've got an array full of nothings. Or an array full of things that output empty strings from their toString methods, anyway.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center