using UnityEngine;

using System.Collections;


class Item{

public int no;

public string name;

public int price;

}


public class chap10_10 : MonoBehaviour {


// Use this for initialization

void Start () {

Hashtable ht = new Hashtable();

Item a = new Item();

//string text = "2014    Asian Game 2014 World Cup " + " 2014 LOL Game Asian Cup";

string[] ItemList = {"1, HP POTION LV1, 1000", "2, MP POTION LV1, 1000",

    "3, HP POTION LV2, 5000", "3, MP POTION LV2, 5000"} ;

char[] sep = {','} ;

foreach(string data in ItemList){

print (data);

string[] tokens = data.Split(sep);

//print(tokens[0]);

//print(tokens[1]);

//print(tokens[2]);

Item item = new Item();

item.no = int.Parse(tokens[0]);

item.name = tokens[1];

item.price = int.Parse(tokens[2]);

ht[item.no] = item;

}

Item item1 = ht[1] as Item;

if(ht.ContainsKey(1))

item1 = (Item)ht[1];

if(ht.ContainsValue(item1))

print ("item1 Exist");

foreach(Item item in ht.Values){

print (string.Format("Item[{0}] = [{1}], price = {2} ", item.no, item.name, item.price));

}

}

// Update is called once per frame

void Update () {

}

}



using UnityEngine;

using System.Collections;


public class chap10_10 : MonoBehaviour {


// Use this for initialization

void Start () {

Hashtable ht = new Hashtable();

string text = "2014    Asian Game 2014 World Cup " + " 2014 LOL Game Asian Cup";

char[] sep = {' ','.'} ;

string[] tokens = text.Split(sep, System.StringSplitOptions.RemoveEmptyEntries); // System.StringSplitOptions.RemoveEmptyEntries-> empty.null

foreach(string t in tokens){

//print (t);

if(ht.ContainsKey(t))

ht[t] = (int)ht[t] + 1;

else

ht[t] = 1;

}

foreach(DictionaryEntry e in ht)

{

print ("Word = " + e.Key + "  count =" + e.Value);

}

}

// Update is called once per frame

void Update () {

}

}




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

상태 이상시 잘쓰는 비트 연산  (0) 2014.05.20
Item  (0) 2014.05.20
해쉬 테이블 -> 어레이 리스트  (0) 2014.05.20
큐를 이용한 총알 놀이 invoke sendmessage transform  (0) 2014.05.20
해석중  (0) 2014.05.20

using UnityEngine;

using System.Collections;



public class chap10_9 : MonoBehaviour {


// Use this for initialization

void Start () {

Hashtable ht = new Hashtable(10);

ht[10] = "c";

ht[11] = "b";

ht[12] = "a";

ht[13] = 300;

/*

foreach(int key in ht.Keys){

print ("key = " + key);

}

foreach(string s in ht.Values){

print ("data = " + s);

}

*/

ArrayList list = new ArrayList(ht.Values);

foreach(object s in list){

print ("list data = " + s);

}

foreach(string s in list){

print ("list data = " + s);

}

}

// Update is called once per frame

void Update () {

}

}



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

Item  (0) 2014.05.20
문자 짜르기 해쉬 테이블 사용  (0) 2014.05.20
큐를 이용한 총알 놀이 invoke sendmessage transform  (0) 2014.05.20
해석중  (0) 2014.05.20
윤석씨 (바둑)  (0) 2014.05.19

Chap10_8.cs


using UnityEngine;

using System.Collections;


public class Chap10_8 : MonoBehaviour {

public int MaxBullet = 20;

Queue bulletBox = new Queue();

ArrayList bulletFired = new ArrayList();

// Use this for initialization

void Start () {

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

GameObject bullet = GameObject.CreatePrimitive(PrimitiveType.Sphere);

bullet.transform.position = new Vector3(-10, 0, i);

bullet.transform.localScale = Vector3.one * 0.5f;

bullet.AddComponent(typeof(Bullet));

bulletBox.Enqueue(bullet);

}

}

// Update is called once per frame

void Update () {

foreach(GameObject obj in bulletFired){

obj.transform.Translate(0, 0, 5 * Time.deltaTime);

Bullet bulletScript = obj.GetComponent<Bullet>();

if(bulletScript.Life == 0){

bulletFired.Remove(obj);

bulletBox.Enqueue(obj);

obj.transform.position = new Vector3(-10, 0, bulletBox.Count);

break;

}

}

if(Input.GetKeyDown(KeyCode.Space)){

GameObject obj = bulletBox.Dequeue() as GameObject;

obj.transform.position = new Vector3(0, 0, 3);

bulletFired.Add(obj);

obj.SendMessage("Fire");

}

}

}


Bullet.cs

using UnityEngine;

using System.Collections;


public class Bullet : MonoBehaviour {

public float Life;

// Use this for initialization

void Start () {

}

// Update is called once per frame

void Update () {

}

public void Fire(){

Life = 3.0f;

Invoke("Recyle", Life);

}

void Recyle(){

Life = 0.0f;

}

}






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

문자 짜르기 해쉬 테이블 사용  (0) 2014.05.20
해쉬 테이블 -> 어레이 리스트  (0) 2014.05.20
해석중  (0) 2014.05.20
윤석씨 (바둑)  (0) 2014.05.19
선생님 예제 입니다.  (0) 2014.05.19

using UnityEngine;

using System.Collections;

 

public class Baduk : MonoBehaviour {

public GameObject Player_White;

public GameObject Player_Black;

public GameObject Play_Field;

public int map_size = 18;

int turn;//1==black | 2==white

int[,] map_status;

int[,] repeat_status;

GameObject[,] stone_status;

string turnname;

int index_x;

int index_z;

int b_cnt;

int w_cnt;

int clear_b;

int clear_w;

int clear_all;

string winner;

public enum GameMode{

Playing, GameOver

}

GameMode gamemode;

// Use this for initialization

void Start () {

map_status = new int[map_size,map_size];

repeat_status = new int[map_size,map_size];

stone_status = new GameObject[map_size, map_size];

Player_White.transform.position = new Vector3(9.0f, 0.5f, -11.0f);

Player_Black.transform.position = new Vector3(-9.0f, 0.5f, 11.0f);

turn = 1;//black

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

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

Instantiate(Play_Field, new Vector3(-8.5f+1.0f*j, 0.0f, 8.5f-1.0f*i), Quaternion.identity);

map_status[i, j] = 0;

repeat_status[i, j] = 0;

}//for

}//for

index_x = -1;

index_z = -1;

b_cnt = 0;

w_cnt = 0;

clear_b=0;

clear_w=0;

clear_all=0;

gamemode=GameMode.Playing;

winner=null;

}//Start

public void Restart(){

Player_White.transform.position = new Vector3(9.0f, 0.5f, -11.0f);

Player_Black.transform.position = new Vector3(-9.0f, 0.5f, 11.0f);

turn = 1;//black

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

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

Instantiate(Play_Field, new Vector3(-8.5f+1.0f*j, 0.0f, 8.5f-1.0f*i), Quaternion.identity);

map_status[i, j] = 0;

repeat_status[i, j] = 0;

DestroyObject(stone_status[i,j]);

}//for

}//for

index_x = -1;

index_z = -1;

b_cnt = 0;

w_cnt = 0;

clear_b=0;

clear_w=0;

clear_all=0;

gamemode=GameMode.Playing;

winner=null;

}//Restart

 

// Update is called once per frame

void Update () {

if(gamemode == GameMode.Playing){

if(turn == 1){

turnname = "Black";

}   else if(turn == 2){

turnname = "White";

}

Touch_Map();

Clear_Check();

Game_ClearCheck();

}//if

if(Input.GetKeyDown(KeyCode.Space)){

Restart();

}//if

}//Update


public void Game_ClearCheck(){

clear_b = 0;

clear_w = 0;

clear_all = 0;

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

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

if(map_status[i,j] == 1){//count black

clear_b++;

}//if

if(map_status[i,j] == 2){//count white

clear_w++;

}//if

if(map_status[i,j] == 0){//count null

clear_all++;

}//if

}//for

}//for


if(gamemode == GameMode.Playing && clear_all == 0){

GameOver();

}

}//Game_ClearCheck


public void GameOver(){

gamemode = GameMode.GameOver;

if(clear_b > clear_w){

winner="Black";

}   else if(clear_b < clear_w){

winner="White";

}   else if(clear_b == clear_w){

winner="Draw";

}//if

}//GameOver

public void Clear_Check(){

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

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

if(repeat_status[i,j]==0){

if((i == 0 && j == 0) || (i == 0 && j == map_size-1) || (i == map_size-1 && j == 0) || (i == map_size-1 && j == map_size-1)){//corner

if(i == 0 && j == 0 && map_status[i,j]==1 && map_status[i+1,j]==2 && map_status[i,j+1]==2//top left

|| i == 0 && j == 0 && map_status[i,j]==2 && map_status[i+1,j]==1 && map_status[i,j+1]==1){

Move_Stone(map_status[i,j], stone_status[i,j]);

map_status[i,j]=0;

repeat_status[i,j]=1;

else if(i == map_size-1 && j == 0 && map_status[i,j]==1 && map_status[i-1,j]==2 && map_status[i,j+1]==2//top right

|| i == map_size-1 && j == 0 && map_status[i,j]==2 && map_status[i-1,j]==1 && map_status[i,j+1]==1){

Move_Stone(map_status[i,j], stone_status[i,j]);

map_status[i,j]=0;

repeat_status[i,j]=1;

else if(i == 0 && j == map_size-1 && map_status[i,j]==1 && map_status[i+1,j]==2 && map_status[i,j-1]==2//bottom left

|| i == 0 && j == map_size-1 && map_status[i,j]==2 && map_status[i+1,j]==1 && map_status[i,j-1]==1){

Move_Stone(map_status[i,j], stone_status[i,j]);

map_status[i,j]=0;

repeat_status[i,j]=1;

else if(i == map_size-1 && j == map_size-1 && map_status[i,j]==1 && map_status[i-1,j]==2 && map_status[i,j-1]==2//bottom right

|| i == map_size-1 && j == map_size-1 && map_status[i,j]==2 && map_status[i-1,j]==1 && map_status[i,j-1]==1){

Move_Stone(map_status[i,j], stone_status[i,j]);

map_status[i,j]=0;

repeat_status[i,j]=1;

}//if

}   

if(i == 0 || i == map_size-1 || j == 0 || j == map_size-1){//border

if(i == 0 && map_status[i,j]==1 && map_status[i,j-1]==2 && map_status[i,j+1]==2 && map_status[i+1,j]==2//left

|| i == 0 && map_status[i,j]==2 && map_status[i,j-1]==1 && map_status[i,j+1]==1 && map_status[i+1,j]==1){

Move_Stone(map_status[i,j], stone_status[i,j]);

map_status[i,j]=0;

repeat_status[i,j]=1;

else if(i == map_size-1 && map_status[i,j]==1 && map_status[i,j-1]==2 && map_status[i,j+1]==2 && map_status[i-1,j]==2//right

|| i == map_size-1 && map_status[i,j]==2 && map_status[i,j-1]==1 && map_status[i,j+1]==1 && map_status[i-1,j]==1){

Move_Stone(map_status[i,j], stone_status[i,j]);

map_status[i,j]=0;

repeat_status[i,j]=1;

else if(j == 0 && map_status[i,j]==1 && map_status[i-1,j]==2 && map_status[i+1,j]==2 && map_status[i,j+1]==2//top

|| j == 0 && map_status[i,j]==2 && map_status[i-1,j]==1 && map_status[i+1,j]==1 && map_status[i,j+1]==1){

Move_Stone(map_status[i,j], stone_status[i,j]);

map_status[i,j]=0;

repeat_status[i,j]=1;

else if(j == map_size-1 && map_status[i,j]==1 && map_status[i-1,j]==2 && map_status[i+1,j]==2 && map_status[i,j-1]==2//bottom

|| j == map_size-1 && map_status[i,j]==2 && map_status[i-1,j]==1 && map_status[i+1,j]==1 && map_status[i,j-1]==1){

Move_Stone(map_status[i,j], stone_status[i,j]);

map_status[i,j]=0;

repeat_status[i,j]=1;

}//if

}   

if(0 < i && i < map_size-1 && 0 < j && j < map_size-1 ){//inside

if(map_status[i,j]==1 && map_status[i+1,j]==2 && map_status[i,j+1]==2 && map_status[i-1,j]==2 && map_status[i,j-1]==2

|| map_status[i,j]==2 && map_status[i+1,j]==1 && map_status[i,j+1]==1 && map_status[i-1,j]==1 && map_status[i,j-1]==1){

Move_Stone(map_status[i,j], stone_status[i,j]);

map_status[i,j]=0;

repeat_status[i,j]=1;

}//if

}//if

}//repeat if

}//for

}//for

}//Clear_Check

public void Move_Stone(int turn, GameObject stone){

if(turn == 1){//black

stone.transform.position = new Vector3(5.0f-0.5f*b_cnt, 0.5f, -10.0f);

b_cnt++;

else if(turn == 2){//white

stone.transform.position = new Vector3(-5.0f+0.5f*w_cnt, 0.5f, 10.0f);

w_cnt++;

}

}//Move_Stone

public bool Position_Check(int turn, float x, float z){

switch((int)(10*x)){

case -85 : index_x = 0; break;

case -75 : index_x = 1; break;

case -65 : index_x = 2; break;

case -55 : index_x = 3; break;

case -45 : index_x = 4; break;

case -35 : index_x = 5; break;

case -25 : index_x = 6; break;

case -15 : index_x = 7; break;

case -5 : index_x = 8; break;

case 5 : index_x = 9; break;

case 15 : index_x = 10; break;

case 25 : index_x = 11; break;

case 35 : index_x = 12; break;

case 45 : index_x = 13; break;

case 55 : index_x = 14; break;

case 65 : index_x = 15; break;

case 75 : index_x = 16; break;

case 85 : index_x = 17; break;

}

switch((int)(10*z)){

case 85 : index_z = 0; break;

case 75 : index_z = 1; break;

case 65 : index_z = 2; break;

case 55 : index_z = 3; break;

case 45 : index_z = 4; break;

case 35 : index_z = 5; break;

case 25 : index_z = 6; break;

case 15 : index_z = 7; break;

case 5 : index_z = 8; break;

case -5 : index_z = 9; break;

case -15 : index_z = 10; break;

case -25 : index_z = 11; break;

case -35 : index_z = 12; break;

case -45 : index_z = 13; break;

case -55 : index_z = 14; break;

case -65 : index_z = 15; break;

case -75 : index_z = 16; break;

case -85 : index_z = 17; break;

}

//print ("Index_X : "+index_x+", Index_Z : "+index_z);

bool result=false;

if(map_status[index_x, index_z] == 0){

result = true;

}

return result;

}//Position_Check

public void My_Turn(int turn, int index_x, int index_z){

map_status[index_x, index_z] = turn;

if(turn == 1){

stone_status[index_x, index_z] = (GameObject)Instantiate(Player_Black, new Vector3(-8.5f+1.0f*index_x, 0.5f, 8.5f-1.0f*index_z), Quaternion.identity);

else if(turn == 2){

stone_status[index_x, index_z] = (GameObject)Instantiate(Player_White, new Vector3(-8.5f+1.0f*index_x, 0.5f, 8.5f-1.0f*index_z), Quaternion.identity);

}

}//My_Turn

public void Touch_Map(){

if(Input.GetMouseButtonDown(0)){

Vector3 point = Input.mousePosition;

Ray ray = Camera.main.ScreenPointToRay(point);

RaycastHit hit;

if(Physics.Raycast(ray, out hit, 10000.0f) == true){

if(Position_Check(turn, hit.transform.position.x, hit.transform.position.z) == true){// this empty...

My_Turn(turn, index_x, index_z);

if(turn == 1){

turn = 2;

else if(turn == 2){

turn = 1;

}//turn if

}   else{

print ("Wrong Play !!!");

}//Position_Check if

}//Physics if

}//click if

}//Touch_Map

void OnGUI(){

GUI.Label(new Rect(325, 175, 175, 25), "Current Turn : "+turnname);

string temppos="";

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

temppos="";

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

temppos += map_status[j, i]+"\t";

}

GUI.Label(new Rect(25, 100+10*i, 300, 25), temppos);

}

GUI.Label(new Rect(850, 175, 175, 25), "Black "+clear_b+" : "+clear_w+" White");

GUI.Label(new Rect(850, 200, 175, 25), "Winner : "+winner);

}//OnGUI

 

}//class


using UnityEngine;

using System.Collections;


public class Chap10_8 : MonoBehaviour {

public GameObject[] cubes;

Stack stack = new Stack();

// Use this for initialization

void Start () {

for(int i = 0; i < 5; i++)

stack.Push(cubes[i]);

}

// Update is called once per frame

void Update () {

if(Input.GetKeyDown(KeyCode.Space)){

Destroy(stack.Pop() as GameObject);

}

}

}






using UnityEngine;

using System.Collections;


public class multi : MonoBehaviour {

//int[] array5 = new string[]{"hello", "hi", "ohao"};  cannot implictly convert type 'string[]' to 'int[]'

int[] array6 = new int[3]{1, 2, 3} ;

int[] array7 = new int[]{1, 2, 3} ;

int[] array8 = {1 ,2 ,3} ;

void Show(int[,] array){

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

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

print (string.Format("array[{0}, {1}] = {2}", i, j , array[i, j]));

}

}

}

int[,] array1 = {{3, 2} , {1, 4}} ;

int[,] array2 = {{9, 2} , {1, 7}} ; 

//int sum, sum1, sum2, sum3 = 0;

int[,] result = new int[2, 2];

// Use this for initialization

void Start () {

//Show (array1);

//Show (array2);

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

{

result[i, 0]= array1[i,0] * array2[0,0] + array1[i,1] * array2[1,0];

result[i, 1]= array1[i,0] * array2[0,1] + array1[i,1] * array2[1,1];

}

Show(result);

/*

sum = array1[0,0] * array2[0,0] + array1[0,1] * array2[1,0];

sum1 = array1[0,0] * array2[0,1] + array1[0,1] * array2[1,1];

sum2 = array1[1,0] * array2[0,0] + array1[1,1] * array2[1,0];

sum3 = array1[1,0] * array2[0,1] + array1[1,1] * array2[1,1];

print (sum);

print (sum1);

print (sum2);

print (sum3);

*/

Stack stack = new Stack();

stack.Push(1);

stack.Push(2);

stack.Push(3);

stack.Push(4);

stack.Push(5);

while(stack.Count > 0)

print(string.Format("stack.Pop {0} " , stack.Pop()));

Queue que = new Queue();

que.Enqueue(1);

que.Enqueue(2);

que.Enqueue(3);

que.Enqueue(4);

que.Enqueue(5);

while(que.Count > 0)

print(string.Format("que.Dequeue {0} " , que.Dequeue()));

Hashtable ht = new Hashtable();

ht["Microsoft"] = "Microsoft";

ht["URL"] = "www.microsoft.com";

print (string.Format("company : {0}", ht["Microsoft"]));

print (string.Format("URL : {0}", ht["URL"]));

}

// Update is called once per frame

void Update () {

}

}



using UnityEngine;

using System.Collections;


public class Chap10_7 : MonoBehaviour {

void ShowList(IEnumerable list){

//string s = "array =";

string s = list.GetType().Name + " : ";

foreach(object obj in list){

s += " " + obj.GetType().Name + "[" + obj.ToString() + "]";

GameObject go1 = obj as GameObject;

if(go1 != null){

}

}

print(s);

}

// Use this for initialization

void Start () {

/*

ArrayList array = new ArrayList();

array.Add(10);

array.Add(20.1f);

array.Add(30);

array.Add("Hello");

ShowList(array);

array.RemoveAt(2);

ShowList(array);

array.Remove(10);

ShowList(array);

Queue queue = new Queue();

queue.Enqueue(10);

queue.Enqueue("20");

queue.Enqueue("Hello");

ShowList(queue);

object a = queue.Dequeue();

object b = queue.Peek(); //important

print ("peek b = " + b);

ShowList(queue);

while(queue.Count > 0)

print ("deque =" + queue.Dequeue());

Stack stack = new Stack();

stack.Push(10);

stack.Push("20");

stack.Push("Hello");

ShowList(stack);

print ("Pop = " + stack.Pop());

ShowList(stack);

print ("Peek = " + stack.Peek());

ShowList(stack);

GameObject obj;

Hashtable ht = new Hashtable();

ht.Add("book", "C# Programming");

ht.Add("cook", "Kim Bab");

ht.Add ("twee", "Bird sing!");

ht["hobbang"] = "gwang ho ! singing";

ht[4] = "gwang ho2 ! singing";

print (ht["book"]);

print (ht["cook"]);

print (ht["twe"]); //not key -> null output

print (ht["hobbang"]);

print(ht[4]);

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

print("key =" + i + "value =" + ht[i]); //array

}

*/

SortedList slist = new SortedList();

slist.Add(1,20);

slist.Add(3,30);

slist.Add(5,50);

slist.Add(2, 60);

ShowList(slist);

string s = "sorted list";

foreach(DictionaryEntry a in slist){

s += a.Value + ",";

}

print (s);

}

// Update is called once per frame

void Update () {

}

}


+ Recent posts