Project

General

Profile

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

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

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 l=0, layerBmp = alg[1];
47

    
48
    for(; l<32; l++)
49
      {
50
      if( (layerBmp&0x1) !=0 ) break;
51
      layerBmp /= 2;
52
      }
53

    
54
    return scrambleTable[alg[0]][l];
55
    }
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

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

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

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

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

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

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

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

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

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