private bool ReduceBlock(int square, SquareValue reduceValue) { int row = square / Grid.Size; int col = square % Grid.Size; bool valid = true; int[,] block = this.GetBlock(square); for (int k = 0; valid && k < Grid.NumSquaresInBlock; k++) { int i = block[k, 0]; int j = block[k, 1]; if (i != row || j != col) { SquareValue newValue = reducedState.GetSquare(i, j) & ~reduceValue; valid = (newValue != SquareValue.None); if (valid) { reducedState.SetSquare(i, j, newValue); } } } return(valid); }
private bool ReducePeers(int square, SquareValue reduceValue) { bool valid = this.ReduceRow(square, reduceValue); valid = valid && this.ReduceCol(square, reduceValue); return(valid && this.ReduceBlock(square, reduceValue)); }
public string ToDebugString() { StringBuilder builder = new StringBuilder(); for (int row = 0; row < Grid.Size; row++) { for (int col = 0; col < Grid.Size; col++) { SquareValue value = GetSquare(row, col); var values = SplitValues(value); foreach (var v in values) { builder.Append(Grid.ConvertToString(v)); } builder.Append(' ', Size - values.Count); builder.Append(','); } builder.Append(Environment.NewLine); } return(builder.ToString()); }
private void InstantiateMapSquare(Vector2 worldPosition, SquareValue mapValue) { GameObject objectToInstantiate = mapValue == SquareValue.COLLISION ? prefabColision : floorPrefab; if (objectToInstantiate != null) { ((GameObject)Instantiate(objectToInstantiate, new Vector3(worldPosition.x, worldPosition.y, 0), Quaternion.identity)).transform.SetParent(father.transform); } }
public bool Reduce(int square, Grid grid) { SquareValue reduceValue = grid.GetSquare(square); this.reducedState = grid; bool valid = this.ReduceRow(square, reduceValue); valid = valid && this.ReduceCol(square, reduceValue); return(valid && this.ReduceBlock(square, reduceValue)); }
private static bool IsSquareUnique(SquareValue value) { return(value == SquareValue.One || value == SquareValue.Two || value == SquareValue.Three || value == SquareValue.Four || value == SquareValue.Five || value == SquareValue.Six || value == SquareValue.Seven || value == SquareValue.Eight || value == SquareValue.Nine); }
public void Set(int posX, int posY, SquareValue squareValue) { switch (squareValue) { case SquareValue.O: SetO(posX, posY); break; case SquareValue.X: SetX(posX, posY); break; } }
private static SquareValue ConvertToSquareValue(char val) { SquareValue convertedValue = SquareValue.All; switch (val) { case '1': convertedValue = SquareValue.One; break; case '2': convertedValue = SquareValue.Two; break; case '3': convertedValue = SquareValue.Three; break; case '4': convertedValue = SquareValue.Four; break; case '5': convertedValue = SquareValue.Five; break; case '6': convertedValue = SquareValue.Six; break; case '7': convertedValue = SquareValue.Seven; break; case '8': convertedValue = SquareValue.Eight; break; case '9': convertedValue = SquareValue.Nine; break; default: break; } return(convertedValue); }
public Grid Reduce(Grid originalState) { bool valid = true; reducedState = new Grid(originalState); for (int s = 0; valid && s < Grid.SquareCount; s++) { if (originalState.IsSquareUnique(s)) { SquareValue value = originalState.GetSquare(s); valid = ReducePeers(s, value); } } return(valid ? reducedState : null); }
private static string ConvertToString(SquareValue value) { string ans = "."; switch (value) { case SquareValue.One: ans = "1"; break; case SquareValue.Two: ans = "2"; break; case SquareValue.Three: ans = "3"; break; case SquareValue.Four: ans = "4"; break; case SquareValue.Five: ans = "5"; break; case SquareValue.Six: ans = "6"; break; case SquareValue.Seven: ans = "7"; break; case SquareValue.Eight: ans = "8"; break; case SquareValue.Nine: ans = "9"; break; } return(ans); }
/// <summary> /// Generate a valid terminal pattern using Las Vegas randomized approach, letting /// the solver produce the final pattern. /// </summary> /// <returns>The terminal pattern.</returns> private Grid GenerateTerminalPattern() { Grid grid = null; Reducer reducer = new Reducer(); Solver solver = new Solver(); bool solved = false; while (!solved) { int givenCount = 0; grid = new Grid(); while (givenCount < InitialGivenCount) { int square = RandomSquare(random); if (!grid.IsSquareUnique(square)) { SquareValue value = RandomSquareValue(random); Grid tryGrid = (Grid)grid.Clone(); tryGrid.SetSquare(square, value); tryGrid = tryGrid.Reduce(); if (tryGrid != null) { grid = tryGrid; givenCount++; } } } SolverContext solverContext = new SolverContext(); solver.Solve(grid, solverContext, true, TimeSpan.FromSeconds(5)); if (solverContext.UniqueSolution != null) { grid = solverContext.UniqueSolution; solved = true; } } return(grid); }
private static List <SquareValue> SplitValues(SquareValue value) { var values = new List <SquareValue>(); if ((value & SquareValue.One) == SquareValue.One) { values.Add(SquareValue.One); } if ((value & SquareValue.Two) == SquareValue.Two) { values.Add(SquareValue.Two); } if ((value & SquareValue.Three) == SquareValue.Three) { values.Add(SquareValue.Three); } if ((value & SquareValue.Four) == SquareValue.Four) { values.Add(SquareValue.Four); } if ((value & SquareValue.Five) == SquareValue.Five) { values.Add(SquareValue.Five); } if ((value & SquareValue.Six) == SquareValue.Six) { values.Add(SquareValue.Six); } if ((value & SquareValue.Seven) == SquareValue.Seven) { values.Add(SquareValue.Seven); } if ((value & SquareValue.Eight) == SquareValue.Eight) { values.Add(SquareValue.Eight); } if ((value & SquareValue.Nine) == SquareValue.Nine) { values.Add(SquareValue.Nine); } return(values); }
private void PropagateBlocks(Grid problemGrid, Grid solutionGrid) { var perm = Generate0To2Permutation(); Grid colBlockPropagatedProblemGrid = new Grid(); Grid colBlockPropagatedSolutionGrid = new Grid(); Grid finalProblemGrid = new Grid(); Grid finalSolutionGrid = new Grid(); for (int i = 0; i < Grid.Size; i++) { for (int b = 0; b < (Grid.Size / Grid.BlockSize); b++) { for (int j = 0; j < Grid.BlockSize; j++) { int permBlockCol = perm[b]; SquareValue problemValue = problemGrid.GetSquare(i, b * Grid.BlockSize + j); SquareValue solutionValue = solutionGrid.GetSquare(i, b * Grid.BlockSize + j); colBlockPropagatedProblemGrid.SetSquare(i, permBlockCol * Grid.BlockSize + j, problemValue); colBlockPropagatedSolutionGrid.SetSquare(i, permBlockCol * Grid.BlockSize + j, solutionValue); } } } for (int b = 0; b < (Grid.Size / Grid.BlockSize); b++) { for (int i = 0; i < Grid.BlockSize; i++) { for (int j = 0; j < Grid.Size; j++) { int permBlockRow = perm[b]; SquareValue problemValue = colBlockPropagatedProblemGrid.GetSquare(b * Grid.BlockSize + i, j); SquareValue solutionValue = colBlockPropagatedSolutionGrid.GetSquare(b * Grid.BlockSize + i, j); finalProblemGrid.SetSquare(permBlockRow * Grid.BlockSize + i, j, problemValue); finalSolutionGrid.SetSquare(permBlockRow * Grid.BlockSize + i, j, solutionValue); } } } problemGrid.Set(finalProblemGrid); solutionGrid.Set(solutionGrid); }
private bool ReduceCol(int square, SquareValue reduceValue) { int row = square / Grid.Size; int col = square % Grid.Size; bool valid = true; for (int i = 0; valid && i < Grid.Size; i++) { if (i != row) { SquareValue newValue = reducedState.GetSquare(i, col) & ~reduceValue; valid = (newValue != SquareValue.None); if (valid) { this.reducedState.SetSquare(i, col, newValue); } } } return(valid); }
private bool ReduceRow(int square, SquareValue reduceValue) { int row = square / Grid.Size; int col = square % Grid.Size; bool valid = true; for (int j = 0; valid && j < Grid.Size; j++) { if (j != col) { SquareValue newValue = reducedState.GetSquare(row, j) & ~reduceValue; valid = (newValue != SquareValue.None); if (valid) { reducedState.SetSquare(row, j, newValue); } } } return(valid); }
private void PropagateColsAndRows(Grid problemGrid, Grid solutionGrid) { var perm = Generate0To2Permutation(); Grid colPropagatedProblemGrid = new Grid(); Grid colPropagatedSolutionGrid = new Grid(); Grid finalProblemGrid = new Grid(); Grid finalSolutionGrid = new Grid(); for (int i = 0; i < Grid.Size; i++) { for (int j = 0; j < Grid.Size; j++) { int col = Grid.BlockSize * (j / Grid.BlockSize) + perm[j % Grid.BlockSize]; SquareValue problemValue = problemGrid.GetSquare(i, col); SquareValue solutionValue = solutionGrid.GetSquare(i, col); colPropagatedProblemGrid.SetSquare(i, j, problemValue); colPropagatedSolutionGrid.SetSquare(i, j, solutionValue); } } for (int i = 0; i < Grid.Size; i++) { for (int j = 0; j < Grid.Size; j++) { int row = Grid.BlockSize * (i / Grid.BlockSize) + perm[i % Grid.BlockSize]; SquareValue problemValue = colPropagatedProblemGrid.GetSquare(row, j); SquareValue solutionValue = colPropagatedSolutionGrid.GetSquare(row, j); finalProblemGrid.SetSquare(i, j, problemValue); finalSolutionGrid.SetSquare(i, j, solutionValue); } } problemGrid.Set(finalProblemGrid); solutionGrid.Set(finalSolutionGrid); }
public void SetSquareValue(SquareValue newValue) { if (!material) { material = gameObject.GetComponent <Renderer>().material; } squareValue = newValue; switch (newValue) { case SquareValue.Clear: material.mainTexture = null; material.color = even; break; case SquareValue._1: material.mainTexture = textures[0]; material.color = odd; break; case SquareValue._2: material.mainTexture = textures[1]; material.color = even; break; case SquareValue._3: material.mainTexture = textures[2]; material.color = odd; break; case SquareValue._4: material.mainTexture = textures[3]; material.color = even; break; case SquareValue._5: material.mainTexture = textures[4]; material.color = odd; break; case SquareValue._6: material.mainTexture = textures[5]; material.color = even; break; case SquareValue._7: material.mainTexture = textures[6]; material.color = odd; break; case SquareValue._8: material.mainTexture = textures[7]; material.color = even; break; case SquareValue._9: material.mainTexture = textures[8]; material.color = odd; break; case SquareValue._10: material.mainTexture = textures[9]; material.color = even; break; case SquareValue._11: material.mainTexture = textures[10]; material.color = odd; break; case SquareValue._12: material.mainTexture = textures[11]; material.color = even; break; case SquareValue._13: material.mainTexture = textures[12]; material.color = odd; break; case SquareValue._14: material.mainTexture = textures[13]; material.color = even; break; case SquareValue._15: material.mainTexture = textures[14]; material.color = odd; break; default: squareValue = SquareValue.Clear; material.mainTexture = null; break; } }
public void SetSquare(int square, SquareValue value) { SetSquare(square / Size, square % Size, value); }
public static void Main(string[] args) { Board board = new Board(); int turn = 0; while (turn < 9) { Console.WriteLine(board.ToString()); int x, y; SquareValue squareValue = turn % 2 == 0 ? SquareValue.X : SquareValue.O; try { Console.Write("Introduzca fila: "); x = Convert.ToInt32(Console.ReadLine()); Console.Write("Introduzca columna: "); y = Convert.ToInt32(Console.ReadLine()); board.Set(x, y, squareValue); } catch (FormatException) { Console.WriteLine("Valor incorrecto..."); Console.ReadLine(); continue; } catch (ArgumentOutOfRangeException ex) { Console.WriteLine("Posicion incorrecta..."); Console.ReadLine(); continue; } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadLine(); continue; } if (turn % 2 == 0) { if (board.WinX()) { GuardarXML(board); Console.WriteLine("Gana X"); Console.ReadLine(); return; } } if (turn % 2 == 0) { if (board.WinO()) { GuardarXML(board); Console.WriteLine("Gana O"); Console.ReadLine(); return; } } if (turn == 9) { GuardarXML(board); Console.WriteLine("No hay ganador"); Console.ReadLine(); return; } turn++; } }
public void SetSquare(int row, int col, SquareValue value) { this.values[row, col] = value; }