2 Replies - 3838 Views - Last Post: 22 November 2012 - 05:11 PM Rate Topic: -----

#1 AledRhys92   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-November 12

New to c#, Help needed with rectangle colours

Posted 22 November 2012 - 03:09 PM

Hey, I have created a rock paper scissors game on c#. I have 3 boxes in my main window, They are called - boxrock, boxpaper, boxscissors. and 3 buttons - rock, paper, scissors,
What i want is for the selection that the user has made to appear as green, and the computers choice to be yellow. ie.. change the box colours according to the user/computer's choice. Here is my code so far -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 RockPaperScissors
{
    /// <summary>
    /// Interaction logic for Mainwindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        // Variable declaration
       

      
        double result;
        // Random number generator
        Random generator = new Random();
       
        
      



        private void rock_Click(object sender, RoutedEventArgs e)
        {
               
              
                result = 0;
                result = (int)(generator.NextDouble() * 12) + 1;            
              
                    if (result <= 4)
                    {
                        MessageBox.Show("Looks like a draw!");
                       
                    }

                    else if (result >= 8)
                    {
                       
                        MessageBox.Show("Sorry, the result is paper.");
                    }

                    else
                    {
                       
                        MessageBox.Show("Well done, the result was scissors.");
                    }
               
           
        }
        private void paper_Click(object sender, RoutedEventArgs e)
        {
           
                result = 0;
                result = (int)(generator.NextDouble() * 12) + 1;

          
                    if (result <= 4)
                    {
                        MessageBox.Show("Looks like a draw!");
                    }

                    else if (result >= 8)
                    {
                       
                        MessageBox.Show("Sorry, the result is paper.");
                    }

                    else
                    {
                       
                        MessageBox.Show("Well done, the result was scissors.");
                    }
               


           
        }

        private void scissors_Click(object sender, RoutedEventArgs e)
        {
 
          
                result = 0;
                result = (int)(generator.NextDouble() * 12) + 1;
               

               
              
                    if (result <= 4)
                    {
                        MessageBox.Show("Looks like a draw!");
                    }

                    else if (result >= 8)
                    {
                       
                        MessageBox.Show("Sorry, the result is paper.");
                    }

                    else
                    {
                       
                        MessageBox.Show("Well done, the result was scissors.");
                    }
               
           

        }
    }
       

        }


And here is the code for the main window.

<Window x:Class="RockPaperScissors.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Rectangle Height="100" HorizontalAlignment="Left" Margin="27,32,0,0" Name="boxrock" Stroke="Black" VerticalAlignment="Top" Width="115" Fill="#FF3215EB" />
        <Rectangle Height="100" HorizontalAlignment="Left" Margin="188,32,0,0" Name="boxpaper" Stroke="Black" VerticalAlignment="Top" Width="115" Fill="#FF3215EB" />
        <Rectangle Height="100" HorizontalAlignment="Left" Margin="349,32,0,0" Name="boxsisssors" Stroke="Black" VerticalAlignment="Top" Width="115" Fill="#FF3215EB" />
        <Button Content="rock" Height="23" HorizontalAlignment="Left" Margin="45,138,0,0" Name="rock" VerticalAlignment="Top" Width="75" Click="rock_Click" />
        <Button Content="paper" Height="23" HorizontalAlignment="Left" Margin="210,138,0,0" Name="paper" VerticalAlignment="Top" Width="75" Click="paper_Click" />
        <Button Content="scissors" Height="23" HorizontalAlignment="Left" Margin="371,138,0,0" Name="scissors" VerticalAlignment="Top" Width="75" Click="scissors_Click" />
    </Grid>
</Window>


Thank you,
Aled.

*** Edit ***
Please use code tags when posting code
Posted Image

This post has been edited by GunnerInc: 22 November 2012 - 03:53 PM
Reason for edit:: Added elusive code tags


Is This A Good Question/Topic? 0
  • +

Replies To: New to c#, Help needed with rectangle colours

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: New to c#, Help needed with rectangle colours

Posted 22 November 2012 - 05:05 PM

Are you the same poster as for this thread, or just in the same class?
http://www.dreaminco...issors-wpf-app/

Quote

What i want is for the selection that the user has made to appear as green, and the computers choice to be yellow. ie.. change the box colours according to the user/computer's choice. Here is my code so far -


Ok. So what's your question? What have you actually tried to code to accomplish this? I'm not seeing any attempts in your code.

TIP: Don't replace lines of code that don't work. Instead comment them out and put your new attempts below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

Spoiler

If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: New to c#, Help needed with rectangle colours

Posted 22 November 2012 - 05:11 PM

Next: You have three nearly identical methods, one for each button click, with all the same code in it.

We try to always avoid code repetition. THis would be better served by having one method that takes a parameter such as the player's choice.

Messageboxes? Really? You're making a nice WPF application and you're going to pop up a MessageBox every time the player plays a round? How about a TextBlock under the 3 buttons that shows the result of this game, and maybe a running total of games played?

Take a look at the properties tutorial linked in my signature block. I show how to use a string property as Status or feedback to the user.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1