java用数组编写五子棋_Java数组实现五子棋功能

package ch4;

import java.io.*;

/**

* Created by Jiqing on 2016/11/9.

*/

public class Gobang {

// 定义棋盘的大小

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.print("\n");

}

}

public static void main(String[] args) throws Exception

{

Gobang gb = new Gobang();

gb.initBoard();

gb.printBoard();

// 这是用于获取键盘输入的方法

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

String inputStr = null;

// br.readLine():每当在键盘上输入一行内容按回车,用户刚输入的内容将被br读取到。

while ((inputStr = br.readLine()) != null)

{

// 将用户输入的字符串以逗号(,)作为分隔符,分隔成2个字符串

String[] posStrArr = inputStr.split(",");

// 将2个字符串转换成用户下棋的座标

int xPos = Integer.parseInt(posStrArr[0]);

int yPos = Integer.parseInt(posStrArr[1]);

// 把对应的数组元素赋为"●"。

gb.board[yPos - 1][xPos - 1] = "●";

/*

电脑随机生成2个整数,作为电脑下棋的座标,赋给board数组。

还涉及

1.座标的有效性,只能是数字,不能超出棋盘范围

2.如果下的棋的点,不能重复下棋。

3.每次下棋后,需要扫描谁赢了

*/

gb.printBoard();

System.out.println("请输入您下棋的座标,应以x,y的格式:");

}

}

}

36a19a5909c463e87119c525420b24d6.png

9ce270224d3287ae89ecb341934cbfe3.png

7a0a35e5acdf833337ed1f38dede2333.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您解答,以下是一个简单的五子棋游戏的Java程序: ```java import java.util.Scanner; public class GomokuGame { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); char[][] board = new char[15][15]; // 棋盘大小为15*15 initBoard(board); // 初始化棋盘 printBoard(board); // 打印棋盘 int x, y; char player = 'X'; // X先手,O后手 while (true) { System.out.print("请输入" + player + "的落子位置(x,y): "); x = scanner.nextInt(); y = scanner.nextInt(); if (isValid(board, x, y)) { board[x][y] = player; // 落子 printBoard(board); // 打印棋盘 if (isWin(board, x, y)) { System.out.println(player + "获胜!"); break; } player = (player == 'X') ? 'O' : 'X'; // 交换玩家 } else { System.out.println("无效位置,请重新输入!"); } } } // 初始化棋盘 public static void initBoard(char[][] board) { for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { board[i][j] = '+'; } } } // 打印棋盘 public static void printBoard(char[][] board) { System.out.print(" "); for (int i = 0; i < board.length; i++) { System.out.print(i + " "); } System.out.println(); for (int i = 0; i < board.length; i++) { System.out.print(i + " "); for (int j = 0; j < board[i].length; j++) { System.out.print(board[i][j] + " "); } System.out.println(); } } // 判断位置是否有效 public static boolean isValid(char[][] board, int x, int y) { if (x < 0 || x >= board.length || y < 0 || y >= board[0].length) { return false; } return board[x][y] == '+'; } // 判断是否获胜 public static boolean isWin(char[][] board, int x, int y) { int count = 0; char player = board[x][y]; // 水平方向 for (int i = y - 4; i <= y + 4; i++) { if (i < 0 || i >= board.length) { continue; } if (board[x][i] == player) { count++; if (count == 5) { return true; } } else { count = 0; } } count = 0; // 垂直方向 for (int i = x - 4; i <= x + 4; i++) { if (i < 0 || i >= board[0].length) { continue; } if (board[i][y] == player) { count++; if (count == 5) { return true; } } else { count = 0; } } count = 0; // 左上到右下方向 for (int i = x - 4, j = y - 4; i <= x + 4 && j <= y + 4; i++, j++) { if (i < 0 || i >= board.length || j < 0 || j >= board[0].length) { continue; } if (board[i][j] == player) { count++; if (count == 5) { return true; } } else { count = 0; } } count = 0; // 右上到左下方向 for (int i = x - 4, j = y + 4; i <= x + 4 && j >= y - 4; i++, j--) { if (i < 0 || i >= board.length || j < 0 || j >= board[0].length) { continue; } if (board[i][j] == player) { count++; if (count == 5) { return true; } } else { count = 0; } } return false; } } ``` 这个程序使用二维数组来表示棋盘,每个位置用字符表示落子情况(‘+’表示空位,‘X’表示X方落子,‘O’表示O方落子)。程序实现了以下功能: 1. 初始化棋盘 2. 打印棋盘 3. 输入玩家落子位置,落子,并判断是否有效和是否获胜 4. 交换玩家 希望能够帮到您!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值