Project

General

Profile

Download (3.16 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / scrambling / ScrambleState.java @ 6777e712

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.objectlib.scrambling;
11

    
12
import java.util.Random;
13

    
14
///////////////////////////////////////////////////////////////////////////////////////////////////
15

    
16
public class ScrambleState
17
{
18
  private final boolean mMoreThanOneAxis;
19
  private final int mTotal;
20
  private final int[] mEdges;
21

    
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23

    
24
  public ScrambleState(int[] edges, int[][] algorithms)
25
    {
26
    mEdges = edges;
27
    mTotal = edges.length/2;
28

    
29
    int firstAxis= algorithms[edges[0]][0];
30
    boolean moreThanOne = false;
31

    
32
    for(int i=1; i<mTotal; i++)
33
      if( algorithms[edges[2*i]][0]!=firstAxis )
34
        {
35
        moreThanOne = true;
36
        break;
37
        }
38

    
39
    mMoreThanOneAxis = moreThanOne;
40
    }
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
  private int getScrambleValue(int[] alg, int[][] scrambleTable)
45
    {
46
    int layerBmp = alg[1];
47
    int[] table = scrambleTable[alg[0]];
48

    
49
    for(int i : table)
50
      {
51
      if( (layerBmp&0x1) != 0) return i;
52
      layerBmp /= 2;
53
      }
54

    
55
    return 0;
56
    }
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  public void getRandom(Random rnd, int[][] algorithms, int axisExcluded, int[][] scrambleTable, int[] numOccurences, int[] result)
61
    {
62
    int total=0, max=0;
63

    
64
    if( !mMoreThanOneAxis ) axisExcluded=-1;
65

    
66
    for(int i=0; i<mTotal; i++)
67
      {
68
      int[] alg = algorithms[mEdges[2*i]];
69

    
70
      if( alg[0]!=axisExcluded )
71
        {
72
        int value = getScrambleValue(alg,scrambleTable);
73
        if( value>max ) max=value;
74
        }
75
      }
76

    
77
    for(int i=0; i<mTotal; i++)
78
      {
79
      int[] alg = algorithms[mEdges[2*i]];
80
      int previous = (total==0 ? 0 : numOccurences[total-1]);
81
      if( alg[0]!=axisExcluded ) numOccurences[total] = 1+max+previous-getScrambleValue(alg,scrambleTable);
82
      else                       numOccurences[total] = previous;
83
      total++;
84
      }
85

    
86
    float random= rnd.nextFloat()*numOccurences[total-1];
87

    
88
    for(int i=0; i<total; i++)
89
      if( random <= numOccurences[i] )
90
        {
91
        result[0] = mEdges[2*i];
92
        result[1] = mEdges[2*i+1];
93
        break;
94
        }
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  public int getTotal()
100
    {
101
    return mTotal;
102
    }
103
}
(4-4/7)