LAMPIRAN A
A- 1 -
using UnityEngine;using System.Collections;
public class AI : MonoBehaviour {
#region var
private string[] strRandSpawn5 = new string[5]; private int intCurSpawn5 = 0;
public struct Node {
public string strNodeId;
public string strNodeIdParent; public string strNodeIdChild; public int intHighNum;
public int intSpaceLeft; public int intSpawnPos; public string OwnBoard; public string strDirection; public bool isIdle;
}
public Node[] myNode = new Node[1364]; public string strSaveDataAIArah = ""; public string strSaveDataAISpawn = "";
public controller_Gameplay myController_Gameplay; public Module myModule;
#endregion
public void setNodeID() {
int intCurArah = 1; int intIdxParent = -1;
for (int i = 0; i < myNode.Length; i++) {
if (i < 4) {
myNode[i].strNodeIdParent = "";
myNode[i].strNodeId = intCurArah.ToString() + "0000";
} else {
myNode[i].strNodeIdParent = myNode[intIdxParent].strNodeId;
if (i < 20) myNode[i].strNodeId =
myNode[i].strNodeIdParent.Substring(0, 1) + intCurArah.ToString() + "000"; // 16 + 4
else if (i < 84) myNode[i].strNodeId =
LAMPIRAN A
A- 2 -
else if (i < 340) myNode[i].strNodeId =
myNode[i].strNodeIdParent.Substring(0, 3) + intCurArah.ToString() + "0"; // 256 + 64 + 16 + 4
else if (i < 1364) myNode[i].strNodeId =
myNode[i].strNodeIdParent.Substring(0, 4) + intCurArah.ToString(); // 1024 + 256 + 64 + 16 + 4
}
setNodeStrDirection(i, intCurArah); intCurArah++;
if (intCurArah > 4) {
void setNodeStrDirection(int idx, int dir) {
switch (dir) {
case 1: myNode[idx].strDirection = "Up"; break;
case 2: myNode[idx].strDirection = "Down"; break;
case 3: myNode[idx].strDirection = "Left"; break;
case 4: myNode[idx].strDirection = "Right"; break;
} }
public void resetNode() {
for (int i = 0; i < myNode.Length; i++) {
myNode[i].intHighNum = 0; myNode[i].intSpaceLeft = 0; myNode[i].intSpawnPos = -1; myNode[i].OwnBoard = ""; myNode[i].isIdle = false; }
strSaveDataAIArah = ""; strSaveDataAISpawn = "";
for (int j = 0; j < strRandSpawn5.Length; j++) strRandSpawn5[j] =
myModule.RandomItemInString("0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15"); ContinueAI();
}
void simulator4(int idxMyNode) {
bool bAdd = false;
//declare the iBoard as template for move test int[][] iBoard;
LAMPIRAN A
rd, idxString), out iBoard[e][d]);idxString++; }
} }
else if (idxMyNode >= 4) {
idxString = 1;
//find the parent Node
int idxParentGet = findIdxParent(idxMyNode); for (int a = 0; a < myNode.Length; a++)
{
if (myNode[a].strNodeId == myNode[idxMyNode].strNodeIdParent)
{
idxParentGet = a; break;
} }
//pass the parent's OwnBoard to iBoard for (int b = 0; b < 4; b++)
{
for (int c = 0; c < 4; c++) {
int.TryParse(myModule.GetItemPos(myNode[idxParentGet].OwnBoard, idxString), out iBoard[c][b]);
idxString++; }
} }
int idxPick = 1;
int intNextSpawn = -1; bool bolSpawn = false;
//find appropriate spot to spawn next Number from strRandSpawn5
while (!bolSpawn && idxPick <=
LAMPIRAN A
A- 4 -
int.TryParse(myModule.GetItemPos(strRandSpawn5[intCurSpawn5], idxPick), out intNextSpawn);
////print("callout : " + intNextSpawn.ToString()); int kolSpawn = intNextSpawn % 4;
int brsSpawn = intNextSpawn / 4; if (iBoard[kolSpawn][brsSpawn] == 0) {
iBoard[kolSpawn][brsSpawn] = 2; bolSpawn = true;
} else
idxPick++; }
if (bolSpawn) {
// cek direction & calculate //bool bAdd = false;
switch (myNode[idxMyNode].strDirection) {
case "Up":
LAMPIRAN A
A- 5 -
}} break; case "Right":
for (int j = 0; j < 4; j++) case "Down":
LAMPIRAN A
case "Left":LAMPIRAN A
// save spawnPos....ownBoard...highestPoint.... spaceLeft...
// convert back to string string strBoardResult = ""; int inthighNum = 0;
int intFreeSpots = 0;
for (int m = 0; m < 4; m++) {
for (int n = 0; n < 4; n++) {
strBoardResult =
myModule.AddItem(strBoardResult, iBoard[n][m].ToString());
if (iBoard[n][m] > inthighNum) inthighNum = iBoard[n][m];
if (iBoard[n][m] == 0) intFreeSpots += 1; }
}
myNode[idxMyNode].intSpawnPos = intNextSpawn; myNode[idxMyNode].OwnBoard = strBoardResult; myNode[idxMyNode].intHighNum = inthighNum; myNode[idxMyNode].intSpaceLeft = intFreeSpots; if (!bAdd) myNode[idxMyNode].isIdle = true; else
{
if (idxMyNode < 4) myNode[idxMyNode].isIdle = false;
else //follow the parent's idle stat {
myNode[idxMyNode].isIdle = myNode[findIdxParent(idxMyNode)].isIdle;
} }
}
else print("GAMEOVER"); }
void ContinueAI() {
//simulate result
for (int k = 0; k < myNode.Length; k++) {
LAMPIRAN A
//get bottom best value int getTmpIdx = 340; int intHighNum = 0; int intSpaceLeft = 0;
for (int v = 340; v < myNode.Length; v++) {
if (!myNode[v].isIdle && ((intHighNum <
myNode[v].intHighNum) || (intHighNum == myNode[v].intHighNum && intSpaceLeft < myNode[v].intSpaceLeft)))
{
getTmpIdx = v;
intHighNum = myNode[v].intHighNum; intSpaceLeft = myNode[v].intSpaceLeft; }
}
//trace up
for (int w = 5; w > 0; w--) {
strSaveDataAIArah =
myModule.AddItemReversed(strSaveDataAIArah, myNode[getTmpIdx].strDirection);
strSaveDataAISpawn =
myModule.AddItemReversed(strSaveDataAISpawn, myNode[getTmpIdx].intSpawnPos.ToString());
for (int x = 0; x < myNode.Length; x++) {
if (myNode[x].strNodeId == myNode[getTmpIdx].strNodeIdParent)
myController_Gameplay.intUpdate = 10; }
int findIdxParent(int idxChild) {
int idxParentGet = -1;
for (int a = 0; a < myNode.Length; a++) {
if (myNode[a].strNodeId == myNode[idxChild].strNodeIdParent)
{
idxParentGet = a; break;
} }
LAMPIRAN A
LAMPIRAN B
B- 1 -
using UnityEngine;using System.Collections; [System.Serializable] public class Box2048 {
public int intIdx; public int intKol; public int intBrs; public float posX; public float posY; public int FillNum; }
[System.Serializable] public class Number2048 {
public int intIdx; public int intKol; public int intBrs; public float posX; public float posY; public int Number; public GameObject GO; }
public class controller_Gameplay : MonoBehaviour {
#region var
public int intUpdate = -1; public bool isInput = false; public int intWaitMove;
public bool bolAllMove = false;
public Box2048[] myBox = new Box2048[16];
public Number2048[] myNumber = new Number2048[16]; public bool[] isNumberMove = new bool[16];
private int intCountMove = -1; private int intCurSpawn = -1; private string strCurMove = ""; public AI myAI;
public string strMasterBoard = ""; public Module myModule;
#endregion
// Use this for initialization void Start () {
//print("void start");
for (int i = 0; i < myBox.Length; i++) {
myBox[i].intIdx = i;
myBox[i].intKol = (i % 4); myBox[i].intBrs = (i / 4);
LAMPIRAN B
B- 2 -
myBox[i].FillNum = 0; myNumber[i].intIdx = i;myNumber[i].intKol = (i % 4); myNumber[i].intBrs = (i / 4);
myNumber[i].posX = -4.0f + (2.0f * (i % 4)); myNumber[i].posY = 3.0f - (2.0f * (i / 4)); myNumber[i].Number = 0;
}
myAI.setNodeID(); intUpdate = 5; }
// Update is called once per frame void Update()
{
switch (intUpdate) { intCountMove = 0; intUpdate = 15; break;
case 15: intUpdate = 0; intCountMove++;
if (intCountMove > 5) intUpdate = 5; else
{
int.TryParse(myModule.GetItemPos(myAI.strSaveDataAISpawn,intCountMo ve),out intCurSpawn);
strCurMove =
myModule.GetItemPos(myAI.strSaveDataAIArah, intCountMove); intUpdate = 20; GUI.Box(new Rect(0, 0, Screen.width, Screen.height), "The game ends /n the time is "+ Time.time);
break;
case 500 : if (Input.GetKeyDown(KeyCode.Space)) {
LAMPIRAN B
void callAI() {
//print("void callAI");
//convert currentBoard to string strMasterBoard = "";
for (int i = 0; i < myNumber.Length; i++) strMasterBoard = myModule.AddItem(strMasterBoard, myNumber[i].Number.ToString());
//call AI to start myAI.resetNode(); }
void spawnNumber(int intPosSpawn) {
//print("void spawnNumber"); myBox[intPosSpawn].FillNum = 2; myNumber[intPosSpawn].Number = 2; myNumber[intPosSpawn].GO =
Instantiate(Resources.Load("Numbers/GO_2")) as GameObject; myNumber[intPosSpawn].GO.transform.position = new
Vector3(myNumber[intPosSpawn].posX, myNumber[intPosSpawn].posY, 0.0f); intUpdate = 25;
}
public void moveNumber(string direction) {
//print("void moveNumber");
//set EndPos (order based on direction) //#######################################// #region RIGHT
if (direction == "Right") { k].GO.GetComponent<controller_moveAnimTes>().endPos = new
Vector3(myNumber[(4 * j) + i].posX, myNumber[(4 * j) + k].posY, 0.0f); myNumber[(4 * j) +
k].GO.GetComponent<controller_moveAnimTes>().interval =
LAMPIRAN B
k].GO.GetComponent<controller_moveAnimTes>().endPos = newVector3(myNumber[(4 * j) + i].posX, myNumber[(4 * j) + k].posY, 0.0f); myNumber[(4 * j) +
k].GO.GetComponent<controller_moveAnimTes>().interval =
Mathf.Abs(myBox[(4 * j) + k].intKol - myBox[(4 * j) + i].intKol);
isNumberMove[(4 * j) + k] = #region LEFT
if (direction == "Left") {
if (myBox[(4*j)+k].FillNum == 0) { k].GO.GetComponent<controller_moveAnimTes>().endPos = new
LAMPIRAN B
B- 5 -
myNumber[(4 * j) + k].GO.GetComponent<controller_moveAnimTes>().interval =
Mathf.Abs(myBox[(4 * j) + k].intKol - myBox[(4 * j) + i].intKol);
isNumberMove[(4 * j) + k] = true; myBox[(4 * j) + i].FillNum *= 2; myBox[(4*j)+k].FillNum = 0; break; k].GO.GetComponent<controller_moveAnimTes>().endPos = new
Vector3(myNumber[(4 * j) + i].posX, myNumber[(4 * j) + k].posY, 0.0f); myNumber[(4 * j) +
k].GO.GetComponent<controller_moveAnimTes>().interval =
Mathf.Abs(myBox[(4 * j) + k].intKol - myBox[(4 * j) + i].intKol);
isNumberMove[(4 * j) + k] = #region DOWN
if (direction == "Down") { //kosong jd tidak ad yg perlu digeser
{
continue; }
LAMPIRAN B
B- 6 -
{myNumber[(4 * k) + i].GO.GetComponent<controller_moveAnimTes>().endPos = new
Vector3(myNumber[(4 * k) + i].posX, myNumber[(4 * j) + i].posY, 0.0f); myNumber[(4 * k) +
i].GO.GetComponent<controller_moveAnimTes>().interval =
Mathf.Abs(myBox[(4 * k) + i].intBrs - myBox[(4 * j) + i].intBrs);
isNumberMove[(4 * k) + i] = true; nilai.... asal jd nol krn digeser
{
myNumber[(4 * k) + i].GO.GetComponent<controller_moveAnimTes>().endPos = new
Vector3(myNumber[(4 * k) + i].posX, myNumber[(4 * j) + i].posY, 0.0f); myNumber[(4 * k) +
i].GO.GetComponent<controller_moveAnimTes>().interval =
Mathf.Abs(myBox[(4 * k) + i].intBrs - myBox[(4 * j) + i].intBrs);
isNumberMove[(4 * k) + i] = i].FillNum != 0)//tdk bisa geser krn nilai beda
{
if (direction == "Up") { //kosong jd tidak ad yg perlu digeser
LAMPIRAN B
B- 7 -
continue; }
else if (myBox[(4 * k) + i].FillNum == myBox[(4 * j) + i].FillNum) //timpa... nilai sama.... jd tujuan x2... asal jd nol krn digeser
{
myNumber[(4 * k) + i].GO.GetComponent<controller_moveAnimTes>().endPos = new
Vector3(myNumber[(4 * k) + i].posX, myNumber[(4 * j) + i].posY, 0.0f); myNumber[(4 * k) +
i].GO.GetComponent<controller_moveAnimTes>().interval =
Mathf.Abs(myBox[(4 * k) + i].intBrs - myBox[(4 * j) + i].intBrs);
isNumberMove[(4 * k) + i] = true; nilai.... asal jd nol krn digeser
{
myNumber[(4 * k) + i].GO.GetComponent<controller_moveAnimTes>().endPos = new
Vector3(myNumber[(4 * k) + i].posX, myNumber[(4 * j) + i].posY, 0.0f); myNumber[(4 * k) +
i].GO.GetComponent<controller_moveAnimTes>().interval =
Mathf.Abs(myBox[(4 * k) + i].intBrs - myBox[(4 * j) + i].intBrs);
isNumberMove[(4 * k) + i] = i].FillNum != 0)//tdk bisa geser krn nilai beda
{
intWaitMove = 0;
bool isSthMove = false;
for (int k = 0; k < myNumber.Length; k++) {
if (isNumberMove[k]) {
LAMPIRAN B
B- 8 -
myNumber[k].GO.GetComponent<controller_moveAnimTes>().startPos = new Vector3(myNumber[k].posX, myNumber[k].posY, 0.0f);
myNumber[k].GO.GetComponent<controller_moveAnimTes>().startTime = Time.time;
myNumber[k].GO.GetComponent<controller_moveAnimTes>().journeyLength =
Vector3.Distance(myNumber[k].GO.GetComponent<controller_moveAnimTes>().st artPos, myNumber[k].GO.GetComponent<controller_moveAnimTes>().endPos);
myNumber[k].GO.GetComponent<controller_moveAnimTes>().isMoveable = true;
isSthMove = true; }
}
if (isSthMove) bolAllMove = true; else decreaseIntWaitMove();
}
public void decreaseIntWaitMove() {
//print("void decreaseIntWaitMove..."); intWaitMove--;
//print("hasil pengurangan..." + intWaitMove.ToString()); if (intWaitMove <= 0)
{
bolAllMove = false; afterMove();
} }
void afterMove() {
for (int i = 0; i < myNumber.Length; i++) {
if (myNumber[i].Number > 0) {
myNumber[i].GO.transform.position = new Vector3(myNumber[i].posX, myNumber[i].posY, 0.0f);
GameObject.Destroy(myNumber[i].GO.gameObject); }
myNumber[i].Number = myBox[i].FillNum; if (myNumber[i].Number > 0)
{
myNumber[i].GO = Instantiate(Resources.Load("Numbers/GO_" + myNumber[i].Number.ToString())) as GameObject;
myNumber[i].GO.transform.position = new Vector3(myNumber[i].posX, myNumber[i].posY, 0.0f);
LAMPIRAN B
B- 9 -
if (myNumber[i].Number == 2048) {
GameObject.Find("EventSystem").SendMessage("Finish"); }
if (myNumber[i].Number == 1024) {
GameObject.Find("EventSystem").SendMessage("Catat"); }
isNumberMove[i] = false; }
//can Spawn ???
bool canSpawn = false;
for (int j = 0; j < myBox.Length; j++) {
if (myBox[j].FillNum == 0) {
canSpawn = true; break;
} }
//print("void afterMove => cek Can Spawn???"); if (canSpawn)
intUpdate = 15; else
intUpdate = 30; }
void checkGameOver() {
//print("void checkGameOver"); bool isPlayable = false;
for (int j = 0; j < myNumber.Length; j++) {
//not mostLeft if (j % 4 != 0) {
if (myNumber[j].Number == myNumber[j - 1].Number) {
isPlayable = true; break;
} }
//NOT mostRight else if (j % 4 != 3) {
if (myNumber[j].Number == myNumber[j + 1].Number) {
isPlayable = true; break;
} }
//not mostTop
LAMPIRAN B
B- 10 -
{if (myNumber[j].Number == myNumber[j - 4].Number) {
isPlayable = true; break;
} }
//not mostBottom else if (j / 4 != 3) {
if (myNumber[j].Number == myNumber[j + 4].Number) {
isPlayable = true; break;
} }
}
if (isPlayable) intUpdate = 10; else intUpdate = 40;
LAMPIRAN C
C-- 1 --
using UnityEngine;using System.Collections;
public class controller_moveAnimTes : MonoBehaviour { //tes
public GameObject myGO_AnimTes;
public controller_Gameplay myGameplayScript; public Vector3 startPos;
public Vector3 endPos; public Vector4 startColor; public Vector4 endColor; public float speed = 1.0f; public float interval = 1.0f; public float startTime;
public float journeyLength; private float colorLength; public bool isMoveable;
// Use this for initialization void Start () {
//tes
myGameplayScript =
GameObject.Find("Controller_Gameplay").GetComponent<controller_Gameplay>( );
////print("GO found"); speed = 15.0f;
startTime = Time.time;
journeyLength = Vector3.Distance(startPos, endPos); //startColor = new
Vector4(this.gameObject.GetComponent<SpriteRenderer>().color.r, this.gameObject.GetComponent<SpriteRenderer>().color.r,
this.gameObject.GetComponent<SpriteRenderer>().color.r, 1.0f); //endColor = new
Vector4(this.gameObject.GetComponent<SpriteRenderer>().color.r, this.gameObject.GetComponent<SpriteRenderer>().color.r,
this.gameObject.GetComponent<SpriteRenderer>().color.r, 0.0f); //colorLength = Vector4.Distance(startColor, endColor); //---//
}
// Update is called once per frame void Update()
{
if (isMoveable && myGameplayScript.bolAllMove) {
//print("initial calculate ");
float distCovered = (Time.time - startTime) * speed * interval;
float fracJourney = distCovered / journeyLength; //print("about to transform");
myGO_AnimTes.transform.position = Vector3.Lerp(startPos, endPos, fracJourney);
//print("transform is done ");
LAMPIRAN C
C-- 2 --
isMoveable = false;myGameplayScript.decreaseIntWaitMove(); }
//float fadeCovered = (Time.time - startTime) * speed; //float fracFading = fadeCovered / colorLength;
//myGO_AnimTes.GetComponent<SpriteRenderer>().color = Vector4.Lerp(startColor, endColor, fracFading);
} }
LAMPIRAN C
C-3-
using UnityEngine;using System.Collections;
public class Module : MonoBehaviour {
public string AddItem(string sourceString, string addString) {
string temp = sourceString; if (temp == "")
{
temp += addString; }
else {
temp += "," + addString; }
return temp; }
public string AddItemReversed(string sourceString, string addString)
{
string temp = sourceString; if (temp == "")
{
temp += addString; }
else {
temp = addString + "," + temp; }
return temp; }
public string RandomItemInString(string pstr) {
int roundDirection; string temp1 = pstr; string temp2 = "";
int maxItem = GetItemCount(pstr); int maxRound = Random.Range(5, 10); int front = 1;
LAMPIRAN C
C-4-
if (roundDirection == 1) {
temp2 += GetItemPos(temp1, front); front += 1;
} else {
temp2 += GetItemPos(temp1, last); last -= 1;
return temp2; }
public int GetItemCount(string pstr) {
int temp = 1;
if (pstr == "") return temp; int maxChar = pstr.Length;
for (int i = 0; i < maxChar; i++) {
if (pstr.Substring(i, 1) == ",") {
temp += 1; }
}
return temp; }
public string GetItemPos(string pstr, int pos) {
string temp = ""; int count = 1;
int maxChar = pstr.Length;
for (int i = 0; i < maxChar; i++) {
if (pstr.Substring(i, 1) == ",") {
if (count >= pos) {
return temp; }
LAMPIRAN C
C-5-
}}
if ((count + 1) < pos) temp = ""; return temp;
LAMPIRAN D
D-1-
using UnityEngine;using System.Collections;
public class Module : MonoBehaviour {
public string AddItem(string sourceString, string addString) {
string temp = sourceString; if (temp == "")
{
temp += addString; }
else {
temp += "," + addString; }
return temp; }
public string AddItemReversed(string sourceString, string addString)
{
string temp = sourceString; if (temp == "")
{
temp += addString; }
else {
temp = addString + "," + temp; }
return temp; }
public string RandomItemInString(string pstr) {
int roundDirection; string temp1 = pstr; string temp2 = "";
int maxItem = GetItemCount(pstr); int maxRound = Random.Range(5, 10); int front = 1;
int last = maxItem;
for (int i = 1; i < maxRound; i++) {
if (temp2 != "") {
temp1 = temp2; temp2 = ""; }
front = 1; last = maxItem;
LAMPIRAN D
D-2-
{roundDirection = Random.Range(1, 3); if (roundDirection == 1)
{
temp2 += GetItemPos(temp1, front); front += 1;
} else {
temp2 += GetItemPos(temp1, last); last -= 1;
return temp2; }
public int GetItemCount(string pstr) {
int temp = 1;
if (pstr == "") return temp; int maxChar = pstr.Length;
for (int i = 0; i < maxChar; i++) {
if (pstr.Substring(i, 1) == ",") {
temp += 1; }
}
return temp; }
public string GetItemPos(string pstr, int pos) {
string temp = ""; int count = 1;
int maxChar = pstr.Length;
for (int i = 0; i < maxChar; i++) {
if (pstr.Substring(i, 1) == ",") {
if (count >= pos) {
return temp; }
temp = ""; count += 1; }
LAMPIRAN D
D-3-
{temp += pstr.Substring(i, 1); }
}
if ((count + 1) < pos) temp = ""; return temp;
LAMPIRAN E
E-1-
using UnityEngine;using System.Collections;
public class tesKlik5 : MonoBehaviour { // Use this for initialization void Start () {
}
// Update is called once per frame void Update () {
}
public void OnMouseDown1() {
Application.LoadLevel("Gameplay"); }
LAMPIRAN F
F-1-
using UnityEngine;using UnityEngine.UI; using System.Collections;
public class Timer : MonoBehaviour { // Use this for initialization public Text timerText;
public Text timerText2; private float startTime;
private bool finished = false; private float t1024;
void Start () {
startTime = Time.time; }
// Update is called once per frame void Update () {
if (finished == true)
Time.timeScale=0; else {
float t = Time.time - startTime;
string minutes = ((int)t / 60).ToString ();
string seconds = (t % 60).ToString ("f2");
timerText.text = minutes + ":" + seconds;
} }
public void Finish() {
finished = true;
timerText.color = Color.red; }
public void Catat() {
if (t1024 == 0) {
float t = Time.time - startTime;
string minutes = ((int)t / 60).ToString ();
string seconds = (t % 60).ToString ("f2");
timerText2.text = minutes + ":" + seconds;
t1024++; } }