# Problem bei Schachprogramm



## Esswurm74 (3. Mai 2007)

Habe mit meinem Programm folgendes Problem und ich finde einfach den Fehler nicht:
Also ich mache einen Zug welcher falsch ist -> Wrong move   ok passt
Aber dann mache ich eine neue Eingabe und er springt mir dann zu "move violates the rules"

ich weiß nicht warum hier der Source Code der betreffenden Klassen:

package comaGame;

import comaGame.Board;
import comaGame.Player;

public abstract class Game {
  public Game()
  {
	  createBoard();
	  createPlayers();
  }

  public void run()
  {
  	Move m1, m2;
	  while(!isGameFinished()) {
		  int tmp;
		  do {
			  m1 = _player1.nextMove();
			  tmp = _board.doMove(m1);
			  _player2.opponentMove(m1);
		  }
		  while (tmp != 0);
		  do {
			  m2 = _player2.nextMove();
			  tmp = _board.doMove(m2);
			  _player1.opponentMove(m2);
		  }
		  while (tmp != 0);
	  }
  }

	public String toString() {
		return _board.toString();
	}

	public abstract int getWinner();

  protected abstract void createBoard();
  protected abstract void createPlayers();
  protected abstract boolean isGameFinished();

  protected Board _board;
  protected Player _player1, _player2;
}

package comaGame;

import java.io.*;
import comaGame.Square;
import comaGame.GameMove;

public class HumanPlayer extends FairPlayer //throws Exception
{


	public HumanPlayer(Board board, int color)
	{
	  super(board, color);
	}

	public Move nextMove() {
		// TODO check user input
		Square s, t;
		GameMove m;
    BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
    while(true) {
   		try {
        System.out.print("[".concat(this.toString().concat(", please move]\nstart:")));
		    s = new Square(bin.readLine());
      	System.out.print("target:");
		    t = new Square(bin.readLine());
		    m = new GameMove(s, t);
   	    if(!m.isWellFormed())
	      	System.out.println("your input is not correct:".concat(m.toString()));
		    else if(checkMove(m))
	      	System.out.println("your movement violates the rules");
		    else
		      return m;
		  }


		  catch(IOException e) 
		  {
		  	System.out.println(e);
	  	}
    }
	}

	public void opponentMove(Move move) {
		// TODO Auto-generated method stub

	}

}

package comaGame;

import comaGame.Square;

public abstract class Figure {
	public abstract boolean isValidMove(Move m);
	public abstract String toString();

	public Square _position;
	public int _color;
}

package co2_149;

import comaGame.*;
import java.lang.Character;
import java.lang.Math;

abstract class ChessFigure extends Figure 
{
	public boolean isValidMove(Move m) {
		return true;
	}

	public boolean isWayFree(Move m) {
		// TODO change bitchin FreeWay-WayFree name & implement
		return true;
	}

	protected abstract char getName();

	public String toString() {
		char c; 
		if(_color == Player.WHITE)
		  c = Character.toUpperCase(getName());
		else
		  c = getName();
		return new Character(c).toString();
	}
}

class King extends ChessFigure 
{
	public boolean isValidMove(Move m) {
		Square s = m.getStart();
		Square t = m.getTarget();
	  if(Math.abs(s.getRow() - t.getRow()) > 1 || Math.abs(s.getCol() - t.getCol()) > 1)
  		return false;
		return true;
	}

	public char getName() {
		return 'k';
	}
}

class Queen extends ChessFigure 
{
	public boolean isValidMove(Move m) {
		Square s = m.getStart();
		Square t = m.getTarget();
	  // Can move like runner OR tower
	  if((Math.abs(s.getRow() - t.getRow()) != Math.abs(s.getCol() - t.getCol())) &&
		  ((s.getRow() - t.getRow()) * (s.getCol() - t.getCol()) != 0))
	  	return false;
		// Must not jump
	  if(!isWayFree(m))
	  	return false;
		return true;
	}

	public char getName() {
		return 'q';
	}
}

class Horse extends ChessFigure 
{
	public boolean isValidMove(Move m) {
		Square s = m.getStart();
		Square t = m.getTarget();
		// 'Surface' of an L is 2
	  if(Math.abs((s.getRow() - t.getRow()) * (s.getCol() - t.getCol())) != 2)
  		return false;
		return true;
	}

	public char getName() {
		return 'h';
	}
}

class Runner extends ChessFigure 
{
	public boolean isValidMove(Move m) {
		Square s = m.getStart();
		Square t = m.getTarget();
	  if(Math.abs(s.getRow() - t.getRow()) != Math.abs(s.getCol() - t.getCol()))
  		return false;
	  // Must not jump
	  if(!isWayFree(m))
	  	return false;
		return true;
	}

	public char getName() {
		return 'r';
	}
}

class Tower extends ChessFigure 
{
	public boolean isValidMove(Move m) {
		Square s = m.getStart();
		Square t = m.getTarget();
		// 'Surface' of a line is 0
	  if((s.getRow() - t.getRow()) * (s.getCol() - t.getCol()) != 0)
  		return false;
	  // Must not jump
	  if(!isWayFree(m))
	  	return false;
		return true;
	}

	public char getName() {
		return 't';
	}
}

class Pawn extends ChessFigure 
{
	public boolean isValidMove(Move m) {
		 Square s = m.getStart();
		 Square t = m.getTarget();

		if (s.getCol() != t.getCol())
			return false;

		if(_color == Player.WHITE && (t.getRow()-s.getRow()==1))
			return true;

		if(_color == Player.BLACK && (s.getRow()-t.getRow()==1))
			return true;

		else return false;   

		/*if(!isWayFree(m))
		  	return false;
			return true;	*/
	}

	public char getName() {
		return 'p';
	}
}

ackage comaGame;

/**
 * does basic index checking via isWellFormed()
 * 
 */
public class GameMove implements Move {
  public GameMove()
  {

  }

	public GameMove(Square s, Square t)
  {
  	_start = s;
  	_target = t;
  }

  public boolean isWellFormed()
  {
  	// index check
  	if(_start.getRow() < 0 || _start.getRow() > 7 ||
  	  _target.getRow() < 0 || _target.getRow() > 7)
    	return false;
    // move must not rest
  	if(_start.equals(_target))
      	return false;
  	return true;
  }

  public String toString()
  {
  	return _start.toString().concat(" - ").concat(_target.toString());
  }

  public Square[] getCaptures() {
		return _captures;
	}

	public Square getStart() {
		return _start;
	}

	public Square getTarget() {
		return _target;
	}

	Square[] _captures;
	Square _start;
	Square _target;
}


----------



## Esswurm74 (4. Mai 2007)

Hallo hier noch eine wichtige Klasse die ich vergessen habe.

package comaGame;

import comaGame.Figure;
/**
 * klass to show the structure and the movement of the play
 * @author co2-149
 *
 */

public abstract class Board {

	public Board()
  {
	  _board = new Figure[8][8];
  }

  public int doMove(Move m)
  {
	  int r = m.getStart().getRow();
	  int c = m.getStart().getCol();
	  Figure f = _board[r][c];
	  _board[r][c] = null;

	  Square t = m.getTarget();
	  _board[t.getRow()][t.getCol()] = f;
	  f._position = t;

	  Square[] capt = m.getCaptures();
	  if(capt != null)
	    for(int i = 0; i < capt.length; i++)
	  	  _board[capt_.getRow()][capt.getCol()] = null;

	  if (f.isValidMove(m)) {
		  updateRepresentation();
		  return 0;
	  }
	  else {
		  System.out.println("Wrong move");
		  return 1;
	  }
  }

  public Figure getAt(int r, int c) {
	  return _board[r][c];
  }
  public String toString()
  {
	  String s = new String("   A B C D E F G H\n  -----------------\n");
	  for(int i = 7; i >= 0; i--) {
	  	s += String.valueOf(i + 1).concat(" |");
      for(int j = 0; j <= 7; j++) {
        if(_board[j] != null)
        	 s += _board[j].toString().concat("|");
        else
          s += " |";
      }
      s += "\n  -----------------\n";
	  }
	  return s;
  }

  protected void updateRepresentation()
  {
  	System.out.print(this);
  }

  protected Figure[][] _board;
}_


----------



## johanness (4. Mai 2007)

Offenbar liefert checkMove() false zurück, aber die Methode hast Du nicht mitgeliefert.

Wie soll man Dir da helfen?


----------



## Gast (21. Jun 2007)

Hatte eben Probleme mit String.concat(String). Stattdessen funktioniert String += String wie gewünscht.


----------

