Advertisement
Guest User

Untitled

a guest
May 27th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4. using System.Diagnostics;
  5.  
  6. namespace Battleshipv3
  7. {
  8. public partial class Level1 : Form
  9. {
  10. List<Button> playerShips;
  11. List<Button> enemyShips;
  12. Random rand = new Random();
  13. int playerShipsNo = 3;
  14. int enemyShipsNo = 3;
  15. int rounds = 50;
  16. int playerScore = 0;
  17. int enemyScore = 0;
  18.  
  19.  
  20. public Level1()
  21. {
  22. InitializeComponent();
  23. Attack.Hide();
  24. 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 };
  25. 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 };
  26. enemyPositionPicker.Start();
  27. for (int i = 0; i < enemyShips.Count; i++)
  28. {
  29. enemyShips[i].Tag = null;
  30. }
  31. }
  32.  
  33. //enemy chooses a random position where to attack the player
  34. private void EnemyAttackPlayer(object sender, EventArgs e)
  35. {
  36. if (playerShips.Count > 0 && rounds > 0)
  37. {
  38. rounds--;
  39. int index = rand.Next(playerShips.Count);
  40. if(playerShips[index].Tag == "playership")
  41. {
  42. playerShips[index].Enabled = false;
  43. playerShips[index].BackgroundImage = ((System.Drawing.Image)(Properties.Resources.hh));
  44. playerShips.RemoveAt(index);
  45. enemyScore++;
  46. enemyPlaytimer.Stop();
  47. }
  48. else
  49. {
  50. playerShips[index].Enabled = false;
  51. playerShips[index].BackgroundImage = ((System.Drawing.Image)(Properties.Resources.miss2));
  52. playerShips.RemoveAt(index);
  53. enemyPlaytimer.Stop();
  54. }
  55. }
  56.  
  57. if(rounds < 1 || playerScore == 3 || enemyScore == 3)
  58. {
  59. if (playerScore > enemyScore)
  60. MessageBox.Show("You won!!!", "Winning");
  61.  
  62. if(playerScore == enemyScore)
  63. MessageBox.Show("It's a draw!", "Draw");
  64.  
  65. if (playerScore < enemyScore)
  66. { for(int i = 0; i < enemyShips.Count; i++)
  67. {
  68. if(enemyShips[i].Tag == "enemyship")
  69. {
  70. enemyShips[i].BackgroundImage = ((System.Drawing.Image)(Properties.Resources.hh));
  71. }
  72. }
  73. MessageBox.Show("You lost!", "Losing");
  74. }
  75. }
  76.  
  77. }
  78.  
  79. //enemy places ships randomly
  80. private void EnemyPicksPosition(object sender, EventArgs e)
  81. {
  82.  
  83. int index = rand.Next(enemyShips.Count);
  84. if(enemyShips[index].Enabled == true && enemyShips[index].Tag == null)
  85. {
  86. enemyShips[index].Tag = "enemyship";
  87. enemyShipsNo--;
  88. }
  89. else
  90. index = rand.Next(enemyShips.Count);
  91.  
  92. if (enemyShipsNo < 1)
  93. enemyPositionPicker.Stop();
  94. }
  95.  
  96. //player picks position of his ships
  97. private void PlayerPicksPosition(object sender, EventArgs e)
  98. {
  99. if(playerShipsNo > 0)
  100. {
  101. var button = (Button)sender;
  102. button.Enabled = false;
  103. button.Tag = "playership";
  104. button.BackColor = System.Drawing.Color.DarkSlateBlue;
  105. playerShipsNo--;
  106. }
  107.  
  108. if(playerShipsNo == 0)
  109. {
  110. Message.Hide();
  111. Attack.Show();
  112. }
  113.  
  114. }
  115.  
  116. //player attacks enemy
  117. private void attackEnemyPosition(object sender, EventArgs e)
  118. {
  119. var button = (Button)sender;
  120. Attack.Show();
  121. if (button.Enabled && rounds > 0)
  122. {
  123. rounds--;
  124.  
  125. if (button.Tag == "enemyship")
  126. {
  127. button.Enabled = false;
  128. button.BackColor = System.Drawing.Color.DarkSlateBlue;
  129. button.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.hh));
  130. playerScore++;
  131. enemyPlaytimer.Start();
  132. }
  133. else
  134. {
  135. button.Enabled = false;
  136. button.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.miss2));
  137. enemyPlaytimer.Start();
  138. }
  139. }
  140. }
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement