Java面向对象练习-基于数组和循环实现的控制台五子棋

用Java写的一个控制台五子棋,只需结合Java中的循环与数组即可
运行后控制台输入坐标
代码如下


import java.util.Scanner;

public class GameTest {

    String a = "●";
    String b = "○";
    boolean bool = true;//作为黑白棋交替标记;
    String[][] qipan = new String[25][25];

    public GameTest() {}

    public void startGame(){
        //初始化棋盘
        this.initDesk();
        //开始下棋
        this.xiaqi();
    };
    //棋盘构造25*25,遍历二维数组赋值“+”每25个换行即可
    public void initDesk() {
        for (int i = 0; i < 25; i++) {
            for (int j = 0; j < 25; j++) {
                qipan[i][j] = "+";
                System.out.print(qipan[i][j]);
                while (j == 24) {
                    System.out.print("\n");
                    break;
                }
            }
        }
    }
    //操作下棋的方法
    public void xiaqi() {
        System.out.println("请输入坐标:");
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int y = sc.nextInt();
        if (x > 24 || y > 24) {
            System.out.println("超出坐标范围,请重新输入!");
            xiaqi();
        } else {
            if(qipan[x][y]==a||qipan[x][y]==b){
                System.out.println("坐标已被其它旗子占用,请重新输入");
                xiaqi();
            }else{
                if (bool) {
                    qipan[x][y] = a;
                    bool = false;
                } else {
                    qipan[x][y] = b;
                    bool = true;
                }
            }
            for (int i = 0; i < 25; i++) {
                for (int j = 0; j < 25; j++) {
                    System.out.print(qipan[i][j]);
                    while (j == 24) {
                        System.out.print("\n");
                        break;
                    }
                }
            }
            panduan();
        }
    }
    //判断输赢的方法
    public void panduan() {
        for (int i = 0; i < 21; i++) {
            for (int j = 0; j < 21; j++) {
                if (qipan[i][j] == a && qipan[i][j + 1] == a
                        && qipan[i][j + 2] == a && qipan[i][j + 3] == a
                        && qipan[i][j + 4] == a) {
                    System.out.println("黑方赢");
                    System.out.println("是否进行下一局?\n Y、进行下一局\n N、取消");
                    Scanner sc = new Scanner(System.in);
                    String f=sc.next();
                    if(f.equals("Y")){
                        initDesk();
                    }else{
                        System.out.println("gameover");
                    }
                } else if (qipan[i][j] == a && qipan[i + 1][j] == a
                        && qipan[i + 2][j] == a && qipan[i + 3][j] == a
                        && qipan[i + 4][j] == a) {
                    System.out.println("黑方赢");
                    System.out.println("是否进行下一局?\n Y、进行下一局\n N、取消");
                    Scanner sc = new Scanner(System.in);
                    String f=sc.next();
                    if(f.equals("Y")){
                        initDesk();
                    }else{
                        System.out.println("gameover");
                    }
                } else if (qipan[i][j] == a && qipan[i + 1][j + 1] == a
                        && qipan[i + 2][j + 2] == a && qipan[i + 3][j + 3] == a
                        && qipan[i + 4][j + 4] == a) {
                    System.out.println("黑方赢");
                    System.out.println("是否进行下一局?\n Y、进行下一局\n N、取消");
                    Scanner sc = new Scanner(System.in);
                    String f=sc.next();
                    if(f.equals("Y")){
                        initDesk();
                    }else{
                        System.out.println("gameover");
                    }
                } else if (qipan[i][j + 4] == a && qipan[i + 1][j + 3] == a
                        && qipan[i + 2][j + 2] == a && qipan[i + 3][j + 1] == a
                        && qipan[i + 4][j + 0] == a) {
                    System.out.println("黑方赢");
                    System.out.println("是否进行下一局?\n Y、进行下一局\n N、取消");
                    Scanner sc = new Scanner(System.in);
                    String f=sc.next();
                    if(f.equals("Y")){
                        initDesk();
                    }else{
                        System.out.println("gameover");
                    }
                } else if (qipan[i][j] == b && qipan[i][j + 1] == b
                        && qipan[i][j + 2] == b && qipan[i][j + 3] == b
                        && qipan[i][j + 4] == b) {
                    System.out.println("白方赢");
                    System.out.println("是否进行下一局?\n Y、进行下一局\n N、取消");
                    Scanner sc = new Scanner(System.in);
                    String f=sc.next();
                    if(f.equals("Y")){
                        initDesk();
                    }else{
                        System.out.println("gameover");
                    }
                } else if (qipan[i][j] == b && qipan[i + 1][j] == b
                        && qipan[i + 2][j] == b && qipan[i + 3][j] == b
                        && qipan[i + 4][j] == b) {
                    System.out.println("白方赢");
                    System.out.println("是否进行下一局?\n Y、进行下一局\n N、取消");
                    Scanner sc = new Scanner(System.in);
                    String f=sc.next();
                    if(f.equals("Y")){
                        initDesk();
                    }else{
                        System.out.println("gameover");
                    }
                } else if (qipan[i][j] == b && qipan[i + 1][j + 1] == b
                        && qipan[i + 2][j + 2] == b && qipan[i + 3][j + 3] == b
                        && qipan[i + 4][j + 4] == b) {
                    System.out.println("白方赢");
                    System.out.println("是否进行下一局?\n Y、进行下一局\n N、取消");
                    Scanner sc = new Scanner(System.in);
                    String f=sc.next();
                    if(f.equals("Y")){
                        initDesk();
                    }else{
                        System.out.println("gameover");
                    }
                } else if (qipan[i][j + 4] == b && qipan[i + 1][j + 3] == b
                        && qipan[i + 2][j + 2] == b && qipan[i + 3][j + 1] == b
                        && qipan[i + 4][j + 0] == b) {
                    System.out.println("白方赢");
                    System.out.println("是否进行下一局?\n Y、进行下一局\n N、取消");
                    Scanner sc = new Scanner(System.in);
                    String f=sc.next();
                    if(f.equals("Y")){
                        initDesk();
                    }else{
                        System.out.println("game over!");
                    }
                }else{
                    xiaqi();
                }
            }
        }
    }


    public static void main(String[] args) {
        GameTest wzq = new GameTest();
        wzq.startGame();
    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值