Advertisement
franklinbitencourt

Jogo da velha

Jan 15th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices.WindowsRuntime;
  6. using Windows.Foundation;
  7. using Windows.Foundation.Collections;
  8. using Windows.UI;
  9. using Windows.UI.Xaml;
  10. using Windows.UI.Xaml.Controls;
  11. using Windows.UI.Xaml.Controls.Primitives;
  12. using Windows.UI.Xaml.Data;
  13. using Windows.UI.Xaml.Input;
  14. using Windows.UI.Xaml.Media;
  15. using Windows.UI.Xaml.Navigation;
  16. using System.ComponentModel;
  17. using Windows.Phone.UI.Input;
  18. using Windows.UI.Popups;
  19.  
  20. namespace teste_Jogo_da_velha
  21. {
  22.     public sealed partial class MainPage : Page, INotifyPropertyChanged
  23.     {
  24.         private Button[] _buttonArray;
  25.         private bool _isX;          // to determine if X or O is current character
  26.         private bool _isGameOver;   // if the game is over
  27.         private string _jogador = "X";
  28.         public string Jogador
  29.         {
  30.             get { return _jogador; }
  31.             set
  32.             {
  33.                 if (_jogador != value)
  34.                 {
  35.                     _jogador = value;
  36.                     NotifyPropertyChanged("Jogador");
  37.                 }
  38.             }
  39.         }
  40.         public MainPage()
  41.         {
  42.             this.InitializeComponent();
  43.             this.NavigationCacheMode = NavigationCacheMode.Required;
  44.             Loaded += MainPage_Loaded;
  45.             HardwareButtons.BackPressed += HardwareButtons_BackPressed;
  46.         }
  47.  
  48.         private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
  49.         {
  50.             e.Handled = true;
  51.             if (Frame.CanGoBack)
  52.             {
  53.                 Frame.GoBack();
  54.             }
  55.             else
  56.             {
  57.                 Application.Current.Exit();
  58.             }
  59.         }
  60.  
  61.         void MainPage_Loaded(object sender, RoutedEventArgs e)
  62.         {
  63.             _buttonArray = new Button[9] { but1, but2, but3, but4, but5, but6, but7, but8, but9 };
  64.  
  65.             for (int i = 0; i < 9; i++)
  66.                 this._buttonArray[i].Click += ClickHandler;
  67.  
  68.             InitTicTacToe();  // set the defaults
  69.         }
  70.  
  71.         private void ClickHandler(object sender, RoutedEventArgs e)
  72.         {
  73.             var obj = sender as Button;
  74.             var content = obj.Content;
  75.            
  76.             if (this._isGameOver)
  77.             {
  78.                 lblResultado.Visibility = Windows.UI.Xaml.Visibility.Visible;
  79.                 lblResultado.Text = "Parabéns jogador " + Jogador + " você venceu!";
  80.                 return;
  81.             }
  82.             if (_isX)   // put the character in the Text property
  83.             {
  84.                 obj.Content = "X";
  85.                 obj.IsEnabled = false;
  86.                 Jogador = "0";
  87.             }
  88.             else
  89.             {
  90.                 obj.Content = "O";
  91.                 obj.IsEnabled = false;
  92.                 Jogador = "X";
  93.             }
  94.  
  95.             _isX = !_isX;   // prepare for next character
  96.  
  97.             this._isGameOver = TicTacToeUtils.CheckAndProcessWinner(_buttonArray);
  98.            
  99.         }
  100.         public async void MessageBoxDisplay(string content)
  101.         {
  102.             //Creating instance for the MessageDialog Class  
  103.             //and passing the message in it's Constructor  
  104.             MessageDialog msgbox = new MessageDialog(content);
  105.             //Calling the Show method of MessageDialog class  
  106.             //which will show the MessageBox  
  107.             await msgbox.ShowAsync();
  108.         }
  109.  
  110.  
  111.     private void InitTicTacToe()
  112.         {
  113.             for (int i = 0; i < 9; i++)
  114.             {
  115.                 _buttonArray[i].Content = " ";
  116.                 _buttonArray[i].Background = new SolidColorBrush(Colors.Transparent);
  117.                 _buttonArray[i].IsEnabled = true;
  118.             }
  119.             this._isX = true;
  120.             Jogador = "X";
  121.             lblResultado.Visibility = Visibility.Collapsed;
  122.             this._isGameOver = false;
  123.         }
  124.         protected override void OnNavigatedTo(NavigationEventArgs e)
  125.         {
  126.         }
  127.  
  128.         public event PropertyChangedEventHandler PropertyChanged;
  129.         private void NotifyPropertyChanged(String propertyName)
  130.         {
  131.             PropertyChangedEventHandler handler = PropertyChanged;
  132.             if (null != handler)
  133.             {
  134.                 handler(this, new PropertyChangedEventArgs(propertyName));
  135.             }
  136.         }
  137.  
  138.         private void butPlay_Click(object sender, RoutedEventArgs e)
  139.         {
  140.             //MessageBoxDisplay(((Button)sender).Content.ToString());
  141.             InitTicTacToe();
  142.         }
  143.     }
  144.     public class TicTacToeUtils
  145.     {
  146.         static MainPage mp;
  147.         // Winners contains all the array locations of
  148.         // the winning combination -- if they are all
  149.         // either X or O (and not blank)
  150.         static private int[,] Winners = new int[,]
  151.                    {
  152.                         {0,1,2},
  153.                         {3,4,5},
  154.                         {6,7,8},
  155.                         {0,3,6},
  156.                         {1,4,7},
  157.                         {2,5,8},
  158.                         {0,4,8},
  159.                         {2,4,6}
  160.                    };
  161.  
  162.         //--------------------------------------------------------------
  163.         // CheckAndProcessWinner determines if either X or O has won.
  164.         // Once a winner has been determined, play stops.
  165.         //--------------------------------------------------------------
  166.         static public bool CheckAndProcessWinner(Button[] myControls)
  167.         {
  168.             bool gameOver = false;
  169.             for (int i = 0; i < 8; i++)
  170.             {
  171.                 int a = Winners[i, 0], b = Winners[i, 1], c = Winners[i, 2];   //get the indices of the winners
  172.  
  173.                 //Button b1 = myControls[a], b2 = myControls[b], b3 = myControls[c];// just to make the
  174.                 var obj1 = myControls[a] as Button;
  175.                 var obj2 = myControls[b] as Button;
  176.                 var obj3 = myControls[c] as Button;
  177.  
  178.                 // the code readable
  179.  
  180.                 if (obj1.Content.ToString() == " " || obj2.Content.ToString() == " " || obj3.Content.ToString() == " ")    // any of the squares blank
  181.                     continue;                                           // try another -- no need to go further
  182.  
  183.                 if (obj1.Content.ToString() == obj2.Content.ToString() && obj2.Content.ToString() == obj3.Content.ToString()) // are they the same?
  184.                 {                                                       // if so, they WIN!
  185.                     obj1.Foreground = obj2.Foreground = obj3.Foreground = obj3.Foreground = new SolidColorBrush(Colors.LimeGreen);
  186.                     gameOver = true;
  187.                     foreach (Button item in myControls)
  188.                     {
  189.                         item.IsEnabled = false;
  190.                     }
  191.                     break;  // don't bother to continue
  192.                 }
  193.             }
  194.             return gameOver;
  195.         }
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement