Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine.Networking;
- public class Controller : MonoBehaviour
- {
- public byte gravity, maxmatch, halt, nextA, nextB;
- public List<Vector2> delete;
- int[,] board;
- int[,] testboard;
- public bool check, move;
- bool[,] visited;
- public GameObject spawnBlock;
- public GameObject[] Blocks;
- float timer;
- bool lost;
- bool win;
- bool isRemote, isLocal;
- public UnityEngine.UI.Text testText;
- // Use this for initialization
- void Start()
- {
- delete = new List<Vector2>();
- board = new int[6, 6];
- halt = 0;
- visited = new bool[6, 6];
- testboard = new int[6, 6];
- //move = true;
- check = true;
- for (int i = 0; i < 36; i++)
- {
- board[i % 6, i / 6] = 0;
- Blocks[i] = (GameObject)Instantiate(spawnBlock, new Vector3(-1000, -1000, -1000), this.transform.rotation);
- }
- Screen.SetResolution(270, 480, false);
- nextA = (byte)Random.Range(1, 7);
- nextB = (byte)Random.Range(1, 7);
- lost = false;
- win = false;
- }
- void ShiftRight()
- {
- for(int y = 0; y < 6; y++)
- {
- for(int x = 4; x >= 0; x--)
- {
- if (board[y, x] > 0 && board[y, x + 1] == 0)
- {
- int movehere = 1;
- while (x + movehere < 6 && board[y, x + movehere] == 0)
- {
- movehere++;
- }
- movehere--;
- if (movehere > 0)
- {
- board[y, x + movehere] = board[y, x];
- board[y, x] = 0;
- }
- }
- }
- }
- }
- void ShiftUp()
- {
- for (int x = 0; x < 6; x++)
- {
- for (int y = 4; y >= 0; y--)
- {
- if (board[y, x] > 0 && board[y + 1, x] == 0)
- {
- int movehere = 1;
- while (y + movehere < 6 && board[y + movehere, x] == 0)
- {
- movehere++;
- }
- movehere--;
- if (movehere > 0)
- {
- board[y + movehere, x] = board[y, x];
- board[y, x] = 0;
- }
- }
- }
- }
- }
- void ShiftLeft()
- {
- for (int y = 0; y < 6; y++)
- {
- for (int x = 1; x < 6; x++)
- {
- if (board[y, x] > 0 && board[y, x - 1] == 0)
- {
- int movehere = 1;
- while (x-movehere > -1 && board[y, x - movehere] == 0)
- {
- movehere++;
- }
- movehere--;
- if (movehere > 0)
- {
- board[y, x - movehere] = board[y, x];
- board[y, x] = 0;
- }
- }
- }
- }
- }
- void ShiftDown()
- {
- for (int x = 0; x < 6; x++)
- {
- for (int y = 1; y < 6; y++)
- {
- if (board[y, x] > 0 && board[y -1, x] == 0)
- {
- int movehere = 1;
- while (y - movehere > -1 && board[y - movehere, x] == 0)
- {
- movehere++;
- }
- movehere--;
- if (movehere > 0)
- {
- board[y - movehere, x] = board[y, x];
- board[y, x] = 0;
- }
- }
- }
- }
- }
- // Update is called once per frame
- void Update()
- {
- for (int i = 0; i < 37; i++)
- {
- if (i < 36 && board[i/6, i%6] == 0)
- {
- break;
- }
- if (i == 36)
- {
- lost = true;
- }
- }
- if (!lost && isLocal)
- {
- if (Input.GetKeyDown(KeyCode.RightArrow))
- {
- timer = 1f;
- gravity = 0;
- ShiftRight();
- check = true;
- move = true;
- }
- if (Input.GetKeyDown(KeyCode.UpArrow))
- {
- timer = 1f;
- gravity = 1;
- ShiftUp();
- check = true;
- move = true;
- }
- if (Input.GetKeyDown(KeyCode.LeftArrow))
- {
- timer = 1f;
- gravity = 2;
- ShiftLeft();
- check = true;
- move = true;
- }
- if (Input.GetKeyDown(KeyCode.DownArrow))
- {
- timer = 1f;
- gravity = 3;
- ShiftDown();
- check = true;
- move = true;
- }
- } else if (lost)
- {
- testText.text = "You Lose.";
- Bluetooth.Instance().Send("Win");
- } else if (win)
- {
- testText.text = "You Win.";
- }
- for (int i = 0; i < 36; i++) //this is the for loop that basically draws the board on the screen
- {
- 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)
- {
- Blocks[i].transform.position = new Vector3(-1000, -1000, -1000);
- }
- if (board[i/6, i%6] != 0)
- {
- Blocks[i].transform.position = new Vector3(((i%6) * 32) + transform.position.x, ((i/6) * 32) + transform.position.y, 0);
- Blocks[i].GetComponent<Block>().color = board[i / 6, i % 6];
- }
- }
- if (move)
- {
- move = false;
- for (int i = 0; i < 36; visited[i / 6, i % 6] = false, i++) ;
- for (int i = 0; i < 36; i++) //going through all the spaces
- {
- 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
- {
- continue; //skip it
- }
- Delete(0, i % 6, i / 6); //run the delete algorithm on the current space
- byte x = (byte)delete.Count; //setting a variable to be the size of delete
- if (x > 0 && x < maxmatch) //if x is less than the minimum amount we need to delete x
- for (int j = 0; j < x; delete.Remove(delete[0]), j++);//just remove everything from the delete list
- if (x >= maxmatch)//otherwise
- {
- move = true; //reset the move state to true
- DeleteBlocks(x); //run the algorithm to delete the blocks for real
- }
- }
- if (move) //if we deleted some blocks
- {
- if (gravity == 0) //shift the blocks depending on the gravity
- {
- ShiftRight();
- }
- if (gravity == 1)
- {
- ShiftUp();
- }
- if (gravity == 2)
- {
- ShiftLeft();
- }
- if (gravity == 3)
- {
- ShiftDown();
- }
- timer = 1f;
- }
- }
- if (check && !move) //if we're done moving/deleting blocks
- {
- Vector2 spawnHere = new Vector3(-1, -1);
- Vector2 spawnThere = new Vector3(-1, -1);
- bool[] looked = new bool[6];
- for (int j = 0; j < 6; looked[j] = false, j++) ;
- bool dontspawn = false;
- do
- {
- int checkedall = 0;
- 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
- {
- if (looked[j])
- {
- checkedall++;
- }
- }
- if (checkedall >= 4)//if we looked at all the spaces between 1 and 4
- {
- dontspawn = true; //dont spawn the block
- break;
- }
- int i = Random.Range(0, 5); //setting a random value between 0 and 4
- spawnHere = gravity == 0 ? new Vector2(0, i) : gravity == 1 ? new Vector2(i, 0) :
- gravity == 2 ? new Vector2(5, i) : new Vector2(i, 5); //finding a place to spawn it
- spawnThere = gravity == 0 ? new Vector2(0, spawnHere.y + 1) : gravity == 1 ? new Vector2(spawnHere.x + 1, 0) :
- gravity == 2 ? new Vector2(5, spawnHere.y + 1) : new Vector2(spawnHere.x + 1, 5); //find a place to spawn its companion
- looked[i] = true;//mark the two spaces as true
- looked[i + 1] = true;
- } while (board[(int)spawnHere.y, (int)spawnHere.x] != 0 || board[(int)spawnThere.y, (int)spawnThere.x] != 0);
- if (!dontspawn)
- {
- board[(int)spawnHere.y, (int)spawnHere.x] = nextA; //spawning the bricks
- board[(int)spawnThere.y, (int)spawnThere.x] = nextB;
- nextA = (byte)Random.Range(1, 7); //setting the color of the next brick
- nextB = (byte)Random.Range(1, 7);
- for (int i = 0; i < 36; visited[i / 6, i % 6] = false, i++) ; //same as earlier except we dont shift again
- for (int i = 0; i < 36; i++)
- {
- if (board[i / 6, i % 6] <= 0 || visited[i / 6, i % 6])
- {
- continue;
- }
- Delete(0, i % 6, i / 6);
- int x = delete.Count;
- if (x > 0 && x < maxmatch)
- for (int j = 0; j < x; delete.Remove(delete[0]), j++);
- if (x >= maxmatch)
- {
- DeleteBlocks(x);
- }
- }
- }
- check = false;
- }
- }
- public void DeleteBlocks(int size) //we just set everything listed in the delete list to 0 and then remove those coordinates
- {
- for (int j = 0; j <= size; j++)
- {
- if (delete.Count > 0)
- {
- board[(int)delete[0].y, (int)delete[0].x] = 0;
- delete.Remove(delete[0]);
- }
- }
- }
- public void Delete(int recs, int x, int y)
- {
- 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
- {
- return;
- }
- visited[y, x] = true;
- if (board[y, x] == -1) //this basically means if you get a match 3, the adjacent junk blocks are also deleted
- {
- delete.Add(new Vector2(x, y));
- return;
- }
- 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
- {
- Debug.Log("AAAAAAAAAAAAAA");
- Delete(recs + 1, x - 1, y);
- }
- if (x < 5 && !visited[y, x + 1] && board[y, x + 1] == board[y, x])
- {
- Debug.Log("AAAAAAAAAAAAAA");
- Delete(recs + 1, x + 1, y);
- }
- if (y > 0 && !visited[y - 1, x] && board[y - 1, x] == board[y, x])
- {
- Debug.Log("AAAAAAAAAAAAAA");
- Delete(recs + 1, x, y - 1);
- }
- if (y < 5 && !visited[y + 1, x] && board[y + 1, x] == board[y, x])
- {
- Debug.Log("AAAAAAAAAAAAAA");
- Delete(recs + 1, x, y + 1);
- }
- delete.Add(new Vector2(x, y));//then we add the nodes to the delete list recursively
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement