/// <summary> /// display the letter if player pressed the right one /// </summary> /// <param name="ClickedLetter"></param> private void Game(char ClickedLetter) { State = State.Waiting; int WinnerFlag = 0; for (int i = 0; i < WordToGuessChar.Length; i++) { if (WordToGuessChar[i] == ClickedLetter) { WordToGuessSymbols[i] = ClickedLetter; State = State.Playing; } if (WordToGuessSymbols[i] == '_') { WinnerFlag++; } } if (WinnerFlag == 0) { State = State.Winner; } ShowWord(); string ChangedWordToGuess = ""; for (int z = 0; z < WordToGuessSymbols.Length; z++) { ChangedWordToGuess += WordToGuessSymbols[z]; } MsgToSend = "game" + ";" + RoomID + ";" + State.ToString() + ";" + ClickedLetter + ";" + ChangedWordToGuess; SocketClient.Send(Encoding.ASCII.GetBytes(MsgToSend), 0, MsgToSend.Length, SocketFlags.None); if (WinnerFlag == 0) { // display winner and ask if wants to play again PopUpMessage CongratMsg = new PopUpMessage("Congratulations! You won! Do you want to play again?"); CongratMsg.ShowDialog(); //accept playing again if (CongratMsg.DialogResult == DialogResult.OK) { MsgToSend = "rematch;" + RoomID; SocketClient.Send(Encoding.ASCII.GetBytes(MsgToSend), 0, MsgToSend.Length, SocketFlags.None); labelWinner.Text = ""; labelGamer2Name.Text = ""; } else // refuse playing again { MsgToSend = "quit;" + RoomID; SocketClient.Send(Encoding.ASCII.GetBytes(MsgToSend), 0, MsgToSend.Length, SocketFlags.None); this.FormClosing -= new FormClosingEventHandler(RoomForm_FormClosing); EndConnectionFlag = 1; this.Close(); } } }
/// <summary> /// Sends to the server if the player wants to join or watch a game /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void listViewAllRooms_SelectedIndexChanged(object sender, EventArgs e) { if (listViewAllRooms.SelectedItems.Count > 0) { string player2_state = listViewAllRooms.SelectedItems[0].SubItems[4].Text; if (player2_state == "") { MsgToSend = "join;" + listViewAllRooms.SelectedItems[0].SubItems[0].Name; PopUpMessage = new PopUpMessage("Waiting for response..."); PopUpMessage.DisableButtons(); PopUpMessage.Show(); ClientSocket.Send(Encoding.ASCII.GetBytes(MsgToSend), 0, MsgToSend.Length, SocketFlags.None); } else { MsgToSend = "watch;" + listViewAllRooms.SelectedItems[0].SubItems[0].Name; ClientSocket.Send(Encoding.ASCII.GetBytes(MsgToSend), 0, MsgToSend.Length, SocketFlags.None); } } }
/// <summary> /// recieve messages from server to manage the game if player wants to play with the owner and handle cases when game finished if players want to play again together /// </summary> private void RecieveFromServer() { while (EndConnectionFlag == 0) { CheckForIllegalCrossThreadCalls = false; try { byte[] MsgBT = new byte[1024]; int size = SocketClient.Receive(MsgBT); RecievedMsg = Encoding.ASCII.GetString(MsgBT, 0, size); string[] RecievedMsgArr = RecievedMsg.Split(';'); //someone wants to join if (RecievedMsgArr[0] == "request") { Player2Name = RecievedMsgArr[1]; string RequestMsg = Player2Name + " wants to play with you."; PopUpMessage request = new PopUpMessage(RequestMsg); request.ShowDialog(); //accept if (request.DialogResult == DialogResult.OK) { labelGamer2Name.Text = RecievedMsgArr[1]; btnStartGame.Show(); MsgToSend = "accept;" + RoomID; SocketClient.Send(Encoding.ASCII.GetBytes(MsgToSend), 0, MsgToSend.Length, SocketFlags.None); } else //refuse { MsgToSend = "refuse;" + RoomID; SocketClient.Send(Encoding.ASCII.GetBytes(MsgToSend), 0, MsgToSend.Length, SocketFlags.None); } } // start the game if (RecievedMsgArr[0] == "start") { WordToGuess = RecievedMsgArr[1]; if (RecievedMsgArr[2] == "Playing") { State = State.Playing; } else if (RecievedMsgArr[2] == "Waiting") { State = State.Waiting; } grpBoxKeyBoard.Enabled = true; EnableButtons(); grpBoxKeyBoard.Enabled = false; ShowSymbols(); } // game if (RecievedMsgArr[0] == "game") { CheckState(RecievedMsgArr[2]); DisableButton(RecievedMsgArr[3][0]); WordToGuessSymbols = RecievedMsgArr[4].ToCharArray(); ShowWord(); if (State == State.Loser) { PopUpMessage Message = new PopUpMessage("Hard Luck. You lost. Do you want to play again?"); Message.ShowDialog(); if (Message.DialogResult == DialogResult.OK) { MsgToSend = "rematch;" + RoomID; SocketClient.Send(Encoding.ASCII.GetBytes(MsgToSend), 0, MsgToSend.Length, SocketFlags.None); labelWinner.Text = ""; labelGamer2Name.Text = ""; } else { MsgToSend = "quit;" + RoomID; SocketClient.Send(Encoding.ASCII.GetBytes(MsgToSend), 0, MsgToSend.Length, SocketFlags.None); this.FormClosing -= new FormClosingEventHandler(RoomForm_FormClosing); EndConnectionFlag = 1; this.Close(); } } } // play again if (RecievedMsgArr[0] == "rematch") { if (RecievedMsgArr.Length == 3) { btnStartGame.Show(); } labelGamer2Name.Text = RecievedMsgArr[1]; } // watcher if (RecievedMsg.IndexOf("watch") > -1) { labelWinner.Text = ""; WordToGuessSymbols = RecievedMsgArr[2].ToCharArray(); ShowWord(); DisableButton(RecievedMsgArr[3][0]); if (RecievedMsgArr[1] == "Playing") { labelGamer1Name.BackColor = Color.PaleGreen; labelGamer2Name.BackColor = Color.Tomato; } else { labelGamer2Name.BackColor = Color.PaleGreen; labelGamer1Name.BackColor = Color.Tomato; } } // player left the room if (RecievedMsgArr[0] == "left") { labelWordToGuess.Text = ""; grpBoxKeyBoard.Enabled = false; PopUpMessage Message = new PopUpMessage(labelGamer2Name.Text + " left the room. Do you want to play again?"); Message.ShowDialog(); // play again with other player if (Message.DialogResult == DialogResult.OK) { MsgToSend = "rematch;" + RoomID; SocketClient.Send(Encoding.ASCII.GetBytes(MsgToSend), 0, MsgToSend.Length, SocketFlags.None); labelWinner.Text = ""; labelGamer2Name.Text = ""; } else // leave the room { MsgToSend = "quit;" + RoomID; SocketClient.Send(Encoding.ASCII.GetBytes(MsgToSend), 0, MsgToSend.Length, SocketFlags.None); this.FormClosing -= new FormClosingEventHandler(RoomForm_FormClosing); EndConnectionFlag = 1; this.Close(); } } } catch (EndOfStreamException) { MessageBox.Show("Connection ended. Server is down."); EndConnectionFlag = 1; } catch (IOException) { MessageBox.Show("Connection ended. Server is down."); EndConnectionFlag = 1; } } }
/// <summary> /// Awaits to recieve data from server and invokes convenient action /// </summary> private void RecieveFromServer() { while (EndConnectionFlag == 0) { CheckForIllegalCrossThreadCalls = false; try { byte[] MsgBt = new byte[1024]; int size = ClientSocket.Receive(MsgBt); RecievedMsg = Encoding.ASCII.GetString(MsgBt, 0, size); string[] RecievedMsgArray = RecievedMsg.Split(';'); //Create a room if (RecievedMsg.IndexOf("room") > -1) { RoomForm roomForm = new RoomForm(ClientSocket, RecievedMsg, listboxCategory.SelectedItem.ToString(), listboxDifficulty.SelectedItem.ToString()); Invoke((MethodInvoker) delegate() { RecieveThread.Suspend(); this.Hide(); roomForm.Show(); roomForm.FormClosed += RoomForm_FormClosed; }); } //Joined a room if (RecievedMsg.IndexOf("joined") > -1) { PopUpMessage.Close(); RoomForm roomForm = new RoomForm(ClientSocket, RecievedMsg, listViewAllRooms.SelectedItems[0].SubItems[1].Text, listViewAllRooms.SelectedItems[0].SubItems[2].Text); Invoke((MethodInvoker) delegate() { gamer2 = RecievedMsgArray[2]; RecieveThread.Suspend(); this.Hide(); roomForm.Show(); roomForm.FormClosed += RoomForm_FormClosed; }); } //Watch a game if (RecievedMsg.IndexOf("Watch_game") > -1) { RoomForm roomForm = new RoomForm(ClientSocket, RecievedMsg, listViewAllRooms.SelectedItems[0].SubItems[1].Text, listViewAllRooms.SelectedItems[0].SubItems[2].Text); Invoke((MethodInvoker) delegate() { RecieveThread.Suspend(); this.Hide(); roomForm.Show(); roomForm.FormClosed += RoomForm_FormClosed; }); } //Show available rooms if (RecievedMsg.IndexOf("refresh") > -1) { string AllRoomsSerialized = RecievedMsg.Split(';')[1]; ShowRooms(AllRoomsSerialized); } //Pressed wrong choice (Join/Watch) if (RecievedMsg.IndexOf("sorry") > -1) { PopUpMessage.Close(); PopUpMessage = null; PopUpMessage = new PopUpMessage("Sorry. This room is not available now."); PopUpMessage.DisableButtons(); PopUpMessage.ShowDialog(); AvailableRooms(); } //Owner refused the Join request if (RecievedMsg.IndexOf("refused") > -1) { PopUpMessage.Close(); PopUpMessage = null; PopUpMessage = new PopUpMessage("Sorry. Your request was declined."); PopUpMessage.DisableButtons(); PopUpMessage.ShowDialog(); AvailableRooms(); } } catch (EndOfStreamException) { MessageBox.Show("Connection ended. Server is down."); EndConnectionFlag = 1; } catch (IOException) { MessageBox.Show("Connection ended. Server is down."); EndConnectionFlag = 1; } } }