c语言小程序-黑白棋

你想干嘛?

我有人人格斗的代码,却写不好人机格斗的AI

这是一个国际问题。

//=====奥赛罗= = = = =

# include & ltiostream & gt

# include & ltcstdio & gt

使用命名空间std

int board[8][8],save board[60][8][8];

int cx,cy,col,pass,empty,black,white

void init(){//初始化

memset(board,-1,sizeof(board));

board[3][3]= 0;

板[3][4]= 1;

board[4][4]= 0;

板[4][3]= 1;

col = 0;

pass = 0;

空= 60;

黑色= 2;

白色= 2;

}

int input(){

char s[1000]= " ";

scanf("%s ",& amps);

if(s[0]& gt;= ' a ' & amp& amps[0]& lt;='h ')

cy = s[0]-' a ';

else if(s[0]& gt;= ' A ' & amp& amps[0]& lt;='H ')

cy = s[0]-' A ';

否则返回0;

if(s[1]>= ' 1 ' & amp;& amps[1]& lt;='8'){

CX = s[1]-' 1 ';

返回1;

}

返回0;

}

int judge(int x,int y){

int i,j,temp

temp =(col+1)% 2;

//左& amp& amp向上

if(board[x-1][y-1]= = temp){

for(i=x-1,j = y-1;我& gt= 0 & amp& ampj & gt=0;i -,j - ){

if(board[I][j]& lt;0)破;

if(col==board[i][j])返回1;

}

}

//向上

if(board[x-1][y]==temp){

for(I = x-1;我& gt=0;我- ){

if(board[I][y]& lt;0)破;

if(col==board[i][y])返回1;

}

}

//right & amp;& amp向上

if(board[x-1][y+1]= = temp){

for(i=x-1,j = y+1;我& gt= 0 & amp& ampj & lt8;i -,j++){

if(board[I][j]& lt;0)破;

if(col==board[i][j])返回1;

}

}

//右

if(board[x][y+1]==temp){

for(j = y+1;j & lt8;j++){

if(board[x][j]& lt;0)破;

if(col==board[x][j])返回1;

}

}

//right & amp;& amp向下

if(board[x+1][y+1]= = temp){

for(i=x+1,j = y+1;我& lt8 & amp& ampj & lt8;i++,j++){

if(board[I][j]& lt;0)破;

if(col==board[i][j])返回1;

}

}

//向下

if(board[x+1][y]==temp){

for(I = x+1;我& lt8;i++){

if(board[I][y]& lt;0)破;

if(col==board[i][y])返回1;

}

}

//左& amp& amp向下

if(board[x+1][y-1]= = temp){

for(i=x+1,j = y-1;我& lt8 & amp& ampj & gt=0;i++,j - ){

if(board[I][j]& lt;0)破;

if(col==board[i][j])返回1;

}

}

//左侧

if(board[x][y-1]==temp){

for(j = y-1;j & gt=0;j - ){

if(board[x][j]& lt;0)破;

if(col==board[x][j])返回1;

}

}

返回0;

}

无效移动(int x,int y){

int i,j,temp,count

temp =(col+1)% 2;

count = 0;

//左& amp& amp向上

if(board[x-1][y-1]= = temp){

for(i=x-1,j = y-1;我& gt= 0 & amp& ampj & gt=0;i -,j - ){

if(board[I][j]& lt;0)破;

if(col==board[i][j]){

而(我!=x){

board[++ I][++ j]= col;

count++;

}

count-;

打破;

}

}

}

//向上

if(board[x-1][y]==temp){

for(I = x-1;我& gt=0;我- ){

if(board[I][y]& lt;0)破;

if(col==board[i][y]){

而(我!=x){

board[++ I][y]= col;

count++;

}

count-;

打破;

}

}

}

//right & amp;& amp向上

if(board[x-1][y+1]= = temp){

for(i=x-1,j = y+1;我& gt= 0 & amp& ampj & lt8;i -,j++){

if(board[I][j]& lt;0)破;

if(col==board[i][j]){

而(我!=x){

board[++ I][-j]= col;

count++;

}

count-;

打破;

}

}

}

//右

if(board[x][y+1]==temp){

for(j = y+1;j & lt8;j++){

if(board[x][j]& lt;0)破;

if(col==board[x][j]){

而(j!=y){

board[x][-j]= col;

count++;

}

count-;

打破;

}

}

}

//right & amp;& amp向下

if(board[x+1][y+1]= = temp){

for(i=x+1,j = y+1;我& lt8 & amp& ampj & lt8;i++,j++){

if(board[I][j]& lt;0)破;

if(col==board[i][j]){

而(我!=x){

板[-I][-j]=栏;

count++;

}

count-;

打破;

}

}

}

//向下

if(board[x+1][y]==temp){

for(I = x+1;我& lt8;i++){

if(board[I][y]& lt;0)破;

if(col==board[i][y]){

而(我!=x){

board[-I][y]= col;

count++;

}

count-;

打破;

}

}

}

//左& amp& amp向下

if(board[x+1][y-1]= = temp){

for(i=x+1,j = y-1;我& lt8 & amp& ampj & gt=0;i++,j - ){

if(board[I][j]& lt;0)破;

if(col==board[i][j]){

而(我!=x){

board[-I][++ j]= col;

count++;

}

count-;

打破;

}

}

}

//左侧

if(board[x][y-1]==temp){

for(j = y-1;j & gt=0;j - ){

if(board[x][j]& lt;0)破;

if(col==board[x][j]){

而(j!=y){

board[x][++ j]= col;

count++;

}

count-;

打破;

}

}

}

板[x][y]=栏;

if(col){

黑+=计数;

白色-=计数;

黑++的;

}

否则{

黑色-=计数;

白+=计数;

white++;

}

空-;

}

void输出(){

char c;

printf(" ");

for(int I = 0;我& lt8;i++){

c = ' A '+I;

printf("%2c ",c);

}

printf(" \ n ");

for(int I = 0;我& lt8;i++){

printf("%d ",I+1);

for(int j = 0;j & lt8;j++){

if(board[i][j]==-1)

c =“”;

else if(board[i][j]==0)

c = ' O

其他

c = ' X

printf("%2c ",c);

}

printf(" \ n ");

}

printf("黑色:%3d白色:%3d\n ",黑色,白色);

}

int passjudge(){

int f = 0;

for(int I = 0;我& lt8;i++)

for(int j = 0;j & lt8;j++)

if(board[I][j]& lt;0)

f+=judge(i,j);

返回f;

}

void startprint(){

printf("1、新游戏\n2、setboard\n0、Exit \ n ");

}

void pvp(){

而(空& amp& amp通过& lt2){

//黑色或白色

col++;

col % = 2;

output();

//输入

如果(!input()){

如果(!passjudge()){

printf("通过!\ n ");

pass++;

}

否则{

col++;

printf("不通过!\ n请输入正确的石头!\ n ");

}

继续;

}

if(judge(cx,cy)){

move(cx,cy);

pass = 0;

}

否则{

col++;

printf("斯通小姐\ n ");

}

}

output();

if(black & gt;白色)

printf("黑赢!\ n ");

else if(black & lt;白色)

printf("白胜!\ n ");

其他

printf("抽奖游戏!\ n ");

start print();

}

void setboard(){

char c;

for(int I = 0;我& lt8;i++)

for(int j = 0;scanf("%c ",& ampc)和amp& ampc!= ' \ nj++){

if(j & gt;=8)继续;

if(c = = ' o ' & amp;& ampc=='O ')

board[I][j]= 0;

else if(c = = ' x ' & amp;& ampc=='X ')

board[I][j]= 1;

}

printf("白起还是黑起?\ n(W/B)");

scanf("%c ",& ampc);

if(c=='w' || c=='W ')

col = 1;

if(c=='b' || c=='B ')

col = 0;

}

int main(int argc,char* argv[]){

int n;

start print();

while(scanf("%d ",& ampn)& amp;& ampn){

init();

如果(n==1)

PVP();

if(n==2){

set board();

PVP();

}

}

返回0;

}