samedi 30 janvier 2016

Create an array of combinations checking an item in it

I'm trying to generate a random list of combinations. Each combination has a number from 1 to 6, and a letter from a to cc (z, aa, bb, cc). When the number appears again, the letter will be the next.

For instance:

ID | COMBINATION
1A = 5,1,2
2A = 2,2,1
3A = 1,3,1
4A = 1,4,3
5A = 1,5,4
6A = 1,6,4
1B = 9,1,2

The list of combinations is generated randomly, and checking the last 3 items, so the combination won't be repeated. I get something like:

3416352645234156342561456235

I don't add the letter in code, but it would be something like:

3A 4A 1A 6A 3B 5A 2A 6B 4B 5B 2B 3C 4C 1B

And so on.

Well, now I would like the code to check the previous combinations and check if the first number of the last 3 combinations is different than the current one.

So, if the generated combinations were:

3A = 1,3,1
4A = 1,4,3
1A = 5,1,2
6A = 1,6,4
3B = 2,3,1

The combination id is different so is ok, but the first item of combination 3A and 4A are the same, so it would be consecutive and I don't want it.

In that case, 4A would be replaced with 4B, which would be something like:

4B = 2,4,3

Then there's 1A, which first item is different than 4B and 3A, so is ok.

But then there's 6A, which first item is the same as 3A's first one. So it would be replaced with 6B, which would be:

6B = 2,6,4

But then 6B first item, is the same as 4B, so it would be replaced with 6C, which would be:

6C = 5,6,4

But then 6C first item is the same as 1A, so it would replaced with 6D....

And so on.

At the end, the skipped items, will be readded. So, in last cases 4A and 6A were replaced, which means when 4CC is used, the next to be added will be 4A, and when 6CC is used, the next to be added will be 6A. Until every combination (29 per number) will be used.

I'm not sure if you get me, but if you don't, leave a comment before thinking it's related to another question. I have already done a lot of research about this, without success.

Here are my current code, and a picture of the combinations. In the code I tried creating a class for combinations, but I'm out of ideas about the logic to achieve what I want.

Code:

import java.io.*;
import java.util.*;

public class Randomizer {

    public static int[] one = { 5, 9, 11 },
            two = { 2, 3, 5, 6, 10, 11, 13, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 13, 1, 2, 6, 7, 9, 10, 11, 12 },
            three = { 1, 2, 4, 1, 2, 3, 7, 8, 1, 2, 4, 6, 7, 8 }, four = { 1, 2, 4, 5, 6 },
            five = { 1, 2, 5, 6, 9, 11, 12 }, six = { 1, 2, 5, 6, 9, 11, 12 };

    public static int posuno = 0, posdos = 0, postres = 0, poscuatro = 0, poscinco = 0, posseis = 0;

    public static void main(String[] args) {

        int[] nums = new int[2000];

        for (int i = 0; i < nums.length; i++) {

            Integer[] arr = new Integer[6];
            for (int j = 0; j < arr.length; j++) {
                arr[j] = j + 1;
            }

            Collections.shuffle(Arrays.asList(arr));

            for (int j = 0; j < arr.length; j++) {
                if (i < nums.length) {
                    nums[i] = arr[j];
                }
            }

        }

        String numbers = Arrays.toString(nums);
        numbers = numbers.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll(",", "").replaceAll(" ", "");

        StringBuilder solution = new StringBuilder();
        int[] nextValidPos = { -1, -1, -1, -1, -1, -1 };
        int pos = 0;
        while (solution.length() < 203) {
            int nextNumber = Integer.valueOf(numbers.substring(pos, pos + 1));
            if (nextValidPos[nextNumber - 1] <= solution.length()) {
                solution.append(nextNumber);
                nextValidPos[nextNumber - 1] = solution.length() + 3;
                if (nextNumber == 1)
                    nextValidPos[nextNumber - 1] += 4;
            }
            pos++;
        }
        // show(solution.toString());

        int[] list = getIntArrayFromString(solution.toString());
        generateFile(list);

        List<Combo> comboUno = new ArrayList<Combo>();
        List<Combo> comboDos = new ArrayList<Combo>();
        List<Combo> comboTres = new ArrayList<Combo>();
        List<Combo> comboCuatro = new ArrayList<Combo>();
        List<Combo> comboCinco = new ArrayList<Combo>();
        List<Combo> comboSeis = new ArrayList<Combo>();

        for (int a = 0; a < list.length; a++) {
            switch (list[a]) {
            case 1:
                for (int b = 0; b < one.length; b++) {
                    comboUno.add(new Combo(list[a], one[b]));
                }
                break;

            case 2:
                for (int b = 0; b < two.length; b++) {
                    comboDos.add(new Combo(list[a], two[b]));
                }
                break;

            case 3:
                for (int b = 0; b < three.length; b++) {
                    comboTres.add(new Combo(list[a], three[b]));
                }
                break;

            case 4:
                for (int b = 0; b < four.length; b++) {
                    comboCuatro.add(new Combo(list[a], four[b]));
                }
                break;

            case 5:
                for (int b = 0; b < five.length; b++) {
                    comboCinco.add(new Combo(list[a], five[b]));
                }
                break;

            case 6:
                for (int b = 0; b < six.length; b++) {
                    comboSeis.add(new Combo(list[a], six[b]));
                }
                break;
            }
        }

    }

    public static void show(String s) {
        for (int i = 0; i < s.length(); i++) {
            System.out.print(s.substring(i, i + 1));
            if (i != s.length() - 1)
                System.out.print("-");
        }
    }

    public static int[] getIntArrayFromString(String s) {
        int[] array = new int[203];
        for (int i = 0; i < array.length; i++) {
            array[i] = Integer.valueOf((s.substring(i, i + 1)));
        }
        return array;
    }

    public static void generateFile(int[] array) {
        PrintWriter writer;

        int cur = -1;

        try {
            writer = new PrintWriter("combos.txt");

            for (int i = 0; i < array.length; i++) {
                if (cur + 7 == i) {
                    cur = i;
                    writer.println(array[i]);
                } else {
                    writer.print(array[i]);
                }
            }

            writer.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    private static class Combo {

        int id, c;

        public Combo(int id, int c) {
            this.id = id;
            this.c = c;
        }

        public int getId() {
            return id;
        }

        public int getC() {
            return c;
        }

    }

}

Picture: http://ift.tt/20enB1v

In the picture they have a letter too 1A = C5, P1, Z2 But I think those letters (c,p,z) could be ignored in the code.

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire