private void GenerateOutputField() { _inputParentPanel.SetActive(false); _outputParentPanel.SetActive(true); float initialPositionX = -(Size - 1.0f) / 2.0f * 50.0f; float initialPositionY = (Size - 1.0f) / 2.0f * 50.0f; for (int y = 0; y < Size; y++) { float actualPositionY = initialPositionY - 50.0f * y; for (int x = 0; x < Size; x++) { CalculationField field = CalculationFields[x, y]; float actualPositionX = initialPositionX + 50.0f * x; GameObject newInputPanel = Instantiate(_outputFieldPrefab, _outputParentPanel.transform); RectTransform rect = newInputPanel.GetComponent <RectTransform>(); rect.localPosition = new Vector3(actualPositionX, actualPositionY, 0); if (field.TargetValue != -1) { Text text = newInputPanel.GetComponentInChildren <Text>(); text.text = field.TargetValue.ToString(); } Image image = newInputPanel.GetComponent <Image>(); image.color = Colors[field.Color]; } } }
private bool HeathCheck() { for (int x = 0; x < Size; x++) { for (int y = 0; y < Size; y++) { CalculationField field = CalculationFields[x, y]; if (field.TargetValue == -1) { continue; } // Check if field sees more than allowed int sum = 0; foreach (Direction direction in Directions) { var neighbours = field.Neighbours[direction]; foreach (CalculationField neighbour in neighbours) { if (neighbour.Color == FieldColor.Blue) { sum++; } if (neighbour.Color == FieldColor.Empty) { break; } } } if (sum > field.TargetValue) { Debug.LogError("Error: Last Step overloaded field " + field.X + "," + field.Y); return(false); } // Check if less neighbours than needed sum = 0; foreach (Direction direction in Directions) { var neighbours = field.Neighbours[direction]; sum += neighbours.Length; } if (sum < field.TargetValue) { Debug.LogError("Error: Last Step underloaded field " + field.X + "," + field.Y); return(false); } } } return(true); }
private void OverloadsField() { foreach (Direction direction in Directions) { if (!PossibleDirections[direction]) { continue; } // get first empty neighbour in direction var neighbours = Neighbours[direction]; CalculationField emptyNeighbour = null; foreach (CalculationField neighbour in neighbours) { if (neighbour.Color == FieldColor.Empty) { emptyNeighbour = neighbour; break; } } if (emptyNeighbour == null) { continue; } // set neighbour blue emptyNeighbour.Color = FieldColor.Blue; // update missing UpdateMissing(); // if missing < 0 -> set neighbour red; continue; if (Missing < 0) { Debug.Log(emptyNeighbour.X + "," + emptyNeighbour.Y + " would overload field " + X + "," + Y); Debug.Log("---Setting field " + emptyNeighbour.X + "," + emptyNeighbour.Y + " to red."); emptyNeighbour.Color = FieldColor.Red; Singleton.ChangeMade = true; return; } // set nieghbour empty emptyNeighbour.Color = FieldColor.Empty; } }
private void Step6_FillHoles() { for (int x = 0; x < Size; x++) { for (int y = 0; y < Size; y++) { CalculationField field = CalculationFields[x, y]; if (field.TargetValue != -1 || field.Color != FieldColor.Empty) { continue; } field.UpdateField(); // STEP 6: Set surrounded by red also to red bool fill = true; foreach (Direction direction in Directions) { var neighbours = field.Neighbours[direction]; if (neighbours.Length == 0) { continue; } fill = false; break; } if (!fill) { continue; } Debug.Log("Field " + field.X + "," + field.Y + " is surrounded by red -> setting red"); field.Color = FieldColor.Red; field.Finished = true; } } }
private void GenerateCalculationField() { CalculationFields = new CalculationField[Size, Size]; for (var y = 0; y < Size; y++) { for (var x = 0; x < Size; x++) { var actualInputField = _inputFields[x, y]; var actualInputValue = actualInputField.GetComponentInChildren <InputField>().text; switch (actualInputValue) { case "r": CalculationFields[x, y] = new CalculationField(-1, FieldColor.Red); break; case "": CalculationFields[x, y] = new CalculationField(-1, FieldColor.Empty); break; default: int val; int.TryParse(actualInputValue, out val); CalculationFields[x, y] = new CalculationField(val, FieldColor.Blue); break; } if (x == 0) { CalculationFields[x, y].PossibleDirections[Direction.Left] = false; } if (x == Size - 1) { CalculationFields[x, y].PossibleDirections[Direction.Right] = false; } if (y == 0) { CalculationFields[x, y].PossibleDirections[Direction.Up] = false; } if (y == Size - 1) { CalculationFields[x, y].PossibleDirections[Direction.Down] = false; } // Tell fields their coordinates CalculationFields[x, y].X = x; CalculationFields[x, y].Y = y; if (CalculationFields[x, y].TargetValue != -1) { _fieldsToCalculate.Add(CalculationFields[x, y]); } } } }