import java.util.Scanner;
// x o 下落棋
public class Start {
public void minu(){//菜单类
Scanner sc=new Scanner(System.in);
System.out.println();
System.out.println("***************************");
System.out.println("* 欢迎使用小月半下落棋 *\n" +
"* --1.开始游戏 *\n" +
"* --2.退出游戏 *");
System.out.println("***************************");
int choose;
try {
choose = sc.nextInt();
switch (choose){//菜单的选择
case 1: Referee referee=new Referee();//开始游戏
referee.choosfirst();
referee.Startgame();
break;
case 2:System.exit(0);//退出
default:System.out.println("错误的指令!");
}
} catch (Exception e) {
System.out.println("错误的指令!");
}
}
public static void main(String[] args) {
Start start=new Start();
while (true){//循环打印菜单
start.minu();
}
}
}
class Player{//玩家
String name;
int column;
String logo;
public Player(String name,String logo){
this.name=name;
this.logo=logo;
}
public void playchess(){//落子
while (true){
try {
Scanner sc=new Scanner(System.in);
System.out.println("请"+name+"输入列坐标:");
column=sc.nextInt();
if (column>9){
System.out.println("下标越界 请重新输入!");
}else {
return;
}
} catch (Exception e) {
System.out.println("请输入数字 请重新输入!");
}
}
}
}
class Chessboard{//棋盘类
public String[][] strings=new String[10][9];
public void printChessboard(int[] count){//打印棋盘
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 9; j++) {
if (i==0){
System.out.print( "| "+(j+1)+" ");
}else{
if (strings[i][j]==null){
strings[i][j]="_";
}
System.out.print( "| "+strings[i][j]+" ");
}
}
System.out.println("|");
}
}
}
class Referee{//裁判类
Player A=new Player("A","X");
Player B=new Player("B","O");
Chessboard chessboard=new Chessboard();
int count[]={1,1,1,1,1,1,1,1,1};
Player turn=null;
boolean gameover=false;
public void choosfirst(){//现行
System.out.println("选择现行棋的棋手: 1.A or 2.B");
while (true){
Scanner sc;
try {
sc = new Scanner(System.in);
int name=sc.nextInt();
switch (name){
case 1:turn=A;
return;
case 2:turn=B;
return;
default: System.out.println("错误的棋手!");
}
} catch (Exception e) {
System.out.println("错误的棋手!");
}
}
}
public void Startgame(){
System.out.println("开始游戏");
chessboard.printChessboard(count);
while(!gameover){
Judg();
}
System.out.println();
System.out.println("恭喜! "+turn.name+"获得了比赛胜利!!!!");
}
public void Judg(){//判决
put(turn,chessboard);
chessboard.printChessboard(count);
for (int i = 4; i < 10; i++) {
for (int j = 3; j < 9; j++) {
if((chessboard.strings[i][j]==turn.logo&&chessboard.strings[i-1][j-1]==turn.logo
&&chessboard.strings[i-2][j-2]==turn.logo&&chessboard.strings[i-3][j-3]==turn.logo)){
gameover=true;
return;
}
}
}for (int i = 1; i < 10; i++) {
for (int j = 3; j < 9; j++) {
if((chessboard.strings[i][j]==turn.logo&&chessboard.strings[i][j-1]==turn.logo
&&chessboard.strings[i][j-2]==turn.logo&&chessboard.strings[i][j-3]==turn.logo)){
gameover=true;
return;
}
}
}for (int i = 4; i < 10; i++) {
for (int j = 0; j < 9; j++) {
if((chessboard.strings[i][j]==turn.logo&&chessboard.strings[i-1][j]==turn.logo
&&chessboard.strings[i-2][j]==turn.logo&&chessboard.strings[i-3][j]==turn.logo)){
gameover=true;
return;
}
}
}
if(turn==A){
turn=B;
}else {
turn=A;
}
}
public void put(Player player,Chessboard chessboard){
player.playchess();
for (int i = 9; i > 0; i--) {
if(count[player.column-1]>9){
System.out.println("这一排已经放满了 请重新输入");
if(turn==A){
turn=B;
}else {
turn=A;
}
break;
}else if(chessboard.strings[i][player.column-1]=="_"){
chessboard.strings[i][player.column-1]=player.logo;
count[player.column-1]++;
break;
}
}
}
}
网友评论