如何在Java中将0整数转换为空白?

时间:2019-02-21 07:22:30

标签: java project minesweeper

我是Java新手,目前正在从事一个小型初学者项目。我应该构建一个Minesweeper Java游戏。但是,我一直希望将游戏板上的整数“ 0”更改为空白。也许你们可以帮我解决这个问题。谢谢

public class Game {
    private Gameloop gameloop;
    boolean finish = false;
    boolean win = false;
    int turn=0;
    
    public Game(){
        gameloop = new Gameloop();
        Play(gameloop);
    }
    
    public void Play(Gameloop gameloop){
        do{
            turn++;
            System.out.println("There are a total of 10 mines in the mine field");
            gameloop.show();
            finish = gameloop.checkLocation();
            
            if(!finish){
                gameloop.numberOfMines();
                finish = gameloop.win();
            }
            
        }while(!finish);
        
        if(gameloop.win()){
            System.out.println("You Win!");
            gameloop.showMines();
        } else {
            System.out.println("Game Over!");
            gameloop.showMines();
        }
    }



    public class Gameloop {
    private int[][] mines;
    private char[][] boardgame;
    private int Line, Column;
    Random random = new Random();
    Scanner input = new Scanner(System.in);
    
    public Gameloop (){
        mines = new int[12][12];
        boardgame = new char[12][12];
        startMines();
        randomFillGrid();
        fillTips();
        startBoard();
        
    }
    
    public boolean win(){
        int count=0;
        for(int line = 1 ; line < 11 ; line++)
            for(int column = 1 ; column < 11 ; column++)
                if(boardgame[line][column]=='_')
                    count++;
        if(count == 12)
            return true;
        else
            return false;                
    }
    
    public void numberOfMines(){
        for(int i=-1 ; i<2 ; i++)
            for(int j=-1 ; j<2 ; j++)
                if( (mines[Line+i][Column+j] != -1) && (Line != 0 && Line != 11 && Column != 0 && Column != 11) )
                    boardgame[Line+i][Column+j]=Character.forDigit(mines[Line+i][Column+j], 12);
        
    }
    
    public int getPosition(int Line, int Column){
        return mines[Line][Column];
    }
    
    public boolean checkLocation(){
            
            do{
                System.out.println("Enter the row number then the column number"); 
                System.out.print(""); 
                Line = input.nextInt();
                System.out.print(""); 
                Column = input.nextInt();
                
                if( (boardgame[Line][Column] != '*') && ((Line < 11 && Line > 0) && (Column < 11 && Column > 0)))
                    System.out.println("Field already shown");
                
                if( Line < 1 || Line > 10 || Column < 1 || Column > 10)
                    System.out.println("Choose a number between 1 and 10");
                
            }while((Line < 1 || Line > 10 || Column < 1 || Column > 10) || (boardgame[Line][Column] != '.') );
            
            if(getPosition(Line, Column)== -1)
                return true;
            else
                return false;
            
    }
    
    public void show(){
        for(int Line = 10 ; Line > 0 ; Line--){
            System.out.print("       "+Line + " ");
            
            for(int Column = 1 ; Column < 11 ; Column++){
                    System.out.print("   "+ boardgame[Line][Column]);
            }
                
            System.out.println();
        }
            
        System.out.println("\n            1   2   3   4   5   6   7   8   9   10 ");
        
        
    }
    
    public void fillTips(){
        for(int line=1 ; line < 11 ; line++)
            for(int column=1 ; column < 11 ; column++){
                
                    for(int i=-1 ; i<=1 ; i++)
                        for(int j=-1 ; j<=1 ; j++)
                            if(mines[line][column] != -1)
                                if(mines[line+i][column+j] == -1)
                                    mines[line][column]++;
                
            }
            
    }
    
    public void showMines(){
        for(int i=1 ; i < 11; i++)
            for(int j=1 ; j < 11 ; j++)
                if(mines[i][j] == -1)
                    boardgame[i][j]='*';
        
        show();
    }
    
    public void startBoard(){
        for(int i=1 ; i<mines.length ; i++)
            for(int j=1 ; j<mines.length ; j++)
                boardgame[i][j]= '.';
    }
    
    public void startMines(){
        for(int i=0 ; i<mines.length ; i++)
            for(int j=0 ; j<mines.length ; j++)
                mines[i][j]= 0;
    }
    
    public void randomFillGrid(){
        boolean raffled;
        int Line, Column;
        for(int i=0 ; i<12 ; i++){
            
            do{
                Line = random.nextInt(10) + 1;
                Column = random.nextInt(10) + 1;
                
                if(mines[Line][Column] == -1)
                    raffled=true;
                else
                    raffled = false;
            }while(raffled);
            
            mines[Line][Column] = -1;
        }

    }
        }
    }

This is what my board looks like. I want to change the zeros for blank so it looks similar to the second board example.
   
      10    .   .   .   .   .   .   .   .   .   .
       9    .   .   .   .   .   .   .   .   .   .
       8    .   .   .   .   .   .   .   .   .   .
       7    .   .   .   .   .   .   .   .   .   .
       6    .   .   .   .   .   .   2   1   .   .
       5    .   .   .   .   .   .   1   1   1   .
       4    .   .   .   .   .   .   0   0   0   .
       3    .   .   .   .   .   .   .   .   .   .
       2    1   .   2   .   .   .   .   .   .   .
       1    1   1   1   .   .   .   .   .   .   .

            1   2   3   4   5   6   7   8   9   10 
  **Second example:**
      10    .   .   .   .   .   .   .   .   .   .
       9    .   .   .   .   .   .   .   .   .   .
       8    .   .   .   .   .   .   .   .   .   .
       7    .   .   .   .   .   .   .   .   .   .
       6    .   .   .   .   .   .   2   1   .   .
       5    .   .   .   .   .   .   1   1   1   .
       4    .   .   .   .   .   .           .   .
       3    .   .   .   .   .   .   .   .   .   .
       2    1   .   2   .   .   .   .   .   .   .
       1    1   1   1   .   .   .   .   .   .   .

            1   2   3   4   5   6   7   8   9   10 

5 个答案:

答案 0 :(得分:1)

代替:

private int[][] mines;

为什么不使用:

private char[][] mines;
mines[i][j]=' ';

答案 1 :(得分:0)

int值中没有空白。因此,最简单的方法是编写一个返回char或字符串的方法。它应该以char或string的形式返回数字本身(在转换时要小心,数字可能无法通过简单的转换正确解释,也许有一个.toChar之类的东西)或''(如果它是0

答案 2 :(得分:0)

更改

boardgame[Line+i][Column+j]=Character.forDigit(mines[Line+i][Column+j], 12);

进入

char ch = Character.forDigit(mines[Line+i][Column+j], 12); // 12 digits 0, 1, .. 9, a, b
boardgame[Line+i][Column+j] = ch == '0' ? ' ' : ch;

答案 3 :(得分:0)

到目前为止,我还不知道,唯一的方法是进行逻辑检查,如果该值为0,则显示空白。

如果需要用字符表示,可以使用它。

Character ch = Character.MIN_VALUE;

答案 4 :(得分:0)

System.out.print(" "+ (boardgame[Line][Column] == '0' ? "" : boardgame[Line][Column]));

show方法用上面的行替换打印行。您将获得所需的结果。