2017-06-27 4 views
0

私はこのSchachChecker.javaファイルを持っています。このファイルは、基本的にチェス盤上の特定の構成を読み込みます。このチェスプログラムでこのブール値を見つける方法は?

私の目標は、Schachbrett.javaに関数(moeglicheZuege)を実装して、プレーヤーがこのターンに行うことができるすべての可能な移動を出力し、SchachCheckerクラスによって呼び出されることです。

私のアプローチは、すべての四角形を繰り返し、その上にどのピースがあり、どのプレイヤー(黒か白か)をチェックしてから、このピースがどのような動きをすることができるかを確認することです。

これまでのところ、私は反復を行い、どのピースが各四角に座っているのか把握できましたが、ピースの色を知ることはできません。

SchachChecker.java:

import java.util.Arrays; 
import java.util.Map; 
import java.util.HashMap; 
import java.util.Set; 
import java.io.BufferedReader; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.nio.charset.Charset; 

public class SchachChecker { 
    public static final Map<Character, Class<? extends Schachbrett.Feld>> pieceMap = 
     new HashMap<Character, Class<? extends Schachbrett.Feld>>() { 
     { 
      put('b', Schachbrett.Bauer.class); 
      put('s', Schachbrett.Springer.class); 
      put('l', Schachbrett.Laeufer.class); 
      put('t', Schachbrett.Turm.class); 
      put('d', Schachbrett.Dame.class); 
      put('k', Schachbrett.Koenig.class); 
      put(' ', Schachbrett.Feld.class); 
     } 
     }; 

    public static void main(String[] args) throws Exception { 
    if (args.length < 1) { 
     throw new RuntimeException("Parameter fehlt: Schachbrett"); 
    } else if (args.length < 2) { 
     throw new RuntimeException("Parameter fehlt: wer ist am Zug?"); 
    } 
    final boolean whiteToMove = "w".equals(args[1]); 
    if (!(whiteToMove || "s".equals(args[1]))) { 
     throw new RuntimeException("Unbekannte Farbe: '" + args[1] + "'"); 
    } 
    final BufferedReader reader = Files.newBufferedReader(
     Paths.get(args[0]), Charset.forName("UTF-8")); 
    final Schachbrett board = new Schachbrett(); 
    for (int lineIndex = 8; lineIndex >= 1; lineIndex--) { 
     // Lines are enumerated from bottom to top, that's why the first 
     // read line is the one with number 8. 
     final String line = reader.readLine(); 
     if (line == null) { 
     throw new RuntimeException("Zu wenig Zeilen!"); 
     } 
     for (char column = 'a'; column <= 'h'; column++) { 
     final char cur = 
      column - 'a' >= line.length() ? ' ' : line.charAt(column - 'a'); 
     final Class<? extends Schachbrett.Feld> pieceClass = 
      pieceMap.get(Character.toLowerCase(cur)); 
     if (pieceClass == null) { 
      throw new RuntimeException("Nicht erlaubtes Zeichen: '" + cur + "''"); 
     } 
     Schachbrett.Feld f; 
     if (cur == ' ') { 
      // leeres Feld ist nicht schwarz oder weiß 
      f = pieceClass.getConstructor().newInstance(); 
     } else { 
      f = pieceClass.getConstructor(
       Boolean.TYPE).newInstance(Character.isUpperCase(cur)); 
      System.out.println(f); 
     } 
     board.setFeld(lineIndex, column, f); 
     } 
    } 
    reader.close(); 
    Set<Schachbrett.Zug> zugSet = board.moeglicheZuege(whiteToMove); 
    Schachbrett.Zug[] zuege = zugSet.toArray(new Schachbrett.Zug[zugSet.size()]); 
    Arrays.sort(zuege); 
    for (final Schachbrett.Zug zug: zuege) { 
     System.out.println(zug.toString()); 
    } 
    } 
} 

Schachbrett.java:

import java.lang.Comparable; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 
import java.util.Set; 

public class Schachbrett { 
    /** 
    * --------------------------------------------------------------------------- 
    * Variable part: Classes for the pieces on the squares. 
    * --------------------------------------------------------------------------- 
    * 
    * Note: The constructors must be preserved. 
    */ 

    public static class Feld {} 

    /** 
    * A square, on which a piece is placed. 
    * The piece is either black or white. 
    */ 
    public static abstract class Figur extends Feld { 
    final boolean white; 
    protected Figur(boolean white) { this.white = white; } 

    public boolean isWhite() { return white; } 

    public char id() { 
     return getClass().getSimpleName().charAt(0); 
    } 
    } 

    public static final class Bauer extends Figur { 
    public Bauer(boolean white) { super(white); } 
    } 

    public static final class Laeufer extends Figur { 
    public Laeufer(boolean white) { super(white); } 
    } 

    public static final class Springer extends Figur { 
    public Springer(boolean white) { super(white); } 
    } 

    public static final class Turm extends Figur { 
    public Turm(boolean white) { super(white); } 
    } 

    public static final class Dame extends Figur { 
    public Dame(boolean white) { super(white); } 
    } 

    public static final class Koenig extends Figur { 
    public Koenig(boolean white) { super(white); } 
    } 

    /** 
    * --------------------------------------------------------------------------- 
    * End variable part: Classes for the pieces on the squares. 
    * --------------------------------------------------------------------------- 
    */ 

    /** 
    * Array, that holds all squares of the chessboard. Access results 
    * with getFeld(line, column), see below. 
    */ 
    private final Feld[] felder = new Feld[64]; 

    /** 
    * Initialization: all squares empty. 
    */ 
    { 
    for (int i = 0; i < 64; ++i) { 
     felder[i] = new Feld(); 
    } 
    } 

    private static int feldIndex(int line, char column) { 
    return (line - 1) * 8 + ((int) column - (int) 'a'); 
    } 

    public Feld getFeld(int line, char column) { 
    return felder[feldIndex(line, column)]; 
    } 

    public void setFeld(int line, char column, Feld value) { 
    felder[feldIndex(line, column)] = value; 
    } 

    /** 
    * A move of the piece FigurId from square (lineFrom, columnFrom) to square 
    * (lineTo, columnTo) 
    */ 
    public final class Zug implements Comparable<Zug> { 
    public int lineFrom, lineTo; 
    public char columnFrom, columnTo; 
    public char pieceId; 

    public Zug (int lineFrom, char columnFrom, int lineTo, char columnTo, 
       char pieceId) { 
     this.lineFrom = lineFrom; this.columnFrom = columnFrom; 
     this.lineTo = lineTo; this.columnTo = columnTo; 
     this.pieceId = pieceId; 
    } 

    @Override 
    public String toString() { 
     return new StringBuilder().append(pieceId).append(columnFrom).append(
      Integer.toString(lineFrom)).append('-').append(columnTo).append(
      Integer.toString(lineTo)).toString(); 
    } 

    // required methods for sorting during the output 
    @Override 
    public int hashCode() { 
     final int fromIndex = feldIndex(lineFrom, columnFrom); 
     final int toIndex = feldIndex(lineTo, columnTo); 
     int pieceIndex; 
     switch(Character.toUpperCase(this.pieceId)) { 
     case 'B': pieceIndex = 0; break; 
     case 'S': pieceIndex = 1; break; 
     case 'L': pieceIndex = 2; break; 
     case 'T': pieceIndex = 3; break; 
     case 'D': pieceIndex = 4; break; 
     case 'K': pieceIndex = 5; break; 
     default: throw new RuntimeException("Can never happen"); 
     } 
     if (Character.isUpperCase(pieceIndex)) { 
     pieceIndex = pieceIndex + 6; 
     } 
     // perfect hash (squareindex is at most 63 == 2^6 - 1) 
     return toIndex + (fromIndex << 6) + (pieceIndex << 12); 
    } 

    public int compareTo(final Zug other) { 
     return hashCode() - other.hashCode(); 
    } 
    } 

    public Set<Zug> moeglicheZuege(boolean whiteToMove) { 
    /* 
    * ------------------------------------------------------------------------- 
    * Variable part: algorithm 
    * ------------------------------------------------------------------------- 
    */ 
    return Collections.<Zug>emptySet(); 
    // End 
    } 
} 
+0

は、スタックオーバーフローへようこそ!ヘルプセンター、特に[良い質問をする方法](https://stackoverflow.com/help/how-to-ask)をご覧ください。 – Yahya

+0

なぜ人々はこの質問を下げているのですか?ヒットを終了してテクニックを実行し、代わりに役に立つものを追加してください。 – usefulBee

+1

FYI:変数名や例外メッセージには、コメントについて話すことなく、言語を混在させるべきではありません。 – AxelH

答えて

0

は、コンピュータ科学の宿題.... の作品のように見えますが、そのすべてが

public boolean isWhite() { return white; } 

あなたの質問を理解して、黒の表現を知りたいのであれば&白...白が真で黒が偽である

0

与えられた正方形にピースを見つけるgetFigur()はありません。

あなたの正方形はFeldあるので、これは、クラスが現在すべての部分のスーパークラスであること

/** 
* Array, that holds all squares of the chessboard. Access results 
* with getFeld(line, column), see below. 
*/ 
private final Feld[] felder = new Feld[64]; 

によって確認されました。

public static abstract class Figur extends Feld { 

ここでは、これらの2つのエンティティの関係について間違いを犯しています。

  • Piece
  • Piece
  • Squareによってニコラウスされ、 Squareありません。

これらのステートメントに一致するようにリレーションシップを更新する必要があります。以下のような空のクラスを持つ

public class Feld{ 
    private Figur figur; 

    public Figur getFigur(){ return figur;} 
} 

あなたFeldに問題があることを手掛かりは、一般的に持っていました。

そして作品がそこから正方形の

public class Figur { 
final boolean white; 
    protected Figur(boolean white) { this.white = white; } 

    public boolean isWhite() { return white; } 
    ... 
} 

を拡張してはならない、あなたのボードを反復処理することができ、そしてその上にfigur(またはしない)を取得します

関連する問題