2016-10-11 1 views
0
{ 
static int[] location = { 0, 0 }; 
static int player = 0; 

static void runGame() 
{ 
    int start = Convert.ToInt32(Console.ReadLine()); 
    if (start == 1) 
    { 
     location1(); 
    } 


    else if (start == 2) 
    { 
     location2(); 
    } 


    else if (start == 3) 
    { 
     location3(); 
    } 


    else if (start == 4) 
    { 
     location4(); 
    } 
} 

static void swapPlayer() 
{ 
    if (player == 1) 
    { 
     player = 0; 
    } 
    else 
    { 
     player = 1; 
    } 
} 

static void location1() 
{ 

    Console.WriteLine(" Player " + (player + 1) + " , you are in the kitchen   you can go to either \n1: Living room \n2: Bathroom "); 
    int input = int.Parse(Console.ReadLine()); 
    if (input == 1) { 
     location[player] = 2; 
     start = 2; 
     swapPlayer(); 
     location2(); 
    } 
    else if (input == 2) { 
     location[player] = 3; 
     start = 3; 
     swapPlayer(); 
     location3(); 
    } 




} 

static void location2() 
{ 

    Console.WriteLine(" Player " + (player + 1) + " you are in the living room you can go to either \n1: Kitchen\n2: Bedroom "); 
    int input = int.Parse(Console.ReadLine()); 
    if (input == 1) { 
    location[player] = 1; 
    start = 1; 
    swapPlayer(); 
    location1(); 
    } 
    else if (input == 2) { 
    location[player] = 4; 
    start = 4; 
    swapPlayer(); 
    location4(); 
    } 

} 

static void location3() 
{ 

    Console.WriteLine(" Player " + (player + 1) + " you are in the bathroom you can go to either \n1: Kitchen \n2: Bedroom "); 
    int input = int.Parse(Console.ReadLine()); 
    if (input == 1) 
    { 
    location[player] = 1; 
    start = 1; 
    swapPlayer(); 
    location1(); 

    } 
    else if (input == 2) 
    { 
    location[player] = 4; 
    start = 4; 
    swapPlayer(); 
    location4(); 
    } 

} 

static void location4() { 

    Console.WriteLine(" Player " + (player + 1) + ", you are in the kitchen you can go to either \n1: Living room \n2: Bathroom "); 
    int input = int.Parse(Console.ReadLine()); 

    if (input == 1) 
    { 
    location[player] = 1; 
    start = 1; 
    swapPlayer(); 
    location2(); 

    } 
    else if (input == 2) 
    { 
    location[player] = 4; 
    start = 4; 
    swapPlayer(); 
    location3(); 
    } 


} 

static void Main(string[] args) 
{ 

    Console.WriteLine("welcome , find the ghost and navigate through the house"); 
    Console.Write("You are in the main hall way you can go to any of these rooms"); 
    Console.Write(" kitchen, living room, bath room , bedroom"); 
    Console.WriteLine("choose a room number 1 , 4 from the list "); 
    int start = Convert.ToInt32(Console.ReadLine()); 

    bool play = true; 

    while (play== true) 
    { 
     runGame(); 
    } 




} 
    } 

다음은 간단한 2 플레이어 텍스트 모험의 코드입니다. runGame 절차에서 변수 시작 위치를 궁금 해서요. 어떻게 액세스 할 수 있습니까? 프로 시저 밖에서, 또는 불가능한 경우에는이 문제를 해결할 수있는 방법에 대한 다른 해결책이 있습니까? 당신은 할 수 없습니다절차 외의 절차에서 변수에 액세스하는 방법

답변

1

, 그 지역 변수의 요점 : 여러 방법에서 vatiable에 액세스하려면 세계는 당신에게,

외부 자신의 가치에 대해 신경 안 (또는 자신의 존재) ' 이것을 (이 경우 정적 인) 반원에게 올려야합니다 :

static int[] location = { 0, 0 }; 
static int player = 0; 
static int start;