Exemplo n.º 1
0
    private string GetReasonString(FillReason reason)
    {
        string s = string.Empty;

        switch (reason)
        {
        case FillReason.QuestionInput:
            // Do nothing
            break;

        case FillReason.RowRemain:
            s = "Row Remain";
            break;

        case FillReason.ColumnRemain:
            s = "Column Remain";
            break;

        case FillReason.SquareRemain:
            s = "Square Remain";
            break;

        case FillReason.SlotExclude:
            s = "Slot Exlude";
            break;

        case FillReason.CommonSolution:
            s = "Common Solution";
            break;
        }

        return(s);
    }
Exemplo n.º 2
0
    private void AddSolutionLog(int slotIndex, int value, FillReason reason)
    {
        UISolutionLog newLog = Instantiate(_solutionLogRes, _goSolutionLogRoot.transform);

        newLog.SetFillReason(slotIndex, value, reason);
        newLog.SetOnClickAction(UISolutionLogOnClickAction);
        _solutionLogList.Add(newLog);
    }
Exemplo n.º 3
0
    public void SetFillReason(int slotIndex, int value, FillReason reason)
    {
        _targetSlotIndex = slotIndex;

        SudokuUtility.ConvertToIndex(slotIndex, out int rowIndex, out int columnIndex, out int _);
        _text.text = string.Format("Solution {0} found at Row {1}, Column {2} by {3} method",
                                   value, rowIndex, columnIndex, GetReasonString(reason), slotIndex);
    }
Exemplo n.º 4
0
    public void SetSlotValueAndReason(int slotIndex, int value, FillReason reason)
    {
        _slotDataList[slotIndex].Value  = value;
        _slotDataList[slotIndex].Reason = reason;

        if (_onInputChanged != null)
        {
            _onInputChanged();
        }
    }
Exemplo n.º 5
0
    public SudokuData(CompleteData data, FillReason deafultReason)
    {
        Init();

        for (int i = 0; i < data.ArrayData.GetLength(0); i++)
        {
            for (int j = 0; j < data.ArrayData.GetLength(1); j++)
            {
                int value = data.ArrayData[i, j];
                SetSlotValueAndReason(i, j, value, deafultReason);
            }
        }

        Update();
    }
Exemplo n.º 6
0
    private void FillSolutionIntoSlot(int rowIndex, int columnIndex, int value, FillReason reason)
    {
        int slotIndex = rowIndex * SudokuUtility.PUZZLE_LENGTH + columnIndex;

        // Update data
        _sData.SetSlotValueAndReason(slotIndex, value, reason);

        UISudokuSlot uiSlot = _uiSlotBoard.GetUISlot(slotIndex);

        //// Add to undo
        //if (_undoIndex >= _undoCmdList.Count) {
        //    _undoCmdList.Add(new UndoCommand());
        //}
        //_undoCmdList[_undoIndex].AddNewCommand(uiSlot.Value, uiSlot.FillReason, slotIndex);

        uiSlot.SetValueAndReason(value, reason);

        //if (reason != FillReason.QuestionInput) {
        //    AddSolutionLog(rowIndex, columnIndex, value, reason);
        //}
    }
Exemplo n.º 7
0
    public static Color GetFillReasonColor(FillReason reason)
    {
        Color c = Color.black;

        switch (reason)
        {
        case FillReason.RowRemain:
        case FillReason.ColumnRemain:
        case FillReason.SquareRemain:
        case FillReason.SlotExclude:
        case FillReason.CommonSolution:
        case FillReason.PlayerInput:
            c = Color.red;
            break;

        case FillReason.None:
        case FillReason.QuestionInput:
        default:
            c = Color.black;
            break;
        }

        return(c);
    }
Exemplo n.º 8
0
    private void ExecuteUndo()
    {
        if (_undoCmdList.Count <= 0)
        {
            return;
        }

        UndoCommand cmd = _undoCmdList[_undoCmdList.Count - 1];

        for (int i = 0; i < cmd.ValueList.Count; i++)
        {
            int        slotIndex = cmd.SlotIndexList[i];
            int        value     = cmd.ValueList[i];
            FillReason reason    = cmd.FillReasonList[i];

            _sData.SetSlotValueAndReason(slotIndex, value, reason);
            _sData.Update();
        }

        _undoIndex -= 1;
        _undoCmdList.RemoveAt(_undoCmdList.Count - 1);

        Refresh();
    }
Exemplo n.º 9
0
 public void SetValueAndReason(int value, FillReason reason)
 {
     _value  = value;
     _reason = reason;
     RefreshDisplay();
 }
Exemplo n.º 10
0
 public void AddNewCommand(int value, FillReason reason, int slotIndex)
 {
     ValueList.Add(value);
     FillReasonList.Add(reason);
     SlotIndexList.Add(slotIndex);
 }
Exemplo n.º 11
0
    public void SetSlotValueAndReason(int rowIndex, int columnIndex, int value, FillReason reason)
    {
        int slotIndex = SudokuUtility.ConvertToSlotIndex(rowIndex, columnIndex);

        SetSlotValueAndReason(slotIndex, value, reason);
    }
Exemplo n.º 12
0
    private void FillSolutionIntoSlot(int slotIndex, int value, FillReason reason)
    {
        SudokuUtility.ConvertToIndex(slotIndex, out int rowIndex, out int columnIndex, out int _);

        FillSolutionIntoSlot(rowIndex, columnIndex, value, reason);
    }
Exemplo n.º 13
0
    private bool RemoveNumberFromData(SudokuData sData, int count)
    {
        // 1. Remove value of a "Filled" slot randomly
        // 2. Check this data is solvable.
        //    If solvable, count by one, if not solvable, undo step 1.

        sData.Update();
        List <int> filledSlotList = sData.GetAllFilledSlotIndex();
        int        retryCount     = filledSlotList.Count;
        int        exeCount       = 0;
        int        rndSlotIndex   = -1;
        int        rndSlotValue   = -1;
        FillReason rndSlotReason  = FillReason.None;

        while (exeCount < retryCount)
        {
            // Revert if previous try is available (previous try failed)
            if (rndSlotIndex != -1)
            {
                sData.SetSlotValueAndReason(rndSlotIndex, rndSlotValue, rndSlotReason);
            }

            // Try remove random slot
            if (filledSlotList.Count == 0)
            {
                return(false);
            }

            int rndListIndex = Random.Range(0, filledSlotList.Count);
            rndSlotIndex = filledSlotList[rndListIndex];
            SlotData slotData = sData.GetSlotData(rndSlotIndex);
            rndSlotValue  = slotData.Value;
            rndSlotReason = slotData.Reason;
            filledSlotList.RemoveAt(rndListIndex);

            sData.SetSlotValueAndReason(rndSlotIndex, 0, FillReason.None);
            exeCount += 1;

            if (!sData.IsSolvable())
            {
                continue;
            }

            if (count <= 1)
            {
                return(true);
            }

            bool success = RemoveNumberFromData(sData, count - 1);
            if (success)
            {
                return(true);
            }
            else
            {
                continue;
            }
        }

        return(false);
    }