Project

General

Profile

Download (5.57 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / solvers / SolverDino4.java @ 99c2e327

1 8afa10ef Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2023 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.solvers;
11
12
import android.content.res.Resources;
13
14
import org.distorted.main.R;
15 da57afae Leszek Koltunski
import org.distorted.objectlib.helpers.OperatingSystemInterface;
16 eb9263dd leszek
import org.distorted.objectlib.metadata.ListObjects;
17 8afa10ef Leszek Koltunski
import org.distorted.objectlib.main.TwistyObject;
18 cf14e8f1 leszek
import org.distorted.objectlib.shape.ShapeColors;
19 2ee27c13 Leszek Koltunski
import org.distorted.objectlib.shape.ShapeHexahedron;
20 8afa10ef Leszek Koltunski
import org.distorted.objectlib.tablebases.ImplementedTablebasesList;
21
import org.distorted.objectlib.tablebases.TBDino4;
22
import org.distorted.objectlib.tablebases.TablebasesAbstract;
23
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
26
public class SolverDino4 extends SolverTablebase
27
{
28
  private static final int ERROR_EDGE_THREE  = -1;
29
  private TablebasesAbstract mSolver;
30
  private int mErrorColor;
31
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33
34 da57afae Leszek Koltunski
  public SolverDino4(OperatingSystemInterface os, Resources res, TwistyObject object)
35 8afa10ef Leszek Koltunski
    {
36 da57afae Leszek Koltunski
    super(os, res,object);
37 8afa10ef Leszek Koltunski
    }
38
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
41
  private void getEdges(TwistyObject object, int[][] edges)
42
    {
43
    for(int i=0; i<12; i++)
44
      {
45
      edges[0][i] = object.getCubitFaceStickerIndex(i,0);
46
      edges[1][i] = object.getCubitFaceStickerIndex(i,1);
47
      }
48
    }
49
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51
52
  private int checkEdges(int[][] edges)
53
    {
54 2ee27c13 Leszek Koltunski
    int numB=0,numR=0, numY=0, numW=0;
55
    int indB=0,indY=0, indW=0, indR=0;
56
57
    int[] hexColors = ShapeHexahedron.FACE_COLORS;
58
59
    for(int i=0; i<6; i++)
60
      {
61
      int color = hexColors[i];
62
63 99c2e327 leszek
      if( color == ShapeColors.COLOR_WHITE) indW = i;
64
      if( color == ShapeColors.COLOR_YELLOW) indY = i;
65
      if( color == ShapeColors.COLOR_RED) indR = i;
66
      if( color == ShapeColors.COLOR_BLUE) indB = i;
67 2ee27c13 Leszek Koltunski
      }
68 8afa10ef Leszek Koltunski
69
    for(int i=0; i<12; i++)
70
      {
71
      int e = edges[0][i];
72
73
      if( e==edges[1][i] )
74 2ee27c13 Leszek Koltunski
        {
75
        if( e==indY ) numY++;
76
        if( e==indW ) numW++;
77
        if( e==indR ) numR++;
78
        if( e==indB ) numB++;
79
        }
80 8afa10ef Leszek Koltunski
      }
81
82 2ee27c13 Leszek Koltunski
    if( numY !=3 ) { mErrorColor=0; return ERROR_EDGE_THREE; }
83
    if( numW !=3 ) { mErrorColor=1; return ERROR_EDGE_THREE; }
84
    if( numB !=3 ) { mErrorColor=2; return ERROR_EDGE_THREE; }
85
    if( numR !=3 ) { mErrorColor=3; return ERROR_EDGE_THREE; }
86 8afa10ef Leszek Koltunski
87
    return 0;
88
    }
89
90 8316f9ab Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
91
92
  private void remap(int[] perm, int[] edges, int index, int section)
93
    {
94
    int val = edges[index];
95
96
    for(int i=index;i<12; i++)
97
      if( edges[i]==val )
98
        {
99
        edges[i]=-1;
100
        perm[i] = section;
101
        }
102
    }
103
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
106
  private int[] getPermutation(int[] edges)
107
    {
108
    int[] perm = new int[12];
109
110
    int index0 = 0;
111
    remap(perm,edges,index0,0);
112
113
    int index1 = index0+1;
114
115
    for(;index1<12;index1++)
116
      if( edges[index1]>=0 ) break;
117
    remap(perm,edges,index1,1);
118
119
    int index2 = index1+1;
120
121
    for(;index2<12;index2++)
122
      if( edges[index2]>=0 ) break;
123
    remap(perm,edges,index2,2);
124
125
    int index3 = index2+1;
126
127
    for(;index3<12;index3++)
128
      if( edges[index3]>=0 ) break;
129
    remap(perm,edges,index3,3);
130
131
    return perm;
132
    }
133
134 8afa10ef Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
135
136
  public int tablebaseIndex(TwistyObject object)
137
    {
138
    int[][] edges = new int[2][12];
139
    getEdges(object,edges);
140
141
    int result1 = checkEdges(edges);
142
    if( result1<0 ) return result1;
143
144 8316f9ab Leszek Koltunski
    int[] perm = getPermutation(edges[0]);
145
    return TBDino4.indexFromPartition(perm);
146 8afa10ef Leszek Koltunski
    }
147
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
150
  private int getColorIndex7(int color)
151
    {
152
    switch(color)
153
      {
154
      case 0: return R.string.color_yellow7;
155
      case 1: return R.string.color_white7;
156
      case 2: return R.string.color_blue7;
157
      case 3: return R.string.color_red7;
158
      }
159
160
    return -1;
161
    }
162
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
165
  public String error(int index, Resources res)
166
    {
167
    int i = getColorIndex7(mErrorColor);
168
    String color = res.getString(i);
169
170
    return res.getString(R.string.solver_generic_edge_three,color);
171
    }
172
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
175 da57afae Leszek Koltunski
  public int[][] solution(int index, OperatingSystemInterface os)
176 8afa10ef Leszek Koltunski
    {
177
    if( mSolver==null )
178
      {
179 eb9263dd leszek
      mSolver = ImplementedTablebasesList.createPacked(os, ListObjects.DIN4_3.name() );
180 8afa10ef Leszek Koltunski
      }
181
182 bdcf2372 Leszek Koltunski
    return mSolver!=null ? mSolver.solution(index,null,os) : null;
183 8afa10ef Leszek Koltunski
    }
184
}