}//endenemyplaymethod /// <summary> /// Checks if the given ship is destroyed or not. Removes ship if it is /// </summary> /// <param name="r"></param> /// <param name="player"></param> /// <param name="index"></param> private void CheckDestroyed(GridCards r, string player, int index) { //Is it the blue or red player ship we are checking for death if (player == "red") { //Checks if number of pegs exceeds ships HP, if so, delete ship. if (r.Alive == false) { redPlayer_.GridList.RemoveAt(index); textBoxLog.AppendText("You have destroyed an enemy ship!"); textBoxLog.AppendText(Environment.NewLine); blueVictoryCount++; } } else { //Checks if number of pegs exceeds ships HP, if so, delete ship. if (r.Alive == false) { bluePlayer_.GridList.RemoveAt(index); textBoxLog.AppendText("The enemy has destroyed your ship!"); textBoxLog.AppendText(Environment.NewLine); redVictoryCount++; } } }
/// <summary> /// Generates a new grid /// </summary> private void GenerateGrid() { //new list for shuffling BindingList <GridCards> unshuffledGridList_; unshuffledGridList_ = new BindingList <GridCards>(); //Add one card for each of the ships GridCards p = new GridCards(0); unshuffledGridList_.Add(p); p = new GridCards(1); unshuffledGridList_.Add(p); p = new GridCards(2); unshuffledGridList_.Add(p); p = new GridCards(3); unshuffledGridList_.Add(p); p = new GridCards(4); unshuffledGridList_.Add(p); //Add seven ocean cards for (int i = 0; i < 7; i++) { unshuffledGridList_.Add(new GridCards(5)); } //Shuffle it Random random = new Random(); //While the list still has cards in it while (unshuffledGridList_.Count > 0) { //randomly choose one card and add it to other list int cardIndex = random.Next(unshuffledGridList_.Count); gridList_.Add(unshuffledGridList_[cardIndex]); //remove card from deck unshuffledGridList_.RemoveAt(cardIndex); } }
}//endplay /// <summary> /// Plays the selected card. /// </summary> private void PlayCard() { //gets the selected index of the listboxes int blueGridIndex = listBoxBlueGrid.SelectedIndex; int redGridIndex = listBoxRedGrid.SelectedIndex; int handIndex = listBoxHand.SelectedIndex; PlayableCards p = bluePlayer_.HandList[handIndex]; GridCards r = redPlayer_.GridList[redGridIndex]; GridCards b = bluePlayer_.GridList[blueGridIndex]; //Makes sure that a shield isn't trying to be used on a hidden grid card if (p.Type_ == "Shield" && bluePlayer_.GridList[blueGridIndex].FaceDown == true) { labelTurn.Text = "Can't play shield on a hidden grid card."; validTurn = false; //ends this function return; } //Checks if the hand card is a peg card if (p is PegCards) { //if the grid card is face down if (r.FaceDown == true) { //sets the grid to face up r.FaceDown = false; textBoxLog.AppendText("You search the computer's ocean."); textBoxLog.AppendText(Environment.NewLine); //if the played card is a white peg and the targeted grid is a submarine if (r.Type_ == "Submarine" && p.Type_ == "White") { //Attach the card to the ship r.AttachedCards.Add(p); } //if the played card is a red peg, and it is not a miss or a submarine else if (r.Type_ != "MISS" && r.Type_ != "Submarine" && p.Type_ == "Red") { //Attach the card to the ship r.AttachedCards.Add(p); CheckDestroyed(r, "red", redGridIndex); } } //else if card is face up else { //If the player is using a shield if (p.Type_ == "Shield") { //if the player tries using a shield on a hidden spot or a miss if (b.FaceDown == true || b.Type_ == "MISS") { labelTurn.Text = "Can't use a shield on that."; validTurn = false; return; } //otherwise else { b.AttachedCards.Add(p); textBoxLog.AppendText("You place a shield on your ship."); textBoxLog.AppendText(Environment.NewLine); } } else { //if the player tries to attack an empty spot if (r.Type_ == "MISS") { labelTurn.Text = "Don't waste a card on an empty spot!"; validTurn = false; return; } //if the ship is a submarine if (r.Type_ == "Submarine") { //if the played card is a white peg if (p.Type_ == "White") { //Attach the card to the ship r.AttachedCards.Add(p); textBoxLog.AppendText("You attack the computer's Submarine with a white peg."); textBoxLog.AppendText(Environment.NewLine); CheckDestroyed(r, "red", redGridIndex); } else { labelTurn.Text = "You cannot attack a Submarine with a red peg card."; validTurn = false; return; } } //else if the ship is something else else { //if the played card is a red peg if (p.Type_ == "Red") { //Attach the card to the ship r.AttachedCards.Add(p); textBoxLog.AppendText("You attack the computer's ship with a red peg."); textBoxLog.AppendText(Environment.NewLine); //Check for ship death CheckDestroyed(r, "red", redGridIndex); } else { labelTurn.Text = "You cannot attack a non-Submarine with a white peg card."; validTurn = false; return; } } } } //refreshes listbox RefreshListBoxes(); bluePlayer_.HandList.RemoveAt(handIndex); if (turnCounter < 2) { //redraws hand, unless turn counter is at 1 which means that the special card was in effect bluePlayer_.DrawCards(); } }//endifpeg //otherwise if a special card is played else if (p is SpecialCards) { // Create new form for the dialog SpecialCardForm dialog = new SpecialCardForm(p.Type_); // Opens the dialog window dialog.ShowDialog(); //If the user successfully chose an option if (dialog.DialogResult == DialogResult.OK) { //If the first option was chosen if (dialog.ReturnValue == 1) { //if it was the type 1 card if (p.Type_ == "SpecialCard1") { //Discard white peg cards. WhitePegForm whitePegDialog = new WhitePegForm(bluePlayer_.HandList, handIndex); //if user select correct stuff if (whitePegDialog.ShowDialog() == DialogResult.OK) { //replace current handlist with new handlist. bluePlayer_.HandList.Clear(); foreach (PlayableCards c in whitePegDialog.ReturnValue) { bluePlayer_.HandList.Add(c); } textBoxLog.AppendText("You discard white peg(s) from your hand."); textBoxLog.AppendText(Environment.NewLine); //Refresh RefreshListBoxes(); //Redraws hand bluePlayer_.DrawCards(); } else { labelTurn.Text = "Please select only white peg cards."; validTurn = false; } } //if it was the other card else { //Repair a ship, then play a card //if a face up ship is selected, and it has damage on it if (b.Type_ != "MISS" && b.FaceDown == false && b.TotalDamage > 0) { //gets the last peg card attached to it bool pegCardFound = false; //loop downwards through the list for (int i = b.AttachedCards.Count - 1; i >= 0; i--) { if (b.AttachedCards[i].Type_ != "Shield" && pegCardFound == false) { pegCardFound = true; //remove the card b.AttachedCards.RemoveAt(i); } } textBoxLog.AppendText("You remove a peg from your ship."); textBoxLog.AppendText(Environment.NewLine); //refresh RefreshListBoxes(); bluePlayer_.HandList.RemoveAt(handIndex); //adds a turn turnCounter++; //notifies player they can play again textBoxLog.AppendText("You can play another card."); textBoxLog.AppendText(Environment.NewLine); } //was not a valid turn else { labelTurn.Text = "Select a damaged ship first."; validTurn = false; } } } //if the second option was chosen else { //if it was the type 1 card if (p.Type_ == "SpecialCard1") { //Can only play if hand has more than 2 cards if (bluePlayer_.HandList.Count > 2) { //Removes card from hand bluePlayer_.HandList.RemoveAt(handIndex); //Gives 2 turns so player can play 2 cards turnCounter += 2; textBoxLog.AppendText("You can play another card."); textBoxLog.AppendText(Environment.NewLine); } //was not a valid turn else { labelTurn.Text = "Cannot play that special card option without more than 2 cards."; validTurn = false; } } //if it was the other card else { //Draw 3 cards then play one //Removes card from hand bluePlayer_.HandList.RemoveAt(handIndex); //draws 3 cards bluePlayer_.Draw3Cards(); textBoxLog.AppendText("You draw 3 cards."); textBoxLog.AppendText(Environment.NewLine); textBoxLog.AppendText("You can play another card."); textBoxLog.AppendText(Environment.NewLine); turnCounter++; } } } else { //was not a valid turn validTurn = false; } } }//endplaymethod