Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Diagnostics;
  11. using System.Threading;
  12.  
  13.  
  14. namespace WaTor
  15. {
  16. public partial class Form1 : Form
  17. {
  18.  
  19.  
  20. //Gritsize width(x) and height(y)
  21. const int TableX = 10;
  22. const int TableY = 10;
  23. const int pixelSizeX = 19;
  24. const int pixelSizeY = 19;
  25.  
  26. //Fish Properties
  27. int initialNumberOfFish = 2;
  28. int initialNumberOfSharks = 2;
  29. int ageToBreedFish = 5;
  30. int ageToBreedShark = 10;
  31. int yearToStarvation = 5;
  32.  
  33. int sharkScent = 3;
  34.  
  35. Random rnd = new Random();
  36.  
  37. Cell[,] myArray = new Cell[TableX, TableY];
  38.  
  39. public Form1()
  40. {
  41. InitializeComponent();
  42. intitializeArray();
  43. }
  44.  
  45. private void intitializeArray()
  46. {
  47.  
  48. int x = 0; //Position on X-Axis
  49. int y = 0; //Position on Y-Axis
  50. for (int i = 0; i < myArray.GetLength(0); i++)
  51. {
  52. for (int j = 0; j < myArray.GetLength(1); j++)
  53. {
  54. Cell newCellX = new Cell();
  55. myArray[i,j] = newCellX;
  56. myArray[i,j].posX = x;
  57. myArray[i,j].posY = y;
  58. x = x + pixelSizeX+1;
  59.  
  60.  
  61. }
  62. x = 0;
  63. y = y + pixelSizeY+1;
  64.  
  65. }
  66.  
  67. }
  68.  
  69. //Only used to paint the results
  70. private void Canvas_Paint(object sender, PaintEventArgs e)
  71. {
  72.  
  73. Graphics g = Canvas.CreateGraphics();
  74. for (int i = 0; i < myArray.GetLength(0); i++)
  75. {
  76. for (int j = 0; j < myArray.GetLength(1); j++)
  77. {
  78. Brush b = new SolidBrush(myArray[i,j].cellColor);
  79. Rectangle r = new Rectangle(myArray[i,j].posX, myArray[i,j].posY, pixelSizeX, pixelSizeY);
  80. g.FillRectangle(b, r);
  81.  
  82. }
  83. }
  84.  
  85. }
  86. //Only used when initializing a new simulation
  87. public void populate()
  88. {
  89.  
  90. //Clean the grid
  91. for (int i = 0; i < myArray.GetLength(0); i++)
  92. {
  93. for (int j = 0; j < myArray.GetLength(1); j++)
  94. {
  95. kill(i, j);
  96. }
  97. }
  98. //Distribute Fish randomly
  99. int PosX=0;
  100. int PosY=0;
  101. bool isEmpty=false;
  102.  
  103. Random rndX = new Random();
  104. Random rndY = new Random();
  105. PosX = rndX.Next(0, TableX);
  106.  
  107. for (int i = 0; i < initialNumberOfFish; i++)
  108. {
  109. while (!isEmpty)
  110. {
  111. PosX = rndX.Next(TableX);
  112. PosY = rndY.Next(TableY);
  113.  
  114. if (myArray[PosX, PosY].fishType == fishTypeEnum.empty)
  115. isEmpty = true;
  116.  
  117. }
  118.  
  119. myArray[PosX, PosY].fishType = fishTypeEnum.fish;
  120. myArray[PosX, PosY].cellColor = Color.Green;
  121. myArray[PosX, PosY].age = rndX.Next(0, ageToBreedFish);
  122. isEmpty = false;
  123.  
  124. }
  125.  
  126. //Distribute Sharks randomly
  127. for (int i = 0; i < initialNumberOfSharks; i++)
  128. {
  129. while (!isEmpty)
  130. {
  131. PosX = rndX.Next(0, TableX);
  132. PosY = rndY.Next(0, TableY);
  133.  
  134. if (myArray[PosX, PosY].fishType != fishTypeEnum.shark)
  135. isEmpty = true;
  136. }
  137. myArray[PosX, PosY].fishType = fishTypeEnum.shark;
  138. myArray[PosX, PosY].cellColor = Color.Red;
  139. myArray[PosX, PosY].age = rndX.Next(0, yearToStarvation);
  140. myArray[PosX, PosY].ageStarvation = myArray[PosX, PosY].age;
  141. isEmpty = false;
  142.  
  143. }
  144.  
  145. }
  146.  
  147. //A tick is a 'round' where fish swim and breed, sharks hunt and breed
  148. public void tick()
  149. {
  150. bool reproduce;
  151.  
  152. for (int i = 0; i < myArray.GetLength(0); i++)
  153. {
  154. for (int j = 0; j < myArray.GetLength(1); j++)
  155. {
  156.  
  157. //If Cell is Shark and hasent moved
  158. if ((myArray[i, j].fishType == fishTypeEnum.shark) && (!myArray[i, j].moved))
  159. {
  160.  
  161. //Sharks starve
  162. Debug.WriteLine("Hai alter"+myArray[i,j].age);
  163. if (myArray[i, j].ageStarvation > yearToStarvation)
  164. {
  165. kill(i, j);
  166. Debug.WriteLine("Hai verhungert");
  167. }
  168.  
  169. //If not starved shark hunts
  170. hunt(i, j);
  171.  
  172. }
  173.  
  174. else
  175. //If Cell is Fish and hasent moved
  176. if ((myArray[i, j].fishType == fishTypeEnum.fish) && (!myArray[i, j].moved))
  177. {
  178. reproduce = false;
  179. if (myArray[i,j].age >= ageToBreedFish)
  180. {
  181. reproduce = true;
  182. }
  183. move(i, j, fishTypeEnum.fish, reproduce);
  184. }
  185.  
  186. }
  187. }
  188.  
  189. //End Tick
  190. for (int i = 0; i < myArray.GetLength(0); i++)
  191. {
  192. for (int j = 0; j < myArray.GetLength(1); j++)
  193. {
  194. myArray[i, j].moved = false;
  195. }
  196. }
  197.  
  198. }
  199.  
  200. private void kill(int x, int y)
  201. {
  202. myArray[x, y].age = 0;
  203. myArray[x, y].fishType = fishTypeEnum.empty;
  204. myArray[x, y].cellColor = Color.Blue;
  205. myArray[x, y].moved = false;
  206. }
  207.  
  208. private void hunt(int x, int y)
  209. {
  210. Random rnd = new Random();
  211. int direction = 0;
  212. int newX = x;
  213. int newY = y;
  214. bool moved = false;
  215.  
  216. //Check for Fish nearby
  217. direction = fishNearBy(x, y, sharkScent);
  218. Debug.WriteLine("direction: " + direction);
  219.  
  220. if (direction == 0)
  221. direction = rnd.Next(1, 5);
  222. {
  223. switch (direction)
  224. {
  225. case 1://East. If Index 0 move to TableEnde
  226. if (newY > 0)
  227. {
  228. newY = newY - 1;
  229. moved = true;
  230. }
  231. else
  232. {
  233. newY = TableY - 1;
  234. moved = true;
  235. }
  236.  
  237. break;
  238. case 2://West. If TableEnde move to 0.
  239. if (newY < TableY - 1)
  240. {
  241. newY = newY + 1;
  242. moved = true;
  243. }
  244. else
  245. {
  246. newY = 0;
  247. moved = true;
  248. }
  249. break;
  250. case 3://North. If top reached try again
  251. if (newX > 0)
  252. {
  253. newX = newX - 1;
  254. moved = true;
  255. }
  256. break;
  257. case 4://South. If bottom reached try again
  258. if (newX < TableX - 1)
  259. {
  260. newX = newX + 1;
  261. moved = true;
  262. }
  263. break;
  264. default:
  265. break;
  266. }//switch
  267. }//if
  268. if (myArray[newX, newY].fishType == fishTypeEnum.fish)
  269. {
  270. Debug.WriteLine("Fisch gefangen");
  271. eatFish(newX, newY);
  272. myArray[x, y].ageStarvation = 0; ;
  273. }
  274.  
  275. if (moved)
  276. {
  277. Debug.WriteLine("Hai bewegen");
  278. myArray[newX, newY].cellColor = myArray[x, y].cellColor;
  279. myArray[newX, newY].fishType = myArray[x, y].fishType;
  280. myArray[newX, newY].age = myArray[x, y].age + 1;
  281. myArray[newX, newY].ageStarvation = myArray[x, y].ageStarvation + 1;
  282. myArray[newX, newY].moved = true;
  283.  
  284.  
  285. //Shark will breed
  286. if (myArray[newX, newY].age >= ageToBreedShark)
  287. {
  288. myArray[newX, newY].age = 0;
  289. myArray[newX, newY].ageStarvation = 0;
  290. myArray[x, y].age = 0;
  291. myArray[x, y].ageStarvation = 0;
  292. myArray[x, y].moved = true;
  293. }
  294. else
  295. {
  296. kill(x, y);
  297. }
  298.  
  299. }
  300. }
  301.  
  302. private void eatFish(int x, int y)
  303. {
  304. kill(x, y);
  305. }
  306.  
  307. private void resetXY(ref int x, ref int y, int a,int b)
  308. {
  309. x = a;
  310. y = b;
  311. }
  312.  
  313. //Searches for fish. howFar determines how many cells away sharks can find fish
  314. private int fishNearBy(int x, int y, int howFar)
  315. {
  316. int checkX = x;
  317. int checkY = y;
  318. int howClose =howFar+2;
  319. bool fishFound = false;
  320. int direction=0; //1=East, 2=West, 3=North, 4=South
  321.  
  322. //Array for saving the direction. Distance initialized with howfar(maxlength)
  323. int[] directionArray = new int[5];
  324. for (int i = 0; i < directionArray.GetLength(0); i++)
  325. directionArray[i] = howFar + 1;
  326.  
  327.  
  328. //Check East
  329. for (int i = 0; i < howFar; i++)
  330. {
  331. if (checkY < 0)
  332. {
  333. checkY = TableY - 1;
  334. }
  335.  
  336. if (myArray[x, checkY].fishType == fishTypeEnum.fish)
  337. {
  338. //Update distance
  339. directionArray[1] = i;
  340. fishFound = true;
  341. break;
  342. }
  343. checkY--;
  344. directionArray[1] = 99;
  345. }
  346.  
  347. resetXY(ref checkX, ref checkY, x, y);
  348.  
  349. //Check West
  350. for (int i = 0; i < howFar; i++)
  351. {
  352. if (checkY >= TableY)
  353. {
  354. checkY = 0;
  355. }
  356.  
  357. if (myArray[x, checkY].fishType == fishTypeEnum.fish)
  358. {
  359. //Update distance
  360. directionArray[2] = i;
  361. fishFound = true;
  362. break;
  363. }
  364. checkY++;
  365. directionArray[2] = 99;
  366. }
  367.  
  368.  
  369. resetXY(ref checkX, ref checkY, x, y);
  370. //Check North
  371. for (int i = 0; i < howFar; i++)
  372. {
  373. if (checkX <= 0)
  374. {
  375. checkX = 0;
  376. }
  377.  
  378. if (myArray[checkX, y].fishType == fishTypeEnum.fish)
  379. {
  380. //Update distance
  381. directionArray[3] = i;
  382. fishFound = true;
  383. break;
  384. }
  385. checkX--;
  386. directionArray[3] = 99;
  387.  
  388. }
  389.  
  390. resetXY(ref checkX, ref checkY, x, y);
  391.  
  392. //Check South
  393. for (int i = 0; i < howFar; i++)
  394. {
  395. if (checkX >= TableX)
  396. {
  397. checkX = TableX-1;
  398. }
  399.  
  400. if (myArray[checkX, y].fishType == fishTypeEnum.fish)
  401. {
  402. //Update distance
  403. directionArray[4] = i;
  404. fishFound = true;
  405. break;
  406. }
  407. checkX++;
  408. directionArray[4] = 99;
  409.  
  410. }
  411. if (fishFound)
  412. {
  413. //Find the fish that is closest to the shark
  414. for (int i = 1; i < directionArray.GetLength(0); i++)
  415. {
  416. Debug.WriteLine("Fish found, Index: " + Array.IndexOf(directionArray, directionArray[i]) + " Entfernung: " + directionArray[i]);
  417. if (directionArray[i] < howClose)
  418. {
  419. Debug.WriteLine("Fish found, direction: " + Array.IndexOf(directionArray, directionArray[i]));
  420. howClose = directionArray[i];
  421.  
  422. }
  423. direction = Array.IndexOf(directionArray, howClose);
  424. }
  425.  
  426. }
  427. return direction;
  428.  
  429. }
  430.  
  431. private void move(int x, int y,fishTypeEnum type,bool repro)
  432. {
  433.  
  434. int newX=x;
  435. int newY=y;
  436. int direction = 4;
  437. bool cellIsFree = false;
  438. bool noFreeCells = false;
  439. int numberOfTries = 0;
  440. int maxTries = 8;
  441.  
  442. if ((type == fishTypeEnum.fish))
  443. {
  444. while (!cellIsFree && !noFreeCells)
  445. {
  446. direction = rnd.Next(1, 5);//Direction between 1-4
  447. switch (direction)
  448. {
  449. case 1://East. If Index 0 move to TableEnde
  450. if (newY > 0)
  451. {
  452. newY = newY - 1;
  453. }
  454.  
  455. else
  456.  
  457. newY = TableY - 1;
  458. break;
  459.  
  460. case 2://West. If TableEnde move to 0.
  461. if (newY < TableY - 1)
  462. {
  463. newY = newY + 1;
  464. }
  465.  
  466. else
  467.  
  468. newY = 0;
  469. break;
  470.  
  471. case 3://North. If top reached try again
  472. if (newX > 0)
  473. {
  474. newX = newX - 1;
  475. }
  476. break;
  477.  
  478. case 4://South. If bottom reached try again
  479. if (newX < TableX - 1)
  480. {
  481. newX = newX + 1;
  482. }
  483. break;
  484. }//switch
  485. if (myArray[newX, newY].fishType == fishTypeEnum.empty)
  486. {
  487. cellIsFree = true;
  488.  
  489. }
  490. numberOfTries++;
  491.  
  492. if (numberOfTries > maxTries)
  493. noFreeCells = true;
  494.  
  495. }//if
  496.  
  497.  
  498.  
  499. }//while (!cellIsFree)
  500.  
  501.  
  502. //Debug.WriteLine("NewX=" + newX + "NewY=" + newY);
  503. myArray[newX, newY].cellColor = myArray[x, y].cellColor;
  504. myArray[newX, newY].fishType = myArray[x, y].fishType;
  505. myArray[newX, newY].moved = true;
  506. myArray[newX, newY].age = myArray[x, y].age + 1;
  507.  
  508.  
  509. //If fish ist not reproducing remove the previous fish
  510. if (!repro)
  511. {
  512. kill(x, y);
  513. }
  514. else
  515. {
  516. myArray[newX, newY].age = 0;
  517. myArray[x, y].age = 0;
  518. myArray[x, y].moved = true;
  519. }
  520.  
  521. }
  522.  
  523. private void button2_Click(object sender, EventArgs e)
  524. {
  525.  
  526. tick();
  527. Canvas.Refresh();
  528.  
  529. }
  530.  
  531. private void button1_Click(object sender, EventArgs e)
  532. {
  533. populate();
  534. Canvas.Refresh();
  535.  
  536. }
  537.  
  538. }
  539. }
  540.  
  541. using System;
  542. using System.Collections.Generic;
  543. using System.Linq;
  544. using System.Text;
  545. using System.Threading.Tasks;
  546. using System.Drawing;
  547.  
  548. namespace WaTor
  549.  
  550. {
  551. //Fishtype
  552. enum fishTypeEnum { fish, shark, empty };
  553.  
  554. class Cell
  555. {
  556.  
  557. //Color
  558. public Color cellColor { get; set; }
  559.  
  560. //Age
  561. public int age {get;set;}
  562.  
  563. //Age used for starvation
  564. public int ageStarvation { get; set; }
  565.  
  566. //Set&Get Fishtype
  567. public fishTypeEnum fishType {get;set;}
  568.  
  569. //Check if moved during tick
  570. public bool moved { get; set; }
  571.  
  572. //Position
  573. public int posX {get;set;}
  574. public int posY {get;set;}
  575.  
  576. //Constructor
  577. public Cell()
  578. {
  579. cellColor = Color.Blue;
  580. posX = 0;
  581. posY = 0;
  582. age = 0;
  583. ageStarvation = 0;
  584. moved = false;
  585. fishType = fishTypeEnum.empty;
  586. }
  587.  
  588.  
  589.  
  590. }
  591.  
  592.  
  593. }
  594.  
  595. //Distribute Fish randomly
  596. int PosX=0;
  597. int PosY=0;
  598. bool isEmpty=false;
  599. Random rndX = new Random();
  600. Random rndY = new Random();
  601. PosX = rndX.Next(0, TableX);
  602. for (int i = 0; i < initialNumberOfFish; i++)
  603. {
  604. while (!isEmpty)
  605. {
  606. PosX = rndX.Next(TableX);
  607. PosY = rndY.Next(TableY);
  608. if (myArray[PosX, PosY].fishType == fishTypeEnum.empty)
  609. isEmpty = true;
  610. }
  611. myArray[PosX, PosY].fishType = fishTypeEnum.fish;
  612. myArray[PosX, PosY].cellColor = Color.Green;
  613. myArray[PosX, PosY].age = rndX.Next(0, ageToBreedFish);
  614. isEmpty = false;
  615. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement