井字棋盘游戏

问题描述:

井字棋盘游戏规则:一共有三行三列的位置,在这九个位置中如果有其中一行或者一列是相同的则下该棋子的人获胜 你的游戏对手是ai

package game;

public class Main {
    public static void main(String[] args) {
        Player player =new Player();
        Game game=new Game(player);

        while(true) {
            game.initialize();
            game.play();
            game.destroy();
            boolean b=player.queryContinue();
            if(!b) {
                System.out.println("游戏结束");
                break;
            }
        }
    }


}

package game;
//实例化一个Game 是一局游戏,,有三种构造方法  initialize  play  destroy
public class Game {
    private final Chessboard chessboard;
    private final Player player;
    private final AI ai;

    public Game(Player player) {
        this.ai=new AI();
        this.chessboard=new Chessboard();
        this.player=new Player();

    }

    //游戏开始前的准备工作
    public void initialize(){
        System.out.println("进入井字游戏");
        System.out.println(chessboard);
    }

    //游戏主循环
    public void play(){
        while(true) {

            if(aiTurn()) {
                break;
            }

            if(playerTurn()) {
                break;
            }
        }
    }

    private boolean aiTurn() {
        System.out.println("AI 落子");
        int [] rc=ai.getRowColumn();
        int row=rc[0];
        int column=rc[1];
        while (!chessboard.moveCircleAt(row,column)) {
            rc=ai.getRowColumn();
            row=rc[0];
            column=rc[1];

        }

        System.out.println(chessboard);

        return chessboard.getState()!=Chessboard.Continue;
    }

    private boolean playerTurn() {
        System.out.println("玩家落子");
        int [] rc=player.getRowColumn();
        int row=rc[0];
        int column=rc[1];
        while (!chessboard.moveCrossAt(row,column)) {
            System.out.println("落子位置有子,请重新选位置落子");
            rc=player.getRowColumn();
            row=rc[0];
            column=rc[1];
        }
        System.out.println(chessboard);
        return chessboard.getState()!=Chessboard.Continue;
    }
    //游戏结束 {

    public void destroy() {
        chessboard.reset();
    }

}

package game;

import java.util.Arrays;

public class Chessboard {
    private static int 空白=0;
    private static int=1;
    private static int=2;

    private int[][] array={
            {空白,空白,空白},
            {空白,空白,空白},
            {空白,空白,空白}
    };
    public boolean moveCircleAt(int row,int column) {
        if(array[row][column]==||array[row][column]==) {
            return false;
        }
        array[row][column]=;
        return true;
    }

    public boolean moveCrossAt(int row, int column) {
        if(array[row][column]==||array[row][column]==) {
            return false;
        }
        array[row][column]=;
        return true;
    }
    public static final int Circle_win=0;  //圈赢了
    public static final int Cross_win=1;  //×赢了
    public static final int Draw=2;  //平局
    public static final int Continue=3; //继续
    public int getState() {
        for (int i=0;i<3;i++) {
            if(array[0][i]==array[1][i]&&array[0][i]==array[2][i]&&array[0][i]!=空白) {
                if(array[0][i]==) {
                    System.out.println("恭喜ai获胜");
                    return Circle_win;
                } else {
                    System.out.println("恭喜玩家获胜");
                    return Cross_win;
                }
            }
            if(array[i][0]==array[i][1]&&array[i][0]==array[i][2]&&array[i][0]!=空白) {
                if(array[i][0]==) {
                    System.out.println("恭喜ai获胜");
                    return Circle_win;
                } else {
                    System.out.println("恭喜玩家获胜");
                    return Cross_win;
                }
            }

        }
        if(array[0][0]==array[1][1]&&array[0][0]==array[2][2]&&array[0][0]!=空白){  //斜线位置有人赢了
            if(array[0][0]==){
                System.out.println("恭喜ai获胜");
                return Circle_win;
            }else {
                System.out.println("恭喜玩家获胜");
                return Cross_win;
            }
        }
        if(array[0][2]==array[1][1]&&array[0][2]==array[2][0]&&array[0][2]!=空白){  //斜线位置有人赢了
            if(array[0][2]==){
                System.out.println("恭喜ai获胜");
                return Circle_win;
            }else {
                System.out.println("恭喜玩家获胜");
                return Cross_win;
            }
        }
        for (int i=0;i<3;i++) {
            for (int j=0;j<3;j++) {
                if(array[i][j]==空白){
                    System.out.println("游戏继续");
                    return Continue;
                }
            }
        }
        System.out.println("平局");
        return Draw;
    }

    private static String 显示(int i) {
        if(i==空白) {
            return "空白";
        }
        if(i==) {
            return "圈";
        }
        if(i==) {
            return "叉";
        }
        return "  ";
    }

    @Override
    public String toString() {
        String s="+-+-+-+\n";
        for (int i=0;i<2;i++) {
            s+=String.format("|%s|%s|%s|\n", 显示(array[i][0]),显示(array[i][1]),显示(array[i][2]));
            s+="+-+-+-+\n";
        }
        s+=String.format("|%s|%s|%s|\n", 显示(array[2][0]),显示(array[2][1]),显示(array[2][2]));
        s+="+-+-+-+\n";
        return s;
    }

    public void reset() {
        Arrays.fill(array,空白);
    }



}


```java
package game;

import java.util.Random;

public class AI {
    private final Random random=new Random();
    //返回落子位置
    public int [] getRowColumn() {
        int r=random.nextInt(3);
        int c=random.nextInt(3);
        int [] m={r,c};
        return m;
    }
}

package game;

import java.util.Scanner;

public class Player {
    //private Scanner scanner=new Scanner(System.in);

    public int[] getRowColumn() {
        System.out.println("输入要落子的位置  行 ");
        Scanner sc=new Scanner(System.in);
        int r=sc.nextInt();
        System.out.println("输入要落子的位置  列");
        Scanner sca=new Scanner(System.in);
        int c=sca.nextInt();
        System.out.println(">>>");
        if((r<0||r>2)||(c<0||c>2)) {
            System.out.println("不满足有效范围");
            return null;
        }

        return new int[] {r,c};
    }

    //询问用户是否继续
    public boolean queryContinue() {
        System.out.println("是否开始一局新游戏");
        System.out.println(">>>");
        Scanner scanner =new Scanner(System.in);
        return scanner.nextBoolean();
    }
}

你可能感兴趣的:(寒假学习笔记)