Minesweeper Test

TPKey

Mitglied
Hi
ich möchte für dieses Minesweeper Programm 2 Testmethoden implementieren um ein Minenfeld und ein Nicht-Minenfeld zu testen.
Nun frage ich mich, wie ich am besten die void Methoden testen kann

Java:
import java.util.InputMismatchException;
import java.lang.Math;
import java.util.Scanner;



public class Main {
  public static void main(String[] args) {
  
   
    Board b = new Board();
    b.display();
    Scanner input = new Scanner(System.in);
    int v;   
    int h;   
   
   
    while (!b.getGameState()) {
        System.out.print("Bitte geben sie zwei Koordinaten an!");
        boolean check = true;   
        do {
            try {
                v = input.nextInt();
                h = input.nextInt();
                b.uncover(v, h);
                b.display();
                check = false;
            }
            catch (Exception e) {
                System.out.println("Bitte geben Sie erneut zwei Koordinaten an!");
                input.next();
            }
        } while (check);
    }
    System.out.println("Game over");
   
  }
}

class Board {
    private int height = 4;       
    private int width = 4;       
    private int mines = 1;       
    private int uncovered = 0;   
    private int[][] values;       
    private boolean[][] board;   
    private boolean endGame;   
   
   
    public Board(){
        this.values = new int[this.height][this.width];
        this.board = new boolean[this.height][this.width];
        placeMines();
        setValues();
    }
   
   
    public Board(int h, int w, int m) {
        this.height = h;
        this.width = w;
        this.mines = m;
        this.values = new int[this.height][this.width];
        this.board = new boolean[this.height][this.width];
        placeMines();
        setValues();
    }
   

    public void placeMines() {
        int placed = 0;   
        int y = 0;       
        int x = 0;
       
       
        while (placed < this.mines) {
            y = (int)(Math.random() * this.height);
            x = (int)(Math.random() * this.width);
            if (values[0][0] != -1) {
                values[0][0] = -1;
                placed++;
            }
        }
    }
   
   
    public void setValues() {
        for (int i = 0; i < this.height; i++) {
            for (int j = 0; j < this.width; j++) {
                if (values[i][j] != -1) {       
                    values[i][j] = checkAdj(i, j);   
                }
            }
        }
    }
   
   
    public int checkAdj(int y, int x) {
        int sum = 0;   
        for (int a = -1; a <= 1; a++) {
            if (y + a >= 0 && y + a <= this.height - 1) {   
                for (int b = -1; b <= 1; b++) {
                   
                    if (x + b >= 0 && x + b <= this.width - 1 && (b != 0 || a != 0)) {
                        if (values[y + a][x + b] == -1) sum++;
                    }
                }
            }
        }
        return sum;
    }
   

    public void uncover(int y, int x) {
        if (values[y][x] == -1)    {
            endGame = true;
            board[y][x] = true;
        }
        else if (values[y][x] == 0) {   
            if (!board[y][x]) {
                board[y][x] = true;       
                uncovered++;   
            }
            for (int a = -1; a <= 1; a++) {
                if (y + a >= 0 && y + a <= this.height - 1) {   
                    for (int b = -1; b <= 1; b++) {
                       
                        if (x + b >= 0 && x + b <= this.width - 1 && (b != 0 || a != 0)) {
                            if (!board[y + a][x + b])
                                uncover(y + a, x + b);
                        }
                    }
                }
            }
        }
        else
            if (!board[y][x]) {
                board[y][x] = true;       
                uncovered++;   
            }
    }
   
   
    public boolean getGameState() {
       
        if (uncovered >= width * height - mines) {
            endGame = true;
            System.out.println("Du hast gewonnen!");
        }
       
        else if (endGame)
            System.out.println("Du hast verloren!");
        return this.endGame;
    }
   
   
    public void display() {
        for (int a = 0; a < this.height; a++) {
            for (int b = 0; b < this.width; b++) {
                if (board[a][b])
                    System.out.printf("%2d ", values[a][b]);
                else
                    System.out.print(" X ");
            }
            System.out.println();
        }
       
       
        System.out.println();
        System.out.println();
       
       
    }
}
 
Wieso testest du das Programm nicht einfach dort wo es am Ende angezeigt wird? Was benutzt du für die grafische Darstellung überhaupt?
 
Interessante Frage. Ich habe leider auch noch nicht so viel Erfahrung mit Unittesting, aber mal aus dem Bauch heraus:
Um z.B. placeMines() zu testen müsstest du Zugriff auf das Minenfeld haben, dann kannst du im Test kontrollieren, ob die korrekte Anzahl an Minen platziert wurden. Dazu könntest du:
1. Einen getter für das Minenfeld einfügen (finde ich aber nicht so schön, die Kapselung nur für Tests zu vermindern)
2. Das Feld protected machen und in einer abgeleiteten Testklasse den getter einfügen.
3. Reflection verwenden.

Meinungen von erfahrenen Unittestern würden mich auch interessieren.
Gruß,
temi
 

Zurück
Oben