2013-03-28 2 views
0

필자는 수업 시간에 C#을 배우고 자유 시간에 자신을 가르치려고했습니다.배열의 값에 따라 위치를 표시하는 방법

C#으로 첫 번째 게임을 만들려고합니다. 지금까지 내 코드는 다음과 같습니다

static void Main(string[] args) 
    { 

     string aValue; 
     int Limit = 20; 

     int[,] MOVEMENT = new int[20,20];   

     // MOVEMENT 

     for (int i = 0; i < 20;) 
      for (int j = 0; j < 20;) 
      { 
       Console.WriteLine("Which direction do you wish to move in?"); 
       aValue = Console.ReadLine(); 

       // MOVE NORTH 
       if (aValue == "North" || aValue == "north") 
       { 
        i = i + 1; 

        if (i >= 20) 
        { 
         Console.WriteLine("You cannot move any further in this direction!"); 
         i = 20; 
        } 
       } 

       // MOVE SOUTH 
       if (aValue == "South" || aValue == "south") 
       { 
        i = i - 1; 

        if (i <= 0) 
        { 
         Console.WriteLine("You cannot move any further in this direction!"); 
         i = 0; 
        } 
       } 

       // MOVE EAST 
       if (aValue == "East" || aValue == "east") 
       { 
        j = j + 1; 

        if (j >= 20) 
        { 
         Console.WriteLine("You cannot move any further in this direction!"); 
         j = 20; 
        } 
       } 

       // MOVE WEST 
       if (aValue == "West" || aValue == "west") 
       { 
        j = j - 1; 

        if (j <= 0) 
        { 
         Console.WriteLine("You cannot move any further in this direction!"); 
         j = 0; 
        } 
       } 


       // Display where the character is after each movement and list possible events 
       // in the area. 

       Console.WriteLine("You are now at {0},{1}", i, j); 


      } 
     Console.ReadLine(); 
} 

지금 게임에서 다른 위치로 배열 corresponde의 각 요소를 만들고 싶어. 예 : 0,0이 집일 수 있습니다. 0,1은 Village 일 수 있습니다. 0,2가 Desert 일 수 있습니다. 1,5가 호수 일 수 있습니다.

각 위치마다 고유 한 설명 및 일부 이벤트 (If 문 등으로 작성)를 원합니다. Im은 코드가 위치를 표시하는 방법을 확신하지 못합니다 (i는 요소의 값이됩니까?). i와 j도 동일합니다. switch 문을 사용하면이 작업을 수행하는 것이 가장 좋겠지 만 시도해 보았습니다. 작동시키지 못했습니다.

도움을 주시면 감사하겠습니다.

답변

0

나는 당신이 (2 문자열 또는 구조체 - 각각 설명 및 위치) 구조체와 문자열의 dictionary 사용하는 것이 좋습니다 당신은 몇 가지 정보를 다음과 같이 각각의 위치를 ​​할당합니다 :

using System; 
public struct Point 
{ 
    public int x, y; 

    public Point(int p1, int p2) 
    { 
     x = p1; 
     y = p2; 
    } 
} 
public struct Information 
{ 
    public string description, location; 

    public Information(string desc, string loc) 
    { 
     description = desc; 
     location = loc; 
    } 
} 
var info = new Dictionary<Point, Information>(); 

info[Point(i,j)] = Information("This is our lovely lake","Lake"); 
1

물체를 사용하지 왜 ?

할머니 Erix 설명으로

public class Location{ 
    public int Latitude {get;set;} 
    public int Longitude {get;set;} 
    public string Name{get;set;} 
    public string Description {get;set;} 
} 

yourIEnumerableLocation.FirstOrDefault(x=> x.Latitude== yourXposition && x.Longitude == yourYposition) 

LINQ

에 이름과 값을 검색하거나 사전도를 사용
관련 문제