// AI logic private void AIRecieveAction(GameLogic.recieveMessages msg, object data) { switch (msg) { case recieveMessages.alreadyShot: goto case recieveMessages.shotCoords; case recieveMessages.badCoords: goto case recieveMessages.shotCoords; case recieveMessages.shotCoords: Coordinate coord = possibleShots[rand.Next(0, possibleShots.Count)]; SendGameloopAction(sendMessages.shotCoords, coord); break; case recieveMessages.hit: if (AILevel != AILevels.Easy) { var tempCoord = (Coordinate)data; // remove possible directions where it was not shot sectionsHit.Add(tempCoord); if (sectionsHit.Count != 1) { int colDiff = tempCoord.col - sectionsHit[0].col; int rowDiff = tempCoord.row - sectionsHit[0].row; if (colDiff == 0) { possibleDirections.Remove(Directions.E); possibleDirections.Remove(Directions.W); } else if (rowDiff == 0) { possibleDirections.Remove(Directions.N); possibleDirections.Remove(Directions.S); } } if (AILevel == AILevels.Hard) { turnsUntilForce = turnsUntilForceDefault; forceNextTurn = false; } } break; case recieveMessages.miss: if (AILevel != AILevels.Easy) { var tempCoord = (Coordinate)data; // remove a possible directions if that is where it was shot if (sectionsHit.Count != 0) { int colDiff = tempCoord.col - sectionsHit.Last().col; int rowDiff = tempCoord.row - sectionsHit.Last().row; if (colDiff == -1) { possibleDirections.Remove(Directions.W); } else if (colDiff == 1) { possibleDirections.Remove(Directions.E); } else if (rowDiff == 1) { possibleDirections.Remove(Directions.S); } else if (rowDiff == -1) { possibleDirections.Remove(Directions.N); } } if (AILevel == AILevels.Hard) { turnsUntilForce -= 1; if (turnsUntilForce <= 0) { forceNextTurn = true; } } } break; case recieveMessages.sunk: if (AILevel != AILevels.Easy) { // reset values and remove the boat from possible ones possibleLengths.Remove(sectionsHit.Count); possibleDirections.Clear(); possibleDirections.AddRange(new List <Directions> { Directions.N, Directions.E, Directions.S, Directions.W }); sectionsHit.Clear(); if (AILevel == AILevels.Hard) { turnsUntilForce = turnsUntilForceDefault; forceNextTurn = false; } } break; default: break; } }
public void RecieveGameResponse(GameLogic.recieveMessages msg, object data = null) { switch (msg) { // do something depending on each event, most of them are self explanatory case GameLogic.recieveMessages.won: writeToLog($"{playerName} won the game!"); MessageBox.Show($"You won the game against {opponentName}!", "Congratulations!"); this.Close(); selectingShot = false; break; case GameLogic.recieveMessages.lost: writeToLog($"{opponentName} won the game!"); MessageBox.Show($"You lost the game against {opponentName}!", "Better luck next time"); this.Close(); selectingShot = false; break; case GameLogic.recieveMessages.hit: var tempCoord = (GameLogic.Coordinate)data; opponentTableLabels[tempCoord.col, tempCoord.row].Text = "✕"; writeToLog($"{playerName} hit {opponentName}'s boat at {tempCoord}"); ChangeShotStatus("Shot hit!", 0); selectingShot = false; break; case GameLogic.recieveMessages.sunk: tempCoord = (GameLogic.Coordinate)data; opponentTableLabels[tempCoord.col, tempCoord.row].Text = "✕"; writeToLog($"{playerName} hit {opponentName}'s boat at {tempCoord}"); writeToLog($"{playerName} sank one of {opponentName}'s ships!"); ChangeShotStatus("Ship sunk!", 1); selectingShot = false; break; case GameLogic.recieveMessages.miss: tempCoord = (GameLogic.Coordinate)data; opponentTableLabels[tempCoord.col, tempCoord.row].Text = "○"; writeToLog($"{playerName} shot at {tempCoord} and missed"); ChangeShotStatus("Shot missed!", 2); selectingShot = false; break; case GameLogic.recieveMessages.opponentHit: tempCoord = (GameLogic.Coordinate)data; playerTableLabels[tempCoord.col, tempCoord.row].Text = "✕"; writeToLog($"{opponentName} hit {playerName}'s boat at {tempCoord}"); ChangeOpponentStatus("The opponent hit one of your boats!", 2); selectingShot = false; break; case GameLogic.recieveMessages.opponentMiss: tempCoord = (GameLogic.Coordinate)data; playerTableLabels[tempCoord.col, tempCoord.row].Text = "○"; writeToLog($"{opponentName} shot at {tempCoord} and missed"); ChangeOpponentStatus("The opponent missed his shot!", 0); selectingShot = false; break; case GameLogic.recieveMessages.opponentSunk: tempCoord = (GameLogic.Coordinate)data; playerTableLabels[tempCoord.col, tempCoord.row].Text = "✕"; ChangeOpponentStatus("The opponent just destroyed one of your boats!", 3); writeToLog($"{opponentName} hit {playerName}'s boat at {tempCoord}"); writeToLog($"{opponentName} sank one of {playerName}'s ships!"); selectingShot = false; break; case GameLogic.recieveMessages.badCoords: ChangeShotStatus("Those coordinates are bad!"); goto case GameLogic.recieveMessages.shotCoords; case GameLogic.recieveMessages.alreadyShot: ChangeShotStatus("Those coordinates have already been chosen!"); goto case GameLogic.recieveMessages.shotCoords; case GameLogic.recieveMessages.shotCoords: currentActionLabel.Text = "CurrentAction:\nSelect a cell to shoot at..."; selectingShot = true; break; case GameLogic.recieveMessages.waiting: currentActionLabel.Text = "CurrentAction:\nWaiting for opponent..."; selectingShot = false; break; default: break; } return; }