【算法】程序猿不写代码是不对的50

来源:互联网 发布:与该网络签署协议 编辑:程序博客网 时间:2024/06/14 19:32
package com.kingdz.algorithm.time201705;import java.util.ArrayList;import java.util.List;import java.util.Random;import java.util.Scanner;/** * 黑白棋 *  * @author kingdz *  */public class Algo08 {private static final String EMPTY = " ";private static final String BLACK = "X";private static final String WHITE = "O";private static String[][] chessboard;public static void main(String[] args) {chessboard = new String[8][8];init();BWPoint p;// = scanf();output();boolean over = false;while (true) {over = false;p = getBestPointAI(BLACK);System.out.println(p + "\t" + BLACK);if (p.getX() != -1) {putdown(p, BLACK);output();} else {over = true;}p = getBestPointAI(WHITE);System.out.println(p + "\t" + WHITE);if (p.getX() != -1) {putdown(p, WHITE);output();} else {if (over) {break;}}}judgeWinner();}/** * 判断获胜者 */private static void judgeWinner() {int x = 0;int y = 0;for (int i = 0, k = chessboard.length; i < k; i++) {for (int j = 0, l = chessboard[i].length; j < l; j++) {if (BLACK.equals(chessboard[i][j])) {x++;} else if (WHITE.equals(chessboard[i][j])) {y++;}}}System.out.println(BLACK + ":" + x + "\t" + WHITE + ":" + y);if (x > y) {System.out.println(BLACK + "\twin");} else if (x < y) {System.out.println(WHITE + "\twin");} else {System.out.println("NO WINNER");}}/** * 下棋 *  * @param point * @param color */private static void putdown(BWPoint point, String color) {chessboard[point.getX()][point.getY()] = color;// 获得所有的死棋List<BWPoint> list = getDead(point, color);for (BWPoint p : list) {// 杀掉所有的死棋chessboard[p.getX()][p.getY()] = color;}}/** * 计算获得最好的下棋点 *  * @param color * @return */private static BWPoint getBestPointAI(String color) {int max = -1;List<BWPoint> okList = new ArrayList<BWPoint>();okList.add(new BWPoint(-1, -1));for (int i = 0, k = chessboard.length; i < k; i++) {for (int j = 0, l = chessboard[i].length; j < l; j++) {if (EMPTY.equals(chessboard[i][j])) {int size = getDead(new BWPoint(i, j), color).size();if (size != 0) {if (size > max) {okList = new ArrayList<BWPoint>();okList.add(new BWPoint(i, j));max = size;} else if (size == max) {okList.add(new BWPoint(i, j));}}}}}// System.out.println("当前可行的最好走法共:"+okList.size());if (okList.size() == 1) {return okList.get(0);} else {// 当前的走法超过1种,随机选择一种走法Random r = new Random(System.currentTimeMillis());return okList.get(r.nextInt(okList.size()));}}/** * 获得某一个点可以吃到的棋子的列表 *  * @param color *  * @param bwPoint * @return */private static List<BWPoint> getDead(BWPoint bwp, String color) {List<BWPoint> list = new ArrayList<BWPoint>();// WBWPoint newp = getSameColor(bwp, color, 0, -1);list.addAll(getPieceBetween(bwp, newp, 0, -1));// NWnewp = getSameColor(bwp, color, -1, -1);list.addAll(getPieceBetween(bwp, newp, -1, -1));// Nnewp = getSameColor(bwp, color, -1, 0);list.addAll(getPieceBetween(bwp, newp, -1, 0));// NEnewp = getSameColor(bwp, color, -1, 1);list.addAll(getPieceBetween(bwp, newp, -1, 1));// Enewp = getSameColor(bwp, color, 0, 1);list.addAll(getPieceBetween(bwp, newp, 0, 1));// SEnewp = getSameColor(bwp, color, 1, 1);list.addAll(getPieceBetween(bwp, newp, 1, 1));// Snewp = getSameColor(bwp, color, 1, 0);list.addAll(getPieceBetween(bwp, newp, 1, 0));// SWnewp = getSameColor(bwp, color, 1, -1);list.addAll(getPieceBetween(bwp, newp, 1, -1));return list;}/** * 获得start到end之间的棋子列表 *  * @param start * @param end * @param i * @param j * @return */private static List<BWPoint> getPieceBetween(BWPoint start, BWPoint end, int i, int j) {List<BWPoint> list = new ArrayList<BWPoint>();int x = start.getX();int y = start.getY();if (x == -1 || y == -1 || end.getX() == -1 || end.getY() == -1) {return list;}while (true) {x = x + i;y = y + j;if (x < 0 || x >= chessboard.length) {break;} else if (y < 0 || y >= chessboard[x].length) {break;}if (x == end.getX() && y == end.getY()) {break;}list.add(new BWPoint(x, y));}return list;}/** * 获得某一个方向存在的第一个同色点坐标 *  * @param bwp * @param color * @param i * @param j * @return */private static BWPoint getSameColor(BWPoint bwp, String color, int i, int j) {int x = bwp.getX();int y = bwp.getY();while (true) {x = x + i;y = y + j;if (x < 0 || x >= chessboard.length) {return new BWPoint(-1, -1);} else if (y < 0 || y >= chessboard[x].length) {return new BWPoint(-1, -1);} else if (EMPTY.equals(chessboard[x][y])) {return new BWPoint(-1, -1);}if (chessboard[x][y].equals(color)) {return new BWPoint(x, y);}}}/** * 输入下棋坐标 *  * @return */public static BWPoint scanf() {while (true) {System.out.println("请输入下棋坐标");String str = new Scanner(System.in).nextLine();if (str.contains(",")) {String[] array = str.split(",");if (array.length != 2) {continue;}try {int x = Integer.parseInt(array[0]);int y = Integer.parseInt(array[1]);if (x < 0 || x > chessboard.length) {System.out.println("越界");throw new NumberFormatException();}if (y < 0 || y > chessboard.length) {System.out.println("越界");throw new NumberFormatException();}if (!EMPTY.equals(chessboard[x][y])) {System.out.println("只能在空白位置下棋");throw new NumberFormatException();}return new BWPoint(x, y);} catch (NumberFormatException e) {continue;}}}}/** * 初始化 */private static void init() {for (int i = 0, k = chessboard.length; i < k; i++) {for (int j = 0, l = chessboard[i].length; j < l; j++) {chessboard[i][j] = EMPTY;}}chessboard[3][3] = WHITE;chessboard[4][4] = WHITE;chessboard[3][4] = BLACK;chessboard[4][3] = BLACK;}/** * 棋盘的输出 */private static void output() {for (int i = 0, k = chessboard.length; i < k; i++) {for (int j = 0, l = chessboard[i].length; j < l; j++) {System.out.print(chessboard[i][j]);}System.out.println();}System.out.println("-----------");}}class BWPoint {private int x;private int y;public BWPoint(int x, int y) {super();this.x = x;this.y = y;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}@Overridepublic String toString() {return "[" + x + "," + y + "]";}}

0 0
原创粉丝点击