cscの作法 その27 オセロ

# 概要

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

# 練習問題

オセロゲームを作れ。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/5754707a-7117-99fb-c8d6-92a78820dc04.png)


# サンプルコード


```
using System;
using System.Windows.Forms;
using System.Drawing;

class form1: Form {
	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};
	form1() {
		Text = "osero";
		ClientSize = new Size(240, 240);
		this.MouseUp += new MouseEventHandler(mouseUp);
	}
	void mouseUp(object sender, MouseEventArgs e) {
		Text = "mouse: (" + e.X + ", " + e.Y + ")";
		int posx = e.X;
		int posy = e.Y;
		int n = (posx / 30) + (posy / 30) * 8;
		test(n);
		Invalidate();
	}
	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 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;
			}
			if (end == -1)
			{
				MessageBox.Show("over!");
			}
		}
	}
	protected override void OnPaint(PaintEventArgs e) {
		Graphics g = e.Graphics;
		g.FillRectangle(Brushes.Green, 0, 0, 240, 240);
		for (int i = 0; i < 9; i++)
		{
			g.DrawLine(Pens.Pink, 0, 30 * i, 240, 30 * i);
			g.DrawLine(Pens.Pink, 30 * i, 0, 30 * i, 240);
		}
		int x;
		int y;
		for (int i = 0; i < 64; i++)
		{
			x = i % 8 * 30 + 5;
			y = i / 8 * 30 + 5;
			if (ban[i] == 0)
			{
				g.FillEllipse(Brushes.White, x, y, 20, 20);
			}
			if (ban[i] == 2)
			{
				g.FillEllipse(Brushes.Black, x, y, 20, 20);
			}
		}
		base.OnPaint(e);
	}
	[STAThread]
	public static void Main() {
		Application.Run(new form1());
	}
}





```

以上。