Full Source Code Of Simple XO Game

Language :c#   

FrameWork : 3 and above 

IDE: VisualStudio 2010



Source


1:  using System;  
2:  using System.Collections.Generic;  
3:  using System.ComponentModel;  
4:  using System.Data;  
5:  using System.Linq;  
6:  using System.Text;  
7:  using System.Windows.Forms;  
8:  using System.Media;  
9:  namespace WindowsFormsApplication3  
10:  {  
11:    public partial class Form1 : Form  
12:    {  
13:      public string r;  
14:      public int i=1,game = 1;  
15:      public string player1, player2,player;  
16:      public Form1()  
17:      {  
18:        InitializeComponent();  
19:      }  
20:      public void loop(int i)  
21:      {  
22:          if ((i % 2) == 0)  
23:          {  
24:            r = "X";  
25:          }  
26:          else  
27:          {  
28:            r = "O";  
29:          }  
30:       }        
31:      private void Form1_Load(object sender, EventArgs e)  
32:      {  
33:        i = 1;   
34:      }  
35:      public void button1_Click(object sender, EventArgs e)  
36:      {  
37:        i += 1;  
38:        loop(i);  
39:        button1.Text = r;  
40:        button1.Enabled = false;  
41:        rule2("b1");  
42:      }  
43:      public void button2_Click(object sender, EventArgs e)  
44:      {  
45:        i += 1;  
46:        loop(i);  
47:        button2.Text = r;  
48:        button2.Enabled = false;  
49:        rule2("b2");  
50:      }  
51:      public void button3_Click(object sender, EventArgs e)  
52:      {      
53:        i += 1;  
54:        loop(i);  
55:        button3.Text = r;  
56:        button3.Enabled = false;  
57:        rule2("b3");    
58:      }  
59:      public void button4_Click(object sender, EventArgs e)  
60:      {  
61:        i += 1;  
62:        loop(i);  
63:        button4.Text = r;  
64:        button4.Enabled = false;  
65:        rule2("b4");  
66:      }  
67:      public void button5_Click(object sender, EventArgs e)  
68:      {  
69:        i += 1;  
70:        loop(i);  
71:        button5.Text = r;  
72:        button5.Enabled = false;  
73:        rule2("b5");  
74:      }  
75:      public void button6_Click(object sender, EventArgs e)  
76:      {  
77:        i += 1;  
78:        loop(i);  
79:        button6.Text = r;  
80:        button6.Enabled = false;  
81:        rule2("b6");  
82:      }  
83:      public void button7_Click(object sender, EventArgs e)  
84:       {  
85:        i += 1;  
86:        loop(i);  
87:        button7.Text = r;  
88:        button7.Enabled = false;  
89:         rule2("b7");  
90:      }  
91:      public void button8_Click(object sender, EventArgs e)  
92:      {  
93:        i += 1;  
94:        loop(i);  
95:        button8.Text = r;  
96:        button8.Enabled = false;      
97:        rule2("b8");  
98:      }  
99:      public void button9_Click(object sender, EventArgs e)  
100:      {  
101:        i += 1;  
102:        loop(i);  
103:        button9.Text = r;  
104:        button9.Enabled = false;  
105:        rule2("b9");  
106:      }  
107:      public string playerinfo(string user)  
108:      {  
109:        if (user == "O")  
110:        {  
111:          player = player2;  
112:        }  
113:        else  
114:        {  
115:          player = player1;  
116:        }  
117:        return player;  
118:      }  
119:      public void rule2(string x)  
120:      {  
121:        switch(x)  
122:        {  
123:          case "b1":  
124:            if (button1.Text != "" && button5.Text != "" && button9.Text != "")  
125:            {  
126:              if ((button1.Text == button5.Text) && (button5.Text == button9.Text))  
127:              {  
128:                user = button1.Text;  
129:                playerinfo(user);  
130:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
131:                reset();  
132:              }  
133:            }  
134:            else if (button1.Text != "" && button4.Text != "" && button7.Text != "")  
135:            {  
136:              if ((button1.Text == button4.Text) && (button4.Text == button7.Text))  
137:              {  
138:                user = button1.Text;  
139:                playerinfo(user);  
140:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
141:                reset();  
142:              }  
143:            }  
144:            else if ((button1.Text != "") && (button2.Text != "") && (button3.Text != ""))  
145:            {  
146:              if ((button1.Text == button2.Text) && (button2.Text == button3.Text))  
147:              {  
148:                user = button1.Text;  
149:                playerinfo(user);  
150:                sound();  
151:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
152:                reset();  
153:              }  
154:            }  
155:            break;  
156:          case "b2":  
157:            if (button2.Text != "" && button5.Text != "" && button8.Text != "")  
158:            {  
159:              if ((button2.Text == button5.Text) && (button5.Text == button8.Text))  
160:              {  
161:                user = button2.Text;  
162:                playerinfo(user);  
163:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
164:                reset();  
165:              }  
166:            }  
167:            else if ((button1.Text != "") && (button2.Text != "") && (button3.Text != ""))  
168:            {  
169:              if ((button1.Text == button2.Text) && (button2.Text == button3.Text))  
170:              {  
171:                user = button2.Text;  
172:                playerinfo(user);  
173:                sound();  
174:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
175:                reset();  
176:              }  
177:            }  
178:            break;  
179:          case "b3":  
180:            if ((button1.Text != "") && (button2.Text != "") && (button3.Text != ""))  
181:            {  
182:              if ((button1.Text == button2.Text) && (button2.Text == button3.Text))  
183:              {  
184:                user = button3.Text;  
185:                playerinfo(user);  
186:                sound();  
187:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
188:                reset();  
189:              }  
190:            }  
191:            else if (button3.Text != "" && button6.Text != "" && button9.Text != "")  
192:            {  
193:              if ((button3.Text == button6.Text) && (button6.Text == button9.Text))  
194:              {  
195:                user = button3.Text;  
196:                playerinfo(user);  
197:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
198:                reset();  
199:              }  
200:            }  
201:            else if (button3.Text != "" && button5.Text != "" && button7.Text != "")  
202:            {  
203:              if ((button3.Text == button5.Text) && (button5.Text == button7.Text))  
204:              {  
205:                user = button3.Text;  
206:                playerinfo(user);  
207:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
208:                reset();  
209:              }  
210:            }  
211:            break;  
212:          case "b4":  
213:            if (button1.Text != "" && button4.Text != "" && button7.Text != "")  
214:            {  
215:              if ((button1.Text == button4.Text) && (button4.Text == button7.Text))  
216:              {  
217:                user = button4.Text;  
218:                playerinfo(user);  
219:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
220:                reset();  
221:              }  
222:            }  
223:            else if (button4.Text != "" && button5.Text != "" && button6.Text != "")  
224:            {  
225:              if ((button4.Text == button5.Text) && (button5.Text == button6.Text))  
226:              {  
227:                user = button4.Text;  
228:                playerinfo(user);  
229:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
230:                reset();  
231:              }  
232:            }  
233:            break;  
234:          case "b5":  
235:            if (button1.Text != "" && button5.Text != "" && button9.Text != "")  
236:            {  
237:              if ((button1.Text == button5.Text) && (button5.Text == button9.Text))  
238:              {  
239:                user = button5.Text;  
240:                playerinfo(user);  
241:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
242:                reset();  
243:              }  
244:            }  
245:            else if (button4.Text != "" && button5.Text != "" && button6.Text != "")  
246:            {  
247:              if ((button4.Text == button5.Text) && (button5.Text == button6.Text))  
248:              {  
249:                user = button5.Text;  
250:                playerinfo(user);  
251:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
252:                reset();  
253:              }  
254:            }  
255:            else if (button3.Text != "" && button5.Text != "" && button7.Text != "")  
256:            {  
257:              if ((button3.Text == button5.Text) && (button5.Text == button7.Text))  
258:              {  
259:                user = button5.Text;  
260:                playerinfo(user);  
261:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
262:                reset();  
263:              }  
264:            }  
265:            else if (button2.Text != "" && button5.Text != "" && button8.Text != "")  
266:            {  
267:              if ((button2.Text == button5.Text) && (button5.Text == button8.Text))  
268:              {  
269:                user = button5.Text;  
270:                playerinfo(user);  
271:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
272:                reset();  
273:              }  
274:            }  
275:            break;  
276:          case "b6":  
277:            if (button3.Text != "" && button6.Text != "" && button9.Text != "")  
278:            {  
279:              if ((button3.Text == button6.Text) && (button6.Text == button9.Text))  
280:              {  
281:                user = button6.Text;  
282:                playerinfo(user);  
283:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
284:                reset();  
285:              }  
286:            }  
287:            else if (button4.Text != "" && button5.Text != "" && button6.Text != "")  
288:            {  
289:              if ((button4.Text == button5.Text) && (button5.Text == button6.Text))  
290:              {  
291:                user = button6.Text;  
292:                playerinfo(user);  
293:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
294:                reset();  
295:              }  
296:            }  
297:            break;  
298:          case "b7":  
299:            if (button3.Text != "" && button5.Text != "" && button7.Text != "")  
300:            {  
301:              if ((button3.Text == button5.Text) && (button5.Text == button7.Text))  
302:              {  
303:                user = button7.Text;  
304:                playerinfo(user);  
305:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
306:                reset();  
307:              }  
308:            }  
309:            else if (button1.Text != "" && button4.Text != "" && button7.Text != "")  
310:            {  
311:              if ((button1.Text == button4.Text) && (button4.Text == button7.Text))  
312:              {  
313:                user = button7.Text;  
314:                playerinfo(user);  
315:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
316:                reset();  
317:              }  
318:            }  
319:            else if (button7.Text != "" && button8.Text != "" && button9.Text != "")  
320:            {  
321:              if ((button7.Text == button8.Text) && (button8.Text == button9.Text))  
322:              {  
323:                user = button7.Text;  
324:                playerinfo(user);  
325:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
326:                reset();  
327:              }  
328:            }  
329:            break;  
330:          case "b8":  
331:            if (button7.Text != "" && button8.Text != "" && button9.Text != "")  
332:            {  
333:              if ((button7.Text == button8.Text) && (button8.Text == button9.Text))  
334:              {  
335:                user = button8.Text;  
336:                playerinfo(user);  
337:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
338:                reset();  
339:              }  
340:            }  
341:            else if (button2.Text != "" && button5.Text != "" && button8.Text != "")  
342:            {  
343:              if ((button2.Text == button5.Text) && (button5.Text == button8.Text))  
344:              {  
345:                user = button8.Text;  
346:                playerinfo(user);  
347:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
348:                reset();  
349:              }  
350:            }  
351:            break;  
352:          case "b9":  
353:            if (button3.Text != "" && button6.Text != "" && button9.Text != "")  
354:            {  
355:              if ((button3.Text == button6.Text) && (button6.Text == button9.Text))  
356:              {  
357:                user = button9.Text;  
358:                playerinfo(user);  
359:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
360:                reset();  
361:              }  
362:            }  
363:            else if (button1.Text != "" && button5.Text != "" && button9.Text != "")  
364:            {  
365:              if ((button1.Text == button5.Text) && (button5.Text == button9.Text))  
366:              {  
367:                user = button9.Text;  
368:                playerinfo(user);  
369:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
370:                reset();  
371:              }  
372:            }  
373:            else if (button7.Text != "" && button8.Text != "" && button9.Text != "")  
374:            {  
375:              if ((button7.Text == button8.Text) && (button8.Text == button9.Text))  
376:              {  
377:                user = button9.Text;  
378:                playerinfo(user);  
379:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
380:                reset();  
381:              }  
382:            }  
383:            else if (button1.Text != "" && button2.Text != "" && button3.Text != "" && button4.Text != "" && button5.Text != "" && button6.Text != "" && button7.Text != "" && button8.Text != "" && button9.Text != "")  
384:            {  
385:              MessageBox.Show("Match Drawn ...OK to next");  
386:              reset();  
387:            }  
388:            break;  
389:          default:  
390:            break;  
391:        }  
392:      }  
393:      public void textBox1_TextChanged(object sender, EventArgs e)  
394:      {  
395:        player1 = textBox1.Text;  
396:      }  
397:      public void textBox2_TextChanged(object sender, EventArgs e)  
398:      {  
399:        player2 = textBox2.Text;  
400:      }  
401:      public void sound()  
402:      {  
403:          string path;  
404:          path = "C:\\Users\\dell\\Documents\\Visual Studio 2008\\Projects\\Project2\\WindowsFormsApplication3\\WindowsFormsApplication3\\ma.wav";  
405:          SoundPlayer ply = new SoundPlayer(path);  
406:          ply.Play();  
407:      }  
408:      public void reset()  
409:      {  
410:        if (i <= 4)  
411:        {  
412:          i = 1;  
413:        }  
414:        button1.Enabled = true;  
415:        button1.ResetText();  
416:        b1 = false;  
417:        button2.Enabled = true;  
418:        button2.ResetText();  
419:        b2 = false;  
420:        button3.Enabled = true;  
421:        button3.ResetText();  
422:        b3 = false;  
423:        button4.Enabled = true;  
424:        button4.ResetText();  
425:        b4 = false;  
426:        button5.Enabled = true;  
427:        button5.ResetText();  
428:        b5 = false;  
429:        button6.Enabled = true;  
430:        button6.ResetText();  
431:        b6 = false;  
432:        button7.Enabled = true;  
433:        button7.ResetText();  
434:        b7 = false;  
435:        button8.Enabled = true;  
436:        button8.ResetText();  
437:        b8 = false;  
438:        button9.Enabled = true;  
439:        button9.ResetText();  
440:        b9 = false;  
441:      }  
442:      private void button11_Click(object sender, EventArgs e)  
443:      {  
444:        reset();  
445:      }  
446:    }  
447:  }  

No comments:

Post a Comment