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;
}
}