三子棋

     java老师布置的作业有个高大上的名字叫bingo游戏,了解了才知道是所谓的三子棋游戏,这个游戏花费的时间不长,也没有发表,现在才有时间整理下,自己感觉写的很烂,有种感觉几乎是把各种可能都列了出来,请指正。(对AI不是很熟悉,按自己的理解在人机对战中写的)

   下面是代码,分成了两个类 一个是棋盘类,一个数主类:

   

package 三子棋;
import java.util.Scanner;

public class ChessBoard {
int CB[][]=new int [3][3];
ChessBoard()
{
for(int i = 0;i<3;i++)
{
for(int j = 0;j<3;j++)
{
CB[i][j]=0;
}
}
}
void ChessShow()
{
for(int i = 0;i<3;i++)
{
for(int j = 0;j<3;j++)
{
System.out.printf(" "+CB[i][j]);
}
System.out.println();
}
System.out.println();
}
int panduan(int c)
{
int a=0;
if((CB[0][0]==c&&CB[1][1]==c&&CB[2][2]==c)||(CB[0][1]==c&&CB[1][1]==c&&CB[2][1]==c)||(CB[0][2]==c&&CB[1][1]==c&&CB[2][0]==c)||(CB[1][0]==c&&CB[1][1]==c&&CB[1][2]==c)||(CB[0][0]==c&&CB[1][0]==c&&CB[2][0]==c)||(CB[2][0]==c&&CB[2][1]==c&&CB[2][2]==c)||(CB[0][2]==c&&CB[1][2]==c&&CB[2][2]==c)||(CB[0][0]==c&&CB[0][1]==c&&CB[0][2]==c))
{
a=c;
}

return a;
}

void PeopleChoice()
{

while(true)
{
System.out.println(" 请输入第一位选手的落子位置");
Scanner reader =new Scanner(System.in);
int post1 =reader.nextInt();
int post2 =reader.nextInt();
CB[post1][post2]=1;
System.out.printf(" "+CB[post1][post2]);
System.out.println();
ChessShow();
if(panduan(1)==1)
{
System.out.println(" 第一位选手赢了");
break;
}
System.out.println(" 请输入第二位选手的落子位置");
int post3 =reader.nextInt();
int post4 =reader.nextInt();
CB[post3][post4]=2;
ChessShow();
if(panduan(2)==2)
{
System.out.println(" 第二位选手赢了");
break;
}

}
System.out.println(" 游戏结束");
}
void Computer(int a,int b,int c)
{
if(CB[a][b+1]==0&&a<3&&b<=3)
{
CB[a][b+1]=c;
}
else if((b-1)>=0&&CB[a][b-1]==0&&a<3)
{
CB[a][b-1]=c;
}
else if(CB[a+1][b]==0&&a<2&&b>0)
{
CB[a+1][b]=c;
}
else if(a-1>=0&&CB[a-1][b]==0&&b>0)
{
CB[a-1][b]=c;
}
else if(a-1>=0&&b+1<=2&&CB[a-1][b+1]==0)
{
CB[a-1][b+1]=c;
}
else if(a+1<=2&&b-1>=0&&CB[a+1][b-1]==0)
{
CB[a+1][b-1]=c;
}
else if(a-1>=0&&b-1>=0&&CB[a-1][b-1]==0)
{
CB[a-1][b-1]=c;
}
else if(CB[a+1][b+1]==0&&a+1<2&&b+1<2)
{
CB[a+1][b+1]=c;
}


}
void Computer2(int a,int b)
{

if(CB[0][0]==b&&CB[0][1]==b&&CB[0][2]==0)
{
CB[0][2]=a;

}
else if(CB[0][1]==b&&CB[0][2]==b&&CB[0][0]==0)
{
CB[0][0]=a;

}
else if (CB[0][0]==b&&CB[0][2]==b&&CB[0][1]==0)
{
CB[0][1]=a;

}
else if (CB[0][0]==b&&CB[1][0]==b&&CB[2][0]==0)
{
CB[2][0]=a;

}
else if (CB[1][0]==b&&CB[2][0]==b&&CB[0][0]==0)
{
CB[0][0]=a;

}
else if (CB[0][0]==b&&CB[2][0]==b&&CB[1][0]==0)
{
CB[1][0]=a;

}
else if (CB[0][0]==b&&CB[1][1]==b&&CB[2][2]==0)
{
CB[2][2]=a;

}
else if (CB[0][0]==b&&CB[2][2]==b&&CB[1][1]==0)
{
CB[1][1]=a;

}
else if (CB[1][1]==b&&CB[2][2]==b&&CB[0][0]==0)
{
CB[0][0]=a;

}
else if (CB[1][0]==b&&CB[1][1]==b&&CB[1][2]==0)
{
CB[1][2]=a;

}
else if (CB[1][0]==b&&CB[1][2]==b&&CB[1][1]==0)
{
CB[1][1]=a;

}
else if (CB[1][1]==b&&CB[1][2]==b&&CB[1][0]==0)
{
CB[1][0]=a;

}
else if (CB[2][0]==b&&CB[2][1]==b&&CB[2][2]==0)
{
CB[2][2]=a;

}
else if (CB[2][0]==b&&CB[2][2]==b&&CB[2][1]==0)
{
CB[2][1]=a;

}
else if (CB[2][1]==b&&CB[2][2]==b&&CB[2][0]==0)
{
CB[2][0]=a;

}
else if (CB[0][1]==b&&CB[1][1]==b&&CB[2][1]==0)
{
CB[2][1]=a;

}
else if (CB[1][1]==b&&CB[2][1]==b&&CB[0][1]==0)
{
CB[0][1]=a;

}
else if (CB[0][1]==b&&CB[2][1]==b&&CB[1][1]==0)
{
CB[1][1]=a;

}
else if (CB[0][2]==b&&CB[1][2]==b&&CB[2][2]==0)
{
CB[2][2]=a;

}
else if (CB[1][2]==b&&CB[2][2]==b&&CB[0][2]==0)
{
CB[0][2]=a;

}
else if (CB[0][2]==b&&CB[2][2]==b&&CB[0][2]==0)
{
CB[0][2]=a;

}
else if (CB[0][2]==b&&CB[1][1]==b&&CB[2][0]==0)
{
CB[2][0]=a;

}
else if (CB[2][0]==b&&CB[1][1]==b&&CB[0][2]==0)
{
CB[0][2]=a;

}
else if (CB[2][0]==b&&CB[0][2]==b&&CB[1][1]==0)
{
CB[1][1]=a;

}

}
int Equalgame()
{ int add=0;
for(int i = 0;i<3;i++)
{
for(int j = 0;j<3;j++)
{
add+=CB[i][j];
}

}
return add;

}
void ComPKpeo(int a)
{
int m=1;
if(a==1)
{ ChessShow();
while(true)
{

System.out.println(" 请输入您输入落子位置");

Scanner reader =new Scanner(System.in);
int post1 =reader.nextInt();
int post2 =reader.nextInt();
CB[post1][post2]=1;
System.out.println();
ChessShow();
if(panduan(1)==1)
{
System.out.println(" 第一位选手赢了");
break;
}
System.out.println(" 电脑落子中");
if(m==1)
{
Computer(post1,post2,2);
ChessShow();
m++;
}
else
{
Computer2(2,2);
if(panduan(2)==2)
{
ChessShow();
System.out.println(" 电脑赢了");
break;
}
Computer2(2,1);
ChessShow();
if(panduan(2)==2)
{ ChessShow();
System.out.println(" 电脑赢了");
break;
}
if(Equalgame()==13)
{
System.out.println(" 结果: 平局");
break;
}

}


}
System.out.println(" 游戏结束");
}
if(a==2)
{
int f=1,w=1;
int o=(int)(Math.random() * 2);
int t=(int)(Math.random() * 2);
while(true)
{


if(w==1)
{
System.out.println(" 电脑落子中");

CB[o][t]=2;
w++;
}
else
{
ChessShow();
if(panduan(1)==1)
{
System.out.println(" 第一位选手赢了");
break;
}
System.out.println(" 请输入您输入落子位置");
Scanner reader =new Scanner(System.in);
int post1 =reader.nextInt();
int post2 =reader.nextInt();
CB[post1][post2]=1;

if(panduan(1)==1)
{
System.out.println(" 第一位选手赢了");
break;
}
if(f==1)
{
ChessShow();
System.out.println(" 电脑落子中");
Computer(o,t,2);
f++;
}
else
{
ChessShow();
System.out.println(" 电脑落子中");
Computer2(2,2);
if(Equalgame()==12)
{
for(int i=0;i<=2;i++)
for(int j=0;j<=2;j++)
{
if (CB[i][j]==0)
{
CB[i][j]=2;
}
}
ChessShow();
}
Computer2(2,1);
if(panduan(2)==2)
{
ChessShow();
System.out.println(" 电脑赢了");
break;
}

if(Equalgame()==14)
{
System.out.println(" 结果: 平局");
break;
}

}

}
}
System.out.println(" 游戏结束");
}
}
void Outgame()
{
System.exit(0);
}
}

package 三子棋;
import java.util.Scanner;
public class MainFun {

public static void main(String[] args) {

ChessBoard ChBo =new ChessBoard();
System.out.println(" 欢迎来到三子棋的世界");
System.out.println(" 请选择");
System.out.println(" *① 双人对战");
System.out.println(" *② 人机对战");
System.out.println(" *③ 退出游戏");
System.out.println();
System.out.println();
ChBo.ChessShow();
System.out.println();
System.out.println(" 请输入你的选择");
Scanner reader =new Scanner(System.in);
int choice =reader.nextInt();

if(choice==1)
{
ChBo.PeopleChoice();
}
if(choice==2)
{
System.out.println(" 先手选一 后手选二 ");
int choice1 =reader.nextInt();
if(choice1==1)
{
ChBo.ComPKpeo(1);
}
if(choice1==2)
{
ChBo.ComPKpeo(2);
}

}
if(choice==3)
{
ChBo.Outgame();
}

}

}

运行结果:

                                                                     

                                                                                                                                                                                   by:暖暖要坚持

                                                                                                                                                                                   20150515

原文地址:https://www.cnblogs.com/2714585551summer/p/4505391.html