Monday 7 April 2014

A Simple XO Multiplayer Game Using C# Windows Form Application



A Simple XO Multiplayer Game Using C# Windows Form Application


Construction of User Interface

For Implementing this Program firstly what we should do is to design the user interface that looks like the game we play with a paper and a pen  it should be like the picture shown below




     so to make it simple we just draw a structure of the game board with ms paint like the one below
note: we are not concentrating on the graphical effects as we are looking for the logic behind



Now set the windows form background as the image we created as shown bellow
for doing this we need to open visual studio
  next to create a new project 



Select the windows form application and enter the project name and click > Ok





Empty windows form opens :
1. Click the form the property window shows its related properties,if property window is not shown 
     click the view in the tool bar and select property window to view
2. Inside the property window select background option and click
3. Select resource window opens in the window click import to add the constructed picture for xo
4. Right pane shows the picture selected check if the picture is a fit one and click Ok


Now the background is set as shown in the figure....







Now place the button as shown in the figure bellow 

and make the text option of each button as null




finish the UI as bellow 





Logic for Coding

     The basic idea of the program is to firstly the player should enter their names and player one must be x
     and player two is o .the player1 clicks the button first and the button displayes x as its text and then                player2 clicks the button in his position then it must display o as its text like vise it contineus .

    first when the page loads it initialise variable i =1



 

then the method loop that is used to swap the current player with every press as shown bellow it is called
in the button click event that is created when the button is added to the form as shown early.  

   
1:  public void loop(int i)  
2:      {  
3:          if ((i % 2) == 0)  
4:          {  
5:            r = "X";  
6:          }  
7:          else  
8:          {  
9:            r = "O";  
10:          }  
11:       }  

The loop rule is to check for the status of the game whether players got bingo .this method also deployed with every press event associated with the particular button for a case: where button 3 is pressed then the rule2 method is called with a string parameter passed that is the button number,the method checks for the same button text in its surroundings. the code is shown bellow.reset method is used to replace every thing to the default values for the next game.



1:  public void rule2(string x)  
2:      {  
3:        switch(x)  
4:        {  
5:          case "b1":  
6:            if (button1.Text != "" && button5.Text != "" && button9.Text != "")  
7:            {  
8:              if ((button1.Text == button5.Text) && (button5.Text == button9.Text))  
9:              {  
10:                user = button1.Text;  
11:                playerinfo(user);  
12:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
13:                reset();  
14:              }  
15:            }  
16:            else if (button1.Text != "" && button4.Text != "" && button7.Text != "")  
17:            {  
18:              if ((button1.Text == button4.Text) && (button4.Text == button7.Text))  
19:              {  
20:                user = button1.Text;  
21:                playerinfo(user);  
22:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
23:                reset();  
24:              }  
25:            }  
26:            else if ((button1.Text != "") && (button2.Text != "") && (button3.Text != ""))  
27:            {  
28:              if ((button1.Text == button2.Text) && (button2.Text == button3.Text))  
29:              {  
30:                user = button1.Text;  
31:                playerinfo(user);  
32:                sound();  
33:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
34:                reset();  
35:              }  
36:            }  
37:            break;  
38:          case "b2":  
39:            if (button2.Text != "" && button5.Text != "" && button8.Text != "")  
40:            {  
41:              if ((button2.Text == button5.Text) && (button5.Text == button8.Text))  
42:              {  
43:                user = button2.Text;  
44:                playerinfo(user);  
45:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
46:                reset();  
47:              }  
48:            }  
49:            else if ((button1.Text != "") && (button2.Text != "") && (button3.Text != ""))  
50:            {  
51:              if ((button1.Text == button2.Text) && (button2.Text == button3.Text))  
52:              {  
53:                user = button2.Text;  
54:                playerinfo(user);  
55:                sound();  
56:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
57:                reset();  
58:              }  
59:            }  
60:            break;  
61:          case "b3":  
62:            if ((button1.Text != "") && (button2.Text != "") && (button3.Text != ""))  
63:            {  
64:              if ((button1.Text == button2.Text) && (button2.Text == button3.Text))  
65:              {  
66:                user = button3.Text;  
67:                playerinfo(user);  
68:                sound();  
69:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
70:                reset();  
71:              }  
72:            }  
73:            else if (button3.Text != "" && button6.Text != "" && button9.Text != "")  
74:            {  
75:              if ((button3.Text == button6.Text) && (button6.Text == button9.Text))  
76:              {  
77:                user = button3.Text;  
78:                playerinfo(user);  
79:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
80:                reset();  
81:              }  
82:            }  
83:            else if (button3.Text != "" && button5.Text != "" && button7.Text != "")  
84:            {  
85:              if ((button3.Text == button5.Text) && (button5.Text == button7.Text))  
86:              {  
87:                user = button3.Text;  
88:                playerinfo(user);  
89:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
90:                reset();  
91:              }  
92:            }  
93:            break;  
94:          case "b4":  
95:            if (button1.Text != "" && button4.Text != "" && button7.Text != "")  
96:            {  
97:              if ((button1.Text == button4.Text) && (button4.Text == button7.Text))  
98:              {  
99:                user = button4.Text;  
100:                playerinfo(user);  
101:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
102:                reset();  
103:              }  
104:            }  
105:            else if (button4.Text != "" && button5.Text != "" && button6.Text != "")  
106:            {  
107:              if ((button4.Text == button5.Text) && (button5.Text == button6.Text))  
108:              {  
109:                user = button4.Text;  
110:                playerinfo(user);  
111:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
112:                reset();  
113:              }  
114:            }  
115:            break;  
116:          case "b5":  
117:            if (button1.Text != "" && button5.Text != "" && button9.Text != "")  
118:            {  
119:              if ((button1.Text == button5.Text) && (button5.Text == button9.Text))  
120:              {  
121:                user = button5.Text;  
122:                playerinfo(user);  
123:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
124:                reset();  
125:              }  
126:            }  
127:            else if (button4.Text != "" && button5.Text != "" && button6.Text != "")  
128:            {  
129:              if ((button4.Text == button5.Text) && (button5.Text == button6.Text))  
130:              {  
131:                user = button5.Text;  
132:                playerinfo(user);  
133:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
134:                reset();  
135:              }  
136:            }  
137:            else if (button3.Text != "" && button5.Text != "" && button7.Text != "")  
138:            {  
139:              if ((button3.Text == button5.Text) && (button5.Text == button7.Text))  
140:              {  
141:                user = button5.Text;  
142:                playerinfo(user);  
143:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
144:                reset();  
145:              }  
146:            }  
147:            else if (button2.Text != "" && button5.Text != "" && button8.Text != "")  
148:            {  
149:              if ((button2.Text == button5.Text) && (button5.Text == button8.Text))  
150:              {  
151:                user = button5.Text;  
152:                playerinfo(user);  
153:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
154:                reset();  
155:              }  
156:            }  
157:            break;  
158:          case "b6":  
159:            if (button3.Text != "" && button6.Text != "" && button9.Text != "")  
160:            {  
161:              if ((button3.Text == button6.Text) && (button6.Text == button9.Text))  
162:              {  
163:                user = button6.Text;  
164:                playerinfo(user);  
165:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
166:                reset();  
167:              }  
168:            }  
169:            else if (button4.Text != "" && button5.Text != "" && button6.Text != "")  
170:            {  
171:              if ((button4.Text == button5.Text) && (button5.Text == button6.Text))  
172:              {  
173:                user = button6.Text;  
174:                playerinfo(user);  
175:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
176:                reset();  
177:              }  
178:            }  
179:            break;  
180:          case "b7":  
181:            if (button3.Text != "" && button5.Text != "" && button7.Text != "")  
182:            {  
183:              if ((button3.Text == button5.Text) && (button5.Text == button7.Text))  
184:              {  
185:                user = button7.Text;  
186:                playerinfo(user);  
187:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
188:                reset();  
189:              }  
190:            }  
191:            else if (button1.Text != "" && button4.Text != "" && button7.Text != "")  
192:            {  
193:              if ((button1.Text == button4.Text) && (button4.Text == button7.Text))  
194:              {  
195:                user = button7.Text;  
196:                playerinfo(user);  
197:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
198:                reset();  
199:              }  
200:            }  
201:            else if (button7.Text != "" && button8.Text != "" && button9.Text != "")  
202:            {  
203:              if ((button7.Text == button8.Text) && (button8.Text == button9.Text))  
204:              {  
205:                user = button7.Text;  
206:                playerinfo(user);  
207:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
208:                reset();  
209:              }  
210:            }  
211:            break;  
212:          case "b8":  
213:            if (button7.Text != "" && button8.Text != "" && button9.Text != "")  
214:            {  
215:              if ((button7.Text == button8.Text) && (button8.Text == button9.Text))  
216:              {  
217:                user = button8.Text;  
218:                playerinfo(user);  
219:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
220:                reset();  
221:              }  
222:            }  
223:            else if (button2.Text != "" && button5.Text != "" && button8.Text != "")  
224:            {  
225:              if ((button2.Text == button5.Text) && (button5.Text == button8.Text))  
226:              {  
227:                user = button8.Text;  
228:                playerinfo(user);  
229:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
230:                reset();  
231:              }  
232:            }  
233:            break;  
234:          case "b9":  
235:            if (button3.Text != "" && button6.Text != "" && button9.Text != "")  
236:            {  
237:              if ((button3.Text == button6.Text) && (button6.Text == button9.Text))  
238:              {  
239:                user = button9.Text;  
240:                playerinfo(user);  
241:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
242:                reset();  
243:              }  
244:            }  
245:            else if (button1.Text != "" && button5.Text != "" && button9.Text != "")  
246:            {  
247:              if ((button1.Text == button5.Text) && (button5.Text == button9.Text))  
248:              {  
249:                user = button9.Text;  
250:                playerinfo(user);  
251:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
252:                reset();  
253:              }  
254:            }  
255:            else if (button7.Text != "" && button8.Text != "" && button9.Text != "")  
256:            {  
257:              if ((button7.Text == button8.Text) && (button8.Text == button9.Text))  
258:              {  
259:                user = button9.Text;  
260:                playerinfo(user);  
261:                MessageBox.Show("Bingo..!" + player + "WON.. it");  
262:                reset();  
263:              }  
264:            }  
265:            else if (button1.Text != "" && button2.Text != "" && button3.Text != "" && button4.Text != "" && button5.Text != "" && button6.Text != "" && button7.Text != "" && button8.Text != "" && button9.Text != "")  
266:            {  
267:              MessageBox.Show("Match Drawn ...OK to next");  
268:              reset();  
269:            }  
270:            break;  
271:          default:  
272:            break;  
273:        }  
274:      }  


Method playerinfo  is used to identify the player name entered in the text box to display the winner.
Initialize variables of string type and store them with textboxs values that is the players name as shown bellow. 
 
1:  public string playerinfo(string user)  
2:      {  
3:        if (user == "O")  
4:        {  
5:          player = player2;  
6:        }  
7:        else  
8:        {  
9:          player = player1;  
10:        }  
11:        return player;  
12:      }  

if we want we can use a sound that indicates that the game has been won as code below shows .
reader is asked to edit the path of the sound the .wav file for this purpose. 

1:   public void sound()  
2:      {  
3:          string path;  
4:          path = "C:\\Users\\dell\\Documents\\Visual Studio 2008\\Projects\\Project2\\WindowsFormsApplication3\\WindowsFormsApplication3\\ma.wav";  
5:          SoundPlayer ply = new SoundPlayer(path);  
6:          ply.Play();  
7:      }  

The reset method as said above resets the value to the default for the next game to be continued .it is called in every win moment and draw moment in the game .

1:  public void reset()  
2:      {  
3:        if (i <= 4)  
4:        {  
5:          i = 1;  
6:        }  
7:        button1.Enabled = true;  
8:        button1.ResetText();  
9:        b1 = false;  
10:        button2.Enabled = true;  
11:        button2.ResetText();  
12:        b2 = false;  
13:        button3.Enabled = true;  
14:        button3.ResetText();  
15:        b3 = false;  
16:        button4.Enabled = true;  
17:        button4.ResetText();  
18:        b4 = false;  
19:        button5.Enabled = true;  
20:        button5.ResetText();  
21:        b5 = false;  
22:        button6.Enabled = true;  
23:        button6.ResetText();  
24:        b6 = false;  
25:        button7.Enabled = true;  
26:        button7.ResetText();  
27:        b7 = false;  
28:        button8.Enabled = true;  
29:        button8.ResetText();  
30:        b8 = false;  
31:        button9.Enabled = true;  
32:        button9.ResetText();  
33:        b9 = false;  
34:      }  

Full source code


No comments:

Post a Comment