前田稔(Maeda Minoru)の超初心者のプログラム入門
![]()
![]()
//★ 三目並べの学習機能 前田 稔 ★
using System;
using System.Collections; //ArrayList を使うとき
class Cell
{ public ArrayList AL = null;
public int n; // N 手目
public int y; // Y 座標
public int x; // X 座標
}
class Prog
{
static int[,] dat = new int[9, 2]
{{1,1}, {0,0}, {0,2}, {2,0}, {1,0}, {1,2}, {0,1}, {2,1}, {2,2}};
public static void Main()
{
Cell top = new Cell();
Cell pt;
int i,j;
pt = top;
for(i=0; i<dat.GetLength(0); i++)
{
j = AddCell(pt,i,dat[i,0],dat[i,1]);
pt = (Cell)pt.AL[j];
}
Print(top);
Console.ReadLine();
}
// pt の ArrayList に i, y, x のセルを追加する(x,y が存在するときはパス)
public static int AddCell(Cell pt, int i, int y, int x)
{ Cell wk,wk2;
int j;
wk = new Cell();
wk.n = i;
wk.y = y;
wk.x = x;
if (pt.AL==null) pt.AL= new ArrayList();
for (j=0; j<pt.AL.Count; j++)
{ wk2 = (Cell)pt.AL[j];
if (wk2.y==y && wk2.x==x) break;
}
if (j>=pt.AL.Count) pt.AL.Add(wk);
return j;
}
// top セルから再帰で印字
public static void Print(Cell top)
{ Cell wk;
int i;
for (i=0; i<top.AL.Count; i++) // ArrayList(手番で列挙)の印字
{ wk = (Cell)top.AL[i];
Console.WriteLine("N:" + wk.n + " Y:" + wk.y + " X:" + wk.x);
if (wk.AL != null) Print(wk); // AL[i] から Cell にリンク
}
}
}
|
static int[,] dat = new int[9, 2]
{{1,1}, {0,0}, {0,2}, {2,0}, {1,0}, {1,2}, {0,1}, {2,1}, {2,2}};
|
public static void Main()
{ Cell top = new Cell();
Cell pt;
int i,j;
pt = top;
for(i=0; i<dat.GetLength(0); i++)
{ j = AddCell(pt,i,dat[i,0],dat[i,1]);
pt = (Cell)pt.AL[j];
}
Print(top);
Console.ReadLine();
}
|
public static int AddCell(Cell pt, int i, int y, int x)
{ Cell wk,wk2;
int j;
wk = new Cell();
wk.n = i;
wk.y = y;
wk.x = x;
if (pt.AL==null) pt.AL= new ArrayList();
for (j=0; j<pt.AL.Count; j++)
{ wk2 = (Cell)pt.AL[j];
if (wk2.y==y && wk2.x==x) break;
}
if (j>=pt.AL.Count) pt.AL.Add(wk);
return j;
}
|
![]()
static int[,,] dat = new int[3, 9, 2]
{{{1,1}, {0,0}, {0,2}, {2,0}, {1,0}, {1,2}, {0,1}, {2,1}, {2,2}},
{{1,1}, {0,0}, {0,2}, {2,0}, {1,0}, {1,2}, {2,1}, {0,1}, {2,2}},
{{1,1}, {2,2}, {0,2}, {2,0}, {2,1}, {0,1}, {1,2}, {1,0}, {0,0}}};
|
public static void Main()
{ Cell top = new Cell();
Cell pt;
int i,j,k;
for(i=0; i<dat.GetLength(0); i++)
{ pt = top;
for(j=0; j<dat.GetLength(1); j++)
{ k = AddCell(pt,j,dat[i,j,0],dat[i,j,1]);
pt = (Cell)pt.AL[k];
}
}
Print(top);
Console.ReadLine();
}
|
![]()
class Cell
{ public ArrayList AL = null;
public int n; // N 手目
public int y; // Y 座標
public int x; // X 座標
public int v=0; // 点数
}
|
static int[,,] dat = new int[4, 9, 2]
{{{1,1}, {0,0}, {0,2}, {2,0}, {1,0}, {1,2}, {0,1}, {2,1}, {2,2}},
{{1,1}, {0,1}, {2,2}, {0,0}, {0,2}, {2,0}, {1,2}, {1,0}, {2,1}},
{{1,1}, {2,1}, {2,2}, {0,0}, {0,2}, {2,0}, {1,2}, {1,0}, {0,1}},
{{0,0}, {2,2}, {2,0}, {1,0}, {0,1}, {0,2}, {1,1}, {1,2}, {2,1}}};
static int[] val = { 0, 1, 1, -1 };
|
for(i=0; i<dat.GetLength(0); i++)
{ pt = top;
for(j=0; j<dat.GetLength(1); j++)
{ k = AddCell(pt,j,dat[i,j,0],dat[i,j,1]);
pt = (Cell)pt.AL[k];
pt.v += val[i];
}
}
|
public static void Print(Cell top)
{ Cell wk;
int i;
for (i=0; i<top.AL.Count; i++) // ArrayList(手番で列挙)の印字
{ wk = (Cell)top.AL[i];
Console.WriteLine("N:" + wk.n + " Y:" + wk.y + " X:" + wk.x + " V:" + wk.v);
if (wk.AL != null) Print(wk); // AL[i] から Cell にリンク
}
}
|
![]()