Ejemplo n.º 1
0
        public int[] compare(Guess g)
        {
            int[] result = new int[2];
            result[0] = 0;
            result[1] = 0;
            if (g.getNumSpaces() != this.numSpaces)
            {
                MessageBox.Show("comparing two guess with different numbers of spaces.");
                return(result);
            }

            // count same color same place
            for (int i = 0; i < numSpaces; i++)
            {
                if (g.getColor(i) == getColor(i))
                {
                    result[0] = result[0] + 1;
                }
            }

            // count same color different place
            // for this we need to get the intersection of the two color arrays
            // keeping duplicates
            // got this from http://stackoverflow.com/questions/5011948/how-do-i-do-an-integer-list-intersection-while-keeping-duplicates
            ILookup <System.Drawing.Color, System.Drawing.Color> lookup1 = colors.ToLookup(i => i);
            List <System.Drawing.Color> tempOther = new List <System.Drawing.Color>();

            for (int i = 0; i < numSpaces; i++)
            {
                tempOther.Add(g.getColor(i));
            }
            ILookup <System.Drawing.Color, System.Drawing.Color> lookup2 = tempOther.ToLookup(i => i);

            System.Drawing.Color[] intersection = (
                from group1 in lookup1
                let group2 = lookup2[group1.Key]
                             where group2.Any()
                             let smallerGroup = group1.Count() < group2.Count() ? group1 : group2
                                                from i in smallerGroup
                                                select i
                ).ToArray();
            result[1] = intersection.Count() - result[0];
            return(result);
        }
Ejemplo n.º 2
0
        private void solutionDataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            e.Graphics.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            int   ellipseLocationX       = e.CellBounds.X + 6;
            int   ellipseLocationY       = e.CellBounds.Y + 6;
            int   ellipseWidth           = Math.Min(e.CellBounds.Height, e.CellBounds.Width) - 10;
            int   ellipseHeight          = ellipseWidth;
            float emptySpotGradientAngle = (float)70.0;

            System.Drawing.Rectangle spotEllipseRectangle = new System.Drawing.Rectangle(ellipseLocationX, ellipseLocationY, ellipseWidth, ellipseHeight);
            System.Drawing.Drawing2D.LinearGradientBrush normalPenBrush = new LinearGradientBrush(spotEllipseRectangle, Color.PaleGoldenrod, Color.Black, emptySpotGradientAngle, false);
            System.Drawing.Pen normalPen = new System.Drawing.Pen(normalPenBrush, 4.0f);
            System.Drawing.Drawing2D.LinearGradientBrush br;

            if (revealSolution)
            {
                float filledSpotGradientAngle = (float)220.0;
                System.Drawing.Rectangle gradientBigRectangle = new System.Drawing.Rectangle(ellipseLocationX, ellipseLocationY, ellipseWidth, ellipseHeight * 2);
                Color c = solution.getColor(e.ColumnIndex);
                br = new System.Drawing.Drawing2D.LinearGradientBrush(gradientBigRectangle, Color.Black, c, filledSpotGradientAngle, false);
                e.Graphics.FillEllipse(br, spotEllipseRectangle);

                //  hilite ellipse = based on http://www3.telus.net/ryanfransen/article_glassspheres.html
                int       r3w = Convert.ToInt16(spotEllipseRectangle.Width * 0.5);
                int       r3h = Convert.ToInt16(spotEllipseRectangle.Height * 0.3);
                Rectangle r3  = new Rectangle(
                    new Point(spotEllipseRectangle.Location.X + (spotEllipseRectangle.Width / 4), spotEllipseRectangle.Location.Y + 2),
                    new Size(r3w, r3h));
                LinearGradientBrush br2 = new LinearGradientBrush(r3, Color.White, Color.Transparent, 90);
                br2.WrapMode = WrapMode.TileFlipX;
                e.Graphics.FillEllipse(br2, r3);
                br2.Dispose();
            }
            else
            {
                br = new System.Drawing.Drawing2D.LinearGradientBrush(spotEllipseRectangle, Color.Black, Color.PaleGoldenrod, emptySpotGradientAngle, false);
                e.Graphics.FillEllipse(br, spotEllipseRectangle);
            }
            e.Graphics.DrawEllipse(normalPen, spotEllipseRectangle);
            e.PaintContent(e.ClipBounds);
            e.Handled = true;
            br.Dispose();
            normalPen.Dispose();
            normalPenBrush.Dispose();
        }
Ejemplo n.º 3
0
        public bool isSame(Guess g)
        {
            if (g.getNumSpaces() != this.numSpaces)
            {
                return(false);
            }
            bool sameColors = true;

            for (int i = 0; i < numSpaces; i++)
            {
                if (g.getColor(i) != this.getColor(i))
                {
                    sameColors = false;
                }
            }
            return(sameColors);
        }
Ejemplo n.º 4
0
        private void guessHistoryDGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            e.Graphics.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            int ellipseLocationX = e.CellBounds.X + 6;
            int ellipseLocationY = e.CellBounds.Y + 6;
            int ellipseWidth     = Math.Min(e.CellBounds.Height, e.CellBounds.Width) - 10;
            int ellipseHeight    = ellipseWidth;

            System.Drawing.Rectangle spotEllipseRectangle = new System.Drawing.Rectangle(ellipseLocationX, ellipseLocationY, ellipseWidth, ellipseHeight);
            System.Drawing.Rectangle gradientBigRectangle = new System.Drawing.Rectangle(ellipseLocationX, ellipseLocationY, ellipseWidth, ellipseHeight * 2);
            float filledSpotGradientAngle = (float)220.0;
            float emptySpotGradientAngle  = (float)70.0;

            System.Drawing.Drawing2D.LinearGradientBrush highlightPenBrush = new LinearGradientBrush(gradientBigRectangle, Color.DarkGray, selectionBackgroundColor, emptySpotGradientAngle, false);
            System.Drawing.Drawing2D.LinearGradientBrush normalPenBrush    = new LinearGradientBrush(spotEllipseRectangle, Color.LightGray, Color.Black, emptySpotGradientAngle, false);
            System.Drawing.Pen normalPen    = new System.Drawing.Pen(normalPenBrush, 4.0f);
            System.Drawing.Pen highlightPen = new System.Drawing.Pen(highlightPenBrush, 4.0f);

            System.Drawing.Drawing2D.LinearGradientBrush br;

            // empty spots above the current guess
            if (e.RowIndex < numberOfAllowedGuesses - 1 - numberOfGuesses)
            {
                br = new System.Drawing.Drawing2D.LinearGradientBrush(spotEllipseRectangle, Color.Black, Color.LightGray, emptySpotGradientAngle, false);
                e.Graphics.FillEllipse(br, spotEllipseRectangle);
            }
            // current guess
            else if (e.RowIndex == numberOfAllowedGuesses - 1 - numberOfGuesses)
            {
                // player has already chosen a color in this spot of the current guess
                if (guessInProgress[e.ColumnIndex] != backgroundColor)
                {
                    Color c = guessInProgress[e.ColumnIndex];
                    br = new System.Drawing.Drawing2D.LinearGradientBrush(gradientBigRectangle, Color.Black, c, filledSpotGradientAngle, false);
                    e.Graphics.FillEllipse(br, spotEllipseRectangle);
                    //  hilite ellipse = based on http://www3.telus.net/ryanfransen/article_glassspheres.html
                    int       r3w = Convert.ToInt16(spotEllipseRectangle.Width * 0.5);
                    int       r3h = Convert.ToInt16(spotEllipseRectangle.Height * 0.3);
                    Rectangle r3  = new Rectangle(
                        new Point(spotEllipseRectangle.Location.X + (spotEllipseRectangle.Width / 4), spotEllipseRectangle.Location.Y + 2),
                        new Size(r3w, r3h));
                    LinearGradientBrush br2 = new LinearGradientBrush(r3, Color.White, Color.Transparent, 90);
                    br2.WrapMode = WrapMode.TileFlipX;
                    e.Graphics.FillEllipse(br2, r3);
                    br2.Dispose();
                }
                // player has not chosen a color, so this spot is the same as the empty rows above
                else
                {
                    br = new System.Drawing.Drawing2D.LinearGradientBrush(spotEllipseRectangle, Color.Black, Color.LightGray, emptySpotGradientAngle, false);
                    e.Graphics.FillEllipse(br, spotEllipseRectangle);
                }
            }
            // previous guesses: fill color based on guess history
            else
            {
                int   guessHistoryIndex = numberOfAllowedGuesses - e.RowIndex - 1;
                Guess guessAtRow        = guessHistory.ElementAt(guessHistoryIndex);
                Color guessColor        = guessAtRow.getColor(e.ColumnIndex);
                br = new System.Drawing.Drawing2D.LinearGradientBrush(gradientBigRectangle, Color.Black, guessColor, filledSpotGradientAngle, false);
                e.Graphics.FillEllipse(br, spotEllipseRectangle);

                //  hilite ellipse = based on http://www3.telus.net/ryanfransen/article_glassspheres.html
                int       r3w = Convert.ToInt16(spotEllipseRectangle.Width * 0.5);
                int       r3h = Convert.ToInt16(spotEllipseRectangle.Height * 0.3);
                Rectangle r3  = new Rectangle(
                    new Point(spotEllipseRectangle.Location.X + (spotEllipseRectangle.Width / 4), spotEllipseRectangle.Location.Y + 2),
                    new Size(r3w, r3h));
                LinearGradientBrush br2 = new LinearGradientBrush(r3, Color.White, Color.Transparent, 90);
                br2.WrapMode = WrapMode.TileFlipX;
                e.Graphics.FillEllipse(br2, r3);
                br2.Dispose();
            }
            // add the external ring
            e.Graphics.DrawEllipse(normalPen, spotEllipseRectangle);
            if (e.ColumnIndex == selectedCell.ColumnIndex && e.RowIndex == selectedCell.RowIndex)
            {
                e.Graphics.DrawEllipse(highlightPen, spotEllipseRectangle);
            }
            e.PaintContent(e.ClipBounds);
            e.Handled = true;
            normalPen.Dispose();
            highlightPen.Dispose();
            br.Dispose();
        }
Ejemplo n.º 5
0
        private void newGame()
        {
            numberOfGuesses = 0;
            revealSolution  = false;
            solutionDGV.Hide();
            colorDataGridView.Hide();
            guessHistoryDGV.Hide();
            cluesDGV.Hide();

            guessHistoryDGV.RowCount    = numberOfAllowedGuesses;
            guessHistoryDGV.ColumnCount = numberOfSpaces;
            foreach (DataGridViewColumn c in guessHistoryDGV.Columns)
            {
                c.Width = guessHistoryDGV.Width / guessHistoryDGV.ColumnCount;
            }
            foreach (DataGridViewRow r in guessHistoryDGV.Rows)
            {
                r.Height = guessHistoryDGV.Height / guessHistoryDGV.RowCount;
            }
            rowHeight = guessHistoryDGV.Height / guessHistoryDGV.RowCount;

            cluesDGV.RowCount    = numberOfAllowedGuesses;
            cluesDGV.ColumnCount = numberOfSpaces;
            foreach (DataGridViewColumn c in cluesDGV.Columns)
            {
                c.Width = cluesDGV.Width / cluesDGV.ColumnCount;
                for (int i = 0; i < numberOfAllowedGuesses; i++)
                {
                    cluesDGV[c.Index, i].Style.BackColor          = backgroundColor;
                    cluesDGV[c.Index, i].Style.SelectionBackColor = backgroundColor;
                }
            }
            foreach (DataGridViewRow r in cluesDGV.Rows)
            {
                r.Height = cluesDGV.Height / cluesDGV.RowCount;
            }

            solutionDGV.RowCount    = 1;
            solutionDGV.ColumnCount = numberOfSpaces;
            foreach (DataGridViewColumn c in solutionDGV.Columns)
            {
                c.Width = solutionDGV.Width / solutionDGV.ColumnCount;
            }
            foreach (DataGridViewRow r in solutionDGV.Rows)
            {
                r.Height = solutionDGV.Height / solutionDGV.RowCount;
            }

            // initialize the list of possible colors and guess history grid
            guessHistory = new List <Guess>();

            colorDataGridView.RowCount    = 1;
            colorDataGridView.ColumnCount = numberOfColours;
            foreach (DataGridViewColumn c in colorDataGridView.Columns)
            {
                c.Width = colorDataGridView.Width / colorDataGridView.ColumnCount;
            }
            foreach (DataGridViewRow r in colorDataGridView.Rows)
            {
                r.Height = colorDataGridView.Height / colorDataGridView.RowCount;
            }

            guessInProgress = new System.Drawing.Color[numberOfSpaces];
            for (int i = 0; i < numberOfSpaces; i++)
            {
                guessInProgress[i] = backgroundColor;
            }

            // initialize the list of possible solutions
            allSolutions = new List <Guess>();
            int factor;

            //int current_guess_index = 0;
            for (int i = 0; i < totalSolutionsWithRepeats; i++)
            {
                Guess newGuess = new Guess(numberOfSpaces);
                factor = 1;
                for (int j = numberOfSpaces - 1; j >= 0; j--)
                {
                    double d           = i / factor;
                    int    mod         = Convert.ToInt32(Math.Floor(d));
                    int    color_index = mod % numberOfColours;
                    if (!repeatsAllowed)
                    {
                        for (int k = numberOfSpaces - 1; k > j; k--)
                        {
                            // need to go to next i because this color already exists in this guess
                            if (newGuess.getColor(k) == colorArray[color_index])
                            {
                                goto newI;
                            }
                        }
                    }
                    newGuess.setColor(j, colorArray[color_index]);
                    factor = factor * numberOfColours;
                }
                allSolutions.Add(newGuess);
                newI :; // skipped combinations (due to repeats not being allowed) jump to here
            }

            // pick one of the solutions as The Solution
            int solutionIndex = rnd.Next(0, totalSolutions);

            solution = allSolutions.ElementAt(solutionIndex);

            // show and hide the appropriate items
            label4.Text     = label2.Text;
            submit.Enabled  = true;
            submit.Location = new Point(submit.Location.X, cluesDGV.Location.Y + cluesDGV.Height - rowHeight);
            submit.Show();
            solutionDGV.Show();
            showSolutions.Show();
            repeatsAllowedDisplayCheckbox.Show();
            panel3.Hide();
            checkGuessButton.Show();
            checkGuessResult.Show();
            cluesDGV.Show();
            guessHistoryDGV.Show();
            colorDataGridView.Show();
            selectCell(0, numberOfAllowedGuesses - 1);
        }