using UnityEngine;

using System.Collections;


public class GameManager : MonoBehaviour {

public const int Line = 9;

public const int MaxStone = 30;

public int Grid = 1; //None

const int EMPTY = 0;

const int BLACK = 1;

const int WHITE = 2;

const int WALL = 3;

int[] current = {0, 0} ;

int[] captured = {0, 0} ;

public Transform[] StoneBox;//parent

bool[,] lifePoint = new bool[Line + 2, Line + 2];//11*11

int color = BLACK;//1

public GameObject[,] Stones = new GameObject[2, MaxStone];

string[] StoneName = {"Black", "White"} ;

int[,] checkPoint = new int[4, 2] { {0, 1} , {1, 0} , {0, -1} , {-1, 0}  };

public int [,] Jum = new int[Line + 2, Line + 2];

public GameObject [, ] JumObject = new GameObject[Line + 2, Line + 2];//11*11

void Start () {

for (int i = 0; i < 2; i++) {

for (int j = 0; j < MaxStone; j++) {

Stones[i, j] = GameObject.CreatePrimitive(PrimitiveType.Sphere);

Stones[i, j].renderer.material.color = (i == 0) ? Color.black : Color.white;

Stones[i, j].name = string.Format("{0}[{1}]", StoneName[i], j);

Stones[i, j].transform.position = new Vector3(-7 + 14 * i, 0, -3.0f + (float)j * 0.3f);

Stones[i, j].transform.parent = StoneBox[i];

Stones[i, j].transform.localScale = new Vector3(1.0f, 0.4f, 1.0f);

Stones[i, j].AddComponent(typeof(Stone));

}//for

}//for

for (int x = 0; x <= Line; x++) {// 0 ~ 9

Jum[x, 0] = WALL;

Jum[x, Line + 1] = WALL;

}//for

for (int z = 0; z <= Line; z++) {// 0 ~ 9

Jum[0, z] = WALL;

Jum[Line + 1, z] = WALL;

}//for

}//Start

void Update () {

if (Input.GetMouseButtonDown(0)) {//left click

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//camera to touchpoint

RaycastHit hit;

if (Physics.Raycast(ray, out hit) && current[color - 1] < MaxStone) {

Vector3 pos = hit.point;

//print ("Hit.Point : "+pos);

pos.x = Mathf.Clamp(Mathf.Round (pos.x), -Line / 2, Line / 2);

pos.y = 0.2f;

pos.z = Mathf.Clamp(Mathf.Round (pos.z), -Line / 2, Line / 2);

int px, pz;

px = 1 + (int)(pos.x + Line * 0.5f);

pz = 1 + (int)(pos.z + Line * 0.5f);

if (Jum[px, pz] != EMPTY) {

JumObject[px, pz].SendMessage("PlayScale");

return;

}//Empty if

GameObject stone = Stones[color - 1, current[color - 1]];

stone.transform.position = pos;

//print (string.Format("Move : color = {0}, px = {1}, pz = {2}", (color == BLACK) ? "B" : "W", px, pz));

Jum[px, pz] = color;

JumObject[px, pz] = stone;

checkLife2(px, pz);

current[color - 1]++;

color = (color == BLACK) ? WHITE : BLACK;

}//Physics if

}//Click if

}//Update

bool getLifePoint(int x, int z) {

int i = 0;

for (i = 0; i < 4; i++) {

int x1 = x + checkPoint[i, 0];// 0, 1, 0, -1

int z1 = z + checkPoint[i, 1];// 1, 0, -1, 0

if (Jum[x1, z1] == EMPTY)

return true;

}//for

return false;

}//getLifePoint

void deleteJum (int x, int z) {

if (Jum[x, z] == BLACK) {

Jum[x, z] = EMPTY;

captured[0]++;

JumObject[x, z].transform.position = new Vector3(2.0f + captured[0] * 0.2f, 0.5f, 6.0f);

else if (Jum[x, z] == WHITE) {

Jum[x, z] = EMPTY;

captured[1]++;

JumObject[x, z].transform.position = new Vector3(-2.0f - captured[1] * 0.2f, 0.5f, 6.0f);

}//if

}//deleteJum

void checkLife2 (int lastX, int lastZ) {

for (int x = 1; x <= Line; x++) {// 1 ~ 9

for (int z = 1; z <= Line; z++) {// 1 ~ 9

lifePoint[x, z] = getLifePoint(x, z);

}//for

}//for

for (int x = 1; x <= Line; x++) {// 1 ~ 9

for (int z = 1; z <= Line; z++) {// 1 ~ 9

if (Jum[x, z] == EMPTY)

continue;

for (int i = 0; i < 4; i++) {

int x1 = x + checkPoint[i, 0];

int z1 = z + checkPoint[i, 1];

if (Jum[x, z] == Jum[x1, z1] && lifePoint[x1, z1] == true) {

lifePoint[x, z] = true;

break;

}//if

}//for

}//for

}//for

for (int x = 1; x <= Line; x++) {

for (int z = 1; z <= Line; z++) {

if (Jum[x, z] != EMPTY && lifePoint[x, z] == false)

deleteJum(x, z);

}//for

}//for

}//checkLife2

void checkLife () {

for (int x = 1; x <= Line; x++) {

for (int z = 1; z <= Line; z++) {

if (Jum[x, z] == EMPTY)

continue;

int i = 0;

for (i = 0; i < 4; i++) {

int x1 = x + checkPoint[i, 0];

int z1 = z + checkPoint[i, 1];

if (Jum[x1, z1] == Jum[x, z] || Jum[x1, z1] == EMPTY) {

break; // LIFE 

}//if

}//for

if (i < 4)

continue;

deleteJum(x, z);

}//for

}//for

}//checkLife

}//class


'Computer Language > 유니티' 카테고리의 다른 글

[ContextMenu("Arrange")] 활용법  (0) 2014.06.23
진행중 gamecontroller  (0) 2014.06.10
값과 주소.  (0) 2014.05.26
LookAt()  (0) 2014.05.23
vector3 사용예제  (0) 2014.05.23

+ Recent posts