2019-10-21 21일차 2차원 배열(2) 좌표를 인덱스로 변환하고 캐릭터 움직이기
Console Programming/C# Console 2019. 10. 24. 09:43
Program.cs
namespace Console1021
{
class Program
{
static void Main(string[] args)
{
new App();
}
}
}
App.cs
using System;
namespace Console1021
{
public class App
{
//arr2D는 지형의 정보를 담은 데이터이다(전역변수로 설정함)
int[,] arr2D =
{
{ 1,1,1,1,2,2,2},
{ 1,1,1,1,1,-1,-1},
{ 1,1,1,1,-1,-1,-1}
};
public App()
{
//지형정보를 본다.
ShowMap();
//할아버지 캐릭터를 만든다. 기본좌표는(0,0)
Character oldMan = new Character();
oldMan.position.x = 0;
oldMan.position.y = 0;
//할아버지의 좌표를 출력한다.
ShowCoordinate(oldMan);
//움직일 좌표의 개체를 만든다.
Position movingPositon = new Position(2, 3);
//할아버지를 키보드로 움직인다 (오른쪽화살표를 누르면 오른쪽으로 한칸 이동한다)
oldMan.MovebyKeyboard(arr2D);
}
//지형정보를 살펴보는 메서드 Show
public void ShowMap()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 7; j++)
{
Console.Write(arr2D[i, j]);
}
Console.WriteLine();
}
}
public void ShowCoordinate(Character character)
{
Console.WriteLine($"({character.position.x}, {character.position.y})");
}
}
}
Character.cs
using System;
namespace Console1021
{
public class Character
{
public int[,] index = new int[1, 2] { {0, 0} };
public Position position=new Position();
public Character()
{
}
public void MovebyKeyboard(int[,] arr2D)
{
ConsoleKeyInfo keyInfo = Console.ReadKey();
if (keyInfo.Key.ToString() == "RightArrow")
{
Position movingPosition = new Position(this.position.x + 1, this.position.y);
this.Move(movingPosition, arr2D);
}
}
public void Move(Position position, int[,] arr2D)
{
Position movedPosition = new Position();
movedPosition.x = this.position.x + position.x;
movedPosition.y = this.position.y + position.y;
var IsMovable = CheckCoordinate(movedPosition, arr2D);
if (IsMovable)
{
Console.WriteLine($"{this.position.x},{this.position.y} 에서 { movedPosition.x} {movedPosition.y}로 이동했습니다.");
this.position = movedPosition;
}
else
{
Console.WriteLine($"{ movedPosition.x}, { movedPosition.y} 로 이동할 수 없습니다.");
}
}
public Position ConvertToCoordinate(int row, int column)
{
Position coordinate = new Position();
coordinate.x = column;
coordinate.y = row;
return coordinate;
}
public int[,] ConvertToIndex(Position position)
{
int[,] index = new int[1, 2];
index[0, 0] = position.y;
index[0, 1] = position.x;
return index;
}
public bool CheckCoordinate(Position position, int[,] arr2D)
{
int[,] arr = ConvertToIndex(position);
if (arr[0, 0] < 0 && arr[0, 1] < 0)
{
return false;
}
else
{
if (arr2D[position.x, position.y]==1 || arr2D[position.x, position.y] ==2)
{
return true;
}
else
{
return false;
}
}
}
}
}
Position.cs
namespace Console1021
{
public class Position
{
public int x;
public int y;
public Position()
{
}
public Position(int x, int y)
{
this.x = x;
this.y = y;
}
}
}
'Console Programming > C# Console' 카테고리의 다른 글
2019-10-22 22일차 2048게임 만들기 (0) | 2019.10.24 |
---|---|
백준 6679 싱기한 네자리 숫자 (0) | 2019.10.24 |
2019-10-21 21일차 2차원 배열 (좌표<------> 배열의인덱스) (0) | 2019.10.23 |
백준 2167 (0) | 2019.10.22 |
2019-10-18 20일차 롤 챔피언 Attack메시지 출력 (json, Dictionary, 클래스상속사용) (0) | 2019.10.18 |