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(); 
    } 




} 
    } 

Voici le code derrière une simple aventure de texte à 2 joueurs que je fais, je me demandais où j'ai utilisé la variable start dans la procédure runGame comment puis-je accéder en dehors de la procédure, ou n'est-ce pas possible, auquel cas vous avez une autre solution sur la façon dont je peux contourner cela.Comment accéder aux variables à partir d'une procédure en dehors de la procédure

Répondre

1

Vous ne pouvez pas, et c'est le point de variables locales: le monde extérieur ne devrait pas se soucier de leur valeur (ou même leur existence)

Si vous souhaitez accéder au vatiable à partir de plusieurs méthodes, vous Il faut l'élever à un membre de classe (dans ce cas statique):

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