package newPaca;
import java.util.Scanner;
public class jingziqi {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int qipan[][]=new int[3][3];
inItQiPan(qipan);
printQiPan(qipan);
gameStart(qipan);
}
public static void inItQiPan(int[][] qipan){//初始化棋盘
for(int i=0;i
for(int j=0;j
qipan[i][j]=0;
}
}
}
public static void printQiPan(int[][] qipan){//打印棋盘
String show[]={" ","X","O"};
for(int i=0;i
for(int j=0;j
System.out.print("|"+show[qipan[i][j]]);
}
System.out.print("|");
System.out.println();
}
}
public static int ifEnd(int[][] qipan){//判断游戏状况、是否结束
int i=0;
for(int b=0;b<3;b++){
if((qipan[b][0]==1&&qipan[b][1]==1&&qipan[b][2]==1)
||(qipan[0][b]==1&&qipan[1][b]==1&&qipan[2][b]==1)
||(qipan[0][0]==1&&qipan[1][1]==1&&qipan[2][2]==1)
||(qipan[0][2]==1&&qipan[1][1]==1&&qipan[2][0]==1)){i=1;}
else if((qipan[b][0]==2&&qipan[b][1]==2&&qipan[b][2]==2)
||(qipan[0][b]==2&&qipan[1][b]==2&&qipan[2][b]==2)
||(qipan[0][0]==2&&qipan[1][1]==2&&qipan[2][2]==2)
||(qipan[0][2]==2&&qipan[1][1]==2&&qipan[2][0]==2)){i=2;}
}
return i;
}
public static int e=1;
public static void gameStart(int[][] qipan){//游戏开始
Scanner input=new Scanner(System.in);
int r,l;
System.out.println("请第"+e+"位玩家输入下步棋X所在的行和列:");
r=input.nextInt();
l=input.nextInt();
ifGameOver(qipan,r,l);
}
public static int ifLocaltionTrue(int[][] qipan,int r,int l){//判断玩家输入的位置是否合法
if(qipan[r-1][l-1]!=0||r>3||l>3||r<1||l<1){
return 0;
}
else
{
return 1;
}
}
public static void ifGameOver(int[][] qipan,int r,int l){//控制游戏显示有无结束
Scanner input=new Scanner(System.in);
if(ifLocaltionTrue(qipan,r,l)==1) {
qipan[r-1][l-1]=e;
printQiPan(qipan);
if(ifEnd(qipan)==e){System.out.println("玩家"+e+"赢了!");}
else{e=3-e;gameStart(qipan);}
}
else {
System.out.println("您的输入有误!请重新输入:");
r=input.nextInt();
l=input.nextInt();
ifGameOver(qipan,r,l);
}
}
}
网友评论