コード例 #1
0
ファイル: ListItem.cs プロジェクト: mikef907/Sudoku-Unity
 public void SetContent(SudokuGame game)
 {
     Seed.text      = game.Seed.ToString();
     Time.text      = game.Time.ToString(@"hh\:mm\:ss\:ff");
     Attempt.text   = game.Attempt.ToString();
     Completed.text = game.Solved ? "Yes" : "No";
 }
コード例 #2
0
        public void ToPrettyString4()
        {
            // test 9*9, get GetCell, 3 high by 3 wide
            string expected = " 1 2 3 | 4 5 6 | 7 8 9\n";

            expected += " 2 3 4 | 5 6 7 | 8 9 1\n";
            expected += " 3 4 5 | 6 7 8 | 9 1 2\n";
            expected += "-------+-------+-------\n";
            expected += " 4 5 6 | 7 8 9 | 1 2 3\n";
            expected += " 5 6 7 | 8 9 1 | 2 3 4\n";
            expected += " 6 7 8 | 9 1 2 | 3 4 5\n";
            expected += "-------+-------+-------\n";
            expected += " 7 8 9 | 1 2 3 | 4 5 6\n";
            expected += " 8 9 1 | 2 3 4 | 5 6 7\n";
            expected += " 9 1 2 | 3 4 5 | 6 7 8\n";

            SudokuGame igame = new SudokuGame();

            igame.maxValue     = 9;
            igame.squareHeight = 3;
            igame.squareWidth  = 3;
            igame.sudokuArray  = new int[9 * 9] {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 6, 7, 8, 9, 1, 3, 4, 5, 6, 7, 8, 9, 1, 2, 4, 5, 6, 7, 8, 9, 1, 2, 3, 5, 6, 7, 8, 9, 1, 2, 3, 4, 6, 7, 8, 9, 1, 2, 3, 4, 5, 7, 8, 9, 1, 2, 3, 4, 5, 6, 8, 9, 1, 2, 3, 4, 5, 6, 7, 9, 1, 2, 3, 4, 5, 6, 7, 8
            };

            string actual = igame.ToPrettyString();

            Assert.AreEqual(expected, actual);
        }
コード例 #3
0
        public void TestSetValue()
        {
            SudokuGame game = new SudokuGame();

            game.NewGame();

            /*Assert.AreEqual(true, game.SetValue(new Position()
             * {
             *  X = 1,
             *  Y = 1,
             *  Value = 5
             * }));*/


            while (!game.IsCompleted())
            {
                game.SetValue(game.GetHint());
            }

            Assert.AreEqual(true, game.IsValid());

            int count = 0;

            foreach (Position p in game.GetBoard())
            {
                if (p.Value > 0)
                {
                    count++;
                }
            }

            Assert.AreEqual(81, count);
        }
コード例 #4
0
        private static SolutionCheckResult CheckBoard(SudokuGame game, SudokuGame solvedGame)
        {
            var result = SolutionCheckResult.Complete;

            for (var i = 0; i < SUDOKU_BOARD_SIZE; i++)
            {
                for (var j = 0; j < SUDOKU_BOARD_SIZE; j++)
                {
                    if (game.Board[i, j] == 0)
                    {
                        result = SolutionCheckResult.NotComplete;
                    }
                    else if (game.Board[i, j] != solvedGame.Board[i, j])
                    {
                        result = SolutionCheckResult.HasErrors;
                        break;
                    }
                }
                if (result == SolutionCheckResult.HasErrors)
                {
                    break;
                }
            }

            return(result);
        }
コード例 #5
0
        public void VaildValueBySquare4()
        {
            //4*4,
            var expected = new List <int>()
            {
                2, 3
            };

            SudokuGame igame = new SudokuGame();

            igame.maxValue     = 4;
            igame.squareHeight = 2;
            igame.squareWidth  = 2;
            igame.sudokuArray  = new int[16];


            //igame.sudokuArray[10] = 2;
            //igame.sudokuArray[11] = 3;
            igame.sudokuArray[14] = 1;
            igame.sudokuArray[15] = 4;

            List <int> actual = igame.VaildValueBySquare(11);

            CollectionAssert.AreEqual(expected, actual);
        }
コード例 #6
0
        public SudokuGame applyAlgorithm(SudokuGame game)
        {
            SudokuGame bruteForceGame = BruteForceAlgoMapper.BaseGameToBruteForceGame(game.DeepCloneWithSerialization());

            // inizializzo la prima scelta ed appiattisco le celle rimanenti
            List <BruteForceAlgoCell> bfCellList = new List <BruteForceAlgoCell>();

            foreach (List <SudokuCell> row in bruteForceGame.GameMatrix)
            {
                foreach (BruteForceAlgoCell cell in row)
                {
                    if (!cell.isValueReallyFinal())
                    {
                        bfCellList.Add(cell);
                    }
                }
            }

            if (bfCellList.Count > 0)
            {
                solveGameRecursively(bruteForceGame, bfCellList, 0);
            }

            return(bruteForceGame);
            // nessuna soluzione trovata
            //throw new SudokuSolutionNotFound("soluzione non trovata");
        }
コード例 #7
0
        public void VaildValueBySquare2()
        {
            //6*6, 3 high by 2 wide
            var expected = new List <int>()
            {
                1, 4
            };

            SudokuGame igame = new SudokuGame();

            igame.maxValue     = 6;
            igame.squareHeight = 3;
            igame.squareWidth  = 2;
            igame.sudokuArray  = new int[36];


            igame.sudokuArray[4] = 6;
            igame.sudokuArray[5] = 3;
            //igame.sudokuArray[10] = 1;
            //igame.sudokuArray[11] = 4;
            igame.sudokuArray[16] = 2;
            igame.sudokuArray[17] = 5;
            List <int> actual = igame.VaildValueBySquare(11);

            CollectionAssert.AreEqual(expected, actual);
        }
コード例 #8
0
        public ActionResult Hint()
        {
            SudokuGame sudoku = GetGameSession();
            Position   pos    = sudoku.GetHint();

            //x en y zijn om de een of andere rede omgekeerd
            //MessageBox.Show(pos.X + "=Y;" + pos.Y + "=X: VALUE = " + pos.Value);

            //tel de hoeveelheid die opgelost moet worden
            int unsolvedCount = 0;

            foreach (Position position in sudoku.GetBoard())
            {
                if (position.Value == 0)
                {
                    unsolvedCount++;
                }
            }

            //Alles behalve 2 moet opgelost worden
            int solveCount = unsolvedCount - 2;

            for (int x = 0; x < solveCount; x++)
            {
                Position position = sudoku.GetHint();
                sudoku.SetValue(position);
            }

            return(RedirectToAction("Index"));
        }
コード例 #9
0
        public void VaildValueBySquare1()
        {
            var expected = new List <int>()
            {
                2, 3, 5
            };

            SudokuGame igame = new SudokuGame();

            igame.maxValue     = 9;
            igame.squareHeight = 3;
            igame.squareWidth  = 3;
            igame.sudokuArray  = new int[81];

            igame.sudokuArray[3] = 6;
            //igame.sudokuArray[4] = 5;
            igame.sudokuArray[5]  = 1;
            igame.sudokuArray[12] = 7;
            //igame.sudokuArray[13] = 2;
            igame.sudokuArray[14] = 9;
            //igame.sudokuArray[21] = 3;
            igame.sudokuArray[22] = 8;
            igame.sudokuArray[23] = 4;

            List <int> actual = igame.VaildValueBySquare(21);

            CollectionAssert.AreEqual(expected, actual);
        }
コード例 #10
0
        private bool solveGameRecursively(SudokuGame game, List <BruteForceAlgoCell> bfCellList, int listIndex)
        {
            if (bfCellList != null && bfCellList.Count > listIndex)
            {
                BruteForceAlgoCell currentCell = bfCellList[listIndex];
                foreach (int tempValue in currentCell.AllowableValues)
                {
                    currentCell.TempValue = tempValue;

                    // se il valore scelto non è ammissibile, provo con il successivo
                    if (!game.isValid())
                    {
                        continue;
                    }

                    bool solvedGame = true;
                    if (bfCellList.Count > (listIndex + 1))
                    {
                        solvedGame = solveGameRecursively(game, bfCellList, listIndex + 1);
                    }
                    // se il gioco è valido, la soluzione è stata raggiunta
                    if (game.isValid() && solvedGame)
                    {
                        return(true);
                    }
                }
                // se nessun valore ha dato esito positivo, ripristino il valore nullo
                currentCell.TempValue = 0;
            }

            // spazzate tutte le combinazioni, nessuna soluzione trovata
            return(false);
        }
コード例 #11
0
        private void NewGameSession()
        {
            var game = new SudokuGame();

            game.NewGame();
            Session["game"] = game;
        }
コード例 #12
0
        public void ToPrettyString3()
        {
            // test 6*6, get GetCell, 2 high by 3 wide
            string expected = " * * 3 | 4 5 6\n";

            expected += " * 3 4 | 5 6 1\n";
            expected += "-------+-------\n";
            expected += " * 4 5 | 6 1 2\n";
            expected += " * 5 6 | 1 2 3\n";
            expected += "-------+-------\n";
            expected += " 5 * 1 | 2 3 4\n";
            expected += " 6 1 2 | 3 * *\n";

            SudokuGame igame = new SudokuGame();

            igame.maxValue     = 6;
            igame.squareHeight = 2;
            igame.squareWidth  = 3;
            igame.sudokuArray  = new int[6 * 6] {
                0, 0, 3, 4, 5, 6, 0, 3, 4, 5, 6, 1, 0, 4, 5, 6, 1, 2, 0, 5, 6, 1, 2, 3, 5, 0, 1, 2, 3, 4, 6, 1, 2, 3, 0, 0
            };

            string actual = igame.ToPrettyString();

            Assert.AreEqual(expected, actual);
        }
コード例 #13
0
        public void saveGameToCSV(SudokuGame game, string path)
        {
            StreamWriter sw = new System.IO.StreamWriter(path);

            foreach (List <SudokuCell> row in game.GameMatrix)
            {
                StringBuilder sRowCSV = new StringBuilder(20);
                foreach (SudokuCell cell in row)
                {
                    if (cell.Value != 0)
                    {
                        sRowCSV.Append(cell.Value.ToString());
                        sRowCSV.Append(csvSep[0]);
                    }
                    else
                    {
                        sRowCSV.Append(csvEmptyValue);
                        sRowCSV.Append(csvSep[0]);
                    }
                }
                sRowCSV.Remove(sRowCSV.Length - 1, 1);

                sw.WriteLine(sRowCSV.ToString());
            }
            sw.Close();
        }
コード例 #14
0
ファイル: SudokuISetTest.cs プロジェクト: Zilin-Li/Sudoku
        public void SetBySquare_Test()
        {
            // set last cell on 4*4
            SudokuGame igame = new SudokuGame();

            igame.maxValue     = 4;
            igame.squareWidth  = 2;
            igame.squareHeight = 2;
            igame.sudokuArray  = new int[16];
            //set value as:
            //0123
            //0123
            //0123
            //0123

            //row
            for (int i = 0; i < 4; i++)
            {
                //colum
                for (int j = 0; j < 4; j++)
                {
                    int SquareIndex = i / 2 * 2 + j / 2;
                    igame.SetBySquare(j, SquareIndex, j % 2 + i % 2 * 2);
                }
            }

            //test value
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Assert.AreEqual(j, igame.sudokuArray[i * 4 + j]);
                }
            }
        }
コード例 #15
0
        public SudokuGame getGameByCSV(string path)
        {
            SudokuGame sGame = new SudokuGame();

            string[] csv = File.ReadAllLines(path);
            foreach (string row in csv)
            {
                List <SudokuCell> sudokuRow = new List <SudokuCell>();
                string[]          cells     = row.Split(csvSep, StringSplitOptions.None);
                foreach (string cell in cells)
                {
                    if (string.IsNullOrWhiteSpace(cell) || cell.Equals(csvEmptyValue))
                    {
                        sudokuRow.Add(new SudokuCell());
                    }
                    else
                    {
                        sudokuRow.Add(new SudokuCell(int.Parse(cell)));
                    }
                }

                sGame.GameMatrix.Add(sudokuRow);
            }

            return(sGame);
        }
コード例 #16
0
        public void VaildValueBySquare3()
        {
            //6*6, 2 high by 3 wide
            var expected = new List <int>()
            {
                1, 3, 5
            };

            SudokuGame igame = new SudokuGame();

            igame.maxValue     = 6;
            igame.squareHeight = 2;
            igame.squareWidth  = 3;
            igame.sudokuArray  = new int[36];


            igame.sudokuArray[15] = 6;
            //igame.sudokuArray[16] = 3;
            //igame.sudokuArray[17] = 1;
            igame.sudokuArray[21] = 4;
            igame.sudokuArray[22] = 2;
            //igame.sudokuArray[23] = 5;
            List <int> actual = igame.VaildValueBySquare(23);

            CollectionAssert.AreEqual(expected, actual);
        }
コード例 #17
0
        public void A_game_with_some_constraints_should_be_solved()
        {
            var game = new SudokuGame();

            game.SetConstrainedCell(new Coords(2, 2), 5);
            game.SetConstrainedCell(new Coords(5, 5), 5);
            game.SetConstrainedCell(new Coords(8, 8), 5);
            game.UnsetConstrainedCell(new Coords(8, 8));

            game.SetConstrainedCell(new Coords(1, 1), 1);
            game.SetConstrainedCell(new Coords(3, 1), 2);
            game.SetConstrainedCell(new Coords(5, 1), 3);
            game.SetConstrainedCell(new Coords(1, 3), 4);
            game.SetConstrainedCell(new Coords(3, 3), 6);
            game.SetConstrainedCell(new Coords(5, 3), 7);

            var iterator = game.GetLinearIterator();
            var solver   = new SudokuGameSolver(iterator, game);
            var result   = solver.Solve();

            Assert.AreEqual(SolveResults.Solved, result);
            Assert.AreEqual(5, game.GetCellAt(new Coords(2, 2)).CurrentValue);
            Assert.AreEqual(5, game.GetCellAt(new Coords(5, 5)).CurrentValue);
            Assert.AreNotEqual(5, game.GetCellAt(new Coords(8, 8)).CurrentValue);
        }
コード例 #18
0
        public SolutionCheckResult CheckSolution(SudokuGame game)
        {
            ValidateBoard(game);
            SudokuGame          solvedGame = _solvedGames[game.Id];
            SolutionCheckResult result     = CheckBoard(game, solvedGame);

            return(result);
        }
コード例 #19
0
        public void saveGameSerialized(SudokuGame game, string path)
        {
            var          serializer = new XmlSerializer(game.GetType());
            StreamWriter sw         = new System.IO.StreamWriter(path);

            serializer.Serialize(sw, game);
            sw.Close();
        }
コード例 #20
0
 private void RetrieveGameSession()
 {
     _currentGame = (SudokuGame)Session["game"];
     if (_currentGame == null)
     {
         NewGameSession();
         _currentGame = (SudokuGame)Session["game"];
     }
 }
コード例 #21
0
        public void An_empty_game_should_be_solved()
        {
            var game     = new SudokuGame();
            var iterator = game.GetLinearIterator();
            var solver   = new SudokuGameSolver(iterator, game);
            var result   = solver.Solve();

            Assert.AreEqual(SolveResults.Solved, result);
        }
コード例 #22
0
        public void FromCSV_Test()
        {
            SudokuGame igame   = new SudokuGame();
            string     CSVFile = "";

            CSVFile  = "6,3,2" + '\n';
            CSVFile += ("0,0,3,4,5,6,0,3,4,5,6,1,0,4,5,6,1,2,0,5,6,1,2,3,5,0,1,2,3,4,6,1,2,3,0,0" + '\n');
            igame.FromCSV(CSVFile);
            Assert.AreEqual(CSVFile, igame.CSVFile);
        }
コード例 #23
0
        public void ToCSV_Test()
        {
            string     expected = "1,2,3,4,5,6,7,8,9,\n";
            SudokuGame igame    = new SudokuGame();

            igame.sudokuArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            string actual = igame.ToCSV();

            Assert.AreEqual(expected, actual);
        }
コード例 #24
0
        public ActionResult SetValue(short indexx, short indexy, short value)
        {
            SudokuGame sudoku = GetGameSession();

            sudoku.SetValue(new Position()
            {
                X = indexx, Y = indexy, Value = value
            });
            return(RedirectToAction("Index"));
        }
コード例 #25
0
        protected bool onePassAlgorithm(SudokuGame game)
        {
            bool atLeastOneOperation = false;

            atLeastOneOperation |= useRowConstraint(game);
            atLeastOneOperation |= useColumnConstraint(game);
            atLeastOneOperation |= useBoxConstraint(game);

            return(atLeastOneOperation);
        }
コード例 #26
0
ファイル: SudokuIGameTest.cs プロジェクト: Zilin-Li/Sudoku
        public void SetMaxValue_Test1()
        {
            //test SetMaxValue when maxValue equal 3
            int        expected = 3;
            SudokuGame igame    = new SudokuGame();

            igame.SetMaxValue(3);
            int actual = igame.maxValue;

            Assert.AreEqual(expected, actual);
        }
コード例 #27
0
ファイル: SudokuIGameTest.cs プロジェクト: Zilin-Li/Sudoku
        public void SetSquareHeight2()
        {
            //test with equal 3
            int        expected = 3;
            SudokuGame igame    = new SudokuGame();

            igame.SetSquareHeight(3);
            int actual = igame.squareHeight;

            Assert.AreEqual(expected, actual);
        }
コード例 #28
0
ファイル: SudokuIGameTest.cs プロジェクト: Zilin-Li/Sudoku
        public void SetSquareWidth1()
        {
            //test with equal 2
            int        expected = 2;
            SudokuGame igame    = new SudokuGame();

            igame.SetSquareWidth(2);
            int actual = igame.squareWidth;

            Assert.AreEqual(expected, actual);
        }
コード例 #29
0
        private SudokuGame GetGameSession()
        {
            SudokuGame game = Session["game"] as SudokuGame;

            if (game == null)
            {
                NewGameSession();
                game = Session["game"] as SudokuGame;
            }

            return(game);
        }
コード例 #30
0
        private SudokuGame finalizeGame(SudokuGame game)
        {
            foreach (List <SudokuCell> row in game.GameMatrix)
            {
                foreach (BruteForceAlgoCell cell in row)
                {
                    cell.Value = cell.TempValue;
                }
            }

            return(game);
        }
コード例 #31
0
ファイル: SudokuControl.cs プロジェクト: kubiw/sudoku-Csharp
        public SudokuControl()
        {
            this.game = null;
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.Opaque, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);

            InitializeComponent();

            this.SelectedColor = Color.AliceBlue;
            this.ErrorColor = Color.Red;
            this.DefaultColor = Color.LightGray;
            this.LineColor = Color.LightGray;
            this.ThickLineColor = Color.Bisque;
            this.SelecedBox = new Point();
        }