Java程序设计之四子连

Java程序设计之四子连

题目要求

在这里插入图片描述

代码示例

package ch08Q20;

import java.util.Scanner;

public class ch08Q20 {
    public static void main(String[] args) {
        final int ROW_CHESS = 6;
        final int COL_CHESS = 7;
        int counter = 0;
        int winner;
        int[][] chess = new int[ROW_CHESS][COL_CHESS];
        while (counter < (ROW_CHESS * COL_CHESS)) {
            display(chess);
            chess = input(chess, counter);
            counter++;
            winner = checkWinner(chess);
            if (winner == 1) {
                display(chess);
                System.out.println("The red player won!");
                System.exit(0);
            } else if (winner == 2) {
                display(chess);
                System.out.println("The yellow player won!");
                System.exit(0);
            }
        }
        display(chess);
        System.out.println("You are tied!");
        System.exit(0);
    }

    public static void display(int[][] chess) {
        for (int i = (chess.length - 1); i >= 0; i--) {
            for (int j = 0; j < (chess[i].length * 2 + 1); j++) {
                if (j % 2 == 0) {
                    System.out.print("|");
                } else {
                    if (chess[i][(j - 1) / 2] == 1) {
                        System.out.print("R");
                    } else if (chess[i][(j - 1) / 2] == 2) {
                        System.out.print("Y");
                    } else {
                        System.out.print(" ");
                    }
                }
            }
            System.out.println();
        }
        System.out.println("---------------");
    }

    public static int[][] input(int[][] chess, int counter) {
        int dropChess;
        if ((counter % 2) == 0) {
            System.out.print("Drop a red disk at column (0-6): ");
        } else if ((counter % 2) == 1) {
            System.out.print("Drop a yellow disk at column (0-6): ");
        }
        Scanner scanner = new Scanner(System.in);
        dropChess = scanner.nextInt();
        if (dropChess >= 0 && dropChess <= 6) {
            if ((counter % 2) == 0) {
                for (int i = 0; i <= 6; i++) {
                    if (i < 6 && chess[i][dropChess] == 0) {
                        chess[i][dropChess] = 1;
                        break;
                    } else if (i == 6) {
                        System.out.println("You have drop a out-of-bounds disk!");
                        System.out.println("Please resume the game!");
                        System.exit(1);
                    }
                }
            } else if ((counter % 2) == 1) {
                for (int i = 0; i <= 6; i++) {
                    if (i < 6 && chess[i][dropChess] == 0) {
                        chess[i][dropChess] = 2;
                        break;
                    } else if (i == 6) {
                        System.out.println("You have drop a out-of-bounds disk!");
                        System.out.println("Please resume the game!");
                        System.exit(1);
                    }
                }
            }
        } else {
            System.out.println("You have drop a out-of-bounds disk!");
            System.out.println("Please resume the game!");
            System.exit(1);
        }
        return chess;
    }

    public static int checkWinner(int[][] chess) {
        int winnerRow, winnerCol, winnerDiag, winner = 0;
        winnerRow = checkWinnerRow(chess);
        winnerCol = checkWinnerCol(chess);
        winnerDiag = checkWinnerDiag(chess);
        if (winnerRow == 1 || winnerCol == 1 || winnerDiag == 1) {
            winner = 1;
        } else if (winnerRow == 2 || winnerCol == 2 || winnerDiag == 2) {
            winner = 2;
        }
        return winner;
    }

    public static int checkWinnerRow(int[][] chess) {
        int winnerRow = 0;
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 4; j++) {
                if (chess[i][j] == chess[i][j + 1] && chess[i][j + 1] == chess[i][j + 2] && chess[i][j + 2] == chess[i][j + 3] && chess[i][j] == 1) {
                    winnerRow = 1;
                } else if (chess[i][j] == chess[i][j + 1] && chess[i][j + 1] == chess[i][j + 2] && chess[i][j + 2] == chess[i][j + 3] && chess[i][j] == 2) {
                    winnerRow = 2;
                }
            }
        }
        return winnerRow;
    }

    public static int checkWinnerCol(int[][] chess) {
        int winnerCol = 0;
        for (int i = 0; i < 7; i++) {
            for (int j = 0; j < 3; j++) {
                if (chess[j][i] == chess[j + 1][i] && chess[j + 1][i] == chess[j + 2][i] && chess[j + 2][i] == chess[j + 3][i] && chess[j][i] == 1) {
                    winnerCol = 1;
                } else if (chess[j][i] == chess[j + 1][i] && chess[j + 1][i] == chess[j + 2][i] && chess[j + 2][i] == chess[j + 3][i] && chess[j][i] == 2) {
                    winnerCol = 2;
                }
            }
        }
        return winnerCol;
    }

    public static int checkWinnerDiag(int[][] chess) {
        int winnerDiag = 0;
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 7; j++) {
                if (((i + 3) < 6) && ((j + 3) < 7)) {
                    if (chess[i][j] == chess[i + 1][j + 1] && chess[i + 1][j + 1] == chess[i + 2][j + 2] && chess[i + 2][j + 2] == chess[i + 3][j + 3] && chess[i][j] == 1) {
                        winnerDiag = 1;
                        break;
                    } else if (chess[i][j] == chess[i + 1][j + 1] && chess[i + 1][j + 1] == chess[i + 2][j + 2] && chess[i + 2][j + 2] == chess[i + 3][j + 3] && chess[i][j] == 2) {
                        winnerDiag = 2;
                        break;
                    }
                } else if (((i - 3) >= 0) && ((j + 3) < 7)) {
                    if (chess[i][j] == chess[i - 1][j + 1] && chess[i - 1][j + 1] == chess[i - 2][j + 2] && chess[i - 2][j + 2] == chess[i - 3][j + 3] && chess[i][j] == 1) {
                        winnerDiag = 1;
                        break;
                    } else if (chess[i][j] == chess[i - 1][j + 1] && chess[i - 1][j + 1] == chess[i - 2][j + 2] && chess[i - 2][j + 2] == chess[i - 3][j + 3] && chess[i][j] == 2) {
                        winnerDiag = 2;
                        break;
                    }
                }
            }
        }
        return winnerDiag;
    }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值