유니티3D 프로그래밍
C# 2주차 4일 수업 내용 (21.03.18) 본문
stack 복습
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
//Stack<string> 변수 선언과 인스턴스 생성 및 초기화
Stack<string> names = new Stack<string>();
//요소 추가
names.Push("질롯");
names.Push("템플러");
names.Push("드라군");
names.Push("다크템플러");
//foreach문을 사용해 요소 출력
foreach(string name in names)
{
Console.WriteLine(name);
}
//Pop
string popElement = names.Pop();
Console.WriteLine(popElement);
//Count
Console.WriteLine(names.Count);
//Contains
bool isContains = names.Contains("질롯");
Console.WriteLine(isContains);
//Peek
string peekElement = names.Peek();
Console.WriteLine("peek : {0}",peekElement);
//stack -> array
//요소 복사본을 포함하는 새 배열 반환
string[] arrNames = names.ToArray();
//for
Console.WriteLine("****** Array ******");
for(int i = 0; i < arrNames.Length; i++)
{
Console.WriteLine("index: {0}, name: {1}", i, arrNames[i]);
}
//foreach
Console.WriteLine("****** Stack ******");
foreach(string name in names)
{
Console.WriteLine(name);
}
//copyTo
Console.WriteLine("***** Array arrNames copy to arrNames2 *****");
string[] arrNames2 = new string[arrNames.Length * 2];
Console.WriteLine(arrNames2.Length);
arrNames.CopyTo(arrNames2, arrNames.Length);
for(int i = 0; i < arrNames.Length; i++)
{
if (string.IsNullOrEmpty(arrNames2[i]))
{
Console.WriteLine("[Enpty]");
}
else
Console.WriteLine(arrNames2[i]);
}
//Clear
names.Clear();
Console.WriteLine(names);
}
}
}
Queue 복습
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
//Queue<string> 인스턴스 생성
Queue<string> names = new Queue<string>();
//요소 추가
names.Enqueue("질롯");
names.Enqueue("드라군");
names.Enqueue("템플러");
names.Enqueue("다크템플러");
Console.WriteLine("************************");
//foreach문으로 요소 출력
foreach(string name in names)
{
Console.WriteLine(name);
}
//Dequeue
string dequeueElement = names.Dequeue();
Console.WriteLine(dequeueElement);
Console.WriteLine("********* Array *********");
string[] arrNames = names.ToArray();
for(int i = 0; i < arrNames.Length; i++)
{
Console.WriteLine("index : {0} name : {1}", i, arrNames[i]);
}
Console.WriteLine("********* names copy to namesCopy *********");
Queue<string> namesCopy = new Queue<string>(arrNames);
//foreach문으로 요소 출력
foreach (string name in names)
{
Console.WriteLine(name);
}
//Contains
bool isContains = names.Contains("드라군");
Console.WriteLine(isContains);
//Clear
names.Clear();
Console.WriteLine(names);
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
//List<Item> 변수 선언
List<Item> list;
//List<Item> 객체 생성 및 할당
list = new List<Item>();
//Item 객체 생성
Item item1 = new Item(100, "장검");
Item item2 = new Item(101, "단검");
Item item3 = new Item(102, "활");
//List에 요소 추가
list.Add(item1);
list.Add(item2);
list.Add(item3);
//List<Item> 요소 수 출력
Console.WriteLine(list.Count);
//for문으로 요소 출력 (id, name)
for(int i = 0; i < list.Count; i++)
{
Item item = list[i];
Console.WriteLine("{0} {1}", item.Id, item.Name);
}
Console.WriteLine("----------------------------");
//foreach문으로 요소 출력(id, name)
foreach(Item item in list)
{
Console.WriteLine("{0} {1}", item.Id, item.Name);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
class Item
{
public int Id { get; private set; }
public string Name { get; private set; }
//생성자
public Item(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
}
다차원 배열
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
//배열 변수 선언 및 초기화
int[,] arr = new int[,]
{
{1, 2 },
{3, 4 },
{5, 6 },
{7, 8 },
};
//차원의 배열의 길이 출력
Console.WriteLine(arr.GetLength(0));
Console.WriteLine(arr.GetLength(1));
//배열의 요소 출력
for (int i = 0; i < arr.GetLength(0); i++)
{
for(int j = 0; j < arr.GetLength(1); j++)
{
Console.WriteLine("arr[{0},{1}] : {2}", i, j, arr[i, j]) ;
}
}
}
}
}
ng System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
//배열 선언 및 초기화
int[,] arr = new int[3,4];
arr[1, 1] = -1;
arr[1, 2] = -1;
//배열의 각 차원 길이 출력
Console.WriteLine(arr.GetLength(0));
Console.WriteLine(arr.GetLength(1));
//배열의 요소 출력
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write("{0, 2 }, ", arr[i, j]);
}
Console.WriteLine();
}
}
}
}
소규모 맵 만들기
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
//배열 선언 및 초기화
int[,] map = new int[3,4];
for(int i = 0; i < map.GetLength(0); i++)
{
for(int j = 0; j < map.GetLength(1); j++)
{
if (i == 0)
{
map[i, j] = 100;
}
if(i == 1)
{
map[i, j] = 101;
}
if(i == 2)
{
if (j == 3)
map[i, j] = 101;
else
map[i, j] = 100;
}
}
}
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write ("{0} ", map[i, j]);
}
Console.WriteLine();
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
//배열 선언 및 초기화
int[,] map = new int[,]
{
{100, 301, 302, 303, 304},
{100, 100, 305, 306, 307},
{101, 100, 100, 305, 308},
{101, 101, 101, 101, 101},
};
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write ("{0} ", map[i, j]);
}
Console.WriteLine();
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
//배열 선언 및 초기화
int[,] map = new int[,]
{
{100, 101, 101, 100},
{101, 101, 100, 100},
{101, 100, 100, 100},
};
int[,] mapCharacter = new int[,]
{
{0, 0, 0, 1},
{0, 0, 0, 0},
{0, 0, 0, 0},
};
Console.WriteLine("맵 출력.");
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write ("{0} ", map[i, j]);
}
Console.WriteLine();
}
Console.WriteLine("**********************");
Console.WriteLine("1 : 캐릭터 위치 출력");
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write("{0} ", mapCharacter[i, j]);
}
Console.WriteLine();
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
//배열 선언 및 초기화
int[,] map = new int[,]
{
{301, 100, 100, 101},
{101, 100, 101, 101},
{101, 101, 101, 100},
{101, 101, 101, 101},
};
Console.WriteLine("맵 출력.");
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write ("{0} ", map[i, j]);
}
Console.WriteLine();
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
//배열 선언 및 초기화
int[,] map = new int[,]
{
{100, 100, 100, 101, 101},
{100, 100, 101, 101, 101},
{101, 101, 100, 100, 100},
{101, 100, 100, 100, 100},
};
int[,] mapCharacter = new int[,]
{
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 0, 0},
};
Console.WriteLine("맵 출력.");
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write ("{0} ", map[i, j]);
}
Console.WriteLine();
}
Console.WriteLine("*************************");
Console.WriteLine("1 : 캐릭터 위치");
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write("{0} ", mapCharacter[i, j]);
}
Console.WriteLine();
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
//배열 선언 및 초기화
int[,] map = new int[,]
{
{100, 100, 100, 100, 101, 101, 101},
{100, 100, 100, 101, 101, 101, 101},
{100, 101, 101, 100, 100, 100, 100},
{101, 101, 100, 100, 100, 100, 100},
{101, 100, 100, 100, 100, 100, 100},
{301, 302, 302, 303, 100, 100, 100}
};
int[,] mapCharacter = new int[,]
{
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
};
Console.WriteLine("맵 출력.");
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write ("{0} ", map[i, j]);
}
Console.WriteLine();
}
Console.WriteLine("*************************");
Console.WriteLine("1 : 캐릭터 위치");
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write("{0} ", mapCharacter[i, j]);
}
Console.WriteLine();
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
//배열 선언 및 초기화
int[,] map = new int[,]
{
{301, 100, 101, 101},
{302, 303, 100, 101},
{304, 100, 100, 101},
{101, 100, 101, 101},
};
Console.WriteLine("맵 출력.");
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write ("{0} ", map[i, j]);
}
Console.WriteLine();
}
Console.WriteLine("*************************");
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
//배열 선언 및 초기화
int[,] map = new int[,]
{
{100, 101, 101, 101},
{100, 100, 301, 302},
{101, 303, 304, 301},
{100, 100, 100, 101},
};
Console.WriteLine("맵 출력.");
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write ("{0} ", map[i, j]);
}
Console.WriteLine();
}
Console.WriteLine("*************************");
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
//배열 선언 및 초기화
int[,] map = new int[,]
{
{101, 101, 101, 101},
{100, 100, 100, 101},
{100, 100, 100, 100},
};
Console.WriteLine("맵 출력.");
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write ("{0} ", map[i, j]);
}
Console.WriteLine();
}
Console.WriteLine("*************************");
Console.WriteLine("맵 변경");
map[0, 3] = 300;
Console.WriteLine("맵 출력.");
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write("{0} ", map[i, j]);
}
Console.WriteLine();
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
//배열 선언 및 초기화
int[,] map = new int[,]
{
{300, 301 },
{100, 302 },
{100, 100 },
{101, 101 }
};
Console.WriteLine("맵 출력.");
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write ("{0} ", map[i, j]);
}
Console.WriteLine();
}
Console.WriteLine("*************************");
Console.WriteLine("맵 변경");
map[0, 0] = 100;
Console.WriteLine("맵 출력.");
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write("{0} ", map[i, j]);
}
Console.WriteLine();
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
//배열 선언 및 초기화
int[,] map = new int[,]
{
{100, 100, 101},
{100, 100, 100},
{100, 100, 100},
};
Console.WriteLine("맵 출력.");
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write ("{0} ", map[i, j]);
}
Console.WriteLine();
}
Console.WriteLine("*************************");
Console.WriteLine("맵 변경");
map[0, 0] = 300;
map[0, 1] = 301;
map[0, 2] = 302;
map[1, 1] = 303;
map[1, 2] = 304;
map[2, 2] = 303;
Console.WriteLine("맵 출력.");
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write("{0} ", map[i, j]);
}
Console.WriteLine();
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
//배열 선언 및 초기화
int[,] map = new int[,]
{
{300, 302, 303, 304, 100},
{100, 301, 307, 306, 305},
{100, 100, 301, 308, 100},
};
Console.WriteLine("맵 출력.");
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write ("{0} ", map[i, j]);
}
Console.WriteLine();
}
Console.WriteLine("*************************");
Console.WriteLine("맵 변경");
map[0, 0] = 100;
map[0, 1] = 100;
map[0, 2] = 101;
map[0, 3] = 101;
map[0, 4] = 101;
map[1, 1] = 100;
map[1, 2] = 100;
map[1, 3] = 308;
map[1, 4] = 304;
map[2, 2] = 310;
map[2, 3] = 309;
map[2, 4] = 308;
Console.WriteLine("맵 출력.");
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write("{0} ", map[i, j]);
}
Console.WriteLine();
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
//배열 선언 및 초기화
int[,] map = new int[,]
{
{0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0 },
{0, 0, 1, 1, 0, 0 },
{0, 0, 1, 1, 0, 0 },
{0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0 }
};
Console.WriteLine("맵 출력.");
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write ("{0} ", map[i, j]);
}
Console.WriteLine();
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
TileMapManager tmm = new TileMapManager();
tmm.Init();
tmm.PrintMap();
Hero hero = new Hero(new Coord(0, 0));
hero.Move(new Coord(1, 1));
Console.WriteLine();
hero.Move(new Coord(2, 2));
Console.WriteLine();
hero.Move(new Coord(4, 1));
Console.WriteLine();
hero.Move(new Coord(1, 3));
Console.WriteLine();
//Coord coord = hero.Coord;
//Console.WriteLine("hero coord: ({0},{1})", coord.x, coord.y);
//MapData data = tmm.GetMapData(coord);
//Console.WriteLine("map data: {0}", data.data);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
public class Coord
{
public int x;
public int y;
//생성자
public Coord(int x, int y)
{
this.x = x;
this.y = y;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
public class Hero : TileMapManager
{
public Coord Coord
{
get; private set;
}
//생성자
public Hero(Coord coord)
{
this.Coord = coord;
}
public void Move(Coord coord)
{
//if (this.arr.GetLength(0) > coord.x && this.arr.GetLength(1) > coord.y)
//{
if (this.Coord.x - coord.x != 0)
{
int numX = Math.Abs(this.Coord.x - coord.x);
for (int i = 0; i < numX; i++)
{
if (this.Coord.x - coord.x > 0)
{
Console.WriteLine("x 방향 왼쪽으로 이동합니다.");
}
else
{
Console.WriteLine("x 방향 오른쪽으로 이동합니다.");
}
}
}
if (this.Coord.y - coord.y != 0)
{
int numY = Math.Abs(this.Coord.y - coord.y);
for (int i = 0; i < numY; i++)
{
if (this.Coord.y - coord.y > 0)
{
Console.WriteLine("y 방향 위쪽으로 이동합니다.");
}
else
{
Console.WriteLine("y 방향 아래쪽으로 이동합니다.");
}
}
}
Console.WriteLine("hero move: ({0},{1}) -> ({2},{3})", this.Coord.x, this.Coord.y, coord.x, coord.y);
this.Coord = coord;
//}
//else
//{
//Console.WriteLine("인덱스 범위 초과 : 이동할 수 없습니다.");
//Console.WriteLine("hero move: ({0},{1})", this.Coord.x, this.Coord.y);
//}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
public class MapData
{
public int data;
//생성자
public MapData(int data)
{
this.data = data;
}
}
}
using System;
namespace Study01
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Main");
new App();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
public class TileMapManager
{
private int row;
private int col;
public MapData[,] map;
public int[,] arr = {
{ 101, 100, 100 },
{ 101, 101, 101 },
{ 100, 100, 100 },
};
//생성자
public TileMapManager()
{
}
public void Init()
{
this.row = this.arr.GetLength(0);
this.col = this.arr.GetLength(1);
this.map = new MapData[this.row, this.col];
for (int i = 0; i < this.row; i++)
{
for (int j = 0; j < this.col; j++)
{
var data = new MapData(this.arr[i, j]);
this.map[i, j] = data;
}
}
}
public MapData[,] GetMap()
{
return this.map;
}
public void PrintMap()
{
for (int i = 0; i < this.row; i++)
{
for (int j = 0; j < this.col; j++)
{
MapData data = this.map[i, j];
Console.Write("{0, 2} ", data.data);
}
Console.WriteLine();
}
}
public MapData GetMapData(Coord coord)
{
return this.map[coord.y, coord.x];
}
public int GetRow()
{
return this.row;
}
public int GetCol()
{
return this.col;
}
}
}
'C# > 수업내용' 카테고리의 다른 글
C# 3주차 2일 수업 내용 : LINQ 및 람다, EVENT(21.03.23) (0) | 2021.03.23 |
---|---|
C# 3주차 1일 수업 내용 : Delegate, Callback (21.03.22) (0) | 2021.03.22 |
C# 2주차 3일 수업 내용 : 인터페이스, 상속, Dictionary(21.03.17) (0) | 2021.03.17 |
C# 2주차 2일 수업 내용 (21.03.16) (0) | 2021.03.16 |
C# 2주차 1일 수업 내용 (21.03.15) (0) | 2021.03.15 |