简易五子棋的实现

做一个简易五子棋的步骤:

1、棋盘初始化

2、打印出棋盘

3、开始下棋(黑白交替)

4、判断坐标是否有效

5、每下一步棋,判断输赢

第一步,创建一个15*15的数组,对棋盘初始化

static char[][] qp = new char[15][15];
static static char[] num = {'⒈','⒉','⒊','⒋','⒌','⒍','⒎','⒏','⒐','⒑','⒒','⒓','⒔','⒕','⒖'};
public static void init(){
    for(int i = 0;i < qp.length;i++){
        for(int j = 0;j < qp[i].length;j++){
            qp[i][j] = '十';
            if(i == qp.length-1){            //判断是否为第14行
                qp[i][j] = num[j];           //如果为第14行,就给他的每一列赋num的每一个元素
            }
            if(j == qp[i].length-1){         //判断每一行是否为第14列
                qp[i][j] = num[i]            //如果为第14列,就给他的每一行赋num的每一个元素
            }
        }
   
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴回答您的问题。以下是一个简单的五子棋Java实现,共300行代码: ```java import java.util.Scanner; public class FiveInARow { private static final int ROWS = 15; private static final int COLUMNS = 15; private static final int EMPTY = 0; private static final int BLACK = 1; private static final int WHITE = 2; private static final int FIVE = 5; private static int[][] board = new int[ROWS][COLUMNS]; private static int currentPlayer = BLACK; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); boolean gameOver = false; while (!gameOver) { System.out.println("Player " + currentPlayer + "'s turn:"); int row = -1; int column = -1; boolean validInput = false; while (!validInput) { System.out.print("Row (1-15): "); row = scanner.nextInt() - 1; System.out.print("Column (1-15): "); column = scanner.nextInt() - 1; if (isValidPosition(row, column)) { validInput = true; } else { System.out.println("Invalid position. Please try again."); } } board[row][column] = currentPlayer; printBoard(); if (hasWon(currentPlayer, row, column)) { System.out.println("Player " + currentPlayer + " has won!"); gameOver = true; } else if (isBoardFull()) { System.out.println("It's a tie!"); gameOver = true; } else { currentPlayer = (currentPlayer == BLACK) ? WHITE : BLACK; } } } private static boolean isValidPosition(int row, int column) { if (row < 0 || row >= ROWS || column < 0 || column >= COLUMNS) { return false; } return board[row][column] == EMPTY; } private static void printBoard() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLUMNS; j++) { System.out.print(board[i][j] + " "); } System.out.println(); } } private static boolean hasWon(int player, int row, int column) { return (countConsecutivePieces(player, row, column, 1, 0) >= FIVE || countConsecutivePieces(player, row, column, 0, 1) >= FIVE || countConsecutivePieces(player, row, column, 1, -1) >= FIVE || countConsecutivePieces(player, row, column, 1, 1) >= FIVE); } private static int countConsecutivePieces(int player, int row, int column, int rowIncrement, int columnIncrement) { int count = 1; int r = row + rowIncrement; int c = column + columnIncrement; while (r >= 0 && r < ROWS && c >= 0 && c < COLUMNS && board[r][c] == player) { count++; r += rowIncrement; c += columnIncrement; } r = row - rowIncrement; c = column - columnIncrement; while (r >= 0 && r < ROWS && c >= 0 && c < COLUMNS && board[r][c] == player) { count++; r -= rowIncrement; c -= columnIncrement; } return count; } private static boolean isBoardFull() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLUMNS; j++) { if (board[i][j] == EMPTY) { return false; } } } return true; } } ``` 这个代码实现了一个简单的五子棋游戏,包括玩家交替下子、判断胜负、判断是否平局等功能。您可以运行这个程序,与计算机下五子棋

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值