package chess; import java.util.Hashtable; import java.util.Vector; import javax.crypto.NullCipher; /* * Main.java * * Created on February 10, 2007, 5:05 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ /** * * @author Stanislav Hromek */ public class Game { private Board board; // parsing error holder public static String errorMessage = new String(); // special case, when the pawn is taken on square, where is not standing static String previousPawnMove; // set starting values String pieceColor = "white"; // holds the current move number int move = 0; private Move[] moves; /** Creates a new instance of Main */ public Game() { board = new Board(); } /** * @param args the command line arguments */ public static void main(String[] args) { // instead of arg there should be lines from some window Game game = new Game(); // new game if(! game.startNewGame(args)) return; // play game.playGame(); } protected Move[] createMoves(String[] lines){ moves = new Move[lines.length]; // preprocess lines for syntactic errors for (int i = 0; i < lines.length; i++) { moves[i] = EntryLine.processLine(lines[i]); } return moves; } public boolean playGame(){ // now play moves, if any error, give message // this strange construction is, because we wanted to use playOneMove method, needed for button for ( ;move < moves.length; ) { // System.out.println("move = " + (move + 1) + " line = "+moves[move]); if(isEnd()) return true; if(! this.playOneMove()) return false; } return true; } protected boolean isEnd(){ // no more moves if(! isMoveOk()) return true; if(! isMoveOk2()) return true; return false; } public boolean startNewGame(String[] lines){ this.getBoard().setTheStartingPosition(); // white starts this.pieceColor = "white"; // in chess we start from one, but here we have array this.move = 0; // todo, if in future in line only move of eg. white, then change like // 11. Ke2-e3 // 11. ... Qe8-e4+ (fun, this move doesnt make sence, king can be taken) moves = createMoves(lines); // if moves is null, there was no lines to process, end if(moves == null) return false; else return true; } /** * if move null, we are done, no more moves */ public boolean isMoveOk(){ // if move null, we have an error from process line if(moves[move] == null){ // print error (there should be set message from process lines) return false; } else return true; } /** * if move piece is null, we are done, no more playing, means last move is white, black gave up */ public boolean isMoveOk2(){ // if move null, we have an error from process line if(moves[move].blackOrWhite(pieceColor) == null){ // print error (there should be set message from process lines) return false; } else return true; } public boolean playOneMove(){ // no more moves if(isEnd()) return true; // (i + 1) is move number if(! moves[move].playMove(getBoard(), move + 1, pieceColor) ){ // there was some problem System.out.println("move = " + (move + 1) + " line = "+moves[move] + " " + this.errorMessage); return false; } System.out.println("OK move = " + (move + 1) + " line = "+moves[move]); if(pieceColor.equals("white")){ pieceColor = "black"; } else{ pieceColor = "white"; move++; } return true; } public Board getBoard() { return board; } /** * parse whole bunch of games to vector of games, now not needed */ // public Vector parsePgnChessGames(String[] pgnGames){ // Vector result = new Vector(); // boolean newGame = false; // Vector game = new Vector(); // for (int i = 0; i < pgnGames.length; i++) { // if(pgnGames[i].startsWith("[") && ! newGame){ // if(game.size() > 0){ // result.add(this.parsePgnChessGame((String[])game.toArray(new String[0]))); // } // game = new Vector(); // newGame = true; // } else{ // newGame = false; // } // game.add(pgnGames[i]); // } // if(game.size() > 0){ // result.add(this.parsePgnChessGame((String[])game.toArray(new String[0]))); // } // // return result; // } public String[] parsePgnChessGame(String[] pgnGame){ Vector result = new Vector(); // pgnGames.split("\n"); if its one long string (it is in javascript textarea) String[] first = pgnGame; String game = ""; for (int i = 0; i < first.length; i++) { if(first[i].startsWith("[")){ result.add(first[i]); } else{ // eliminate empty lines if(first[i] != "" && first[i] != "\n"){ if(game.equals("")) game = first[i]; // else game = game + " " + first[i]; // } } } result.add(game); return (String[])result.toArray(new String[0]); } protected String[] divideGameToLines(String game) { // divide game string by space // if part starts with a number, it is next move if(game == null) return new String[0]; String[] gamePieces = game.split(" "); Vector result = new Vector(0); String tempMove = ""; for (int i = 0; i < gamePieces.length; i++) { System.out.println("gamePiece = '" + gamePieces[i]+"'"); // why do i need the first part of if ? (equals) if(!gamePieces[i].equals("") && Character.isDigit(gamePieces[i].charAt(0))){ if( tempMove.length()>0) result.add(tempMove); tempMove = gamePieces[i]; } else{ tempMove = tempMove + (" "); tempMove = tempMove + gamePieces[i]; } } // add last one too if(! tempMove.equals("")) result.add(tempMove); return (String[])result.toArray(new String[0]); } }