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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值