cscの作法 その418

# 概要
cscの作法、調べてみた。
練習問題、やってみた。

# 練習問題
automationでexcelにオセロを打て。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/0e888d13-d58c-fd4b-4b45-cebc346ba053.png)


# サンプルコード

```


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Automation;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;


namespace App
{
	class form1: Form {
		TextBox bo1;
		int[] ban = {
			1, 1, 1, 1, 1, 1, 1, 1,
			1, 1, 1, 1, 1, 1, 1, 1,
			1, 1, 1, 1, 1, 1, 1, 1,
			1, 1, 1, 0, 2, 1, 1, 1,
			1, 1, 1, 2, 0, 1, 1, 1,
			1, 1, 1, 1, 1, 1, 1, 1,
			1, 1, 1, 1, 1, 1, 1, 1,
			1, 1, 1, 1, 1, 1, 1, 1};
		AutomationElement aeForm;
		form1() {
			Text = "オセロ";
			ClientSize = new Size(250, 200);
			StartPosition = FormStartPosition.Manual;
			Location = new Point(950, 100);
			Label label = new Label();
			label.Location = new Point(50, 30);
			label.Text = "あなたは、白";
			Controls.AddRange(new Control[] {
				label
			});
			bo1 = new TextBox();
			bo1.Location = new Point(50, 50);
			bo1.Text = "d3";
			Controls.AddRange(new Control[] {
				bo1
			});
			Button btn1 = new Button();
			btn1.Location = new Point(50, 80);
			btn1.Text = "打つ";
			btn1.Click += btn1_Click;
			Controls.AddRange(new Control[] {
				btn1
			});
			Process excel = Process.Start(@"C:\Program Files (x86)\Microsoft Office\Office12\excel.exe");
			Thread.Sleep(2000);
			aeForm = AutomationElement.FromHandle(excel.MainWindowHandle);
			draw();
		}
		int check(int put, int d) {
			int x = put % 8;
			int y = put / 8;
			int res = 0;
			if (x == 0 && (d == -9 || d == -1 || d ==  7))
				res = 1;
			if (x == 7 && (d == -7 || d ==  1 || d ==  9))
				res = 1;
			if (y == 0 && (d == -9 || d == -8 || d == -7))
				res = 1;
			if (y == 7 && (d ==  7 || d ==  8 || d ==  9))
				res = 1;
			return res;
		}
		int sasu() {
			int[] suji = {0, 7, 56, 63, 18, 21, 42, 45, 2, 16, 5, 23, 40, 58, 47, 61, 3, 4, 11, 12, 19, 20, 24, 25, 26, 32, 33, 34, 29, 30, 31, 37, 38, 39, 43, 44, 51, 52, 59, 60, 1, 8, 9, 10, 17, 6, 13, 14, 15, 22, 41, 48, 49, 50, 57, 46, 53, 54, 55, 62};
			int res = -1;
			int all = 0;
			int iro = 2;
			int turn = 0;
			int[] dir = {-9, 9, -7, 7, -1, 1, -8, 8};
			int i;
			int j;
			int put;
			int count;
			int put1 = 0;
			int f1;
			for (j = 0; j < 60; j++)
			{
				put = suji[j];
				if (ban[put] == 1)
				{
					for (i = 0; i < 8; i++)
					{
						count = 0;
						if (check(put, dir[i]) == 0)
						{
							put1 = put + dir[i];
							f1 = 0;
							do
							{
								if (ban[put1] == iro)
								{
									count++;
									if (check(put1, dir[i]) == 0)
									{
										put1 += dir[i];
									}
									else
									{
										f1 = 1;
									}
								}
								else
								{
									f1 = 1;
								}
							} while (f1 == 0);
						}
						if ((count > 0) && (ban[put1] == turn))
						{
							all += count;
						}
					}
				}
				if (all > 0)
				{
					res = put;
					break;
				}
			}
			return res;
		}
		int oku(int put, int iro) {
			int res = 0;
			int turn = 0;
			if (iro == 0)
				turn = 2;
			int[] dir = {-9, -8, -7, -1, 1, 7, 8, 9};
			int tugi;
			int i;
			int count;
			if (ban[put] == 1)
			{
				for (i = 0; i < 8; i++)
				{
					count = 0;
					tugi = put;
					do
					{
						if (check(tugi, dir[i]) == 1)
							break;
						count++;
						tugi += dir[i];
					}
					while (ban[tugi] == turn);
					if ((count > 1) && (ban[tugi] == iro))
					{
						res = -1;
						tugi = put;
						do
						{
							ban[tugi] = iro;
							tugi += dir[i];
						}
						while (ban[tugi] == turn);
					}
				}
			}
			return res;
		}
		void draw() {
			aeForm.SetFocus();
			int i;
			int j;
			int s;
			for (i = 0; i < 8; i++)
			{
				for (j = 0; j < 8; j++)
				{
					s = i * 8 + j;
					if (ban[s] == 0)
					{
						SendKeys.SendWait("●");
					}
					if (ban[s] == 2)
					{
						SendKeys.SendWait("〇");
					}
					SendKeys.SendWait("{RIGHT}");
				}
				for (j = 0; j < 8; j++)
				{
					SendKeys.SendWait("{LEFT}");
				}
				SendKeys.SendWait("{DOWN}");
			}
			for (j = 0; j < 8; j++)
			{
				SendKeys.SendWait("{UP}");
			}
			bo1.Focus();
		}
		void test(int n) {
			int put = n;
			int res = oku(put, 2);
			if (res == -1)
			{
				draw();
				put = sasu();
				res = oku(put, 0);
				int end = -1;
				for (int i = 0; i < 64; i++)
				{
					if (ban[i] == 1)
						end = 0;
				}
				if (end == -1)
				{
					MessageBox.Show("over!");
				}
				draw();
				bo1.Text = "";
			}
			else
			{
				MessageBox.Show("buuuu");
			}
		}
		void btn1_Click(object sender, System.EventArgs e) {
			char[] m = bo1.Text.ToCharArray();
			int i = 0;
			int j = 0;
			switch (m[0])
			{
			case 'a':
				i = 0;
			break;
			case 'b':
				i = 1;
			break;
			case 'c':
				i = 2;
			break;
			case 'd':
				i = 3;
			break;
			case 'e':
				i = 4;
			break;
			case 'f':
				i = 5;
			break;
			case 'g':
				i = 6;
			break;
			case 'h':
				i = 7;
			break;
			default:
			break;
			}
			switch (m[1])
			{
			case '1':
				j = 0;
			break;
			case '2':
				j = 1;
			break;
			case '3':
				j = 2;
			break;
			case '4':
				j = 3;
			break;
			case '5':
				j = 4;
			break;
			case '6':
				j = 5;
			break;
			case '7':
				j = 6;
			break;
			case '8':
				j = 7;
			break;
			default:
			break;
			}
			int n = j * 8 + i;
			test(n);
		}
		[STAThread]
		public static void Main() {
			Application.Run(new form1());
		}
	}
}





```

以上。