C#|どう書く?org 文字列の八方向検索

 どう書く?orgのお題、文字列の八方向検索投稿用に書きました。

using System.Collections.Generic;
using System.Text;
using Microsoft.VisualBasic;
namespace どう書く_org文字列の八方向検索 {
  class Program {
    static string search;
    static List<string> list;
    static void Main(string[] args) {
      string sample =
@"リオウウリウ
ウオリウオリ
オリリオリウ
リリオオウオ";
      search = "ウオリ";
      char sp = new char { '\n' };
      int width = sample.Split(sp)[0].Length;
      int height = sample.Split(sp).Length;
      list = new List<string>(sample.Split(sp));
      for(int y = 0; y < list.Count; y++) {
        for(int x = 0; x < list[y].Length; x++) {
          if(list[y][x] == search[0]) {
            if(E(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "右");
            }
            if(W(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "左");
            }
            if(S(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "下");
            }
            if(N(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "上");
            }
            if(NE(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "右上");
            }
            if(SE(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "右下");
            }
            if(NW(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "左上");
            }
            if(SW(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "左下");
            }
          }
        }
      }
      Console.ReadLine();
    }
    static string E(int x, int y) {
      try {
        return list[y].Substring(x, search.Length);
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string W(int x, int y) {
      try {
        return Strings.StrReverse(E(x - search.Length + 1, y));
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string S(int x, int y) {
      try {
        StringBuilder strb = new StringBuilder();
        for(int i = 0; i < search.Length; i++) {
          strb.Append(list[y + i][x]);
        }
        return strb.ToString();
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string N(int x, int y) {
      try {
        return Strings.StrReverse(S(x, y - search.Length + 1));
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string SE(int x, int y) {
      try {
        StringBuilder strb = new StringBuilder();
        for(int i = 0; i < search.Length; i++) {
          strb.Append(list[y + i][x + i]);
        }
        return strb.ToString();
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string NW(int x, int y) {
      try {
        return Strings.StrReverse(SE(x - search.Length + 1, y - search.Length + 1));
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string NE(int x, int y) {
      try {
        StringBuilder strb = new StringBuilder();
        for(int i = 0; i < search.Length; i++) {
          strb.Append(list[y - i][x + i]);
        }
        return strb.ToString();
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string SW(int x, int y) {
      try {
        return Strings.StrReverse(NE(x - search.Length + 1, y + search.Length - 1));
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
  }
}

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください