#include <stdio.h>
#include <stdlib.h>
template
<
class
type>
void
inline
Swap(type &a,type &b)
{
type tmp=a;
a=b;
b=tmp;
}
class
Chess
{
public
:
Chess()
{
int
i,j;
int
c[5][10]=
{{0,1,1,0,1,1,0,1,1,0},
{1,0,0,1,0,1,1,0,1,-1},
{0,0,0,0,-1,-1,-1,-1,-1,-1},
{1,1,1,0,1,1,1,-1,-1,-1},
{1,-1,-1,-1,-1,-1,-1,-1,-1,-1} };
//1是黑棋 0是白棋
for
(i=0;i<5;i++)
for
(j=0;j<10;j++)
chess[i][j]=c[i][j];
rows=5;
}
Chess(Chess &other)
{
int
i,j;
for
(i=0;i<other.rows;i++)
{
for
(j=0;j<10;j++)
chess[i][j]=other.chess[i][j];
}
rows=other.rows;
}
int
isWin(
int
turn)
const
//1该黑棋走,0该白棋走
{
if
(rows==0)
return
1;
int
i,j;
for
(i=0;i<rows;i++)
{
for
(j=0;j<10;j++)
{
if
(chess[i][j]==!turn)
return
0;
}
}
return
1;
}
void
takeout(
int
k,
int
l)
{
int
i,j,flag=0;
for
(j=l;j<10;j++) chess[k][j]=-1;
for
(j=0;j<10;j++)
{
flag=chess[k][j]!=-1;
if
(flag==1)
break
;
}
if
(flag==0)
{
for
(i=k;i<rows-1;i++)
{
for
(j=0;j<10;j++)
{
Swap(chess[i][j],chess[i+1][j]);
}
}
rows--;
}
}
int
CheckInput(
int
i,
int
j)
{
if
(i<0||i>=rows||j<0||j>=10)
{
printf
(
"该棋子不存在!
"
);
return
0;
}
else
{
if
(chess[i][j]==-1) {
printf
(
"该棋子不存在!2
"
);
return
0;
}
if
(chess[i][j]==0) {
printf
(
"您不能选择白棋!
"
);
return
0;
}
}
return
1;
}
double
EigenValue()
const
{
double
value = 0;
int
i,j;
for
(i=0;i<rows;i++)
{
if
(chess[i][0] == 0 && chess[i][1] == 0 && chess[i][2] == -1)
{
value+= 0.5;
}
else
{
for
(j=0;j<10;j++)
{
switch
(chess[i][j])
{
case
1:value--;
break
;
case
0:value++;
break
;
}
}
}
}
return
value;
}
void
print()
const
{
int
i,j;
for
(i=0;i<rows;i++)
{
for
(j=0;j<10;j++)
{
switch
(chess[i][j])
{
case
1:
printf
(
"●"
);
break
;
case
0:
printf
(
"○"
);
break
;
}
}
printf
(
"
"
);
}
}
int
chess[5][10];
int
rows;
};
//AI下棋的程序
void
AIPredict(Chess c,
int
&AIi,
int
&AIj)
{
struct
v{
int
i,j;
double
value;
} value[50];
int
count=0,i,j;
for
(i=0;i<c.rows;i++)
{
for
(j=0;j<10;j++)
{
if
(c.chess[i][j]==0)
{
Chess predict=c;
predict.takeout(i,j);
value[count].i=i;
value[count].j=j;
value[count].value=predict.EigenValue();
count++;
}
}
}
v maxvalue=value[0];
for
(i=1;i<count;i++)
{
if
(value[i].value>maxvalue.value)
{
maxvalue=value[i];
}
}
AIi=maxvalue.i;
AIj=maxvalue.j;
}
int
main()
{
system
(
"color F0"
);
printf
(
"游戏规则:
下面有五行石子,白色空心○的石子都是我的,黑色实心●的石子都是你的。
我们轮流拿走一个自己的石子,并且规定如果一个石子被拿走了,
它后面的所有石子都要被扔掉。谁先没有拿的了,谁就输了。
"
);
Chess c;
c.print();
printf
(
"
"
);
int
i,j,turn;
printf
(
"你想先手还是后手?1为先手,0为后手:"
);
scanf
(
"%d"
, &turn);
printf
(
"
"
);
while
(!c.isWin(turn))
{
if
(turn==1) {
do
{
printf
(
"请输入i j表示你要下的位置:(从0开始)"
);
scanf
(
"%d %d"
,&i,&j);
}
while
(!c.CheckInput(i,j));
c.takeout(i,j);
printf
(
"
"
);
c.print();
}
else
{
AIPredict(c,i,j);
printf
(
"电脑下的位置是:<%d,%d>
"
,i,j);
c.takeout(i,j);
printf
(
"
"
);
c.print();
}
turn = !turn;
}
if
(turn)
{
printf
(
"你赢了!
"
);
}
else
{
printf
(
"你输了!
"
);
}
system
(
"pause"
);
return
0;
}