Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Help (http://www.go4expert.com/forums/help-t20432/)

Theone 18Dec2009 17:54

Hello everybody,
I have decided to make a text based chess game whereby the player/user enters in the following notation when moving a player - e1 to e2.
What i am struggling with is getting the system to accept this input/command and virtually moving the piece into this new position.
As it is text based, no graphical representation is required so the user does not need to see the piece move, I have created a 2d array which is used for the chess board ([8] [8], but am not sure how to make the command work.
Would I use some sort of loop, for/while loop?

I am new to java, so any ideas or help will be much appreciated.
Thank you.

Theone 27Jan2010 22:05

Re: Help
help any1?

virxen 11Feb2010 04:20

Re: Help
this will help you a lot


import java.util.Scanner;

public class ChessMovement{
    private int board [][];//you will make it a char array and you 'll put instead of 1's, a char for each chess piece
    public ChessMovement(){
        board=new int[8][8];
        for (char i='A';i<='H';i++){
            for (char j='1';j<='2';j++) board[getRow(j)][getCol(i)]=1;
            for (char j='7';j<='8';j++) board[getRow(j)][getCol(i)]=1;
    Scanner in=new Scanner(System.in);
    String move="";
            System.out.print("enter a move:");
                    if (!move.equals("exit")) System.out.println("illegal move!!!");
    public static void main(String args[]){
        new ChessMovement();
    private boolean isValidMove(String move){
        move=move.trim().toUpperCase();//remove spaces from start and end of movement
        if (move.length()!=8) return false;//movement must have 8 characters
        String temp[]=move.split(" TO ");//movement must have a " TO " to separate the from To where
        if (temp.length!=2) return false;//movement must have both from and where statements
        String from=temp[0];
        String where=temp[1];
        if (from.length()!=2 || where.length()!=2) return false;//from and where must have length=2 ->(E1)
        char first=from.charAt(0);
        if (first<'A' || first>'H') return false;//from must start with a letter from A-H
        char second=from.charAt(1);
        if (second<'1' || second>'8') return false;//from must have a number after the letter from 1-8
        char firstW=where.charAt(0);
        if (firstW<'A' || firstW>'H') return false;//where must start with a letter from A-H
        char secondW=where.charAt(1);
        if (secondW<'1' || secondW>'8') return false;//where must have a number after the letter from 1-8
        //now we have the appropriate format we want!!!
        //lets fill the array
        if (board[getRow(second)][getCol(first)]==0) return false;//no piece there ,illegal move
        if (board[getRow(secondW)][getCol(firstW)]==1) return false;//there is a piece there ,illegal move
        return true;
    private int getCol(char letter){
            return ((int)letter-'A');       
    private int getRow(char number){
        return 7-((int)number-'1');
    private void printBoard(){
        for (int i=0;i<8;i++){
            for(int j=0;j<8;j++)
                System.out.println(" ");
        System.out.print("  ");
        for(char j='A';j<='H';j++)
                System.out.print(j+" ");
                System.out.println(" ");

All times are GMT +5.5. The time now is 02:23.