竞赛讨论区 > 扫雷游戏 用面向对象编程的方式实现
头像
雨后de水滴
发布于 2019-04-08 16:00
+ 关注

扫雷游戏 用面向对象编程的方式实现

import java.util.Scanner;


class GameMap {
  private int width, height;
  private int[][] map;

  GameMap(int height, int width) {
    this.height=height;
    this.width=width;
    map =new int[height][width];
  }

  public int getWidth() {
    return width;
  }

  public int getHeight() {
    return height;
  }
  /**
   * @return map value if out inside return -1
   */
  public int get(int x,int y) {
    if(x>=height||y>=width||x<0||y<0){
      return -1;
    }
    return map[x][y];
  }
  public void set(int x,int y,int value){
    map[x][y]=value;
  }
}

 class MineSweeper {
  private GameMap mapData;
  public final char MINE = '*';

  MineSweeper(GameMap map) {
    this.mapData = map;
  }
  public int findXY(int x, int y) {
    int count = 0;
    if (mapData.get(x, y) == 0) {
      return -1;
    }
    //right
    if (mapData.get(x + 1, y) == 0) {
      count++;
    }
    //down
    if (mapData.get(x, y + 1) == 0) {
      count++;
    }
    //left
    if (mapData.get(x - 1, y) == 0) {
      count++;
    }
    //up
    if (mapData.get(x, y - 1) == 0) {
      count++;
    }
    //right down
    if (mapData.get(x + 1, y + 1) == 0) {
      count++;
    }
    //left down
    if (mapData.get(x - 1, y + 1) == 0) {
      count++;
    }
    //left up
    if (mapData.get(x - 1, y - 1) == 0) {
      count++;
    }
    //right up
    if (mapData.get(x + 1, y - 1) == 0) {
      count++;
    }
    return count;
  }

  char[][] resultData() {
    char[][] dis = new char[mapData.getHeight()][mapData.getWidth()];
    for (int i = 0; i < dis.length; i++) {
      for (int j = 0; j < dis[i].length; j++) {
        int num = findXY(i, j);
        if (num != -1) {
          dis[i][j] = (char) (num + '0');
        } else {
          dis[i][j] = MINE;
        }
      }
    }
    return dis;
  }
}

class RunGame {
  public GameMap gameMap;
  private void setData(){
    Scanner scanner = new Scanner(System.in);
    int width, height;
    height = scanner.nextInt();
    width = scanner.nextInt();
    gameMap = new GameMap(height, width);
    for (int i = 0; i < height; i++) {
      String a=scanner.next();
      if(a.length()!=width){
        throw new IllegalArgumentException("input error");
      }
      for (int j = 0; j < width; j++) {
        char val=a.charAt(j);
        if (val=='*'){
          gameMap.set(i,j,0);
        }
        if (val=='?'){
          gameMap.set(i,j,1);
        }
      }
    }
  }
  public void result(){
    StringBuilder stringBuilder=new StringBuilder();
    MineSweeper mineSweeper = new MineSweeper(gameMap);
    char[][] data=mineSweeper.resultData();
    for (int i = 0; i < data.length; i++) {
      for (int j = 0; j < data[i].length; j++) {
        stringBuilder.append(data[i][j]);
      }
      stringBuilder.append('\n');
    }
    System.out.print(stringBuilder);
  }
  public void run() {
    setData();
    result();
  }
}

public class Main {
  public static void main(String[] args) {
    RunGame runGame = new RunGame();
    runGame.run();
  }
}



全部评论

(0) 回帖
加载中...
话题 回帖

本文相关内容

等你来战

查看全部

热门推荐