Exemple #1
0
        private void isGameOver()
        {
            var wrongCells = GameMechanics.findWrongAndEmptyCells(cells);

            //check if inputs are wrong or the player wins
            if (wrongCells[0].Any())
            {
                //Highlight the wrong inputs
                wrongCells[0].ForEach(x => x.ForeColor = Color.Red);
                MessageBox.Show("Wrong inputs");
            }
            else if (!wrongCells[1].Any())
            {
                timer.Stop();
                MessageBox.Show("You win!");
            }
        }
        public static bool findValueForNextCell(int i, int j, GridModel[,] cells)
        {
            //Increment i and j to the next cell.
            if (++j > 8)
            {
                j = 0;

                if (++i > 8)
                {
                    return(true);
                }
            }

            var value    = 0;
            var numsLeft = new List <int> {
                1, 2, 3, 4, 5, 6, 7, 8, 9
            };

            //Find a valid number for the cell and go to the next cell and
            //check if it can be allocated a random number
            do
            {
                //If there's no suitable numbers then change the previous cell.
                if (numsLeft.Count < 1)
                {
                    cells[i, j].value = 0;
                    return(false);
                }

                //Take a random number taken from the list
                value             = numsLeft[GameVariablesModel.random.Next(0, numsLeft.Count)];
                cells[i, j].value = value;

                //Remove allocated value
                numsLeft.Remove(value);
            } while (!GameMechanics.isValidNumber(value, i, j, cells) || !GameMechanics.findValueForNextCell(i, j, cells));

            //Remove line after testing
            //cells[i, j].Text = value.ToString();
            return(true);
        }