Java如何实现一个五子棋小游戏项目?

2026-05-26 03:531阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1544个文字,预计阅读时间需要7分钟。

Java如何实现一个五子棋小游戏项目?

本文以五子棋小游戏为例,分享了Java实现的具体代码。项目名称:五子棋小游戏

项目描述:本项目是一个简单的五子棋小游戏,实现了基本的棋盘显示、落子、胜负判断等功能。用户可以通过键盘输入坐标来下棋,系统会根据规则判断胜负。

可以修改的内容:

1.获胜棋子数:可以修改获胜所需的棋子数,默认为5个。

2.胜利优先级:可以调整先手和后手的胜利优先级,默认为先手优先。

3.代码实现:以下为代码实现部分。

代码实现:

javapublic class GobangGame { private int[][] board; private int winCount;

public GobangGame() { board=new int[15][15]; winCount=5; }

public void setWinCount(int count) { winCount=count; }

public boolean isWin(int x, int y, int player) { return checkLine(x, y, player) || checkLine(x, y, player, 1) || checkLine(x, y, player, 2) || checkLine(x, y, player, 3); }

private boolean checkLine(int x, int y, int player, int direction) { int count=1; int i=x; int j=y; while (i >=0 && i =0 && j =0 && i =0 && j =winCount; }

public void placeStone(int x, int y, int player) { if (x >=0 && x =0 && y <15 && board[x][y]==0) { board[x][y]=player; } }

public void printBoard() { for (int i=0; i <15; i++) { for (int j=0; j < 15; j++) { if (board[i][j]==0) { System.out.print( ); } else if (board[i][j]==1) { System.out.print(O); } else { System.out.print(X); } } System.out.println(); } }

public static void main(String[] args) { GobangGame game=new GobangGame(); game.printBoard(); // 示例:玩家1下棋 game.placeStone(7, 7, 1); game.printBoard(); // 示例:玩家2下棋 game.placeStone(8, 8, 2); game.printBoard(); // 判断胜负 if (game.isWin(7, 7, 1)) { System.out.println(玩家1获胜!); } else if (game.isWin(8, 8, 2)) { System.out.println(玩家2获胜!); } else { System.out.println(平局!); } }}

测试类:javapublic class GobangTest { public static void main(String[] args) { GobangGame game=new GobangGame(); game.setWinCount(5); game.placeStone(0, 0, 1); game.placeStone(1, 1, 2); game.placeStone(2, 2, 1); game.placeStone(3, 3, 2); game.placeStone(4, 4, 1); game.placeStone(5, 5, 2); game.placeStone(6, 6, 1); game.placeStone(7, 7, 2); game.placeStone(8, 8, 1); game.placeStone(9, 9, 2); game.placeStone(10, 10, 1); game.placeStone(11, 11, 2); game.placeStone(12, 12, 1); game.placeStone(13, 13, 2); game.placeStone(14, 14, 1); game.printBoard(); if (game.isWin(14, 14, 1)) { System.out.println(玩家1获胜!); } else { System.out.println(玩家2获胜!); } }}

本文实例为大家分享了Java实现五子棋小游戏的具体代码,供大家参考,具体内容如下

项目名称

五子棋小游戏

项目描述

Java如何实现一个五子棋小游戏项目?

可以改变获胜棋子数,率先连成棋数的人获胜

代码实现

测试类

public class Test { public static void main(String[] args) { FiveChess fiveChess = new FiveChess(); fiveChess.start(); } }

主类:实现主方法

public class FiveChess { private static final int CheckerSize = 10; private static final int successSize = 5; private Chess[][] chess; private int xPos; private int yPos; private boolean flag = true; private Scanner scanner = new Scanner(System.in); public FiveChess(){ chess = new Chess[CheckerSize][CheckerSize]; } private void initCheck(){ for(int i=0;i<CheckerSize;i++){ for(int j=0;j<CheckerSize;j++){ chess[i][j] = new Chess("十"); } } } private boolean judge(int xPos,int yPos){ //横向 if(yPos-1>=0 && chess[xPos][yPos].getValue().equals(chess[xPos][yPos-1].getValue()) || yPos+1<CheckerSize && chess[xPos][yPos].getValue().equals(chess[xPos][yPos+1].getValue())){ int count = 1; for (int i = 1; i < successSize; i++) { if (yPos - i >= 0 && chess[xPos][yPos - i].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } for (int i = 1; i < successSize; i++) { if (yPos + i < CheckerSize && chess[xPos][yPos + i].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } return count >= successSize ? true : false; } //纵向 if (xPos-1>=0 && chess[xPos][yPos].getValue().equals(chess[xPos-1][yPos].getValue()) || xPos+1<CheckerSize && chess[xPos][yPos].getValue().equals(chess[xPos+1][yPos].getValue())){ int count = 1; for (int i = 1; i < successSize; i++) { if (xPos- i >= 0 && chess[xPos-i][yPos].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } for (int i = 1; i < successSize; i++) { if (xPos + i < CheckerSize && chess[xPos+i][yPos].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } return count >= successSize ? true : false; } //正斜线 if (xPos-1>=0 && yPos-1>=0 && chess[xPos][yPos].getValue().equals(chess[xPos-1][yPos-1].getValue()) || xPos+1<CheckerSize && yPos+1<CheckerSize && chess[xPos][yPos].getValue().equals(chess[xPos+1][yPos+1].getValue())){ int count = 1; for (int i = 1; i < successSize; i++) { if (xPos - i >= 0 && yPos - i >= 0 && chess[xPos-i][yPos-i].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } for (int i = 1; i < successSize; i++) { if (xPos + i < CheckerSize && yPos + i < CheckerSize && chess[xPos+i][yPos+i].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } return count >= successSize ? true : false; } //反斜线 if (xPos-1>=0 && yPos+1<CheckerSize && chess[xPos][yPos].getValue().equals(chess[xPos-1][yPos+1].getValue()) || xPos+1<CheckerSize && yPos-1>=0 && chess[xPos][yPos].getValue().equals(chess[xPos+1][yPos-1].getValue())){ int count = 1; for (int i = 1; i < successSize; i++) { if (xPos - i >= 0 && yPos + i<CheckerSize && chess[xPos-i][yPos+i].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } for (int i = 1; i < successSize; i++) { if (xPos + i < CheckerSize && yPos - i >= 0 && chess[xPos+i][yPos-i].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } return count >= successSize ? true : false; } return false; } private void runChess(String run){ System.out.println("请输入"+run+"坐标:"); xPos = scanner.nextInt(); yPos = scanner.nextInt(); if(chess[xPos-1][yPos-1].getValue().equals("十")){ if(run.equals("黑棋")){ chess[xPos-1][yPos-1] = new Chess("●"); } else if(run.equals("白棋")){ chess[xPos-1][yPos-1] = new Chess("〇"); } for(int i=0;i<CheckerSize;i++){ for (int j=0;j<CheckerSize;j++){ System.out.print(chess[i][j].getValue()); } System.out.println(); } if(judge(xPos-1,yPos-1)){ flag = false; System.out.println(run+"获胜"); System.out.println("游戏结束,是否重玩"); String finish = scanner.next(); if (finish.equals("是")){ flag = true; start(); } else if (finish.equals("否")){ System.exit(0); } } }else { System.out.println("该处已存在棋子,请重新选择"); runChess(run); } } public void start(){ initCheck(); System.out.println("请选择先走方:黑棋or白棋"); String run = scanner.next(); for(int i=0;i<CheckerSize;i++){ for (int j=0;j<CheckerSize;j++){ System.out.print(chess[i][j].getValue()); } System.out.println(); } while (flag) { switch (run) { case "黑棋": runChess("黑棋"); run = "白棋"; break; case "白棋": runChess("白棋"); run = "黑棋"; break; default: } } } }

结点类

public class Chess { private String value; public Chess(String value){ this.value = value; } public String getValue() { return value; } }

更多有趣的经典小游戏实现专题,分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

JavaScript经典游戏 玩不停

javascript经典小游戏汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计1544个文字,预计阅读时间需要7分钟。

Java如何实现一个五子棋小游戏项目?

本文以五子棋小游戏为例,分享了Java实现的具体代码。项目名称:五子棋小游戏

项目描述:本项目是一个简单的五子棋小游戏,实现了基本的棋盘显示、落子、胜负判断等功能。用户可以通过键盘输入坐标来下棋,系统会根据规则判断胜负。

可以修改的内容:

1.获胜棋子数:可以修改获胜所需的棋子数,默认为5个。

2.胜利优先级:可以调整先手和后手的胜利优先级,默认为先手优先。

3.代码实现:以下为代码实现部分。

代码实现:

javapublic class GobangGame { private int[][] board; private int winCount;

public GobangGame() { board=new int[15][15]; winCount=5; }

public void setWinCount(int count) { winCount=count; }

public boolean isWin(int x, int y, int player) { return checkLine(x, y, player) || checkLine(x, y, player, 1) || checkLine(x, y, player, 2) || checkLine(x, y, player, 3); }

private boolean checkLine(int x, int y, int player, int direction) { int count=1; int i=x; int j=y; while (i >=0 && i =0 && j =0 && i =0 && j =winCount; }

public void placeStone(int x, int y, int player) { if (x >=0 && x =0 && y <15 && board[x][y]==0) { board[x][y]=player; } }

public void printBoard() { for (int i=0; i <15; i++) { for (int j=0; j < 15; j++) { if (board[i][j]==0) { System.out.print( ); } else if (board[i][j]==1) { System.out.print(O); } else { System.out.print(X); } } System.out.println(); } }

public static void main(String[] args) { GobangGame game=new GobangGame(); game.printBoard(); // 示例:玩家1下棋 game.placeStone(7, 7, 1); game.printBoard(); // 示例:玩家2下棋 game.placeStone(8, 8, 2); game.printBoard(); // 判断胜负 if (game.isWin(7, 7, 1)) { System.out.println(玩家1获胜!); } else if (game.isWin(8, 8, 2)) { System.out.println(玩家2获胜!); } else { System.out.println(平局!); } }}

测试类:javapublic class GobangTest { public static void main(String[] args) { GobangGame game=new GobangGame(); game.setWinCount(5); game.placeStone(0, 0, 1); game.placeStone(1, 1, 2); game.placeStone(2, 2, 1); game.placeStone(3, 3, 2); game.placeStone(4, 4, 1); game.placeStone(5, 5, 2); game.placeStone(6, 6, 1); game.placeStone(7, 7, 2); game.placeStone(8, 8, 1); game.placeStone(9, 9, 2); game.placeStone(10, 10, 1); game.placeStone(11, 11, 2); game.placeStone(12, 12, 1); game.placeStone(13, 13, 2); game.placeStone(14, 14, 1); game.printBoard(); if (game.isWin(14, 14, 1)) { System.out.println(玩家1获胜!); } else { System.out.println(玩家2获胜!); } }}

本文实例为大家分享了Java实现五子棋小游戏的具体代码,供大家参考,具体内容如下

项目名称

五子棋小游戏

项目描述

Java如何实现一个五子棋小游戏项目?

可以改变获胜棋子数,率先连成棋数的人获胜

代码实现

测试类

public class Test { public static void main(String[] args) { FiveChess fiveChess = new FiveChess(); fiveChess.start(); } }

主类:实现主方法

public class FiveChess { private static final int CheckerSize = 10; private static final int successSize = 5; private Chess[][] chess; private int xPos; private int yPos; private boolean flag = true; private Scanner scanner = new Scanner(System.in); public FiveChess(){ chess = new Chess[CheckerSize][CheckerSize]; } private void initCheck(){ for(int i=0;i<CheckerSize;i++){ for(int j=0;j<CheckerSize;j++){ chess[i][j] = new Chess("十"); } } } private boolean judge(int xPos,int yPos){ //横向 if(yPos-1>=0 && chess[xPos][yPos].getValue().equals(chess[xPos][yPos-1].getValue()) || yPos+1<CheckerSize && chess[xPos][yPos].getValue().equals(chess[xPos][yPos+1].getValue())){ int count = 1; for (int i = 1; i < successSize; i++) { if (yPos - i >= 0 && chess[xPos][yPos - i].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } for (int i = 1; i < successSize; i++) { if (yPos + i < CheckerSize && chess[xPos][yPos + i].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } return count >= successSize ? true : false; } //纵向 if (xPos-1>=0 && chess[xPos][yPos].getValue().equals(chess[xPos-1][yPos].getValue()) || xPos+1<CheckerSize && chess[xPos][yPos].getValue().equals(chess[xPos+1][yPos].getValue())){ int count = 1; for (int i = 1; i < successSize; i++) { if (xPos- i >= 0 && chess[xPos-i][yPos].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } for (int i = 1; i < successSize; i++) { if (xPos + i < CheckerSize && chess[xPos+i][yPos].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } return count >= successSize ? true : false; } //正斜线 if (xPos-1>=0 && yPos-1>=0 && chess[xPos][yPos].getValue().equals(chess[xPos-1][yPos-1].getValue()) || xPos+1<CheckerSize && yPos+1<CheckerSize && chess[xPos][yPos].getValue().equals(chess[xPos+1][yPos+1].getValue())){ int count = 1; for (int i = 1; i < successSize; i++) { if (xPos - i >= 0 && yPos - i >= 0 && chess[xPos-i][yPos-i].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } for (int i = 1; i < successSize; i++) { if (xPos + i < CheckerSize && yPos + i < CheckerSize && chess[xPos+i][yPos+i].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } return count >= successSize ? true : false; } //反斜线 if (xPos-1>=0 && yPos+1<CheckerSize && chess[xPos][yPos].getValue().equals(chess[xPos-1][yPos+1].getValue()) || xPos+1<CheckerSize && yPos-1>=0 && chess[xPos][yPos].getValue().equals(chess[xPos+1][yPos-1].getValue())){ int count = 1; for (int i = 1; i < successSize; i++) { if (xPos - i >= 0 && yPos + i<CheckerSize && chess[xPos-i][yPos+i].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } for (int i = 1; i < successSize; i++) { if (xPos + i < CheckerSize && yPos - i >= 0 && chess[xPos+i][yPos-i].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } return count >= successSize ? true : false; } return false; } private void runChess(String run){ System.out.println("请输入"+run+"坐标:"); xPos = scanner.nextInt(); yPos = scanner.nextInt(); if(chess[xPos-1][yPos-1].getValue().equals("十")){ if(run.equals("黑棋")){ chess[xPos-1][yPos-1] = new Chess("●"); } else if(run.equals("白棋")){ chess[xPos-1][yPos-1] = new Chess("〇"); } for(int i=0;i<CheckerSize;i++){ for (int j=0;j<CheckerSize;j++){ System.out.print(chess[i][j].getValue()); } System.out.println(); } if(judge(xPos-1,yPos-1)){ flag = false; System.out.println(run+"获胜"); System.out.println("游戏结束,是否重玩"); String finish = scanner.next(); if (finish.equals("是")){ flag = true; start(); } else if (finish.equals("否")){ System.exit(0); } } }else { System.out.println("该处已存在棋子,请重新选择"); runChess(run); } } public void start(){ initCheck(); System.out.println("请选择先走方:黑棋or白棋"); String run = scanner.next(); for(int i=0;i<CheckerSize;i++){ for (int j=0;j<CheckerSize;j++){ System.out.print(chess[i][j].getValue()); } System.out.println(); } while (flag) { switch (run) { case "黑棋": runChess("黑棋"); run = "白棋"; break; case "白棋": runChess("白棋"); run = "黑棋"; break; default: } } } }

结点类

public class Chess { private String value; public Chess(String value){ this.value = value; } public String getValue() { return value; } }

更多有趣的经典小游戏实现专题,分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

JavaScript经典游戏 玩不停

javascript经典小游戏汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。