Java 人机对战五子棋游戏




import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
* Created by Administrator on 2017/10/26.
*/
public class ChessGame {
    private static int  board_size=15;
    private String[][] board;
    public void initBoard(){
        board =new String[board_size][board_size];
        for(int i=0;i<board_size;i++) {
            for (int j = 0; j < board_size; j++) {
                board[i][j] = "+";
            }
        }
    }
    public void printBoard(){
        for(int i=0;i<board_size;i++) {
            for (int j = 0; j < board_size; j++) {
               System.out.print(board[i][j]);
            }
            System.out.println();
        }
    }

    public static void main(String[] args) throws Exception {
        ChessGame cg=new ChessGame();
        cg.initBoard();
        cg.printBoard();
        BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
        String inputStr=null;
        while((inputStr=bf.readLine())!=null){
            String[] posArr=inputStr.split(",");
            int xPos=Integer.parseInt(posArr[0]);
            int yPos=Integer.parseInt(posArr[1]);
            if(cg.board[xPos][yPos]=="+"){
                cg.board[xPos][yPos]="*";
            }else{
                System.out.println("请重新输入:");
                continue;
            }
            int cxPos=(int)(Math.random()*15);
            int cyPos=(int)(Math.random()*15);
            if(cg.board[cxPos][cyPos]=="+"){
                cg.board[cxPos][cyPos]="@";
            }
            cg.printBoard();
            //判断谁输谁赢
            int[] count=new int[4];
            //i=0,从自身开始
            for(int i=0;i<=4&&(xPos+i)<board_size&&cg.board[xPos+i][yPos]=="*";i++){
                count[0]++;
                if(count[0]>=5){
                    System.out.print("我赢了,oh yeah,游戏结束!!!");
                    return;
                }
                //i=1,反方向从邻居开始
            }for(int i=1;i<=4&&(xPos-i)>=0&&cg.board[xPos-i][yPos]=="*";i++){
                count[0]++;
                if(count[0]>=5){
                    System.out.print("我赢了,oh yeah,游戏结束!!!");
                    return;
                }
            }
            for(int i=0;i<=4&&(yPos+i)<board_size&&cg.board[xPos][yPos+i]=="*";i++){
                count[1]++;
                if(count[1]>=5){
                    System.out.print("我赢了,oh yeah,游戏结束!!!");
                    return;
                }
            }for(int i=1;i<=4&&(yPos-i)>=0&&cg.board[xPos][yPos-i]=="*";i++){
                count[1]++;
                if(count[1]>=5){
                    System.out.print("我赢了,oh yeah,游戏结束!!!");
                    return;
                }
            }
            for(int i=0;i<=4&&(xPos+i)<board_size&&(yPos+i)<board_size&&cg.board[xPos+i][yPos+i]=="*";i++){
                count[2]++;
                if(count[2]>=5){
                    System.out.print("机器赢了,oh huo,游戏结束!!!");
                    return;
                }
            }for(int i=1;i<=4&&(xPos-i)>=0&&(yPos-i)>=0&&cg.board[xPos-i][yPos-i]=="*";i++){
                count[2]++;
                if(count[2]>=5){
                    System.out.print("机器赢了,oh huo,,游戏结束!!!");
                    return;
                }
            }
            for(int i=0;i<=4&&(xPos+i)<board_size&&(yPos-i)>=0&&cg.board[xPos+i][yPos-i]=="*";i++){
                count[3]++;
                if(count[3]>=5){
                    System.out.print("机器赢了,oh huo,,游戏结束!!!");
                    return;
                }
            }for(int i=1;i<=4&&(xPos-i)>=0&&(yPos+i)<board_size&&cg.board[xPos-i][yPos+i]=="*";i++){
                count[3]++;
                if(count[3]>=5){
                    System.out.print("机器赢了,oh huo,游戏结束!!!");
                    return;
                }
            }
            //判断机器是否胜利
            for(int i=0;i<=4&&(cxPos+i)<board_size&&cg.board[cxPos+i][cyPos]=="@";i++){
                count[0]++;
                if(count[0]>=5){
                    System.out.print("机器赢了,oh huo,游戏结束!!!");
                    return;
                }
                //i=1,反方向从邻居开始
            }for(int i=1;i<=4&&(cxPos-i)>=0&&cg.board[cxPos-i][cyPos]=="@";i++){
                count[0]++;
                if(count[0]>=5){
                    System.out.print("机器赢了,oh huo,游戏结束!!!");
                    return;
                }
            }
            for(int i=0;i<=4&&(cyPos+i)<board_size&&cg.board[cxPos][cyPos+i]=="@";i++){
                count[1]++;
                if(count[1]>=5){
                    System.out.print("机器赢了,oh huo,游戏结束!!!");
                    return;
                }
            }for(int i=1;i<=4&&(cyPos-i)>=0&&cg.board[cxPos][cyPos-i]=="@";i++){
                count[1]++;
                if(count[1]>=5){
                    System.out.print("机器赢了,oh huo,游戏结束!!!");
                    return;
                }
            }
            for(int i=0;i<=4&&(cxPos+i)<board_size&&(cyPos+i)<board_size&&cg.board[cxPos
                    +i][cyPos+i]=="@";i++){
                count[2]++;
                if(count[2]>=5){
                    System.out.print("机器赢了,oh huo,游戏结束!!!");
                    return;
                }
            }for(int i=1;i<=4&&(cxPos-i)>=0&&(cyPos-i)>=0&&cg.board[cxPos-i][cyPos-i]
                    =="@";i++){
                count[2]++;
                if(count[2]>=5){
                    System.out.print("机器赢了,oh huo,游戏结束!!!");
                    return;
                }
            }
            for(int i=0;i<=4&&(cxPos+i)<board_size&&(cyPos-i)>=0&&cg.board[cxPos+i]
                    [cyPos-i]=="@";i++){
                count[3]++;
                if(count[3]>=5){
                    System.out.print("机器赢了,oh huo,游戏结束!!!");
                    return;
                }
            }for(int i=1;i<=4&&(cxPos-i)>=0&&(cyPos+i)<board_size&&cg.board[cxPos-i]
                    [cyPos+i]=="@";i++){
                count[3]++;
                if(count[3]>=5){
                    System.out.print("机器赢了,oh huo,游戏结束!!!");
                    return;
                }
            }
            /*
            *1.电脑随机生成2个数,只能是数字,不能超出棋盘范围
            * 2.下棋的点不能重复
            * 3.每次下棋之后需要扫描谁赢了
            * 目前存在的问题是:电脑很笨。。。
             */

            System.out.print("请输入下棋的坐标,以x,y的格式:");
        }
    }
}

 

在疯狂Java讲义里面看到的一个题目:

用二维数组来实现五子棋游戏,

思路:

1,用一个Java类来实现,核心是一个数组,这个数组既是棋盘,也是棋子,

2,从控制台输入想要下棋的位置,如果当前位置越界(暂时没处理),或者已经有了棋子,就报错,重新输入,这里用的是

BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));

3,计算机用random()函数来生成随机数,如果已经有了棋子,则重新生成随机数(暂时为实现)

4.判断谁输谁赢,一共有横向,竖向,对角线方向4个方向,分别判断,若有一方胜出,则游戏结束,否则,继续游戏

转载于:https://my.oschina.net/u/3321842/blog/1556887

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的人机五子棋游戏Java语言实现: ```java import java.util.Scanner; public class Gobang { private static final int BOARD_SIZE = 15; // 棋盘大小 private static final int WIN_COUNT = 5; // 获胜所需棋子数 private static final char EMPTY = ' '; // 空位 private static final char HUMAN = 'X'; // 玩家棋子 private static final char COMPUTER = 'O'; // 电脑棋子 private static char[][] board = new char[BOARD_SIZE][BOARD_SIZE]; // 棋盘 private static Scanner scanner = new Scanner(System.in); // 输入流 public static void main(String[] args) { initBoard(); printBoard(); while (true) { humanTurn(); if (checkWin(HUMAN)) { System.out.println("恭喜你,你赢了!"); break; } computerTurn(); if (checkWin(COMPUTER)) { System.out.println("很遗憾,你输了!"); break; } } } // 初始化棋盘 private static void initBoard() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { board[i][j] = EMPTY; } } } // 打印棋盘 private static void printBoard() { System.out.print(" "); for (int i = 0; i < BOARD_SIZE; i++) { System.out.print(i + " "); } System.out.println(); for (int i = 0; i < BOARD_SIZE; i++) { System.out.print(i + " "); for (int j = 0; j < BOARD_SIZE; j++) { System.out.print(board[i][j] + " "); } System.out.println(); } } // 玩家下棋 private static void humanTurn() { int x, y; while (true) { System.out.print("请输入您要下棋的坐标(例如:3 4):"); x = scanner.nextInt(); y = scanner.nextInt(); if (x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE && board[x][y] == EMPTY) { board[x][y] = HUMAN; printBoard(); break; } else { System.out.println("输入有误,请重新输入!"); } } } // 电脑下棋 private static void computerTurn() { int x, y; while (true) { x = (int) (Math.random() * BOARD_SIZE); y = (int) (Math.random() * BOARD_SIZE); if (board[x][y] == EMPTY) { board[x][y] = COMPUTER; printBoard(); break; } } } // 判断是否获胜 private static boolean checkWin(char player) { int count; // 水平方向 for (int i = 0; i < BOARD_SIZE; i++) { count = 0; for (int j = 0; j < BOARD_SIZE; j++) { if (board[i][j] == player) { count++; if (count == WIN_COUNT) { return true; } } else { count = 0; } } } // 垂直方向 for (int i = 0; i < BOARD_SIZE; i++) { count = 0; for (int j = 0; j < BOARD_SIZE; j++) { if (board[j][i] == player) { count++; if (count == WIN_COUNT) { return true; } } else { count = 0; } } } // 左上-右下方向 for (int i = 0; i < BOARD_SIZE - WIN_COUNT + 1; i++) { for (int j = 0; j < BOARD_SIZE - WIN_COUNT + 1; j++) { count = 0; for (int k = 0; k < WIN_COUNT; k++) { if (board[i + k][j + k] == player) { count++; if (count == WIN_COUNT) { return true; } } else { count = 0; } } } } // 左下-右上方向 for (int i = 0; i < BOARD_SIZE - WIN_COUNT + 1; i++) { for (int j = WIN_COUNT - 1; j < BOARD_SIZE; j++) { count = 0; for (int k = 0; k < WIN_COUNT; k++) { if (board[i + k][j - k] == player) { count++; if (count == WIN_COUNT) { return true; } } else { count = 0; } } } } return false; } } ``` 代码中使用一个二维字符数组来表示棋盘,'X'表示玩家的棋子,'O'表示电脑的棋子,空格表示空位。下棋时,玩家通过输入坐标来下棋,电脑通过随机生成坐标来下棋。判断获胜时,分别检查水平方向、垂直方向、左上-右下方向、左下-右上方向是否满足获胜条件。如果满足条件,则返回true,否则返回false。游戏的主循环中,交替进行玩家和电脑的回合,直到有一方获胜。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值