Project

General

Profile

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

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

1 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6 babb7b08 Leszek Koltunski
// 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 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10 10b7e306 Leszek Koltunski
package org.distorted.objectlib.scrambling;
11 29b82486 Leszek Koltunski
12
import java.util.Random;
13
14
///////////////////////////////////////////////////////////////////////////////////////////////////
15
16
public class ScrambleState
17
{
18 9ba7f3f6 Leszek Koltunski
  private final boolean mMoreThanOneAxis;
19
  private final int mTotal;
20
  private final int[] mEdges;
21 29b82486 Leszek Koltunski
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23
24 9ba7f3f6 Leszek Koltunski
  public ScrambleState(int[] edges, int[][] algorithms)
25 29b82486 Leszek Koltunski
    {
26 9ba7f3f6 Leszek Koltunski
    mEdges = edges;
27
    mTotal = edges.length/2;
28 f9a81f52 Leszek Koltunski
29 9ba7f3f6 Leszek Koltunski
    int firstAxis= algorithms[edges[0]][0];
30
    boolean moreThanOne = false;
31 29b82486 Leszek Koltunski
32 9ba7f3f6 Leszek Koltunski
    for(int i=1; i<mTotal; i++)
33
      if( algorithms[edges[2*i]][0]!=firstAxis )
34 29b82486 Leszek Koltunski
        {
35 9ba7f3f6 Leszek Koltunski
        moreThanOne = true;
36
        break;
37 29b82486 Leszek Koltunski
        }
38
39 9ba7f3f6 Leszek Koltunski
    mMoreThanOneAxis = moreThanOne;
40 29b82486 Leszek Koltunski
    }
41
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44 e9ec2e9d Leszek Koltunski
  public void getRandom(Random rnd, int[][] algorithms, int axisExcluded, int[][] scrambleTable, int[] numOccurences, int[] result)
45 29b82486 Leszek Koltunski
    {
46 9ba7f3f6 Leszek Koltunski
    int total=0, max=0;
47 29b82486 Leszek Koltunski
48 9ba7f3f6 Leszek Koltunski
    if( !mMoreThanOneAxis ) axisExcluded=-1;
49 4a5157a1 Leszek Koltunski
50 29b82486 Leszek Koltunski
    for(int i=0; i<mTotal; i++)
51
      {
52 9ba7f3f6 Leszek Koltunski
      int[] alg = algorithms[mEdges[2*i]];
53 29b82486 Leszek Koltunski
54 9ba7f3f6 Leszek Koltunski
      if( alg[0]!=axisExcluded )
55 29b82486 Leszek Koltunski
        {
56 9ba7f3f6 Leszek Koltunski
        int value = scrambleTable[alg[0]][alg[1]];
57 29b82486 Leszek Koltunski
        if( value>max ) max=value;
58
        }
59
      }
60
61
    for(int i=0; i<mTotal; i++)
62
      {
63 9ba7f3f6 Leszek Koltunski
      int[] alg = algorithms[mEdges[2*i]];
64 e9ec2e9d Leszek Koltunski
      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 29b82486 Leszek Koltunski
      }
69
70
    float random= rnd.nextFloat()*numOccurences[total-1];
71
72
    for(int i=0; i<total; i++)
73 e9ec2e9d Leszek Koltunski
      if( random <= numOccurences[i] )
74
        {
75
        result[0] = mEdges[2*i];
76
        result[1] = mEdges[2*i+1];
77
        break;
78
        }
79 f9a81f52 Leszek Koltunski
    }
80
81 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
82
83 9ba7f3f6 Leszek Koltunski
  public int getTotal()
84 29b82486 Leszek Koltunski
    {
85 9ba7f3f6 Leszek Koltunski
    return mTotal;
86 29b82486 Leszek Koltunski
    }
87
}