Help

Discussion in 'Java' started by Theone, Dec 18, 2009.

  1. Theone

    Theone New Member

    Joined:
    Dec 17, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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.
    :shy:
     
  2. Theone

    Theone New Member

    Joined:
    Dec 17, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    help any1?
     
  3. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    this will help you a lot

    Code:
    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;
            }
            printBoard();
        Scanner in=new Scanner(System.in);
        String move="";
            while(!move.equals("exit")){
                System.out.print("enter a move:");
                move=in.nextLine();
                if(isValidMove(move)) 
                    printBoard();
                    else 
                        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
            //first,second--->A,1
            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
            board[getRow(second)][getCol(first)]=0;
            board[getRow(secondW)][getCol(firstW)]=1;
            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++){
                System.out.print(""+(8-i)+"|");
                for(int j=0;j<8;j++)
                    System.out.print(board[i][j]+"|");
                    System.out.println(" ");
                    System.out.println("------------------");
            }
            System.out.print("  ");
            for(char j='A';j<='H';j++)
                    System.out.print(j+" ");
                    System.out.println(" ");
        }
    }
    
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice