/// <summary> /// Setting the viewmodel /// </summary> /// <param name="m"></param> public void SetVM(MazeViewModel m) { myVM = m as MultiViewModel; this.DataContext = myVM; MultiMazeModel tempModel = myVM.MyModel as MultiMazeModel; //Delegate for moving opponent's player on his board tempModel.OpponentMoved += delegate(Object sender, Key e) { int close = 10; this.Dispatcher.Invoke(() => { close = OpponentBoard.MovePlayer(e); //Opponent won if (close == -1 || close == 2) { LoserWindow lw = new LoserWindow(); lw.ShowDialog(); BackToMain(); } //Opponent closed game if (close == -2) { OtherPlayerQuit(); } }); }; myVM.VM_Maze = myVM.MyModel.MazeString(); }
private void TakeShot(object sender, EventArgs e) { // consider adding a check for type in the future to made it more modular Label selected = (Label)sender; // create a Coords for the player shot and send it to the opponent if (selected.BackColor == Color.DodgerBlue) { Coords playerShot = new Coords(OpponentBoard.GetRow(selected), OpponentBoard.GetColumn(selected)); int result = AI.ResolveShot(playerShot); UpdateLabel(true, result); // if a miss, chance to miss color, otherwise turn hit color and check for sunk if (result == 0) { selected.BackColor = Color.AliceBlue; } else { selected.BackColor = Color.Red; // adjust the ship labels if a ship was sunk if (result > 1) { switch (result) { case 2: OpponentPTBoatLabel.Font = new Font(OpponentPTBoatLabel.Font, FontStyle.Strikeout); break; case 3: OpponentSubmarineLabel.Font = new Font(OpponentSubmarineLabel.Font, FontStyle.Strikeout); break; case 4: OpponentDestroyerLabel.Font = new Font(OpponentDestroyerLabel.Font, FontStyle.Strikeout); break; case 5: OpponentBattleshipLabel.Font = new Font(OpponentBattleshipLabel.Font, FontStyle.Strikeout); break; case 6: OpponentCarrierLabel.Font = new Font(OpponentCarrierLabel.Font, FontStyle.Strikeout); break; default: break; } } } } // if the have already selected that space it will not be the normal color and we can discout their click else { return; } if (AI.CheckLose()) { MessageBox.Show("You won!", "Victory!"); Close(); } // now the opponent shoots at us **temporary code *** Coords opponentShot = AI.TakeTurn(); int shotResult = ResolveShot(opponentShot); AI.Report(shotResult); UpdateLabel(false, shotResult); Control label = BoardTable.GetControlFromPosition(opponentShot.x, opponentShot.y); // if a miss, chance to miss color, otherwise turn hit color and check for sunk if (shotResult == 0) { label.BackColor = Color.AliceBlue; } else { label.BackColor = Color.Red; // adjust the ship labels if a ship was sunk if (shotResult > 1) { switch (shotResult) { case 2: PlayerPTBoatLabel.Font = new Font(PlayerPTBoatLabel.Font, FontStyle.Strikeout); break; case 3: PlayerSubmarineLabel.Font = new Font(PlayerSubmarineLabel.Font, FontStyle.Strikeout); break; case 4: PlayerDestroyerLabel.Font = new Font(PlayerDestroyerLabel.Font, FontStyle.Strikeout); break; case 5: PlayerBattleshipLabel.Font = new Font(PlayerBattleshipLabel.Font, FontStyle.Strikeout); break; case 6: PlayerCarrierLabel.Font = new Font(PlayerCarrierLabel.Font, FontStyle.Strikeout); break; default: break; } } } if (shipsSunk > 4) { MessageBox.Show("You lost, better luck next time", "Defeat"); Close(); } }
/// <summary> /// Creating the maze boards for window /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MazeBoard_Loaded(object sender, RoutedEventArgs e) { Board.DrawBoard(false); OpponentBoard.DrawBoard(true); }
// place the ship if the placement is valid private void PlaceButton_Click(object sender, EventArgs e) { if (!validPlacement) { MessageBox.Show("That is an invalid placement for that ship.", "Warning"); } else { ShipInfo ship = shipsToPlace.Dequeue(); string symbol = ship.symbol; // for every coordinate reset the background color and set the symbol foreach (Coords coords in placementOutline) { Control label = BoardTable.GetControlFromPosition(coords.x, coords.y); label.BackColor = Color.DodgerBlue; label.Text = symbol; } // if the first two coords have the same y value it is horizontal, then add the ship based on the symbol // side note: I could probably find a more elegant way to do this but I was feeling tired and just needed it implemented bool horizontal = (placementOutline[0].y == placementOutline[1].y); if (symbol == "C") { playerShips.Add(new Carrier(placementOutline[0], horizontal)); } else if (symbol == "B") { playerShips.Add(new Battleship(placementOutline[0], horizontal)); } else if (symbol == "D") { playerShips.Add(new Destroyer(placementOutline[0], horizontal)); } else if (symbol == "S") { playerShips.Add(new Submarine(placementOutline[0], horizontal)); } else if (symbol == "P") { playerShips.Add(new PTBoat(placementOutline[0], horizontal)); } else { // *** implement the somethign has gone horribly wrong contingency *** } } // if all ships are placed, start the game if (shipsToPlace.Count == 0) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { BoardTable.GetControlFromPosition(i, j).Enabled = false; OpponentBoard.GetControlFromPosition(i, j).Enabled = true; } } PlaceButton.Enabled = false; shipsSunk = 0; } }