Wednesday, November 2, 2022

[FIXED] How do i find an index of x,y from the txt file in c#? Snake game

Issue

Here is my txt file and i need to get x,y of O

####################
#                  #
#                  #
#                  #
#                  #
#                  #
#                  #
#   O              #
#                  #
####################

Solution

try this code:

int yIndex = 0;
using (var fileStream = File.OpenRead("t.txt")) //t.txt your input file
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true))
{                
     String line;
//read line by line and yIndex is added each time
     while ((line = streamReader.ReadLine()) != null)
     {
        for(int i=0; i< line.Length;i++)
             if(line[i]== 'O')
             {
                 Console.WriteLine("X is: {0}    Y is:{1}",i.ToString(),yIndex);
                 return; 
             }
                yIndex++;
    }
}

result:

X is: 4 Y is:7



Answered By - hossein sabziani
Answer Checked By - David Goodson (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.