Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Windows.Forms;
- using System.Diagnostics;
- namespace Battleshipv3
- {
- public partial class Level1 : Form
- {
- List<Button> playerShips;
- List<Button> enemyShips;
- Random rand = new Random();
- int playerShipsNo = 3;
- int enemyShipsNo = 3;
- int rounds = 50;
- int playerScore = 0;
- int enemyScore = 0;
- public Level1()
- {
- InitializeComponent();
- Attack.Hide();
- playerShips = new List<Button> { a1, a2, a3, a4, a5, b1, b2, b3, b4, b5, c1, c2, c3, c4, c5, d1, d2, d3, d4, d5, e1, e2, e3, e4, e5 };
- enemyShips = new List<Button> { w1, w2, w3, w4, w5, x1, x2, x3, x4, x5, y1, y2, y3, y4, y5, z1, z2, z3, z4, z5, q1, q2, q3, q4, q5 };
- enemyPositionPicker.Start();
- for (int i = 0; i < enemyShips.Count; i++)
- {
- enemyShips[i].Tag = null;
- }
- }
- //enemy chooses a random position where to attack the player
- private void EnemyAttackPlayer(object sender, EventArgs e)
- {
- if (playerShips.Count > 0 && rounds > 0)
- {
- rounds--;
- int index = rand.Next(playerShips.Count);
- if(playerShips[index].Tag == "playership")
- {
- playerShips[index].Enabled = false;
- playerShips[index].BackgroundImage = ((System.Drawing.Image)(Properties.Resources.hh));
- playerShips.RemoveAt(index);
- enemyScore++;
- enemyPlaytimer.Stop();
- }
- else
- {
- playerShips[index].Enabled = false;
- playerShips[index].BackgroundImage = ((System.Drawing.Image)(Properties.Resources.miss2));
- playerShips.RemoveAt(index);
- enemyPlaytimer.Stop();
- }
- }
- if(rounds < 1 || playerScore == 3 || enemyScore == 3)
- {
- if (playerScore > enemyScore)
- MessageBox.Show("You won!!!", "Winning");
- if(playerScore == enemyScore)
- MessageBox.Show("It's a draw!", "Draw");
- if (playerScore < enemyScore)
- { for(int i = 0; i < enemyShips.Count; i++)
- {
- if(enemyShips[i].Tag == "enemyship")
- {
- enemyShips[i].BackgroundImage = ((System.Drawing.Image)(Properties.Resources.hh));
- }
- }
- MessageBox.Show("You lost!", "Losing");
- }
- }
- }
- //enemy places ships randomly
- private void EnemyPicksPosition(object sender, EventArgs e)
- {
- int index = rand.Next(enemyShips.Count);
- if(enemyShips[index].Enabled == true && enemyShips[index].Tag == null)
- {
- enemyShips[index].Tag = "enemyship";
- enemyShipsNo--;
- }
- else
- index = rand.Next(enemyShips.Count);
- if (enemyShipsNo < 1)
- enemyPositionPicker.Stop();
- }
- //player picks position of his ships
- private void PlayerPicksPosition(object sender, EventArgs e)
- {
- if(playerShipsNo > 0)
- {
- var button = (Button)sender;
- button.Enabled = false;
- button.Tag = "playership";
- button.BackColor = System.Drawing.Color.DarkSlateBlue;
- playerShipsNo--;
- }
- if(playerShipsNo == 0)
- {
- Message.Hide();
- Attack.Show();
- }
- }
- //player attacks enemy
- private void attackEnemyPosition(object sender, EventArgs e)
- {
- var button = (Button)sender;
- Attack.Show();
- if (button.Enabled && rounds > 0)
- {
- rounds--;
- if (button.Tag == "enemyship")
- {
- button.Enabled = false;
- button.BackColor = System.Drawing.Color.DarkSlateBlue;
- button.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.hh));
- playerScore++;
- enemyPlaytimer.Start();
- }
- else
- {
- button.Enabled = false;
- button.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.miss2));
- enemyPlaytimer.Start();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement