Advertisement
Dimencia

Untitled

Jan 15th, 2021
717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  1.  
  2.  
  3. private Button _blankButton;
  4. private Button[] _originalButtons = buttonPanel.Controls.OfType<Button>(); // Store an un-randomized list of them in order
  5. // For checking results later
  6.  
  7.  
  8. public void form_load() { // Or whatever it's called
  9.     var numbers = Enumerable.Range(1, 8).OrderBy(x => Guid.NewGuid()).ToList(); // Create/Randomize the number list
  10.     var buttons = _originalButtons.OrderBy(x => Guid.NewGuid()).ToList(); // Randomize the button list
  11.     for (var i = 0; i < buttons.Count-1; i++)
  12.     {
  13.         var button = buttons[i];
  14.         button.Text += numbers[i];
  15.         button.Tag = i; // Tag each button to track what position it has in the array
  16.     }
  17.     _blankButton = buttons[buttons.Count-1]; // Save the blankButton for what it is
  18.     _blankButton.Text = " "; // Give it a value so we can treat each of them as a char
  19.     _blankButton.Tag = buttons.Count-1; // Though they all have to get tagged because it could have a number later
  20.  
  21. }
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. button_click(object sender, EventArgs e) // Assign this same event to all of the buttons in the Designer
  32. {
  33.     Button clicked = sender as Button;
  34.     if(!string.IsNullOrWhiteSpace(clicked.Text)) {
  35.         // They clicked a button other than the blank one
  36.         // See if the blank one is adjacent
  37.         // It could be either at buttons[i-1], [i+1], [i-3] or [i+3]
  38.         int startPosition = int.Parse(clicked.Tag); // Remember we stored its array position in Tag earlier
  39.         int blankPosition = int.Parse(_blankButton.Tag);
  40.         // Check our 4 possible positions
  41.         if (blankPosition == startPosition - 1 || blankPosition == startPosition - 3 || blankPosition == startPosition + 1 || blankPosition == startPosition + 3) {
  42.             // They have clicked a number that is adjacent to the blank tile
  43.             // Swap the text from one they clicked with the blank tile.
  44.             _blankButton.Text = clicked.Text;
  45.             clicked.Text = " "; // Must have a space value so we can treat it as a char
  46.             _blankButton = clicked;
  47.            
  48.             // And then check if they have won
  49.             // Do they win only if the blank is in the top left, or can the blank be anywhere?
  50.             // Assuming top left only
  51.            
  52.             // We actually don't have to cast them from strings, they will compare properly with their ascii char values
  53.             // And null, space, etc are all sorted before the numbers in ASCII too
  54.            
  55.             int lastValue = 128;
  56.             bool success = true;
  57.             foreach(var button in _originalButtons) { // Iterate the buttons in their original order (reverse order)
  58.                 int value = (int)button.Text[0];
  59.                 if(value > lastValue)
  60.                 { // If any number on the buttons, in reverse, is higher than a previous one, it's not in order
  61.                     success = false;
  62.                     break;
  63.                 }
  64.                 else
  65.                     lastValue = value;
  66.                 // If all of them are lower than their previous ones, it's in order and success stays true
  67.             }
  68.            
  69.             if (success) {
  70.                 // Do whatever you want to do when they win
  71.             }
  72.         }
  73.  
  74.        
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement