【Java_Base】二维数组实现五子棋

 1 import java.io.*;
 2 public class Gobang{
 3     //定义棋盘大小
 4     private static int BOARD_SIZE=15;
 5     //定义一个二维数组来充当棋盘
 6     private String [][] board;
 7     public void initBoard(){
 8         //初始化棋盘数组
 9         board=new String[BOARD_SIZE][BOARD_SIZE];
10         //把每个元素赋值为“+”,用于在控制台画出棋盘
11         for(int i=0;i<BOARD_SIZE;i++){
12             for(int j=0;j<BOARD_SIZE;j++){
13                 board[i][j]="+";
14             }
15         }
16     }
17     //在控制台输出棋盘的方法
18     public void printBoard(){
19         //打印每个数组元素
20         for(int i=0;i<BOARD_SIZE;i++){
21             for(int j=0;j<BOARD_SIZE;j++){
22                 System.out.print(board[i][j]);
23             }
24             System.out.print("\n");
25         }
26     }
27     public static void main(String args[]) throws Exception{
28         Gobang w=new Gobang();
29         w.initBoard();
30         w.printBoard();
31         //这是用于键盘输入的方法
32         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
33         String inputStr=null;
34         //每当从键盘输入一行内容后按回车,刚输入的内容就会被br接收到
35         while((inputStr=br.readLine())!=null){
36             //将用户输入的字符串用“,”分隔开
37             String[] posStrArr=inputStr.split(",");
38             //将两个字符串转换成用户下棋的坐标
39             int xpos=Integer.parseInt(posStrArr[0]);
40             int ypos=Integer.parseInt(posStrArr[1]);
41             w.board[ypos-1][xpos-1]="●";
42             w.printBoard();
43             System.out.println("请输入您下棋的坐标,格式为x,y:");
44             
45             
46         }
47     }
48 }

转载于:https://www.cnblogs.com/JayAnother/p/5078322.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值