예제 #1
0
 public Game(GameOptions gameOptions)
 {
     GameOptions = gameOptions;
     Guesses = new Guess[10];
     CurrentGuess = new Guess();
     GuessNumber = 0;
     GameSolution = new Solution();
 }  
예제 #2
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);
        }
예제 #3
0
 public bool isSameNumSpaces(Guess g)
 {
     return(g.getNumSpaces() == this.numSpaces);
 }
예제 #4
0
        private void cluesDGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            // first paint over the submit button
            if (e.RowIndex <= numberOfAllowedGuesses - 1 - guessHistory.Count)
            {
                Brush bg = new SolidBrush(backgroundColor);
                e.Graphics.FillRectangle(bg, e.ClipBounds);
                bg.Dispose();
                e.PaintContent(e.ClipBounds);
                e.Handled = true;
            }
            else if (e.RowIndex > numberOfAllowedGuesses - 1 - guessHistory.Count && guessHistory.Count > 0)
            {
                int    guessHistoryIndex    = numberOfAllowedGuesses - e.RowIndex - 1;
                Guess  guessAtRow           = guessHistory.ElementAt(guessHistoryIndex);
                int    rightColorRightPlace = guessAtRow.getRightColorRightPlace();
                int    rightColorWrongPlace = guessAtRow.getRightColorWrongPlace();
                Color  c         = backgroundColor;
                String direction = "";
                if (e.ColumnIndex < rightColorRightPlace)
                {
                    c         = rightColorRightPlaceMarker;
                    direction = "up";
                }
                else if (numberOfSpaces - e.ColumnIndex <= rightColorWrongPlace)
                {
                    c         = rightColorWrongPlaceMarker;
                    direction = "down";
                }
                else
                {
                    e.Handled = true;
                    return;
                }

                e.Graphics.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                int rectangleLocationX = e.CellBounds.X + 3;
                int rectangleLocationY = e.CellBounds.Y + 3;
                int rectangleWidth     = Math.Min(e.CellBounds.Height, e.CellBounds.Width) - 10;
                int rectangleHeight    = rectangleWidth;
                System.Drawing.Rectangle spotRectangle           = new System.Drawing.Rectangle(rectangleLocationX, rectangleLocationY, rectangleWidth, rectangleHeight);
                System.Drawing.Rectangle gradientBigRectangle    = new System.Drawing.Rectangle(rectangleLocationX, rectangleLocationY, rectangleWidth, rectangleHeight * 3);
                System.Drawing.Rectangle gradientBiggerRectangle = new System.Drawing.Rectangle(rectangleLocationX - 10, rectangleLocationY - 10, rectangleWidth + 20, rectangleHeight * 3);
                float filledSpotGradientAngle = (float)220.0;
                float emptySpotGradientAngle  = (float)180.0;
                float anotherGradientAngle    = (float)90.0;
                System.Drawing.Drawing2D.LinearGradientBrush normalPenBrush = new LinearGradientBrush(gradientBiggerRectangle, Color.LightGray, Color.Black, emptySpotGradientAngle, false);
                System.Drawing.Pen normalPen = new System.Drawing.Pen(normalPenBrush, 1.0f);

                System.Drawing.Drawing2D.LinearGradientBrush br;
                br = new System.Drawing.Drawing2D.LinearGradientBrush(gradientBigRectangle, Color.Black, c, filledSpotGradientAngle, false);
                System.Drawing.Drawing2D.LinearGradientBrush br2;
                br2 = new System.Drawing.Drawing2D.LinearGradientBrush(gradientBigRectangle, Color.LightGray, c, emptySpotGradientAngle, false);
                System.Drawing.Drawing2D.LinearGradientBrush br3;
                br3 = new System.Drawing.Drawing2D.LinearGradientBrush(gradientBigRectangle, Color.White, c, anotherGradientAngle, false);

                Point[]   triangle = DrawTriangle(spotRectangle, direction);
                Point[][] pyramid  = DrawPyramid(spotRectangle, direction);
                e.Graphics.FillPolygon(br, pyramid[0]);
                e.Graphics.FillPolygon(br2, pyramid[1]);
                e.Graphics.FillPolygon(br3, pyramid[2]);
                e.Graphics.DrawPolygon(normalPen, triangle);
                e.PaintContent(e.ClipBounds);
                e.Handled = true;
                normalPen.Dispose();
                br.Dispose();
            }
        }
예제 #5
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();
        }
예제 #6
0
        private void submitGuess()
        {
            checkGuessResult.Text = "";
            submit.Hide();
            cluesDGV.Invalidate();
            // initialize currentGuess
            int guessHistoryRowNumber = numberOfAllowedGuesses - 1 - numberOfGuesses;

            currentGuess = new Guess(numberOfSpaces);
            for (int i = 0; i < numberOfSpaces; i++)
            {
                currentGuess.setColor(i, guessInProgress[i]);
                guessInProgress[i] = backgroundColor;
            }

            // get results of guess
            int[] guessResult          = solution.compare(currentGuess);
            int   rightColorRightPlace = guessResult[0];
            int   rightColorWrongPlace = guessResult[1];

            int j = 0;

            for (int k = 0; k < rightColorRightPlace; k++)
            {
                j++;
            }
            for (int k = 0; k < rightColorWrongPlace; k++)
            {
                j++;
            }
            currentGuess.setRightColorRightPlace(rightColorRightPlace);
            currentGuess.setRightColorWrongPlace(rightColorWrongPlace);

            // update possibleSolutions
            guessHistory.Add(currentGuess);
            List <Guess> toRemove = new List <Guess>();

            int[] results;
            foreach (Guess g in allSolutions)
            {
                results = g.compare(currentGuess);
                if (results[0] != currentGuess.getRightColorRightPlace() || results[1] != currentGuess.getRightColorWrongPlace())
                {
                    toRemove.Add(g);
                }
            }
            foreach (Guess g in toRemove)
            {
                allSolutions.Remove(g);
            }

            numPossibleSolutions = allSolutions.Count();
            label4.Text          = numPossibleSolutions.ToString();

            // end of game
            if (rightColorRightPlace == numberOfSpaces || numberOfGuesses == numberOfAllowedGuesses - 1)
            {
                revealSolution = true;
                solutionDGV.Invalidate();
                cluesDGV.Invalidate();
                submit.Enabled = false;
                if (rightColorRightPlace != numberOfSpaces)
                {
                    MessageBox.Show("Better luck next time.");
                }
                else
                {
                    MessageBox.Show("You win!");
                }
                panel3.Show();
                //submit.Hide();
            }
            else
            {
                numberOfGuesses++;
                selectCell(0, numberOfAllowedGuesses - numberOfGuesses - 1);
                submit.Location = new Point(submit.Location.X, submit.Location.Y - rowHeight);
                submit.Show();
            }
            cluesDGV.Invalidate();
        }
예제 #7
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);
        }