0

Edit: included my entire program code at end of post

I am making a deal or no deal game and I have everything working except eliminating the numbers on the sides as they are guessed. Hopefully someone can guide me...

Here is what my GUI looks like

GUI after first round. Box 1 is "my" box, bolded boxes are ones I've already opened

I have a WPF with 2 rich text boxes in it. I have an array of numbers I put half in 1 text box and half in the other. I am displaying the values as currency to fit with the theme of the game.

This is my code for the array and displaying in the text boxes:

//initialize array with dollar values
        displayNumbers = new int[] {0, 1, 5, 10, 25, 50, 75, 100, 200, 300, 400, 500, 750,
            1000, 5000, 10000, 25000, 50000, 75000, 100000, 200000, 300000, 400000, 500000, 750000, 1000000};

        //initialize usedNumbers array with a length
        usedNumbers = new int[displayNumbers.Length];

        //display amounts in 1st textblock. boxSize = 26 (size of array)
        for(int i = 0; i<(boxSize/2); i++)
        {
            textBox1.AppendText(displayNumbers[i].ToString("C0") + " \n");
        }

        //display amounts in 2nd textblock
        for(int i = boxSize/2; i < boxSize; i++)
        {
            textBox2.AppendText(displayNumbers[i].ToString("C0") + "\n");
        }

The way my game currently works is, the array shuffles and no matter what button you press next, the value will be the next randomized value in the array. I display the values on the sides before I shuffled them.

How can I take the current value of the index I am at in the array and find that number in my rich text boxes and change the color or the background, etc. so the user knows that value is off the board?

I'm already using the button's content to display the value that was associated with that button, but I'm supposed to also eliminate the value from the side boxes.

From what I've found online, I think pointers might be my answer? But I don't know anything about how to use those yet...

I'm still a beginner, so if this can be answered in beginner's terms, I'd really appreciate it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DealorNoDeal
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        int[] displayNumbers;
        int[] usedNumbers;
        Random random = new Random();
        int boxSize = 26;
        Button button;
        int turns = 0;
        int boxesToChoose;
        int boxesLeft = 26;
        int bankOffer;
        bool isOffer = false;
        bool deal = false;
        private Button firstChoice;
        int round = 0;

        public MainWindow()
        {
            InitializeComponent();

            //initialize array with dollar values
            displayNumbers = new int[] {0, 1, 5, 10, 25, 50, 75, 100, 200, 300, 400, 500, 750,
                1000, 5000, 10000, 25000, 50000, 75000, 100000, 200000, 300000, 400000, 500000, 750000, 1000000};

            //initialize usedNumbers array with a length
            usedNumbers = new int[displayNumbers.Length];

            //display amounts in 1st textblock
            for(int i = 0; i<(boxSize/2); i++)
            {
                textBox1.AppendText(displayNumbers[i].ToString("C0") + " \n");
            }

            //display amounts in 2nd textblock
            for(int i = boxSize/2; i < boxSize; i++)
            {
                textBox2.AppendText(displayNumbers[i].ToString("C0") + "\n");
            }

            //deal/nodeal buttons disabled on startup only enabled when deal is offered
            buttonDeal.IsEnabled = false;
            buttonNoDeal.IsEnabled = false;

            //randomize dollar values for boxes now that they have been displayed in order
            ShuffleDecks();
        }

        private void ShuffleDecks()
        {
            //fisher-yates shuffle algorithm
            int index;
            string temp;

            for (int i = 0; i < boxSize; i++)
            {
                index = random.Next(boxSize);
                temp = Convert.ToString(displayNumbers[i]);
                displayNumbers[i] = displayNumbers[index];
                displayNumbers[index] = Convert.ToInt32(temp);
            }
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            button = sender as Button;
            if (deal == true)
            {
                if (bankOffer > displayNumbers[0])
                {
                    textBlockDisplay.Text = "You made a good deal! Your winnings: " + bankOffer.ToString("C0");
                }
                else
                {
                    textBlockDisplay.Text = "Better luck next time! Your winnings: " + bankOffer.ToString("C0");
                }

                firstChoice.Content = displayNumbers[0].ToString("C0");
                firstChoice.IsEnabled = false;
            }

            else
            {
                if (isOffer == false)
                {
                    //first turn is to pick a box, not opening a box
                    if (turns == 0)
                    {
                        button.IsEnabled = false;
                        firstChoice = button;
                        boxesToChoose = BoxesToOpen();
                        textBlockDisplay.Text = "Now choose " + boxesToChoose + " box(es)";
                    }

                    //opening boxes
                    else
                    {
                        button.FontWeight = FontWeights.Bold;
                        button.Content = displayNumbers[turns].ToString("C0");
                        usedNumbers[turns] = displayNumbers[turns];
                        boxesToChoose--;
                        textBlockDisplay.Text = "Now choose " + boxesToChoose + " box(es)";
                        //FindText(textBox1,Convert.ToString(button.Content),Brushes.Red);
                    }
                    boxesLeft--;
                    CheckTurns();
                    turns++;
                }
            }
        }

        //see what turn we are on and make an offer
        private void CheckTurns()
        {
            if(turns == 6 || turns == 11 || turns == 15 || turns == 18 || turns == 20 || turns == 21 || turns == 22 || turns == 23 || turns == 24 || turns == 25)
            {
                //enable deal/nodeal buttons
                buttonDeal.IsEnabled = true;
                buttonNoDeal.IsEnabled = true;

                isOffer = true; //disable the other boxes you haven't opened yet

                //calculate the offer
                bankOffer = CalculateOffer(boxesLeft);

                textBlockDisplay.Text = "The bank offers you: " + bankOffer.ToString("C0") +" Deal?";
                round++;
            }
        }

        private void buttonDeal_Click(object sender, RoutedEventArgs e)
        {
            if (turns < 25)

            {
                deal = true;
                GreyAllBoxes();
                firstChoice.IsEnabled = true;
                textBlockDisplay.Text = "Game Over! You walk away with " + bankOffer.ToString("C0") + " Click your first box to see if you made a good deal.";
            }
            else
            {
                textBlockDisplay.Text = "Game Over! You walk away with " + bankOffer.ToString("C0") + " Click your first box to see if you made a good deal.";
            }
        }

        private void buttonNoDeal_Click(object sender, RoutedEventArgs e)
        {
            if (turns < 25)
            {
                isOffer = false;
                boxesToChoose = BoxesToOpen();
                textBlockDisplay.Text = "Now choose " + boxesToChoose + " box(es)";
            }
            else
            {
                //stick with first box or take last box
                textBlockDisplay.Text = "Do you choose your 1st box, or " + bankOffer.ToString("C0") + "?";

                buttonDeal.Content = bankOffer.ToString("C0");
                buttonNoDeal.IsEnabled = false;

                firstChoice.IsEnabled = true;
            }
        }

        private int CalculateOffer(int boxes)
        {
            int offer;
            int mainSum = displayNumbers.Sum();
            int usedSum = usedNumbers.Sum();

            //offer = sum of boxes left, divided by number of boxes left
            offer = (mainSum - usedSum)/boxes;

            return offer;
        }

        private void GreyAllBoxes()
        {
            buttonDeal.IsEnabled = false;
            buttonNoDeal.IsEnabled = false;
            button1.IsEnabled = false;
            button2.IsEnabled = false;
            button3.IsEnabled = false;
            button4.IsEnabled = false;
            button5.IsEnabled = false;
            button6.IsEnabled = false;
            button7.IsEnabled = false;
            button8.IsEnabled = false;
            button9.IsEnabled = false;
            button10.IsEnabled = false;
            button11.IsEnabled = false;
            button12.IsEnabled = false;
            button13.IsEnabled = false;
            button14.IsEnabled = false;
            button15.IsEnabled = false;
            button16.IsEnabled = false;
            button17.IsEnabled = false;
            button18.IsEnabled = false;
            button19.IsEnabled = false;
            button20.IsEnabled = false;
            button21.IsEnabled = false;
            button22.IsEnabled = false;
            button23.IsEnabled = false;
            button24.IsEnabled = false;
            button25.IsEnabled = false;
            button26.IsEnabled = false;
        }

        private void UnGreyAllBoxes()
        {
            button1.IsEnabled = true;
            button2.IsEnabled = true;
            button3.IsEnabled = true;
            button4.IsEnabled = true;
            button5.IsEnabled = true;
            button6.IsEnabled = true;
            button7.IsEnabled = true;
            button8.IsEnabled = true;
            button9.IsEnabled = true;
            button10.IsEnabled = true;
            button11.IsEnabled = true;
            button12.IsEnabled = true;
            button13.IsEnabled = true;
            button14.IsEnabled = true;
            button15.IsEnabled = true;
            button16.IsEnabled = true;
            button17.IsEnabled = true;
            button18.IsEnabled = true;
            button19.IsEnabled = true;
            button20.IsEnabled = true;
            button21.IsEnabled = true;
            button22.IsEnabled = true;
            button23.IsEnabled = true;
            button24.IsEnabled = true;
            button25.IsEnabled = true;
            button26.IsEnabled = true;
        }

        private int BoxesToOpen()
        {
            if (round == 0)
            {
                boxesToChoose = 6;
            }
            else if(round == 1)
            {
                boxesToChoose = 5;
            }
            else if (round == 2)
            {
                boxesToChoose = 4;
            }
            else if (round == 3)
            {
                boxesToChoose = 3;
            }
            else if(round == 4)
            {
                boxesToChoose = 2;
            }
            else
            {
                boxesToChoose = 1;
            }

            return boxesToChoose;
        }
    }
ayocom
  • 3
  • 2
  • can u tell what exactly you wanted to extract from your rich text box? is that the number which says 143,066 or something else ?? – Mohit S Jan 03 '17 at 01:58
  • It is one of the values in my array. They are randomized, so I don't know what number it will be. Basically whatever displayNumbers[i] currently is. – ayocom Jan 03 '17 at 03:52
  • Show the code where you are trying to extract the value? – Mohit S Jan 03 '17 at 04:20
  • This is essentially what I want to do, but this is code from a form, not a WPF. Apparently richTextBoxes don't have a .Find option in WPF? *sorry, couldn't figure out code display in comments... private void ShowAmountsUsed() { for (int i = 0; i < amounts.Length / 2; i++) { richTextBox1.Find(amounts[i].ToString("C0") + " "); richTextBox1.SelectionColor = amountsUsed[i] ? Color.Gray : Color.DarkBlue; } richTextBox1.SelectionLength = 0; } – ayocom Jan 03 '17 at 04:35

1 Answers1

0

Answer to your question requires multiple chunks of code. Since you have not shown much of the part, I am just guiding you with suggestions and references to accomplish the task.

  1. You don't require to keep track of index, as the button has the amount i.e. text that you need to search in richtextbox and process.

  2. Remove the selected amount from text box.

    a. On button click, first search the amount "$" + searchAmount + " \n" in the rich text box. Refer the solution at link1 or link2. Search the textboxes one after the other. If found in 1st no need to seacrh in second textbox.

    b. Select the searched amount and decorate it.

    c. My suggestion is decorate it with StrikeThrough rather than removing it from text box. You may highlight is with Red color too. Deletion of the selected text is simple use textBox1.SelectedText = "";. Red color highlighting can be done with textBox1.SelectionColor = Color.Red;. For strikethrough, you have to use TextRange and TextDecorationCollection. Refer the link3.

Community
  • 1
  • 1
Mukul Varshney
  • 3,131
  • 1
  • 12
  • 19
  • is there a library that needs to be included with WPF for the .Find method? When I try to type "textBox1.Find" I always get an error because it isn't one of the options that intellisense gives me. It only seems to be included in the form. The example in link1 you gave me is exactly what I want to do. – ayocom Jan 03 '17 at 04:59
  • You have to write your own function to find the button text in textbox text. Refer http://stackoverflow.com/questions/22229741/wpf-richtextbox-application-find-text-spanning-multiple-runs. See how its done. – Mukul Varshney Jan 03 '17 at 05:08