// Events protected void raiseUpdateForm(RubikCube cube) { lock (cubeStat) { cubeStat.fromMatrix(cube.ToMatrix()); } }
private RubikCube _resolveCross(RubikCube from, RubikCube to) { if (this.IsCanceled) { return(null); } //AbstractAlgorithm ac = new AlgorithmRandom(500000, 5); AbstractAlgorithm ac = new AlgorithmCombinations(4); ac.Resolve(from, to); while (!ac.IsCompleted) { this.status = ac.Status(); if (this.IsCanceled) { ac.Cancel(); } System.Threading.Thread.Sleep(200); } if (ac.cubeMatch != null) { this.addMoves(ac.winnerMoves); } this.cubeStat = ac.cubeMatch; return(ac.cubeMatch); }
public RubikCube Clone() { RubikCube cubeaux = new RubikCube(); cubeaux.fromMatrix((int[, , ]) this.ToMatrix().Clone()); return(cubeaux); }
private void _resolve(RubikCube from, RubikCube to) { RubikCube working = from.Clone(); RubikCube template = new RubikCube(); template.clear(); template.ToMatrix()[2, 4, 2] = to.ToMatrix()[2, 4, 2]; template.ToMatrix()[2, 4, 3] = to.ToMatrix()[2, 4, 3]; template.ToMatrix()[2, 3, 4] = to.ToMatrix()[2, 3, 4]; working = this._resolveCross(working, template.Clone()); if (working != null) { template.ToMatrix()[1, 4, 2] = to.ToMatrix()[1, 4, 2]; template.ToMatrix()[0, 3, 2] = to.ToMatrix()[0, 3, 2]; working = this._resolveCross(working, template.Clone()); if (working != null) { template.ToMatrix()[2, 4, 1] = to.ToMatrix()[2, 4, 1]; template.ToMatrix()[2, 3, 0] = to.ToMatrix()[2, 3, 0]; working = this._resolveCross(working, template.Clone()); if (working != null) { template.ToMatrix()[3, 4, 2] = to.ToMatrix()[3, 4, 2]; template.ToMatrix()[4, 3, 2] = to.ToMatrix()[4, 3, 2]; working = this._resolveCross(working, template.Clone()); if (working != null) { template.ToMatrix()[2, 2, 4] = to.ToMatrix()[2, 2, 4]; working = this._resolveCross(working, template.Clone()); if (working != null) { template.ToMatrix()[0, 2, 2] = to.ToMatrix()[0, 2, 2]; working = this._resolveCross(working, template.Clone()); if (working != null) { template.ToMatrix()[2, 2, 0] = to.ToMatrix()[2, 2, 0]; working = this._resolveCross(working, template.Clone()); if (working != null) { template.ToMatrix()[4, 2, 2] = to.ToMatrix()[4, 2, 2]; working = this._resolveCross(working, template.Clone()); } } } } } } } this.cubeMatch = working; }
public ProcessParms(RubikCube workingCube, int deep, int currentAxis, ArrayList Moves) { this.Thread = -1; this.WorkingCube = workingCube.Clone(); this.Deep = deep; this.CurrentAxis = currentAxis; this.Operations = 0; this.Moves = (ArrayList)Moves.Clone(); // Console.WriteLine("Thread: {0} Axis: {1}", this.Thread.ToString(), this.CurrentAxis.ToString()); }
public override void Resolve(RubikCube from, RubikCube to) // From: Generalmente mastercube To: Obtener un cubo que contemple el modelo recibido { this.init(); RubikCube WorkingCube = from.Clone(); if (!to.Equals(from)) { this.startProcess(() => _resolve(from, to)); } else { this.cubeMatch = from.Clone(); } }
public override void Resolve(RubikCube from, RubikCube to) // From: Generalmente mastercube To: Obtener un cubo que contemple el modelo recibido { this.init(to); if (!to.Equals(from)) { ProcessParms parms = null; // Load all axis parms = new ProcessParms(from, 0, 9, new ArrayList()); fillCubeAux(parms); } else { this.cubeMatch = from.Clone(); } }
protected virtual void init(RubikCube template) { Console.WriteLine("Abrastract:init()"); this.Template = template; list = new Task[maxthreads]; this.threads = 0; this.operationsByThread = new int[maxthreads]; this.winnerMoves = new ArrayList(); //this.cubeTemplate = to.Clone(); this.cubeMatch = null; this.cubeStat = new RubikCube(); this.isCancel = false; }
private void contructViewPoints() { this.cached = true; RubikCube nc = new RubikCube(); nc = this.Clone(); int idx = 0; for (int y = 0; y <= 3; y++) { nc.rotateFace(1, 1); nc.rotateFace(1, 2); nc.rotateFace(1, 3); for (int z = 0; z <= 3; z++) { nc.rotateFace(2, 1); nc.rotateFace(2, 2); nc.rotateFace(2, 3); this.viewPoints[idx++] = (int[, , ])nc.ToMatrix().Clone(); } } for (int x = 0; x <= 3; x++) { nc.rotateFace(0, 1); nc.rotateFace(0, 2); nc.rotateFace(0, 3); if (x == 0 || x == 2) { for (int z = 0; z <= 3; z++) { nc.rotateFace(2, 1); nc.rotateFace(2, 2); nc.rotateFace(2, 3); this.viewPoints[idx++] = (int[, , ])nc.ToMatrix().Clone(); } } } }
public override bool Equals(Object obj) { if (!this.cached) { contructViewPoints(); } RubikCube ToCompare = (RubikCube)obj; int[, ,] toCompareMatrix = ToCompare.ToMatrix(); for (int idx = 0; idx < 24; idx++) { bool equal = true; // Se realiza la comparación. Aunte una incompatibilidad, se cancela. int x, y, z; for (int i = 0; i < 54 && equal; i++) { x = validPositions[i, 0]; y = validPositions[i, 1]; z = validPositions[i, 2]; if (!(viewPoints[idx][x, y, z] == toCompareMatrix[x, y, z] || // número igual toCompareMatrix[x, y, z] == 0 || viewPoints[idx][x, y, z] == 0 // comodin )) { equal = false; } } if (equal) { return(true); } } return(false); }
// Procedimiento de rotación para tener control de movimientos protected override void init(RubikCube template) { base.init(template); this.solved = 0; }
public abstract void Resolve(RubikCube from, RubikCube to);
/// <summary> /// Do a cube rotation and return true if the process was cancelled or exists a match winner /// </summary> /// <param name="param1">Some Parameter.</param> /// <returns>What this method returns.</returns> protected bool Rotate(ProcessParms parms, int axis, int level) { if (this.cubeMatch != null || this.IsCanceled) { return(true); } // ----------------------------------------- // Add movement to list // ----------------------------------------- bool rotate = true; Move m = null; bool add = false; if (parms.Moves.Count == 0) { add = true; } else { m = (Move)parms.Moves[parms.Moves.Count - 1]; if (m.Axis == axis && m.Level == level) { // If last movment is the 3rd, we remove it because one more is useless if (m.Turns >= 3) { parms.Moves.RemoveAt(parms.Moves.Count - 1); rotate = false; } else { m.Turns++; // Same movement } } else { add = true; } } if (add) { m = new Move(axis, level, 1); parms.Moves.Add(m); } // --------------------------------- // Chequeo si se eliminó el movimiento. En ese caso no es necesario chequear // Se rota pero no se compara, esto es para que regrese a la posición anterior //if (parms.Thread == 2) // Console.WriteLine("Axis: {0} Level: {1} Deep: {2}", axis, level, parms.Deep); parms.WorkingCube.rotateFace(axis, level); if (rotate) { lock (this.winnerMoves) { // Siempre se chequea desde el template para optimizar el cache de viewPoints if (this.Template.Equals(parms.WorkingCube)) { this.cubeMatch = parms.WorkingCube.Clone(); this.winnerThread = parms.Thread; this.winnerMoves = parms.Moves; } lock (this.operationsByThread) { this.operationsByThread[parms.Thread]++; } if (this.Operations % 5000 == 0) { this.raiseUpdateForm(parms.WorkingCube); } } } if (this.cubeMatch != null) { return(true); } else { return(false); } }