Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices.WindowsRuntime;
- using Windows.Foundation;
- using Windows.Foundation.Collections;
- using Windows.UI;
- using Windows.UI.Xaml;
- using Windows.UI.Xaml.Controls;
- using Windows.UI.Xaml.Controls.Primitives;
- using Windows.UI.Xaml.Data;
- using Windows.UI.Xaml.Input;
- using Windows.UI.Xaml.Media;
- using Windows.UI.Xaml.Navigation;
- using System.ComponentModel;
- using Windows.Phone.UI.Input;
- using Windows.UI.Popups;
- namespace teste_Jogo_da_velha
- {
- public sealed partial class MainPage : Page, INotifyPropertyChanged
- {
- private Button[] _buttonArray;
- private bool _isX; // to determine if X or O is current character
- private bool _isGameOver; // if the game is over
- private string _jogador = "X";
- public string Jogador
- {
- get { return _jogador; }
- set
- {
- if (_jogador != value)
- {
- _jogador = value;
- NotifyPropertyChanged("Jogador");
- }
- }
- }
- public MainPage()
- {
- this.InitializeComponent();
- this.NavigationCacheMode = NavigationCacheMode.Required;
- Loaded += MainPage_Loaded;
- HardwareButtons.BackPressed += HardwareButtons_BackPressed;
- }
- private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
- {
- e.Handled = true;
- if (Frame.CanGoBack)
- {
- Frame.GoBack();
- }
- else
- {
- Application.Current.Exit();
- }
- }
- void MainPage_Loaded(object sender, RoutedEventArgs e)
- {
- _buttonArray = new Button[9] { but1, but2, but3, but4, but5, but6, but7, but8, but9 };
- for (int i = 0; i < 9; i++)
- this._buttonArray[i].Click += ClickHandler;
- InitTicTacToe(); // set the defaults
- }
- private void ClickHandler(object sender, RoutedEventArgs e)
- {
- var obj = sender as Button;
- var content = obj.Content;
- if (this._isGameOver)
- {
- lblResultado.Visibility = Windows.UI.Xaml.Visibility.Visible;
- lblResultado.Text = "Parabéns jogador " + Jogador + " você venceu!";
- return;
- }
- if (_isX) // put the character in the Text property
- {
- obj.Content = "X";
- obj.IsEnabled = false;
- Jogador = "0";
- }
- else
- {
- obj.Content = "O";
- obj.IsEnabled = false;
- Jogador = "X";
- }
- _isX = !_isX; // prepare for next character
- this._isGameOver = TicTacToeUtils.CheckAndProcessWinner(_buttonArray);
- }
- public async void MessageBoxDisplay(string content)
- {
- //Creating instance for the MessageDialog Class
- //and passing the message in it's Constructor
- MessageDialog msgbox = new MessageDialog(content);
- //Calling the Show method of MessageDialog class
- //which will show the MessageBox
- await msgbox.ShowAsync();
- }
- private void InitTicTacToe()
- {
- for (int i = 0; i < 9; i++)
- {
- _buttonArray[i].Content = " ";
- _buttonArray[i].Background = new SolidColorBrush(Colors.Transparent);
- _buttonArray[i].IsEnabled = true;
- }
- this._isX = true;
- Jogador = "X";
- lblResultado.Visibility = Visibility.Collapsed;
- this._isGameOver = false;
- }
- protected override void OnNavigatedTo(NavigationEventArgs e)
- {
- }
- public event PropertyChangedEventHandler PropertyChanged;
- private void NotifyPropertyChanged(String propertyName)
- {
- PropertyChangedEventHandler handler = PropertyChanged;
- if (null != handler)
- {
- handler(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- private void butPlay_Click(object sender, RoutedEventArgs e)
- {
- //MessageBoxDisplay(((Button)sender).Content.ToString());
- InitTicTacToe();
- }
- }
- public class TicTacToeUtils
- {
- static MainPage mp;
- // Winners contains all the array locations of
- // the winning combination -- if they are all
- // either X or O (and not blank)
- static private int[,] Winners = new int[,]
- {
- {0,1,2},
- {3,4,5},
- {6,7,8},
- {0,3,6},
- {1,4,7},
- {2,5,8},
- {0,4,8},
- {2,4,6}
- };
- //--------------------------------------------------------------
- // CheckAndProcessWinner determines if either X or O has won.
- // Once a winner has been determined, play stops.
- //--------------------------------------------------------------
- static public bool CheckAndProcessWinner(Button[] myControls)
- {
- bool gameOver = false;
- for (int i = 0; i < 8; i++)
- {
- int a = Winners[i, 0], b = Winners[i, 1], c = Winners[i, 2]; //get the indices of the winners
- //Button b1 = myControls[a], b2 = myControls[b], b3 = myControls[c];// just to make the
- var obj1 = myControls[a] as Button;
- var obj2 = myControls[b] as Button;
- var obj3 = myControls[c] as Button;
- // the code readable
- if (obj1.Content.ToString() == " " || obj2.Content.ToString() == " " || obj3.Content.ToString() == " ") // any of the squares blank
- continue; // try another -- no need to go further
- if (obj1.Content.ToString() == obj2.Content.ToString() && obj2.Content.ToString() == obj3.Content.ToString()) // are they the same?
- { // if so, they WIN!
- obj1.Foreground = obj2.Foreground = obj3.Foreground = obj3.Foreground = new SolidColorBrush(Colors.LimeGreen);
- gameOver = true;
- foreach (Button item in myControls)
- {
- item.IsEnabled = false;
- }
- break; // don't bother to continue
- }
- }
- return gameOver;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement