Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.28 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine.Networking;
  6.  
  7. public class Controller : MonoBehaviour
  8. {
  9. public byte gravity, maxmatch, halt, nextA, nextB;
  10. public List<Vector2> delete;
  11. int[,] board;
  12. int[,] testboard;
  13. public bool check, move;
  14. bool[,] visited;
  15. public GameObject spawnBlock;
  16. public GameObject[] Blocks;
  17. float timer;
  18. bool lost;
  19. bool win;
  20. bool isRemote, isLocal;
  21. public UnityEngine.UI.Text testText;
  22.  
  23. // Use this for initialization
  24. void Start()
  25. {
  26.  
  27. delete = new List<Vector2>();
  28. board = new int[6, 6];
  29. halt = 0;
  30. visited = new bool[6, 6];
  31. testboard = new int[6, 6];
  32. //move = true;
  33. check = true;
  34.  
  35. for (int i = 0; i < 36; i++)
  36. {
  37. board[i % 6, i / 6] = 0;
  38. Blocks[i] = (GameObject)Instantiate(spawnBlock, new Vector3(-1000, -1000, -1000), this.transform.rotation);
  39. }
  40. Screen.SetResolution(270, 480, false);
  41. nextA = (byte)Random.Range(1, 7);
  42. nextB = (byte)Random.Range(1, 7);
  43. lost = false;
  44. win = false;
  45. }
  46.  
  47.  
  48.  
  49. void ShiftRight()
  50. {
  51. for(int y = 0; y < 6; y++)
  52. {
  53. for(int x = 4; x >= 0; x--)
  54. {
  55. if (board[y, x] > 0 && board[y, x + 1] == 0)
  56. {
  57. int movehere = 1;
  58. while (x + movehere < 6 && board[y, x + movehere] == 0)
  59. {
  60. movehere++;
  61. }
  62. movehere--;
  63. if (movehere > 0)
  64. {
  65. board[y, x + movehere] = board[y, x];
  66. board[y, x] = 0;
  67. }
  68. }
  69. }
  70. }
  71. }
  72. void ShiftUp()
  73. {
  74. for (int x = 0; x < 6; x++)
  75. {
  76. for (int y = 4; y >= 0; y--)
  77. {
  78. if (board[y, x] > 0 && board[y + 1, x] == 0)
  79. {
  80. int movehere = 1;
  81. while (y + movehere < 6 && board[y + movehere, x] == 0)
  82. {
  83. movehere++;
  84. }
  85. movehere--;
  86. if (movehere > 0)
  87. {
  88. board[y + movehere, x] = board[y, x];
  89. board[y, x] = 0;
  90. }
  91. }
  92. }
  93. }
  94. }
  95. void ShiftLeft()
  96. {
  97. for (int y = 0; y < 6; y++)
  98. {
  99. for (int x = 1; x < 6; x++)
  100. {
  101. if (board[y, x] > 0 && board[y, x - 1] == 0)
  102. {
  103. int movehere = 1;
  104. while (x-movehere > -1 && board[y, x - movehere] == 0)
  105. {
  106. movehere++;
  107. }
  108. movehere--;
  109. if (movehere > 0)
  110. {
  111. board[y, x - movehere] = board[y, x];
  112. board[y, x] = 0;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. void ShiftDown()
  119. {
  120. for (int x = 0; x < 6; x++)
  121. {
  122. for (int y = 1; y < 6; y++)
  123. {
  124. if (board[y, x] > 0 && board[y -1, x] == 0)
  125. {
  126. int movehere = 1;
  127. while (y - movehere > -1 && board[y - movehere, x] == 0)
  128. {
  129. movehere++;
  130. }
  131. movehere--;
  132. if (movehere > 0)
  133. {
  134. board[y - movehere, x] = board[y, x];
  135. board[y, x] = 0;
  136. }
  137. }
  138. }
  139. }
  140. }
  141.  
  142.  
  143. // Update is called once per frame
  144. void Update()
  145. {
  146.  
  147. for (int i = 0; i < 37; i++)
  148. {
  149. if (i < 36 && board[i/6, i%6] == 0)
  150. {
  151. break;
  152. }
  153. if (i == 36)
  154. {
  155. lost = true;
  156. }
  157. }
  158. if (!lost && isLocal)
  159. {
  160. if (Input.GetKeyDown(KeyCode.RightArrow))
  161. {
  162. timer = 1f;
  163. gravity = 0;
  164. ShiftRight();
  165. check = true;
  166. move = true;
  167.  
  168. }
  169. if (Input.GetKeyDown(KeyCode.UpArrow))
  170. {
  171. timer = 1f;
  172. gravity = 1;
  173. ShiftUp();
  174. check = true;
  175. move = true;
  176.  
  177. }
  178. if (Input.GetKeyDown(KeyCode.LeftArrow))
  179. {
  180. timer = 1f;
  181. gravity = 2;
  182. ShiftLeft();
  183. check = true;
  184. move = true;
  185.  
  186. }
  187. if (Input.GetKeyDown(KeyCode.DownArrow))
  188. {
  189. timer = 1f;
  190. gravity = 3;
  191. ShiftDown();
  192. check = true;
  193. move = true;
  194.  
  195. }
  196. } else if (lost)
  197. {
  198. testText.text = "You Lose.";
  199. Bluetooth.Instance().Send("Win");
  200. } else if (win)
  201. {
  202. testText.text = "You Win.";
  203. }
  204. for (int i = 0; i < 36; i++) //this is the for loop that basically draws the board on the screen
  205. {
  206. if (Blocks[i].transform.position == new Vector3(((i % 6) * 32) + transform.position.x, ((i / 6) * 32) + transform.position.y, 0) && board[i / 6, i % 6] == 0)
  207. {
  208. Blocks[i].transform.position = new Vector3(-1000, -1000, -1000);
  209. }
  210. if (board[i/6, i%6] != 0)
  211. {
  212. Blocks[i].transform.position = new Vector3(((i%6) * 32) + transform.position.x, ((i/6) * 32) + transform.position.y, 0);
  213. Blocks[i].GetComponent<Block>().color = board[i / 6, i % 6];
  214. }
  215. }
  216.  
  217.  
  218. if (move)
  219. {
  220. move = false;
  221. for (int i = 0; i < 36; visited[i / 6, i % 6] = false, i++) ;
  222. for (int i = 0; i < 36; i++) //going through all the spaces
  223. {
  224. if (board[i/6, i%6] <= 0 || visited[i/6, i%6]) //if the space is a junk block, empty, or we already visited it
  225. {
  226. continue; //skip it
  227. }
  228. Delete(0, i % 6, i / 6); //run the delete algorithm on the current space
  229. byte x = (byte)delete.Count; //setting a variable to be the size of delete
  230. if (x > 0 && x < maxmatch) //if x is less than the minimum amount we need to delete x
  231. for (int j = 0; j < x; delete.Remove(delete[0]), j++);//just remove everything from the delete list
  232. if (x >= maxmatch)//otherwise
  233. {
  234. move = true; //reset the move state to true
  235. DeleteBlocks(x); //run the algorithm to delete the blocks for real
  236. }
  237. }
  238.  
  239. if (move) //if we deleted some blocks
  240. {
  241. if (gravity == 0) //shift the blocks depending on the gravity
  242. {
  243. ShiftRight();
  244. }
  245. if (gravity == 1)
  246. {
  247. ShiftUp();
  248. }
  249. if (gravity == 2)
  250. {
  251. ShiftLeft();
  252. }
  253. if (gravity == 3)
  254. {
  255. ShiftDown();
  256. }
  257. timer = 1f;
  258. }
  259. }
  260.  
  261. if (check && !move) //if we're done moving/deleting blocks
  262. {
  263. Vector2 spawnHere = new Vector3(-1, -1);
  264. Vector2 spawnThere = new Vector3(-1, -1);
  265. bool[] looked = new bool[6];
  266. for (int j = 0; j < 6; looked[j] = false, j++) ;
  267. bool dontspawn = false;
  268. do
  269. {
  270. int checkedall = 0;
  271. for (int j = 1; j < 5; j++) //going from 1 to 4 b/c 0 and 5 don't matter since they're in the corners
  272. {
  273. if (looked[j])
  274. {
  275. checkedall++;
  276. }
  277. }
  278. if (checkedall >= 4)//if we looked at all the spaces between 1 and 4
  279. {
  280. dontspawn = true; //dont spawn the block
  281. break;
  282. }
  283. int i = Random.Range(0, 5); //setting a random value between 0 and 4
  284. spawnHere = gravity == 0 ? new Vector2(0, i) : gravity == 1 ? new Vector2(i, 0) :
  285. gravity == 2 ? new Vector2(5, i) : new Vector2(i, 5); //finding a place to spawn it
  286. spawnThere = gravity == 0 ? new Vector2(0, spawnHere.y + 1) : gravity == 1 ? new Vector2(spawnHere.x + 1, 0) :
  287. gravity == 2 ? new Vector2(5, spawnHere.y + 1) : new Vector2(spawnHere.x + 1, 5); //find a place to spawn its companion
  288. looked[i] = true;//mark the two spaces as true
  289. looked[i + 1] = true;
  290. } while (board[(int)spawnHere.y, (int)spawnHere.x] != 0 || board[(int)spawnThere.y, (int)spawnThere.x] != 0);
  291. if (!dontspawn)
  292. {
  293.  
  294. board[(int)spawnHere.y, (int)spawnHere.x] = nextA; //spawning the bricks
  295. board[(int)spawnThere.y, (int)spawnThere.x] = nextB;
  296. nextA = (byte)Random.Range(1, 7); //setting the color of the next brick
  297. nextB = (byte)Random.Range(1, 7);
  298. for (int i = 0; i < 36; visited[i / 6, i % 6] = false, i++) ; //same as earlier except we dont shift again
  299. for (int i = 0; i < 36; i++)
  300. {
  301. if (board[i / 6, i % 6] <= 0 || visited[i / 6, i % 6])
  302. {
  303. continue;
  304. }
  305. Delete(0, i % 6, i / 6);
  306. int x = delete.Count;
  307. if (x > 0 && x < maxmatch)
  308. for (int j = 0; j < x; delete.Remove(delete[0]), j++);
  309. if (x >= maxmatch)
  310. {
  311. DeleteBlocks(x);
  312. }
  313. }
  314. }
  315. check = false;
  316. }
  317. }
  318.  
  319. public void DeleteBlocks(int size) //we just set everything listed in the delete list to 0 and then remove those coordinates
  320. {
  321. for (int j = 0; j <= size; j++)
  322. {
  323. if (delete.Count > 0)
  324. {
  325. board[(int)delete[0].y, (int)delete[0].x] = 0;
  326. delete.Remove(delete[0]);
  327. }
  328. }
  329. }
  330. public void Delete(int recs, int x, int y)
  331. {
  332. if (visited[y, x] || board[y, x] == 0 || (board[y, x] == -1 && delete.Count < maxmatch)) //if the space is blank, we already visited it, or it's -1 and we're under the amount of blocks needed to match
  333. {
  334. return;
  335. }
  336. visited[y, x] = true;
  337. if (board[y, x] == -1) //this basically means if you get a match 3, the adjacent junk blocks are also deleted
  338. {
  339. delete.Add(new Vector2(x, y));
  340. return;
  341. }
  342. if (x > 0 && !visited[y, x - 1] && board[y, x - 1] == board[y, x])//we look in all directions to see if there are any unvisited nodes of the same color
  343. {
  344. Debug.Log("AAAAAAAAAAAAAA");
  345. Delete(recs + 1, x - 1, y);
  346. }
  347. if (x < 5 && !visited[y, x + 1] && board[y, x + 1] == board[y, x])
  348. {
  349. Debug.Log("AAAAAAAAAAAAAA");
  350. Delete(recs + 1, x + 1, y);
  351. }
  352. if (y > 0 && !visited[y - 1, x] && board[y - 1, x] == board[y, x])
  353. {
  354. Debug.Log("AAAAAAAAAAAAAA");
  355. Delete(recs + 1, x, y - 1);
  356. }
  357. if (y < 5 && !visited[y + 1, x] && board[y + 1, x] == board[y, x])
  358. {
  359. Debug.Log("AAAAAAAAAAAAAA");
  360. Delete(recs + 1, x, y + 1);
  361. }
  362. delete.Add(new Vector2(x, y));//then we add the nodes to the delete list recursively
  363. }
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement