PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, November 2, 2022

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

 November 02, 2022     c#, file, txt     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing