Java编程实现五子棋人人对战代码示例-创新互联
利用Java,在控制台操作下,编写的五子棋,作为复习二维数组,面向对象等基础知识。w表示白棋,b表示黑棋
中卫ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!import java.util.Scanner; public class MainMethod { public static char[][] c = new char[10][10]; public static void main(String[] args) { MainMethod mainMethod = new MainMethod(); mainMethod.init(); int i; boolean color; int[] point = new int[2]; Scanner scanner = new Scanner(System.in); for (i = 1; i < 100; i++) { mainMethod.draw();// 绘制棋盘 System.out.println("请输入坐标,范围为(0,0)到(9,9)"); int x = scanner.nextInt(); int y = scanner.nextInt(); if (i % 2 == 1) color = true; else color = false; point[0] = x; point[1] = y; if (mainMethod.paint(point, color) == false) {// System.out.println("下的位置不合理,请重新开始"); break; } if (mainMethod.win() == 1) { mainMethod.draw(); System.out.println("恭喜白棋赢!!"); break; } else if (mainMethod.win() == -1) { mainMethod.draw(); System.out.println("恭喜黑棋赢!!"); break; } } if (i == 100) System.out.println("平局"); } public char[][] init() {// 初始化棋盘 for (int i = 0; i < c.length; i++) { for (int j = 0; j < c.length; j++) { c[i][j] = '+'; } } return c; } public boolean paint(int[] point, boolean colr) {// 判断下的位置是否正确,若正确改变棋盘 boolean b = true; int x = point[0]; int y = point[1]; if (inBoard(point) == false || c[x][y] == 'w' || c[x][y] == 'b' || x < 0) { b = false; return b; } else if (colr == true) {// 白棋下 if (c[x][y] == '+') { c[x][y] = 'w'; } } else if (colr == false) {// 黑棋下 if (c[x][y] == '+') { c[x][y] = 'b'; } } return b; } public boolean inBoard(int[] point) {// 判断point是否在棋盘上 int x = point[0]; int y = point[1]; if (x < 0 || y < 0 || x >= c.length || y >= c.length) return false; else return true; } public int win() {// 判断输赢,1表示白的赢,-1黑的赢,0暂无输赢 int whoWin = 0; int[] point = new int[2]; outer: while (true) { for (int i = 0; i < c.length; i++) { for (int j = 0; j < c.length; j++) { if (c[i][j] == 'w') {// 遍历数组,找到的第一个白棋 point[0] = i; point[1] = j + 4; if (inBoard(point)) {// 如果存在向右数4个的棋盘 if (c[i][j + 1] == 'w' && c[i][j + 2] == 'w' && c[i][j + 3] == 'w' && c[i][j + 4] == 'w') { whoWin = 1; break outer; } } point[0] = i + 4; point[1] = j + 4; if (inBoard(point)) {// 如果存在向右,向下数4个的棋盘 if (c[i + 1][j + 1] == 'w' && c[i + 2][j + 2] == 'w' && c[i + 3][j + 3] == 'w' && c[i + 4][j + 4] == 'w') { whoWin = 1; break outer; } } point[0] = i + 4; point[1] = j; if (inBoard(point)) {// 如果存在向下数4个的棋盘 if (c[i + 1][j] == 'w' && c[i + 2][j] == 'w' && c[i + 3][j] == 'w' && c[i + 4][j] == 'w') { whoWin = 1; break outer; } } point[0] = i - 4; point[1] = j + 4; if (inBoard(point)) {// 如果存在向左,向下数4个的棋盘 if (c[i - 1][j + 1] == 'w' && c[i - 2][j + 2] == 'w' && c[i - 3][j + 3] == 'w' && c[i - 4][j + 4] == 'w') { whoWin = 1; break outer; } } } else if (c[i][j] == 'b') { point[0] = i; point[1] = j + 4; if (inBoard(point)) {// 如果存在向右数4个的棋盘 if (c[i][j + 1] == 'b' && c[i][j + 2] == 'b' && c[i][j + 3] == 'b' && c[i][j + 4] == 'b') { whoWin = -1; break outer; } } point[0] = i + 4; point[1] = j + 4; if (inBoard(point)) {// 如果存在向右,向下数4个的棋盘 if (c[i + 1][j + 1] == 'b' && c[i + 2][j + 2] == 'b' && c[i + 3][j + 3] == 'b' && c[i + 4][j + 4] == 'b') { whoWin = -1; break outer; } } point[0] = i + 4; point[1] = j; if (inBoard(point)) {// 如果存在向下数4个的棋盘 if (c[i + 1][j] == 'b' && c[i + 2][j] == 'b' && c[i + 3][j] == 'b' && c[i + 4][j] == 'b') { whoWin = -1; break outer; } } point[0] = i - 4; point[1] = j + 4; if (inBoard(point)) {// 如果存在向左,向下数4个的棋盘 if (c[i - 1][j + 1] == 'b' && c[i - 2][j + 2] == 'b' && c[i - 3][j + 3] == 'b' && c[i - 4][j + 4] == 'b') { whoWin = -1; break outer; } } } } } whoWin = 0; break outer; } return whoWin; } public void draw() {// 绘制棋盘 for (int i = 0; i < c.length; i++) { for (int j = 0; j < c.length; j++) { System.out.print(c[i][j] + " "); } System.out.println(); } } }
网站栏目:Java编程实现五子棋人人对战代码示例-创新互联
网页网址:http://hbruida.cn/article/csooeg.html