五子棋
一、摘要
- 目标:编程实现控制台版并支持两人对战的五子棋游戏
- 绘制棋盘
- 提示黑方(用1表示)和白方(用2表示)分别下棋(X,Y轴位置)并重新绘制棋盘。
- 每当一方下棋后判断是否获胜。
- 提示:采用二维数组来模拟棋盘。
二、代码实现(PS:挺简单的)
package com.neusoft;import java.util.Scanner;
public class WuZiQi {public static void main(String[] args) {int[][] map = new int[16][16];for (int i = 0; i < 16; i++) {map[0][i] = i;map[i][0] = i;}Scanner input = new Scanner(System.in);Show(map);boolean flag = true;int x;int y;while (true) {do {System.out.println("请黑方落子(格式:行号 列号):");x = input.nextInt();y = input.nextInt();} while (!IsEmpty(map, x, y));map[x][y] = 1;Show(map);if (!Win(map)) {break;}do {System.out.println("请白方落子(格式:行号 列号):");x = input.nextInt();y = input.nextInt();} while (!IsEmpty(map, x, y));map[x][y] = 2;Show(map);if (!Win(map)) {break;}}}public static void Show(int[][] map) {for (int i = 0; i < 16; i++) {for (int j = 0; j < 16; j++) {if ((i == 0 && j > 9) || (j == 0 && i > 9)) {System.out.print(map[i][j] + " ");} else {System.out.print(map[i][j] + " ");}}System.out.println();}}public static boolean Win(int[][] map) {for (int i = 1; i < 12; i++) {for (int j = 1; j < 12; j++) {if (map[i][j] == 1) {if ((map[i][j + 1] == 1 && map[i][j + 2] == 1 && map[i][j + 3] == 1 && map[i][j + 4] == 1) || (map[i + 1][j] == 1 && map[i + 2][j] == 1 && map[i + 3][j] == 1 && map[i + 4][j] == 1) || (map[i + 1][j + 1] == 1 && map[i + 2][j + 2] == 1 && map[i + 3][j + 3] == 1 && map[i + 4][j + 4] == 1)) {System.out.println("黑子获胜");return false;}} else if (map[i][j] == 2) {if ((map[i][j + 1] == 2 && map[i][j + 2] == 2 && map[i][j + 3] == 2 && map[i][j + 4] == 2) || (map[i + 1][j] == 2 && map[i + 2][j] == 2 && map[i + 3][j] == 2 && map[i + 4][j] == 2) || (map[i + 1][j + 1] == 2 && map[i + 2][j + 2] == 2 && map[i + 3][j + 3] == 2 && map[i + 4][j + 4] == 2)) {System.out.println("白子获胜");return false;}}}}return true;}public static boolean IsEmpty(int[][] map, int x, int y) {if (map[x][y] == 0) {return true;}System.out.println("此位置已有棋子,请重新输入");return false;}
}
三、输出