cscの作法 その313

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

# 練習問題
websocket serverを実装せよ。
オセロを返却、せよ。

# 方針
白が先手
4 3 と、指す。

# サンプルコード

```

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.ComponentModel;

namespace WebSocketServer
{
	internal class Program : IDisposable {
		private static WebSocket webSocket;
		private static 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};
		public async void Dispose() {
			if (webSocket != null)
			{
				Console.WriteLine("WebSocketサーバーからの切断");
				await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "WebSocketサーバーからの切断", CancellationToken.None);
				webSocket.Dispose();
			}
		}
		static async Task Receive() {
			while (true)
			{
				if (webSocket == null || (webSocket != null && webSocket.State != WebSocketState.Open))
				{
					Console.WriteLine("WebSocket接続がありません");
					Console.Write(">");
					return;
				}
				var buffer = new byte[1024];
				var byteArray = new ArraySegment<byte>(buffer);
				var ret = await webSocket.ReceiveAsync(byteArray, CancellationToken.None);
				if (ret.MessageType == WebSocketMessageType.Close)
				{
					Console.WriteLine("クライアントから切断されました");
					Console.Write(">");
					await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "クライアントから切断されました", CancellationToken.None);
					return;
				}
				if (ret.MessageType != WebSocketMessageType.Text)
				{
					Console.WriteLine("テキストデータではありません");
					Console.Write(">");
					await webSocket.CloseAsync(WebSocketCloseStatus.InvalidMessageType, "テキストデータではありません", CancellationToken.None);
					return;
				}
				int size = ret.Count;
				while (!ret.EndOfMessage)
				{
					if (buffer.Length <= size)
					{
						Console.WriteLine("バッファーオーバー");
						await webSocket.CloseAsync(WebSocketCloseStatus.InvalidPayloadData, "バッファーオーバー", CancellationToken.None);
						return;
					}
					byteArray = new ArraySegment<byte>(buffer, size, buffer.Length - size);
					ret = await webSocket.ReceiveAsync(byteArray, CancellationToken.None);
					size += ret.Count;
				}
				var message = Encoding.UTF8.GetString(buffer, 0, size);
				Console.WriteLine(message);
				Console.Write(">");
				string[] pos = message.Split(new string[] {
					" "
				}, StringSplitOptions.None);
				int x = Convert.ToInt32(pos[0]);
				int y = Convert.ToInt32(pos[1]);
				int n = x + y * 8 - 9;
				Console.WriteLine(n);
				test(n);
				int i;
				string res = "";
				for (i = 0; i < 64; i++)
				{
					if (ban[i] == 0)
						res += "*";
					if (ban[i] == 1)
						res += "-";
					if (ban[i] == 2)
						res += "o";
					if (i % 8 == 7)
						res += "\r\n";
				}
				buffer = Encoding.UTF8.GetBytes(res);
				byteArray = new ArraySegment<byte>(buffer);
				await webSocket.SendAsync(byteArray, WebSocketMessageType.Text, true, CancellationToken.None);
				Console.WriteLine(res);
				Console.Write(">");
			}
		}
		static async Task CreateWebSocketServerAndReceive() {
			var httpListener = new HttpListener();
			httpListener.Prefixes.Add("http://localhost:50002/");
			httpListener.Start();
			Console.WriteLine("WebSocket受付開始");
			while (true)
			{
				var httpListenerContext = await httpListener.GetContextAsync();
				if (!httpListenerContext.Request.IsWebSocketRequest)
				{
					Console.WriteLine("WebSocketでの接続ではありません");
					httpListenerContext.Response.StatusCode = 400;
					httpListenerContext.Response.Close();
					return;
				}
				var taskHttpListenerContext = await httpListenerContext.AcceptWebSocketAsync(null);
				if (webSocket != null)
				{
					webSocket.Dispose();
				}
				webSocket = taskHttpListenerContext.WebSocket;
				Console.WriteLine("WebSocket接続");
				Console.Write(">");
				var nowait = Receive();
			}
		}
		private static 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;
		}
		private static 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;
		}
		private static 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;
		}
		private static void test(int n) {
			int put = n;
			int res = oku(put, 2);
			if (res == -1)
			{
				put = sasu();
				res = oku(put, 0);
				int end = -1;
				for (int i = 0; i < 64; i++)
				{
					if (ban[i] == 1)
						end = 0;
				}
			}
		}
		static void Main(string[] args) {
			Console.WriteLine("開始:CTRL+Cで停止してください");
			var nowait1 = CreateWebSocketServerAndReceive();
			using (var manualResetEvent = new ManualResetEvent(false))
			{
				manualResetEvent.WaitOne();
			}
		}
	}
}








```

以上。