/// <summary> /// Timer /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer_countdown_Tick(object sender, EventArgs e) { time_countdown--; lblTime.Text = time_countdown.ToString(); if (time_countdown == 0) { timer_countdown.Stop(); time_countdown = 6; if (playerTurn == enPlayerTurn.Player1) { lblNameP2.BackColor = Color.LimeGreen; P2.score++; winner = enWinner.Player2; } else if (playerTurn == enPlayerTurn.Player2) { lblNameP1.BackColor = Color.LimeGreen; P1.score++; winner = enWinner.Player1; } MetroMessageBox.Show(this, winner.ToString(), "WINNER", 90); OnNewGame(); } }
/// <summary> /// Start new game /// </summary> private void OnNewGame() { if (gameMode == enGameMode.Socket_Create || gameMode == enGameMode.Socket_Connect) { byte[] message = new byte[256]; string incomingMsg; frmEntryForm.socket.Send(Encoding.UTF8.GetBytes(" Score?P1")); frmEntryForm.socket.Receive(message); incomingMsg = Edit_IncomingMessage(message); P1.score = Convert.ToInt32(incomingMsg); message = new byte[256]; frmEntryForm.socket.Send(Encoding.UTF8.GetBytes(" Score?P2")); frmEntryForm.socket.Receive(message); incomingMsg = Edit_IncomingMessage(message); P2.score = Convert.ToInt32(incomingMsg); } // Socket game mode control LabelRegulations(); Label[] allLabels = { lbl0, lbl1, lbl2, lbl3, lbl4, lbl5, lbl6, lbl7, lbl8 }; foreach (Label label in allLabels) { label.Text = null; label.BackColor = Color.LightGreen; } // Label reset switch (starting) { case enStarting.None: case enStarting.Player2: playerTurn = enPlayerTurn.Player1; starting = enStarting.Player1; ShowTurn(playerTurn.ToString()); break; case enStarting.Player1: playerTurn = enPlayerTurn.Player2; starting = enStarting.Player2; ShowTurn(playerTurn.ToString()); break; default: break; } if (gameMode == enGameMode.Timed) { timer.Start(); timer.Interval = 500; timer_countdown.Start(); timer_countdown.Interval = 1000; // timer settings pnlTime.Show(); } // Timed gamed control winner = enWinner.None; }
/// <summary> /// Computer game mode /// </summary> private void Computer() { Label[] allLabels = { lbl0, lbl1, lbl2, lbl3, lbl4, lbl5, lbl6, lbl7, lbl8 }; Label[] emptyLabels = new Label[9]; int empty = -1; //Clear all game board cells foreach (Label label in allLabels) { if (label.Text == "") { emptyLabels[++empty] = label; } } Random rand = new Random(); if (empty != -1) { emptyLabels[rand.Next(empty)].Text = P2.choice; } winner = GetWinner(); if (winner != enWinner.None) { playerTurn = enPlayerTurn.None; OnNewGame(); if (starting == enStarting.Player2) { Computer(); } } playerTurn = enPlayerTurn.Player1; ShowTurn(playerTurn.ToString()); }
/// <summary> /// Clicking refres button /// </summary> /// <param name="sender">Clicked button(refresh button)</param> /// <param name="e">Event args</param> private void btnRefresh_Click(object sender, EventArgs e) { LabelXO_Placement(); byte[] message = new byte[256]; string incomingMsg; frmEntryForm.socket.Send(Encoding.UTF8.GetBytes(" Play?")); frmEntryForm.socket.Receive(message); incomingMsg = Edit_IncomingMessage(message); winner = GetWinner(); if (winner != enWinner.None) { foreach (var server in Process.GetProcessesByName("GameServer")) { server.Kill(); } this.Close(); } else if (playerTurn == enPlayerTurn.Player1) { if (incomingMsg == "NY") // Y = Yes ; N = No { startGame = true; ShowTurn(playerTurn.ToString()); } else if (gameMode == enGameMode.Socket_Create && incomingMsg == "--") { startGame = true; ShowTurn(playerTurn.ToString()); lblChoiceP1.BackColor = Color.IndianRed; lblChoiceP2.BackColor = Color.Transparent; } else if (gameMode == enGameMode.Socket_Connect && incomingMsg == "YN") { startGame = true; playerTurn = enPlayerTurn.Player2; ShowTurn(playerTurn.ToString()); } else if (gameMode == enGameMode.Socket_Connect && incomingMsg == "--") { MetroMessageBox.Show(this, "Player1 hasn't played yet", "WAITING...", 90); } else { MetroMessageBox.Show(this, "Player2 hasn't played yet", "WAITING...", 90); } } else if (playerTurn == enPlayerTurn.Player2) { if (incomingMsg == "YN") { startGame = true; ShowTurn(playerTurn.ToString()); } else { MetroMessageBox.Show(this, "Player1 hasn't played yet", "WAITING...", 90); } } else { } refreshGame = true; }
/// <summary> /// Socket game mode /// </summary> /// <param name="clickedLabel">Clicked label information</param> private void OnClick_Socket(Label clickedLabel) { string labelName = clickedLabel.Name; string labelText = clickedLabel.Text; if (playerTurn == enPlayerTurn.Player1) { clickedLabel.Text = P1.choice; } else { clickedLabel.Text = P2.choice; } winner = GetWinner(); labelText = clickedLabel.Text; switch (gameMode) { case enGameMode.Socket_Create: if (frmEntryForm.socket.Connected) { string message_Create = " " + labelName + labelText + "P1"; frmEntryForm.socket.Send(Encoding.UTF8.GetBytes(message_Create)); } if (winner == enWinner.None) { LabelXO_Placement(); ShowTurn("Player2"); } else { this.Close(); } break; case enGameMode.Socket_Connect: if (frmEntryForm.socket.Connected) { string message_Connect = " " + labelName + labelText + "P2"; frmEntryForm.socket.Send(Encoding.UTF8.GetBytes(message_Connect)); } if (winner == enWinner.None) { LabelXO_Placement(); ShowTurn("Player1"); } else { this.Close(); } break; default: break; } }
/// <summary> /// Clicking X-Os /// </summary> /// <param name="sender">Clicked label</param> /// <param name="e">Event args</param> private void OnClick(object sender, EventArgs e) { Label clickedLabel = (Label)sender; string labelName = clickedLabel.Name; string labelText = clickedLabel.Text; if (gameMode == enGameMode.Socket_Connect || gameMode == enGameMode.Socket_Create) { if (refreshGame && startGame) { if (playerTurn == enPlayerTurn.None || labelText != "") { return; } OnClick_Socket(clickedLabel); } else { MetroMessageBox.Show(this, "Please click the \"Refresh\" button", "", 90); } refreshGame = false; startGame = false; } // Socket game mode control else { if (playerTurn == enPlayerTurn.None || labelText != "") { return; } else if (playerTurn == enPlayerTurn.Player1) { clickedLabel.Text = P1.choice; ShowTurn(playerTurn.ToString()); } else { clickedLabel.Text = P2.choice; ShowTurn(playerTurn.ToString()); } winner = GetWinner(); labelText = clickedLabel.Text; switch (gameMode) { case enGameMode.Computer: if (winner == enWinner.None) { Computer(); } else if (winner != enWinner.None) { playerTurn = enPlayerTurn.None; OnNewGame(); if (starting == enStarting.Player2) { Computer(); } } break; case enGameMode.Friend: if (winner == enWinner.None) { //Change turns playerTurn = (enPlayerTurn.Player1 == playerTurn) ? enPlayerTurn.Player2 : enPlayerTurn.Player1; ShowTurn(playerTurn.ToString()); } else { playerTurn = enPlayerTurn.None; OnNewGame(); } break; case enGameMode.Timed: time = -1; time_countdown = 6; if (winner == enWinner.None) { //Change turns playerTurn = (enPlayerTurn.Player1 == playerTurn) ? enPlayerTurn.Player2 : enPlayerTurn.Player1; ShowTurn(playerTurn.ToString()); } else { playerTurn = enPlayerTurn.None; OnNewGame(); } break; default: break; } // controls } // Other game modes control }
/// <summary> /// Winner control /// </summary> private enWinner GetWinner() { Label[] allWinningMoves = { //Check each row lbl0, lbl1, lbl2, lbl3, lbl4, lbl5, lbl6, lbl7, lbl8, //Columns lbl0, lbl3, lbl6, lbl1, lbl4, lbl7, lbl2, lbl5, lbl8, //Diagonal lbl0, lbl4, lbl8, lbl2, lbl4, lbl6 }; //All winning moves X-O for (int i = 0; i < allWinningMoves.Length; i += 3) { if (allWinningMoves[i].Text != "") { if (allWinningMoves[i].Text == allWinningMoves[i + 1].Text && allWinningMoves[i].Text == allWinningMoves[i + 2].Text) { allWinningMoves[i].BackColor = Color.LimeGreen; allWinningMoves[i + 1].BackColor = Color.LimeGreen; allWinningMoves[i + 2].BackColor = Color.LimeGreen; if (allWinningMoves[i].Text == "X") { if (P1.choice == "X") { lblNameP1.BackColor = Color.LimeGreen; P1.score++; winner = enWinner.Player1; } else { lblNameP2.BackColor = Color.LimeGreen; P2.score++; winner = enWinner.Player2; } } else { if (P1.choice == "O") { lblNameP1.BackColor = Color.LimeGreen; P1.score++; winner = enWinner.Player1; } else { lblNameP2.BackColor = Color.LimeGreen; P2.score++; winner = enWinner.Player2; } } pnlTime.Hide(); timer.Stop(); timer_countdown.Stop(); if (gameMode == enGameMode.Computer && winner == enWinner.Player2) { MetroMessageBox.Show(this, "Computer", "WINNER", 90); } else { MetroMessageBox.Show(this, winner.ToString(), "WINNER", 90); } lblScoreP1.Text = P1.score.ToString(); lblScoreP2.Text = P2.score.ToString(); return(winner); } // Winner } } // X-O controls Label[] allLabels = { lbl0, lbl1, lbl2, lbl3, lbl4, lbl5, lbl6, lbl7, lbl8 }; foreach (Label label in allLabels) { if (label.Text == "") { return(enWinner.None); } ; // None } // Is empty controls // Draw lblNameDraw.BackColor = Color.LimeGreen; lblScoreDraw.Text = (++drawScore).ToString(); pnlTime.Hide(); timer.Stop(); timer_countdown.Stop(); MetroMessageBox.Show(this, "DRAW", "RESULT", 90); return(enWinner.Draw); }