Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am building a windows phone 7 application. I have two groups of three buttons p1b1,p1b2,p1b3 & p2b1,p2b2,p2b3 I am loading the images blue.png, red.png, yellow.png on a random basis to the buttons. Each button in a group should have different colors (i.e p1b1,p1b2,p1b3 should have three different colors). I have written the code in the following way, but when i call the ResetForNextRound() to repaint the buttons, some buttons do not change their background. What can be the problem. Am I not storing the Images in a proper way? Please Suggest.

C#
 public partial class GamePage : PhoneApplicationPage
    {
        IList<ImageBrush> images;
        IList<ImageBrush> images2;
        Random rand = new Random(DateTime.Now.Millisecond);
        Uri[] uri = new Uri[3];
        IList p1numlist;
        IList p2numlist;
        int imageindex;
        int p1score;
        int p2score;
        int roundnumber=1;
        public GamePage()
        {
            InitializeComponent();
            LoadImages();
            image2.Visibility = Visibility.Collapsed;
            image3.Visibility = Visibility.Collapsed;
            PaintButtons();
        }

        public void LoadImages()
        {

            images = new List<ImageBrush>();
            images2 = new List<ImageBrush>();
            uri[0] = new Uri("/BtnPics/Blue.png", UriKind.Relative);
            uri[1] = new Uri("/BtnPics/Red.png", UriKind.Relative);
            uri[2] = new Uri("/BtnPics/Yellow.png", UriKind.Relative);
            
            foreach (Uri u in uri)
            {
                BitmapImage bmp = new BitmapImage(u);
                images.Add(new ImageBrush() { ImageSource = bmp });
                images2.Add(new ImageBrush() { ImageSource = bmp });
            }
        }
        
        public void PaintButtons()
        {
// This is where I am assigning the Background for the buttons. I think this part should be considered.
//When the Backgrounds are sequentially assigned, some of them fail to assign and remain with previous values. I have no idea why this is happening.
            p1numlist = GenerateRandom();
            p2numlist = GenerateRandom();

            p1b1.Background = images[(int)p1numlist[0]];
            p1b2.Background = images[(int)p1numlist[1]]; 
            p1b3.Background = images[(int)p1numlist[2]];
            p2b1.Background = images2[(int)p2numlist[0]];
            p2b2.Background = images2[(int)p2numlist[1]];
            p2b3.Background = images2[(int)p2numlist[2]];
            imageindex = rand.Next(3);
            image1.Source = new BitmapImage(uri[imageindex]);

            
        }
        public List<int> GenerateRandom()
        {
            
            List<int> numlist = new List<int>();
            numlist.Add(rand.Next(3));
            int count = 0;
            do
            {
                int num = rand.Next(3);
                if (!numlist.Contains(num))
                {
                    numlist.Add(num);
                    count++;
                }

            } while (count < 2);
            return numlist;
        }
        bool PlayerOne = false;
        bool PlayerTwo = false;
        bool SomeoneScored=false;
        public void ProcessPress(object sender)
        {
            Button b = sender as Button;
            if (b.Name.StartsWith("p1"))
            {
                PlayerOne = true;
                DisableButtonsExceptPressed(sender);
            }
            if(b.Name.StartsWith("p2"))
            { 
                PlayerTwo = true;
                DisableButtonsExceptPressed(sender);
            }
            MatchImage(sender);
        }
        public void DisableButtonsExceptPressed(object sender)
        {
            Button b = sender as Button;

            if (b.Name.StartsWith("p1"))
            {
                p1b1.IsEnabled = false;
                p1b2.IsEnabled = false;
                p1b3.IsEnabled = false;
                b.IsEnabled = true;
            }
            if(b.Name.StartsWith("p2"))
            {
                p2b1.IsEnabled = false;
                p2b2.IsEnabled = false;
                p2b3.IsEnabled = false;
                b.IsEnabled = true;
            }
            
        }

        public void MatchImage(object sender)
        {
            
            Button b = sender as Button;
            int buttonnumber = (int)(b.Name[3]);
            
            if (b.Name.StartsWith("p1"))
                if (imageindex == (int)p1numlist[buttonnumber-49])
                {
                        p1score++;//matched for player one
                        image2.Visibility = Visibility.Visible;
                        SomeoneScored = true;
                        DisableAllButtons(); //disable all buttons
                }
            if(b.Name.StartsWith("p2"))
                if (imageindex == (int)p2numlist[buttonnumber-49])
                {
                        p2score++;//matched for player two
                        image3.Visibility = Visibility.Visible;
                        SomeoneScored = true;
                        DisableAllButtons(); //disable all buttons
                }
            if ((PlayerOne == true && PlayerTwo == true)||SomeoneScored==true)
            {
                roundnumber++;
                ResetForNextRound();
                
            }
            
        }
        public void DisableAllButtons()
        {
            p1b1.IsEnabled = false;
            p1b2.IsEnabled = false;
            p1b3.IsEnabled = false;
            p2b1.IsEnabled = false;
            p2b2.IsEnabled = false;
            p2b3.IsEnabled = false;
            //DisplayWhoScored(sender);
        }
public void ResetForNextRound()
        {
            if (roundnumber <= 10)
            {
                //SetNullBackground();
                EnableAllButtons();
                LoadImages();
                PaintButtons();
                p1Scorelabel.Text = p1score.ToString();
                p2Scorelabel.Text = p2score.ToString();
                p1Roundlabel.Text = roundnumber.ToString();
                p2Roundlabel.Text = roundnumber.ToString() ;
                image2.Visibility = Visibility.Collapsed;
                image3.Visibility = Visibility.Collapsed;
                PlayerOne = false;
                PlayerTwo = false;
                SomeoneScored = false;
            }
           
                
        }
                
        public void EnableAllButtons()
        {
            p1b1.IsEnabled = true;
            p1b2.IsEnabled = true;
            p1b3.IsEnabled = true;
            p2b1.IsEnabled = true;
            p2b2.IsEnabled = true;
            p2b3.IsEnabled = true;
        }

//ignore the following part.
        private void p1b1_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            ProcessPress(sender);
        }

        private void p1b2_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            ProcessPress(sender);
        }

        private void p1b3_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            ProcessPress(sender);
        }

        private void p2b1_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            ProcessPress(sender);
        }

        private void p2b2_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            ProcessPress(sender);
        }

        private void p2b3_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            ProcessPress(sender);
        }

    }</int></int></int>


Maybe I am dumping a whole lot of code here, but I wasn't sure which part is actually causing the problem. Sorry for the trouble.
Posted
Updated 29-Oct-11 2:21am
v2
Comments
Mark Salsbery 29-Oct-11 12:00pm    
The only thing I see weird is p1numlist and p2numlist being IList objects instead of List<int> objects so you don't need to cast to int everywhere, but that wouldn't break anything...
VigneshPT 29-Oct-11 12:45pm    
Hm. That doesnt seem to disturb anything. I am pissed off with this thing. No conceptual error but still not working. What can it be? Link to .cs file

Are you sure that the buttons' backgrounds aren't changed? Maybe they are changed but, to ImageBrushes with the same image.


Since you use Random for generating the images' indices, maybe you got the same indices for the buttons. Try to store the current index for each button and, check that the new indices are differnet from the old indices.

 
Share this answer
 
Comments
hzawary 30-Oct-11 7:52am    
I also had the same opinion.
I tested the codes by a small change in the same using C# WPF, but don't saw a problem.
VigneshPT 30-Oct-11 7:56am    
There is no problem with random number generation. I have checked this by adding watches. There is a problem with the assignment to <Button>.Background for some reason. Most of the times it is not being assigned to the values on the RHS.
VigneshPT 30-Oct-11 8:13am    
Please have a look at this, then you will understand what the problem I am trying to describe here. Link
Shmuel Zang 31-Oct-11 3:26am    
Can you post the style of the Button?
VigneshPT 1-Nov-11 2:43am    
Style is "Default".
and the XAML code <Button Canvas.Left="-12" Canvas.Top="698" Height="122" Name="p1b1" TabIndex="1" Width="190" Background="White" BorderBrush="Transparent" HorizontalContentAlignment="Stretch" BorderThickness="0" Click="p1b1_Click" ClickMode="Press" />
Problem solved in my other post. Problem when assigning Background to Button control[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900