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