SHOW:
|
|
- or go back to the newest paste.
1 | public partial class Form1 : Form | |
2 | - | { |
2 | + | { |
3 | - | |
3 | + | |
4 | - | bool whosTurn = true; //true - X, false - O |
4 | + | bool whosTurn = true; //true - X, false - O |
5 | - | int stalemateCounter = 0; |
5 | + | int stalemateCounter = 0; |
6 | - | |
6 | + | |
7 | ||
8 | - | public Form1() |
8 | + | public Form1() |
9 | - | { |
9 | + | { |
10 | - | InitializeComponent(); |
10 | + | InitializeComponent(); |
11 | - | lblWhosTorn.Text = "X"; |
11 | + | lblWhosTorn.Text = "X"; |
12 | - | } |
12 | + | } |
13 | ||
14 | - | private void InsertSymbol(object button) |
14 | + | private void InsertSymbol(object button) |
15 | - | { |
15 | + | { |
16 | - | Button pressedButton = (Button)button; |
16 | + | Button pressedButton = (Button)button; |
17 | ||
18 | - | if (whosTurn) |
18 | + | if (whosTurn) |
19 | - | { |
19 | + | { |
20 | - | pressedButton.Text = "X"; |
20 | + | pressedButton.Text = "X"; |
21 | - | } |
21 | + | } |
22 | - | else |
22 | + | else |
23 | - | { |
23 | + | { |
24 | - | pressedButton.Text = "O"; |
24 | + | pressedButton.Text = "O"; |
25 | - | } |
25 | + | } |
26 | ||
27 | - | pressedButton.Enabled = false; |
27 | + | pressedButton.Enabled = false; |
28 | ||
29 | - | bool wynik = checkIfSomeoneWon(); |
29 | + | bool wynik = CheckIfSomeoneWon(); |
30 | ||
31 | ||
32 | - | if (wynik == true) |
32 | + | if (wynik) |
33 | - | { |
33 | + | { |
34 | - | string winText; |
34 | + | string winText; |
35 | - | if (whosTurn == true) |
35 | + | if (whosTurn) |
36 | - | { |
36 | + | { |
37 | - | winText = "Player X won! Replay?"; |
37 | + | winText = "Player X won! Replay?"; |
38 | - | } |
38 | + | } |
39 | - | else |
39 | + | else |
40 | ||
41 | - | { |
41 | + | { |
42 | ||
43 | - | winText = "Player 0 won! Replay?"; |
43 | + | winText = "Player 0 won! Replay?"; |
44 | - | } |
44 | + | } |
45 | ||
46 | - | //We check if players want to replay the match. |
46 | + | //We check if players want to replay the match. |
47 | - | DialogResult answer = MessageBox.Show(winText, "Win", MessageBoxButtons.YesNo); |
47 | + | DialogResult answer = MessageBox.Show(winText, "Win", MessageBoxButtons.YesNo); |
48 | ||
49 | - | if (answer == DialogResult.Yes) |
49 | + | if (answer == DialogResult.Yes) |
50 | - | { |
50 | + | { |
51 | - | TurnOnAllButtonsAndReset(); |
51 | + | TurnOnAllButtonsAndReset(); |
52 | - | return; |
52 | + | return; |
53 | - | } |
53 | + | } |
54 | - | else |
54 | + | else |
55 | - | { |
55 | + | { |
56 | - | Close(); |
56 | + | Close(); |
57 | - | } |
57 | + | } |
58 | - | } |
58 | + | } |
59 | ||
60 | - | // We check stalemate |
60 | + | // We check stalemate |
61 | - | if (stalemateCounter == 9) |
61 | + | if (stalemateCounter == 9) |
62 | - | { |
62 | + | { |
63 | - | |
63 | + | |
64 | - | DialogResult answer = MessageBox.Show("Stalemate, replay?", "Stalemate", MessageBoxButtons.YesNo); |
64 | + | DialogResult answer = MessageBox.Show("Stalemate, replay?", "Stalemate", MessageBoxButtons.YesNo); |
65 | ||
66 | - | if (answer == DialogResult.Yes) |
66 | + | if (answer == DialogResult.Yes) |
67 | - | { |
67 | + | { |
68 | - | TurnOnAllButtonsAndReset(); |
68 | + | TurnOnAllButtonsAndReset(); |
69 | - | return; |
69 | + | return; |
70 | - | } |
70 | + | } |
71 | - | else |
71 | + | else |
72 | - | { |
72 | + | { |
73 | - | Close(); |
73 | + | Close(); |
74 | - | } |
74 | + | } |
75 | - | } |
75 | + | } |
76 | ||
77 | ||
78 | - | whosTurn = !whosTurn; //player switch |
78 | + | whosTurn = !whosTurn; //player switch |
79 | - | if (whosTurn == true) |
79 | + | if (whosTurn) |
80 | - | { |
80 | + | { |
81 | - | lblWhosTorn.Text = "X"; |
81 | + | lblWhosTorn.Text = "X"; |
82 | - | } |
82 | + | } |
83 | - | else |
83 | + | else |
84 | - | { |
84 | + | { |
85 | - | lblWhosTorn.Text = "O"; |
85 | + | lblWhosTorn.Text = "O"; |
86 | - | } |
86 | + | } |
87 | ||
88 | - | } |
88 | + | } |
89 | ||
90 | - | private void TurnOnAllButtonsAndReset() |
90 | + | private void TurnOnAllButtonsAndReset() |
91 | - | { |
91 | + | { |
92 | ||
93 | ||
94 | - | btn1.Enabled = true; |
94 | + | btn1.Enabled = true; |
95 | - | btn2.Enabled = true; |
95 | + | btn2.Enabled = true; |
96 | - | btn3.Enabled = true; |
96 | + | btn3.Enabled = true; |
97 | - | btn4.Enabled = true; |
97 | + | btn4.Enabled = true; |
98 | - | btn5.Enabled = true; |
98 | + | btn5.Enabled = true; |
99 | - | btn6.Enabled = true; |
99 | + | btn6.Enabled = true; |
100 | - | btn7.Enabled = true; |
100 | + | btn7.Enabled = true; |
101 | - | btn8.Enabled = true; |
101 | + | btn8.Enabled = true; |
102 | - | btn9.Enabled = true; |
102 | + | btn9.Enabled = true; |
103 | - | btn1.Text = ""; |
103 | + | btn1.Text = ""; |
104 | - | btn2.Text = ""; |
104 | + | btn2.Text = ""; |
105 | - | btn3.Text = ""; |
105 | + | btn3.Text = ""; |
106 | - | btn4.Text = ""; |
106 | + | btn4.Text = ""; |
107 | - | btn5.Text = ""; |
107 | + | btn5.Text = ""; |
108 | - | btn6.Text = ""; |
108 | + | btn6.Text = ""; |
109 | - | btn7.Text = ""; |
109 | + | btn7.Text = ""; |
110 | - | btn8.Text = ""; |
110 | + | btn8.Text = ""; |
111 | - | btn9.Text = ""; |
111 | + | btn9.Text = ""; |
112 | - | stalemateCounter = 0; |
112 | + | stalemateCounter = 0; |
113 | - | whosTurn = true; |
113 | + | whosTurn = true; |
114 | - | lblWhosTorn.Text = "X"; |
114 | + | lblWhosTorn.Text = "X"; |
115 | - | } |
115 | + | } |
116 | ||
117 | ||
118 | - | private bool checkIfSomeoneWon() |
118 | + | private bool CheckIfSomeoneWon() |
119 | - | { |
119 | + | { |
120 | - | |
120 | + | |
121 | - | if (btn1.Text == btn2.Text && btn2.Text == btn3.Text && btn1.Text != "") |
121 | + | if (btn1.Text == btn2.Text && btn2.Text == btn3.Text && btn1.Text != "") |
122 | - | |
122 | + | |
123 | - | { |
123 | + | { |
124 | - | return true; |
124 | + | return true; |
125 | - | } |
125 | + | } |
126 | - | |
126 | + | |
127 | - | else if (btn4.Text == btn5.Text && btn5.Text == btn6.Text && btn4.Text != "") |
127 | + | else if (btn4.Text == btn5.Text && btn5.Text == btn6.Text && btn4.Text != "") |
128 | - | { |
128 | + | { |
129 | - | return true; |
129 | + | return true; |
130 | - | } |
130 | + | } |
131 | - | |
131 | + | |
132 | - | else if (btn7.Text == btn8.Text && btn8.Text == btn9.Text && btn7.Text != "") |
132 | + | else if (btn7.Text == btn8.Text && btn8.Text == btn9.Text && btn7.Text != "") |
133 | - | { |
133 | + | { |
134 | - | return true; |
134 | + | return true; |
135 | - | } |
135 | + | } |
136 | - | |
136 | + | |
137 | - | else if (btn1.Text == btn4.Text && btn4.Text == btn7.Text && btn1.Text != "") |
137 | + | else if (btn1.Text == btn4.Text && btn4.Text == btn7.Text && btn1.Text != "") |
138 | - | { |
138 | + | { |
139 | - | return true; |
139 | + | return true; |
140 | - | } |
140 | + | } |
141 | ||
142 | - | else if (btn2.Text == btn5.Text && btn5.Text == btn8.Text && btn2.Text != "") |
142 | + | else if (btn2.Text == btn5.Text && btn5.Text == btn8.Text && btn2.Text != "") |
143 | - | { |
143 | + | { |
144 | - | return true; |
144 | + | return true; |
145 | - | } |
145 | + | } |
146 | - | |
146 | + | |
147 | - | else if (btn3.Text == btn6.Text && btn6.Text == btn9.Text && btn3.Text != "") |
147 | + | else if (btn3.Text == btn6.Text && btn6.Text == btn9.Text && btn3.Text != "") |
148 | - | { |
148 | + | { |
149 | - | return true; |
149 | + | return true; |
150 | - | } |
150 | + | } |
151 | - | else if (btn1.Text == btn5.Text && btn5.Text == btn9.Text && btn1.Text != "") |
151 | + | else if (btn1.Text == btn5.Text && btn5.Text == btn9.Text && btn1.Text != "") |
152 | - | { |
152 | + | { |
153 | - | return true; |
153 | + | return true; |
154 | - | } |
154 | + | } |
155 | - | |
155 | + | |
156 | - | else if (btn3.Text == btn5.Text && btn5.Text == btn7.Text && btn3.Text != "") |
156 | + | else if (btn3.Text == btn5.Text && btn5.Text == btn7.Text && btn3.Text != "") |
157 | - | { |
157 | + | { |
158 | - | return true; |
158 | + | return true; |
159 | - | } |
159 | + | } |
160 | - | else |
160 | + | else |
161 | - | { |
161 | + | { |
162 | - | stalemateCounter++; |
162 | + | stalemateCounter++; |
163 | - | return false; |
163 | + | return false; |
164 | - | } |
164 | + | } |
165 | - | } |
165 | + | } |
166 | ||
167 | - | private void btn_click(object sender, EventArgs e) |
167 | + | private void btn_click(object sender, EventArgs e) |
168 | - | { |
168 | + | { |
169 | - | InsertSymbol(sender); |
169 | + | InsertSymbol(sender); |
170 | - | } |
170 | + | } |
171 | - | } |
171 | + | } |