unity5.2の作法 その15 game

#概要

unity5.2の作法、調べてみた。
ゲームを作った。オセロ。

#makefile

- menu->file->new sceneする。
- menu->file->save sceneでgameとする。
- menu->gameobject->create emptyする。
- menu->assetes->creates->c# scriptでgame.csつくる。
- ダブルクリックして、コードを書く。
- ヒエラルキーのgameobjectに、コードを重ねる。
- 三角で実行。



#サンプルコード

```
using UnityEngine;
using System.Collections;

public class game : MonoBehaviour {
    int[] ban = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 1, 1, 1,
        1, 1, 1, 2, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
    void Start() {
	
	}
	void Update () {
        if (Input.GetMouseButtonDown(0))
        {
            int posx = (int) Input.mousePosition.x;
            int posy = Screen.height - (int) Input.mousePosition.y;
            int n = (posx / 30) + (posy / 30) * 8;
            Debug.Log(n);
            test(n);
        }
	}
    void OnGUI() {
        GUI.color = Color.yellow;
        GUI.skin.label.fontSize = 28;
        int x;
        int y;
        for (int i = 0; i < 64; i++)
        {
            x = i % 8 * 30 + 5;
            y = i / 8 * 30 + 5;
            if (ban[i] == 0)
            {
                GUI.Label(new Rect(x, y, 30, 30), "○");
            }
            if (ban[i] == 1)
            {
                GUI.Label(new Rect(x, y, 30, 30), "-");
            }
            if (ban[i] == 2)
            {
                GUI.Label(new Rect(x, y, 30, 30), "●");
            }
        }
    }
    int check(int put, int d) {
        int x = put % 8;
        int y = put / 8;
        int res = 0;
        if (x == 0 && (d == -9 || d == -1 || d ==  7)) res = 1;
        if (x == 7 && (d == -7 || d ==  1 || d ==  9)) res = 1;
        if (y == 0 && (d == -9 || d == -8 || d == -7)) res = 1;
        if (y == 7 && (d ==  7 || d ==  8 || d ==  9)) res = 1;
        return res;
    }
    int sasu() {
        int[] suji = {0, 7, 56, 63, 18, 21, 42, 45, 2, 16, 5, 23, 40, 58, 47, 61, 3, 4, 11, 12, 19, 20, 24, 25, 26, 32, 33, 34, 29, 30, 31, 37,
            38, 39, 43, 44, 51, 52, 59, 60, 1, 8, 9, 10, 17, 6, 13, 14, 15, 22, 41, 48, 49, 50, 57, 46, 53, 54, 55, 62};
        int res = -1;
        int all = 0;
        int iro = 2;
        int turn = 0;
        int[] dir = {-9, 9, -7, 7, -1, 1, -8, 8};
        int i;
        int j;
        int put;
        int count;
        int put1 = 0;
        int f1;
        for (j = 0; j < 60; j++)
        {
            put = suji[j];
            if (ban[put] == 1)
            {
                for (i = 0; i < 8; i++)
                {
                    count = 0;
                    if (check(put, dir[i]) == 0)
                    {
                        put1 = put + dir[i];
                        f1 = 0;
                        do
                        {
                            if (ban[put1] == iro)
                            {
                                count++;
                                if (check(put1, dir[i]) == 0)
                                {
                                    put1 += dir[i];
                                }
                                else
                                {
                                    f1 = 1;
                                }
                            }
                            else
                            {
                                f1 = 1;
                            }
                        } while (f1 == 0);
                    }
                    if ((count > 0) && (ban[put1] == turn))
                    {
                        all += count;
                    }
                }
            }
            if (all > 0)
            {
                res = put;
                break;
            }
        }
        return res;
    }
    int oku(int put, int iro) {
        int res = 0;
        int turn = 0;
        if (iro == 0) turn = 2;
        int[] dir = {-9, -8, -7, -1, 1, 7, 8, 9};
        int tugi;
        int i;
        int count;
        if (ban[put] == 1)
        {
            for (i = 0; i < 8; i++)
            {
                count = 0;
                tugi = put;
                do
                {
                    if (check(tugi, dir[i]) == 1) break;
                    count++;
                    tugi += dir[i];
                }
                while (ban[tugi] == turn);
                if ((count > 1) && (ban[tugi] == iro))
                {
                    res = -1;
                    tugi = put;
                    do
                    {
                        ban[tugi] = iro;
                        tugi += dir[i];
                    }
                    while (ban[tugi] == turn);
                }
            }
        }
        return res;
    }
    void test(int n) {
        int put = n;
        int res = oku(put, 2);
        if (res == -1)
        {
            put = sasu();
            res = oku(put, 0);
            int end = -1;
            for (int i = 0; i < 64; i++)
            {
                if (ban[i] == 1) end = 0;
            }
            if (end == -1)
            {
                Debug.Log("over!");
            }
        }
    }
}





```


以上。