Results 1 to 13 of 13
  1. #1
    ヽಠ_ಠᕤ Headrockin' ᕦಠ_ಠ╯
    MPGH Member
    IV2B's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Posts
    1,163
    Reputation
    101
    Thanks
    229
    My Mood
    Inspired

    How to check if *many* variables are the same.

    Hi there,i've been doing a little minigame which is about you moving a "0" around,having to hit randomly placed "X" scattered around the console.
    However,if two or more of these variables have the same X and Y coords,then when "hitting" them you will just carry one of the two along with you,never winning.

    So this is what i have to do:
    I have to check if any of these two groups of variables a,b,c,d,e,f,g,h,i,j and z,x,c,v,b,n,m,j,k,l have at least 2 equal values OR if they are all different.
    while atm i'd have to check if a is equal to any,if b is equal to any,if c is equal to any etc etc,i'd really like to know if there is another way to do this...as it takes a huge amount of space and time to write while it also takes a (relatively) long time to execute.

    P.s. the values are from 2-80 and 2-25.
    P.p.s. yep i already searched on the net but i could only find information about wether a string is composed of numbers or not -.-

  2. #2
    Fovea's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Posts
    325
    Reputation
    101
    Thanks
    411
    My Mood
    Amused
    Instead of checking after you create the set of Xs, simply use std::set.

  3. #3
    Threadstarter
    ヽಠ_ಠᕤ Headrockin' ᕦಠ_ಠ╯
    MPGH Member
    IV2B's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Posts
    1,163
    Reputation
    101
    Thanks
    229
    My Mood
    Inspired
    Quote Originally Posted by Fovea View Post
    Instead of checking after you create the set of Xs, simply use std::set.
    I would be grateful if you could write an example that detects if 10 variables are different all from each other :3

  4. #4
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    Quote Originally Posted by IV2B View Post
    .
    Use an array (or your preferred 'collection' type) to store the "X" locations, and use a loop to iterate over them, checking for a collision. Do not use a new variable for each "X" you want on the screen. No code example, not here to teach you, here to answer questions.

     

    Quote Originally Posted by IV2B View Post
    I would be grateful if you could write an example that detects if 10 variables are different all from each other :3
    Use a set of nested loops. Essentially it will "loop" over each object and then compare that to each object by looping over the list again.

    For each obj In list1
    For each thing In list1

    Next
    Next

    Take care for the case when comparing an object to itsself : p It doesn't have to be For Each loops..use whichever type you deem appropriate.
    Last edited by abuckau907; 11-06-2013 at 08:51 PM. Reason: supercalafragalisticexpialadoshus
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

  5. #5
    dnbotmaker's Avatar
    Join Date
    Nov 2013
    Gender
    male
    Posts
    44
    Reputation
    10
    Thanks
    487
    My Mood
    Busy
    you could use vectors (because im guessing the amount of values shrinks and grows?) and iterator...i can help you more if you don't know how to do this.

  6. #6
    kjbmarr's Avatar
    Join Date
    Sep 2011
    Gender
    male
    Posts
    91
    Reputation
    10
    Thanks
    206
    My Mood
    Tired
    use binary search instead of linear search. much faster
    Let me google that for you

  7. #7
    Threadstarter
    ヽಠ_ಠᕤ Headrockin' ᕦಠ_ಠ╯
    MPGH Member
    IV2B's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Posts
    1,163
    Reputation
    101
    Thanks
    229
    My Mood
    Inspired
    Quote Originally Posted by dnbotmaker View Post
    you could use vectors (because im guessing the amount of values shrinks and grows?) and iterator...i can help you more if you don't know how to do this.
    Yeah,atm i am supposed to know assignments,if,do/while...but i like going by my own.
    In this case,i have 10 values which are "on the x axis" and 10 which are "on the y axis" and are used as coords to output a character,if you move your pointer over it,you gain score. (a very little game yup)
    The issue is when two randomly generated coords are equal (same X1 and X2 while also same Y1 and Y2) as one X will be placed as it should be and se second,instead of overlapping it,is placed on the right...but when trying to hit them,the second will follow you and you can't win anymore.
    :7

    *edit*
    I semi-fixed the code by adding a re-roll feature that resets the objectives position (still not a real fix).
    Last edited by IV2B; 11-07-2013 at 08:32 AM.

  8. #8
    Sixx93's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Posts
    673
    Reputation
    21
    Thanks
    250
    My Mood
    Cool
    Se ti serve aiuto chiedi pure

  9. #9
    Threadstarter
    ヽಠ_ಠᕤ Headrockin' ᕦಠ_ಠ╯
    MPGH Member
    IV2B's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Posts
    1,163
    Reputation
    101
    Thanks
    229
    My Mood
    Inspired
    Quote Originally Posted by Sixx93 View Post
    Se ti serve aiuto chiedi pure
    Eh l'ho già chiesto x)

    Devo controllare che 10 coordinate x,y siano differenti tra di loro,ma per ora l'unico modo che conosco è usare una miriade di if...ci deve essere un modo più comodo <.<

  10. #10
    Sixx93's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Posts
    673
    Reputation
    21
    Thanks
    250
    My Mood
    Cool
    Quote Originally Posted by IV2B View Post
    Eh l'ho già chiesto x)

    Devo controllare che 10 coordinate x,y siano differenti tra di loro,ma per ora l'unico modo che conosco è usare una miriade di if...ci deve essere un modo più comodo <.<
    Non conoscendo il codice, mi viene in mente solo utilizzare un'array di variabili COORD (sono strutture che hanno due attributi di tipo long: x e y). Così potresti fare un piccolo for e indicizzare l'array è più elegante da vedere ( e probabilmente anche più logico e facile ) però sempre lo stesso numero di if verranno fatti, solo che non li scrivi a mano xD

    Se ho capito bene, qualcosa di simile:
    Code:
    COORD v[10];
    bool Check()
    {
    	for( int i = 0; i < (sizeof(v) / sizeof(COORD)) - 1; i++)
    		if(v[i].X == v[i+1].X && v[i].Y == v[i+1].Y)
    			return true;
    	return false;
    }
    Poi vedi tu se lo vuoi globale o no
    Last edited by Sixx93; 11-17-2013 at 03:35 PM.

  11. #11
    Fovea's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Posts
    325
    Reputation
    101
    Thanks
    411
    My Mood
    Amused
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <set>
    #include <utility>
    
    int main() {
      std::set<std::pair<unsigned int, unsigned int>> x_locations;
    
      std::srand(std::time(0));
    
      while (x_locations.size() < 10) {
        unsigned int x = std::rand() % 79 + 2;
        unsigned int y = std::rand() % 24 + 2;
    
        x_locations.emplace(x, y);
      }
      
      for (auto x_loc : x_locations)
        std::cout << x_loc.first << ' ' << x_loc.second << std::endl;
    }
    No need to check afterwards when you can just generate unique instances (the container does the checking for you).

    Edit: Misunderstood what you wanted but if you actually understand how to program you can reimplement this to work on two sets of coordinates to create unique coordinates.
    Last edited by Fovea; 11-17-2013 at 05:23 PM.

  12. #12
    Threadstarter
    ヽಠ_ಠᕤ Headrockin' ᕦಠ_ಠ╯
    MPGH Member
    IV2B's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Posts
    1,163
    Reputation
    101
    Thanks
    229
    My Mood
    Inspired
    Quote Originally Posted by Sixx93 View Post
    Non conoscendo il codice, mi viene in mente solo utilizzare un'array di variabili COORD (sono strutture che hanno due attributi di tipo long: x e y). Così potresti fare un piccolo for e indicizzare l'array è più elegante da vedere ( e probabilmente anche più logico e facile ) però sempre lo stesso numero di if verranno fatti, solo che non li scrivi a mano xD

    Se ho capito bene, qualcosa di simile:
    Code:
    COORD v[10];
    bool Check()
    {
    	for( int i = 0; i < (sizeof(v) / sizeof(COORD)) - 1; i++)
    		if(v[i].X == v[i+1].X && v[i].Y == v[i+1].Y)
    			return true;
    	return false;
    }
    Poi vedi tu se lo vuoi globale o no
    Ora ho 20 variabili chiamate onex,oney,twox,twoy etc etc,questo è il codice:
    Code:
    	srand ( time(NULL) );
    		onex= rand() % 79 + 2;
    		twox= rand() % 79 + 2;
    		threex= rand() % 79 + 2;
    		fourx= rand() % 79 + 2;
    		fivex= rand() % 79 + 2;
    		sixx= rand() % 79 + 2;
    		sevenx= rand() % 79 + 2;
    		eightx= rand() % 79 + 2;
    		ninex= rand() % 79 + 2;
    		tenx= rand() % 79 + 2;
    		oney=rand() % 23 + 2;
    		twoy=rand() % 23 + 2;
    		threey=rand() % 23 + 2;
    		foury=rand() % 23 + 2;
    		fivey=rand() % 23 + 2;
    		sixy=rand() % 23 + 2;
    		seveny=rand() % 23 + 2;
    		eighty=rand() % 23 + 2;
    		niney=rand() % 23 + 2;
    		teny=rand() % 23 + 2;
    Il problema è che in rari casi due coordinate equivalgono ad altre :/
    Per utilizzare il tuo metodo dovrei stravolgere il programma x)
    E ora ho "sistemato" con dei "reroll": se viene premuto tab il programma "re-rolla" le variabili.

    BohBohBohBohBohBohBohBohBohBohBohBohBohBohBoh D:

  13. #13
    Sixx93's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Posts
    673
    Reputation
    21
    Thanks
    250
    My Mood
    Cool
    Quote Originally Posted by IV2B View Post
    Ora ho 20 variabili chiamate onex,oney,twox,twoy etc etc,questo è il codice:
    Code:
    	srand ( time(NULL) );
    		onex= rand() % 79 + 2;
    		twox= rand() % 79 + 2;
    		threex= rand() % 79 + 2;
    		fourx= rand() % 79 + 2;
    		fivex= rand() % 79 + 2;
    		sixx= rand() % 79 + 2;
    		sevenx= rand() % 79 + 2;
    		eightx= rand() % 79 + 2;
    		ninex= rand() % 79 + 2;
    		tenx= rand() % 79 + 2;
    		oney=rand() % 23 + 2;
    		twoy=rand() % 23 + 2;
    		threey=rand() % 23 + 2;
    		foury=rand() % 23 + 2;
    		fivey=rand() % 23 + 2;
    		sixy=rand() % 23 + 2;
    		seveny=rand() % 23 + 2;
    		eighty=rand() % 23 + 2;
    		niney=rand() % 23 + 2;
    		teny=rand() % 23 + 2;
    Il problema è che in rari casi due coordinate equivalgono ad altre :/
    Per utilizzare il tuo metodo dovrei stravolgere il programma x)
    E ora ho "sistemato" con dei "reroll": se viene premuto tab il programma "re-rolla" le variabili.

    BohBohBohBohBohBohBohBohBohBohBohBohBohBohBoh D:
    Utilizzando così delle variabili indipendenti, veramente non mi viene in mente proprio nulla se non una marea infinita di if... il che è fattibile tanto quanto orribile xD a mio avviso, anche per avere un codice più organizzato, meglio adottare una soluzione simile a quella che ti ho proposto.

    Anche perchè dopo, la randomizzazione delle coordinate ti viene veramente di 3 righe, una in più del controllo. Poi, vedi tu xD


    Mi pare comunque di aver capito che questo programma è 4fun quindi può anche essere "alla buona" il codice, quindi... vedi tu xD

    Edit:

    O sennò, cambia il seme! dichiara un indice che ogni random aumenta di tot e lo usi come seme e lo reimposti sistematicamente. Sarà molto meno probabile avere coordinate uguali.
    Last edited by Sixx93; 11-18-2013 at 06:34 AM.

Similar Threads

  1. [Image] Dave and Arun are the same peoples ..
    By Alessandro10 in forum General
    Replies: 29
    Last Post: 04-08-2012, 01:01 AM
  2. This is how some people are the most hated.
    By `Bobs Bees in forum Combat Arms Mod Discussion
    Replies: 5
    Last Post: 08-13-2010, 05:13 AM
  3. Replies: 5
    Last Post: 09-05-2009, 03:16 AM