okay, what i am trying to do is to build a chess game. now i am at the begining of the code and i got stuck.

i created 6 packages for each piece and one package for the chessboard.

now the idea is to create a new array each time the player makes a turn..

package il.co.ChessInterface;
import il.co.King.King;
import il.co.Pawn.Pawn;
import il.co.Queen.Queen;
import il.co.Rook.Rook;

import java.io.Console;


public class ChessInterface {

   public static void main(String[] arg){
	   ChessInterface chesspiece=new ChessInterface();
	   
	    Console console=System.console();
	   System.out.println("what piece do you want to move?");
	   String chessPiece=console.readLine();
	  
	   System.out.println("where do you want to move it to?(type column letter)");
	   String input=console.readLine();
	   char columnEnd=input.charAt(0);
	   System.out.println("where do you want to move it to?(type row number)");
	   int rowEnd=Integer.parseInt(console.readLine());
	   

	   
	    
	   
	    chesspiece [][] chessBoard;
	    chessBoard = new chesspiece [8][8];	   
	    chessBoard[1][0] pn1= new Pawn(a,2,columnEnd,rowEnd);
		chessBoard[1][1] pn2= new Pawn(b,2,columnEnd,rowEnd);
	    chessBoard[1][2] pn3= new Pawn(c,2,columnEnd,rowEnd);
	    chessBoard[1][3] pn4= new Pawn(d,2,columnEnd,rowEnd);
	    chessBoard[1][4] pn5= new Pawn(e,2,columnEnd,rowEnd);
	    chessBoard[1][5] pn6= new Pawn(f,2,columnEnd,rowEnd);
	    chessBoard[1][6] pn7= new Pawn(g,2,columnEnd,rowEnd);
	    chessBoard[1][7] pn8= new Pawn(h,2,columnEnd,rowEnd);
	
	    chessBoard[columnEnd][rowEnd]=new Pawn(columnStart,rowStart, columnEnd, rowEnd);
	   
	
	    
	    
			
	    
	    
	    

   }
}

in the second package , the pawn class..

package il.co.Pawn;

public class Pawn {
char columnEnd;
int rowEnd;
char columnStart;
int rowStart;


	public Pawn(char columnStart,int rowStart, char columnEnd, int rowEnd){
		this.columnEnd=columnEnd;
		this.rowEnd=rowStart;
		this.columnStart=rowStart2;
		this.rowStart=rowStart;
	}
	
	static boolean isValidPawnMove(char columnStart, int rowStart, char columnEnd, int rowEnd)
	{

what i am trying to do is to take info from the user and transfer a piece to another place in the array,
however, i dont know why and how to import informatio from one package to another..
and
i am not sure about how to transfer one object (chessPiece) into another place in teh array...

please help

Recommended Answers

All 14 Replies

Its not common to see one package per class like that. Normally you would have a package, say "chess", with all those classes in it. That way you can make best use of the different scopes (private/protected/public etc) to share the things you want to share between your classes while keeping them secure from other classes that are not in your package..
To "move" an object reference from one place to another in an array you need to
1. copy the obejct reference from the old position to the new position, then
2. Remove the reference in the original position (eg overwrite it with "null")

so lets say i do this
if the user types move pawn to a,4.

Pawn pawn1=new Pawn();

chessBoard[columnEnd][rowEnd]=pawn1;
chessBoard[a][4]=pawn1;

will it move the object to that place in the array..
consider also that that a,4 needs to be sent to another package, where Pawn class is..
will it get there just like that"? or do i need to add something?

Its not a new pawn, so the move is like:

board[1][4] = board[1][2]; // copies existing pawn to new position and...
board[1][2] = null; // removes it from old position

what an interesting syntax. i didnt know you code move the pieces like that..
so i should remove all my new pawns?

ChessInterface [][] chessBoard;
	   chessBoard = new ChessInterface [8][8];	
	   
	   int rowStart;
	   char columnStart;
	   
	   chessBoard[columnEnd][rowEnd]=new Pawn(columnStart,rowStart, columnEnd, rowEnd);
	   
	      
	    
	    
	    Pawn pn1= new Pawn(columnStart,rowStart,columnEnd,rowEnd);
	    Pawn pn2= new Pawn(columnStart,rowStart,columnEnd,rowEnd);
	    Pawn pn3= new Pawn(columnStart,rowStart,columnEnd,rowEnd);
	    Pawn pn4= new Pawn(columnStart,rowStart,columnEnd,rowEnd);
	    Pawn pn5= new Pawn(columnStart,rowStart,columnEnd,rowEnd);
	    Pawn pn6= new Pawn(columnStart,rowStart,columnEnd,rowEnd);
	    Pawn pn7= new Pawn(columnStart,rowStart,columnEnd,rowEnd);
	    Pawn pn8= new Pawn(columnStart,rowStart,columnEnd,rowEnd);
	     
	     
	     chessBoard[1][0]=pn1;
	 	 chessBoard[1][1]=pn2;
	     chessBoard[1][2]=pn3;
	     chessBoard[1][3]=pn4;
	     chessBoard[1][4]=pn5;
	     chessBoard[1][5]=pn6;
	     chessBoard[1][6]=pn7;
	     chessBoard[1][7]=pn8;
	    
	    
			
	    
	    
	    

   }
}

is it wrong with the way i define their positions at teh begining..

That looks ok to me, but i don't understand why you duplicate the position info - ie it's in the board, and its also in the Pawn object, maybe you don't neeed both. You need to think thru how you will do moves & takes, then see what info you need where to make that work.

i think i should make it this way:

if(Pawn.isValidPawnMove(columnStart,rowStart,columnEnd,rowEnd)){//send variables to the method in the Pawn class ,which is in a different package.
	        chessBoard[1][0]=chessBoard[columnEnd][rowEnd];// if the boolean method is true, then do the replacement, else write a message that the method is invalid
              else
               System.out.println("invalid move, try to put another move");
		   }

here is the boolean method in the Pawn class.

public static boolean isValidPawnMove(char columnStart, int rowStart, char columnEnd, int rowEnd)
	{
		int columnStartN, columnEndN;
		switch(columnStart)
		{
			case 'a':  columnStartN=1; break;
			case 'b':  columnStartN=2; break;
			case 'c':  columnStartN=3; break;
			case 'd':  columnStartN=4; break;
			case 'e':  columnStartN=5; break;
			case 'f':  columnStartN=6; break;
			case 'g':  columnStartN=7; break;
			case 'h':  columnStartN=8; break;
			default: return false;
		}
		switch(columnEnd)
		{
			case 'a':  columnEndN=1; break;
			case 'b':  columnEndN=2; break;
			case 'c':  columnEndN=3; break;
			case 'd':  columnEndN=4; break;
			case 'e':  columnEndN=5; break;
			case 'f':  columnEndN=6; break;
			case 'g':  columnEndN=7; break;
			case 'h':  columnEndN=8; break;
			default: return false;
		}
		int deltaX=columnEndN-columnStartN;
		deltaX=deltaX<0?-deltaX:deltaX;
		int deltaY=rowEnd-rowStart;
		deltaY=deltaY<0?-deltaY:deltaY;
		return deltaY==deltaX;

it simply checks if the move is correct.
(actually the method here checks if it is a bishops move, but i am going to correct this)


what do you think? could it work , if i called a method of each piece like that, and if it was true i would make the replacement?

I'm not going to get too critical here, so, yes, that seems a good direction. If you make all the classes Pawn, King etc subclasses of an abstract class ChessPiece that declares abstarct isValidMove(...) then they can all implement their versions of isValidMove(...) - this will save a lot of ifs and switches compared to having isValidPawnMove, iosValidQueenMove etc because you can use the same calling code for any piece.
You store positions as char, int in line with normal chess notation, but this is giving you a lot of switches to convert char to int for the columns. I would use [int][int] and just convert to a..h for output, but if you want to stay with your current scheme then make a little method to do the conversion, ie
public int columnCharToInt(char col) {...}
to save repeating the code. (There's a quicker way than a switch, but we can get to that later.)
Finally, I'll say once more (last time) it's very odd to have different packages for each of these classes.

Great idea, make the pawn abstract..
well thats true, there will be a need to do many switches. i thought to do 2 methods in each class.

one is isValidmove(...parameters..)
print()
System.out.println("|"+" King "+"|"+"|"+/n");
this second method should create a cube-like notation around the king. i am not sure about /n.. i think it would transer a line underneath.. what do you think? will all that work?


The problem is also to print the whole chess boards. it worries me that some places inside the multiple array are empty..
would i be able to print the whole array?

why is it odd to have many classes?

you want me to use inheritance?abstract?
but if you look at the bishop, it doesnt make pawns move..

and besides, i need to create a chess game, so i worry that i will have one long code to type..

the algorithm is quite cumbersome and long


Thats my whole code at the main method so far. Tomorrow i will try to add the two methods i was talking about..

package il.co.ChessInterface;
import il.co.King.King;
import il.co.Pawn.Pawn;
import il.co.Queen.Queen;
import il.co.Rook.Rook;

import java.io.Console;


public class ChessInterface {

   public static void main(String[] arg){
	   ChessInterface chesspiece=new ChessInterface();
	   
	    Console console=System.console();
	   System.out.println("what piece do you want to move?");
	   String chessPiece=console.readLine();
	  
	   System.out.println("where do you want to move it to?(type column letter)");
	   String input=console.readLine();
	   char columnEnd=input.charAt(0);
	   System.out.println("where do you want to move it to?(type row number)");
	   int rowEnd=Integer.parseInt(console.readLine());
	   int rowStart;
	   char columnStart;
	   
	   
	  
	   ChessInterface [][] chessBoard;
	   chessBoard = new ChessInterface [8][8];
	   
		   
		   if(Pawn.isValidPawnMove(columnStart,rowStart,columnEnd,rowEnd)){
			chessBoard[columnStart][rowStart]=chessBoard[columnEnd][rowEnd];
		   }
			else
			{
			System.out.println("move invalid, please try to enter another move");			
			}
		   
		   	
		   
	
	    
	    
	 
	    
	    
			
	    
	    //board[1][4] = board[1][2]; // copies existing pawn to new position and...
	     //board[1][2] = null; // removes it from old position 
	    

   }
}

Read again. I never said too many classes. I said too many packages.
The abstract superclass idea for pieces allows you to have an 8x8 array of Pieces, each element may be null or a specific Piece (eg Pawn, Bishop). You could just call isValidMove for any Piece, and Java will use the appropriate version of that method for each kind of Piece (Pawn, Bishop etc) automatically.
I don't understand your comment about Bishops making Pawns move!
Every Piece has its own version of isValidMove; Bishop allows any diagonal, Pawn allows 1 forward (etc). Think about it, and maybe do a quick refresher read on polymorphism.

System.out.println("where do you want to move it to?(type column letter)");
	   String input=console.readLine();
	   char columnEnd=input.charAt(0);
	   System.out.println("where do you want to move it to?(type row number)");
	   int rowEnd=Integer.parseInt(console.readLine());
	   int rowStart = 0;
	   char columnStart = 0;
	   
	   
	  
	   ChessInterface [][] chessBoard;
	   chessBoard = new ChessInterface [8][8];
	   
	  
		   if(Pawn.isValidPawnMove(columnStart,rowStart,columnEnd,rowEnd)){
		    chessBoard[columnStart][rowStart]=chessBoard[' '][0];   
			chessBoard[columnStart][rowStart]=chessBoard[columnEnd][rowEnd];
		    }
			else
			{
			System.out.println("move invalid, please try to enter another move");			
			}
		   rowStart=rowEnd;
		   columnStart=columnEnd;

could you check if this code is correct so far. i redefined rowStart and columnStart, so that they both be counted later on as the new starting position.

i tried to put abstract before pawn class in another package, but it doesnt save me work.

i could do it in one code, where Pawn is the abstract, but then i will have to create a heirarchy down along the whole page, and that would look long and hard to to follow for the eyes. so i decided to split the long code into sections

Hi NewOrder.
Please do not be offended, but it seems like I'm not expressing myself in any way that is useful to you. That happens sometimes. It;s my fault, not yours.
I think it's best if someone else helps you with from now on.
Good luck
J

i studied polymorphism and inheritance in class.. but i cant see how i could use them in a sunccinct form. i have to put so many algorithms inside, i perfer to make it long but correct , rather than codify everything in a clustered form.

i will post my code today or may be tomorrow when it is updated

I think it's best if someone else helps you with from now on.
Good luck
J

okay.

can someone tell me the condition to print a chessboard.. i have the formula here.. but i am not sure how to make it work..

package il.co.ChessInterface;
import il.co.Pawn.Pawn;
import il.co.Bishop.*;
import il.co.King.*;
import il.co.Queen.*;
import il.co.Rook.*;
import il.co.Knight.*;

import java.io.Console;


public class ChessInterface {

   public static void main(String[] arg){
	   	   	   
	   Console console=System.console();
	   String chessPiece="";
	   int rowStart = 0;
	   char columnStart = 0;
	   int i;
	   
	   for(;i<1;i++){
	   System.out.println("what piece do you want to move?");
	    chessPiece=console.readLine();
	   }
	   
	   
	   System.out.println("where do you want to move it to?(type column letter)");
	   String input=console.readLine();
	   char columnEnd=input.charAt(0);
	   System.out.println("where do you want to move it to?(type row number)");
	   int rowEnd=Integer.parseInt(console.readLine());
	   
	  
	      Object [][] chessBoard;
		  chessBoard = new Object [7][7];		  
		 
		  Pawn bpn1=new Pawn("BP1");
		  Pawn bpn2=new Pawn("BP2");
		  Pawn bpn3=new Pawn("BP3");
		  Pawn bpn4=new Pawn("BP4");
		  Pawn bpn5=new Pawn("BP5");
		  Pawn bpn6=new Pawn("BP6");
		  Pawn bpn7=new Pawn("BP7");
		  Pawn bpn8=new Pawn("BP8");
		  
		  
		  Rook bRook1=new Rook("BP8");
		  Knight bKnight1=new Knight("BP8");
		  Bishop bBishop1=new Bishop("BP8");
		  King bKing=new King("BP8");
		 
		  Queen bQueen=new Queen("BP8");
		  Bishop bBishop2=new Bishop("BP8");
		  Knight bKnight2=new Knight("BP8");
		  Rook bRook2=new Rook("BP8");
		  
		  
		  chessBoard [1][0]=bpn1;
		  chessBoard [1][1]=bpn2;
		  chessBoard [1][2]=bpn3;
		  chessBoard [1][3]=bpn4;
		  chessBoard [1][4]=bpn5;
		  chessBoard [1][5]=bpn6;
		  chessBoard [1][6]=bpn7;
		  chessBoard [1][7]=bpn8;
		  
		  chessBoard [0][0]=bRook1;
		  chessBoard [0][1]=bKnight1;
		  chessBoard [0][2]=bBishop1;
		  chessBoard [0][3]=bKing;
		  chessBoard [0][4]=bQueen;
		  chessBoard [0][5]=bBishop2;
		  chessBoard [0][6]=bKnight2;
		  chessBoard [0][7]=bRook2;
		 
		  System.out.println("____________________________________________");
		  
		  for(Object x:chessBoard){
			  if(chessBoard==null)
				  System.out.println("__ "+"|");
			  else
				  x.print();// problem is here. i want the array to be scanned. if there is a null in a place it prints a line and a perpindicular line. if it isnt, i want it to print the method in each object.. it tells me here that i have a mistake. anyone knows how to correct it?
				  
		  }
		  System.out.println("____________________________________________");
		  
		  ChessInterface[] Bpawn=new ChessInterface[7];
		  String[] Bpiece=new String[8];
		  Bpiece[0]="BP1";
		  Bpiece[1]="BP2";
		  Bpiece[2]="BP3";
		  Bpiece[3]="BP4";
		  Bpiece[4]="BP5";
		  Bpiece[5]="BP6";
		  Bpiece[6]="BP7";
		  Bpiece[7]="BP8";
		
		  for(;i<1;i++){
		  String chr="abcdefgh";
		  int n=0;
		  int sum=0;
		  for(int j=0;j<Bpiece.length;j++){
			  if( chessPiece==Bpiece[j]){
				  rowStart=j;
				  while(n<j)
				  sum+=1;
				  n++;
			  }
				  
		  }
		  columnStart=chr.charAt(sum);
	   
		  }
	   
	  
		   if(Pawn.isValidPawnMove(columnStart,rowStart,columnEnd,rowEnd)){
		    chessBoard[columnStart][rowStart]=null;   
			chessBoard[columnStart][rowStart]=chessBoard[columnEnd][rowEnd];
		    }
			else
			{
			System.out.println("move invalid, please try to enter another move");			
			}
		   rowStart=rowEnd;
		   columnStart=columnEnd;
		   	
		   System.out.println("what piece do you want to move?");
		   String chessPiece2=console.readLine();
		  	  	 
	    
	    			
	    //board[1][4] = board[1][2]; // copies existing pawn to new position and...
	     //board[1][2] = null; // removes it from old position 
	    

   }
}

the x.print() should go to the code/class in the other package. this package:

package il.co.Pawn;

public class Pawn {
char columnEnd;
int rowEnd;
char columnStart;
int rowStart;


        

String name;
public Pawn(String name ){
	this.name=name;
}
	
	public static boolean isValidPawnMove(char columnStart, int rowStart, char columnEnd, int rowEnd)
	{
		int columnStartN, columnEndN;
		switch(columnStart)
		{
			case 'a':  columnStartN=1; break;
			case 'b':  columnStartN=2; break;
			case 'c':  columnStartN=3; break;
			case 'd':  columnStartN=4; break;
			case 'e':  columnStartN=5; break;
			case 'f':  columnStartN=6; break;
			case 'g':  columnStartN=7; break;
			case 'h':  columnStartN=8; break;
			default: return false;
		}
		switch(columnEnd)
		{
			case 'a':  columnEndN=1; break;
			case 'b':  columnEndN=2; break;
			case 'c':  columnEndN=3; break;
			case 'd':  columnEndN=4; break;
			case 'e':  columnEndN=5; break;
			case 'f':  columnEndN=6; break;
			case 'g':  columnEndN=7; break;
			case 'h':  columnEndN=8; break;
			default: return false;
		}
		int deltaX=columnEndN-columnStartN;
		deltaX=deltaX<0?-deltaX:deltaX;
		int deltaY=rowEnd-rowStart;
		deltaY=deltaY<0?-deltaY:deltaY;
		return deltaY==deltaX;


	}

	

public void print(){	   // it should go and print that!
System.out.println(name+"|");
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.