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:
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(" "); } }