1. 程式人生 > >Unity3D學習筆記1

Unity3D學習筆記1

下面是一個井字棋的原始碼

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour {

    int[,] Tic_Tac_Toe;
    int turn;
    int num;
    bool end;

    // Use this for initialization
    void Start () {
        restart();
}
	
	// Update is called once per frame
	void OnGUI () {

        int winner = w_win();

        if (winner == 1)
        {
            GUI.Label(new Rect(160, 0, 40, 40), "O win");
            end = true;
        }

        else if (winner == -1)
        {
            GUI.Label(new Rect(160, 0, 40, 40), "X win");
            end = true;
        }

        else if (num == 9)
        {
            GUI.Label(new Rect(160, 0, 40, 40), "draw");
        }

        if (GUI.Button(new Rect(160, 80, 50, 40), "restart"))
        {
            restart();
        }

        for (int row = 0; row < 3; row++)
        {
            for (int col = 0; col < 3; col++)
            {
                if (Tic_Tac_Toe[row, col] == 0 && GUI.Button(new Rect(row * 40, col* 40, 40, 40), "") && !end)
                {
                    if (turn == 1)
                    {
                        Tic_Tac_Toe[row, col] = 1;
                    }
                    else
                    {
                        Tic_Tac_Toe[row, col] = -1;
                    }
                    turn = -turn;
                    num++;
                }
                if (Tic_Tac_Toe[row, col] == 1)
                {
                    GUI.Button(new Rect(row * 40, col * 40, 40, 40), "O");
                }
                if (Tic_Tac_Toe[row, col] == -1)
                {
                    GUI.Button(new Rect(row * 40, col * 40, 40, 40), "X");
                }
            }
        }
    }

    private void restart()
    {
        turn = 1;
        num = 0;
        end = false;
        if (Tic_Tac_Toe == null)
        {
            Tic_Tac_Toe = new int[3, 3];
        }
        for (int row = 0; row < 3; row++)
        {
            for (int col = 0; col < 3; col++)
            {
                Tic_Tac_Toe[row, col] = 0;
            }
        }
    }
    
    private int w_win()
    {
        
            for (int row = 0; row < 3; row++)
            {
                if (Tic_Tac_Toe[row, 0] != 0 && Tic_Tac_Toe[row, 0] ==  Tic_Tac_Toe[row, 1]  && Tic_Tac_Toe[row, 1] == Tic_Tac_Toe[row, 2])
                {
                    return Tic_Tac_Toe[row, 0];
                }
            }

            for (int col = 0; col < 3; col++)
            {
                if (Tic_Tac_Toe[0, col] != 0 && Tic_Tac_Toe[0, col] == Tic_Tac_Toe[1, col] && Tic_Tac_Toe[1, col] == Tic_Tac_Toe[2, col])
                {
                    return Tic_Tac_Toe[0, col];
                }
            }

            if (Tic_Tac_Toe[0, 0] != 0 && Tic_Tac_Toe[0, 0] ==  Tic_Tac_Toe[1, 1] && Tic_Tac_Toe[1, 1] == Tic_Tac_Toe[2, 2])
            {
                return Tic_Tac_Toe[0, 0];
            }

            if (Tic_Tac_Toe[0, 2] != 0 && Tic_Tac_Toe[0, 2] == Tic_Tac_Toe[1, 1] && Tic_Tac_Toe[1, 1] == Tic_Tac_Toe[2, 0])
            {
                return Tic_Tac_Toe[0, 2];
            }
        
        return 0;
    }
}