Problem of array with loop(would sb know java->c2)

0 favourites
  • 4 posts
From the Asset Store
For problem solving, puzzles, bubbles, platforms and other adventures.
  • public class test {
    	public static void main(String args[]) {
    
            //Number of the card which the user own
    		int card[];
    		card = new int[11];
    		card[1] = 0;
    		card[2] = 1;
    		card[3] = 0;
    		card[4] = 1;
    		card[5] = 0;
    		card[6] = 1;
    		card[7] = 0;
    		card[8] = 1;
    		card[9] = 0;
    		card[10] = 1;
    
            //array that save the card no.  which the user didn't own 
    		int count = 1;
    		int newCard[];
    		newCard = new int[11];
    
    		for (int i = 1; i < card.length; i++) {
    			if (card[i] == 0) {
    				newCard[count] = i;
    				count++;
    			}
    		}
    
            // drawing a random card from the arry which the user didn't own
    		int randomCard = 0;
    
    		do {
    			randomCard = (int)(Math.random() * (newCard.length));
    		} while ( newCard [ randomCard ] == 0);
    
    		// 1,3,5,7,9 is expected to be the result
            System.out.println("RandomCard from Above:" + newCard[randomCard]);
    
    	}
    }
    [/code:3lskg9ji]
    I don't know how to transfer the code above to Construct 2 format, would somebody could help me
  • Yes, you can do this with arrays if you really need to.

    But the cool thing about C2 is that it's much easier to do without any arrays.

    https://www.dropbox.com/s/3xntvn27607zg ... .capx?dl=0

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Example capx, made in last beta r249

    The interest of arrays in the OP's question is to allow to pick out of the cards the user do not own, which your method does not display dop2000.

    gorillakelvin, like in JS, arrays in Construct are zero-based, and the index starts from zero.

    On start of layout I manually set some informations, like you did, and then in the following sub-event, I loop through aUserOwn and dynamically modify aUserNoOwn size so it contains only the "Not own cards".

    Then, when you click the button, you randomly pick a number from the array aUserNoOwn and display it in the textbox.

    Since we are zero based, expected answers are actually 0,2,4,6,8.

  • Kyatric, As I understand from the Java code, Card[] array contains 10 cards. (1-based for simplicity)

    Cards which user owns contain value "1".

    Cards which user doesn't own contain value "0".

    The task is to select a random card which used doesn't own. Basically find a random member of the array with value ="0".

    This can totally be done in C2 without the array and much easier. Pick all cards with instance variable="0", and then pick a random card from them.

    Here is a slightly more accurate example:

    https://www.dropbox.com/s/ul8ly6dpzsho7 ... .capx?dl=0

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)