using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour {
public const int Line = 9;
public const int MaxStone = 50;
const int EMPTY = 0;
const int BLACK = 1;
const int WHITE = 2;
const int WALL = 3;
int[] current = {0, 0} ; // Black, White
int[] captured = {0, 0} ;
public Transform[] StoneBox;
int color = BLACK;
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];
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.red;
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));
print (Stones[i, j].name );
}
}
for (int x = 0; x <= Line; x++) {
Jum[x, 0] = WALL;
Jum[x, Line + 1] = WALL;
}
for (int z = 0; z <= Line; z++) {
Jum[0, z] = WALL;
Jum[Line + 1, z] = WALL;
}
}
void Update () {
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit) && current[color - 1] < MaxStone) {
Vector3 pos = hit.point;
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;
}
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;
checkLife();
current[color - 1]++;
color = (color == BLACK) ? WHITE : BLACK;
}
}
}
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 (i < 4)
continue;
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);
}
}
}
}
}
Stone.cs
using UnityEngine;
using System.Collections;
public class Stone : MonoBehaviour {
float scale = 1.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (scale > 1.0f) {
transform.localScale = new Vector3(scale, 0.5f, scale);
scale -= 0.05f;
}
}
public void PlayScale () {
scale = 1.5f;
}
}
'Computer Language > 유니티' 카테고리의 다른 글
이것만은 알고 갚시다. 10장 배열 컬렉션, 그리고 인덱서 (0) | 2014.05.19 |
---|---|
arraylist, queue, stack, hashtable, sortedlist (0) | 2014.05.19 |
바둑돌 (0) | 2014.05.16 |
가변 배열 (0) | 2014.05.16 |
다차원 배열 (0) | 2014.05.16 |