Project

General

Profile

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

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

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
  public void getRandom(Random rnd, int[][] algorithms, int axisExcluded, int[][] scrambleTable, int[] numOccurences, int[] result)
45
    {
46
    int total=0, max=0;
47

    
48
    if( !mMoreThanOneAxis ) axisExcluded=-1;
49

    
50
    for(int i=0; i<mTotal; i++)
51
      {
52
      int[] alg = algorithms[mEdges[2*i]];
53

    
54
      if( alg[0]!=axisExcluded )
55
        {
56
        int value = scrambleTable[alg[0]][alg[1]];
57
        if( value>max ) max=value;
58
        }
59
      }
60

    
61
    for(int i=0; i<mTotal; i++)
62
      {
63
      int[] alg = algorithms[mEdges[2*i]];
64
      int previous = (total==0 ? 0 : numOccurences[total-1]);
65
      if( alg[0]!=axisExcluded ) numOccurences[total] = 1+max-scrambleTable[alg[0]][alg[1]]+previous;
66
      else                       numOccurences[total] = previous;
67
      total++;
68
      }
69

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

    
72
    for(int i=0; i<total; i++)
73
      if( random <= numOccurences[i] )
74
        {
75
        result[0] = mEdges[2*i];
76
        result[1] = mEdges[2*i+1];
77
        break;
78
        }
79
    }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
  public int getTotal()
84
    {
85
    return mTotal;
86
    }
87
}
(4-4/7)