-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe (WPF Logic)
150 lines (125 loc) · 4.54 KB
/
TicTacToe (WPF Logic)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 TicTacToe
{
public enum ButtonState
{
empty, //0
O, //1
X //2
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private bool playerOneTurn;
private bool gameOver;
private ButtonState[] results;
public MainWindow()
{
InitializeComponent();
NewGame();
}
private void NewGame()
{
results = new ButtonState[9];
for (int i = 0; i < results.Length; i++)
{
results[i] = ButtonState.empty;
}
playerOneTurn = true;
GameBoard.Children.Cast<Button>().ToList().ForEach(button =>
{
button.Content = string.Empty;
button.Background = Brushes.White;
button.Foreground = Brushes.Black;
});
gameOver = false;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (gameOver)
{
NewGame();
return;
}
var button = (Button)sender;
var colum = Grid.GetColumn(button);
var row = Grid.GetRow(button);
var index = colum + (row * 3);
if (results[index] != ButtonState.empty)
return;
results[index] = playerOneTurn ? ButtonState.X : ButtonState.O;
button.Content = playerOneTurn ? "X" : "O";
playerOneTurn ^= true;
WinnerCheck();
}
private void WinnerCheck()
{
//horizontal
if(results[0] != ButtonState.empty && (results[0] & results[1] & results[2]) == results[0])
{
gameOver = true;
Button0.Background = Button1.Background = Button2.Background = Brushes.Aquamarine;
}
else if (results[3] != ButtonState.empty && (results[3] & results[4] & results[5]) == results[3])
{
gameOver = true;
Button3.Background = Button4.Background = Button5.Background = Brushes.Aquamarine;
}
else if (results[6] != ButtonState.empty && (results[6] & results[7] & results[8]) == results[6])
{
gameOver = true;
Button6.Background = Button7.Background = Button8.Background = Brushes.Aquamarine;
}
//vertical
if (results[0] != ButtonState.empty && (results[0] & results[3] & results[6]) == results[0])
{
gameOver = true;
Button0.Background = Button3.Background = Button6.Background = Brushes.Aquamarine;
}
else if (results[1] != ButtonState.empty && (results[1] & results[4] & results[7]) == results[1])
{
gameOver = true;
Button1.Background = Button4.Background = Button7.Background = Brushes.Aquamarine;
}
else if (results[2] != ButtonState.empty && (results[2] & results[5] & results[8]) == results[2])
{
gameOver = true;
Button2.Background = Button5.Background = Button8.Background = Brushes.Aquamarine;
}
//vertical
if (results[0] != ButtonState.empty && (results[0] & results[4] & results[8]) == results[0])
{
gameOver = true;
Button0.Background = Button4.Background = Button8.Background = Brushes.Aquamarine;
}
else if (results[2] != ButtonState.empty && (results[2] & results[4] & results[6]) == results[2])
{
gameOver = true;
Button2.Background = Button4.Background = Button6.Background = Brushes.Aquamarine;
}
else if (!results.Any(f => f == ButtonState.empty))
{
gameOver = true;
GameBoard.Children.Cast<Button>().ToList().ForEach(button =>
{
button.Background = Brushes.Orange;
});
}
}
}
}