Results 1 to 4 of 4

Thread: array problems

  1. #1
    Evan is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default array problems

    im having a problem with using an array of object arrays.

    im trying to create an undo feature for a chess game and am using array of arrays to do this but when i set the new array with the postion of pieces then move a piece it automatically changes the other array

    can any one help!!

    Java Code:
    package chessGame;
    
    import java.awt.*;
    import javax.swing.*;
    import com.sun.corba.se.impl.orbutil.graph.Node;
    import java.awt.event.*;
    import java.io.IOException;
    import java.lang.reflect.Array;
    import java.util.Arrays;
    import java.util.*;
    
    
    
    public class board extends JFrame implements MouseListener, MouseMotionListener, ActionListener, WindowListener
    {
    	public static String location = "C:\\Users\\Squidgy\\workspace\\Chess Game\\images\\";
    	public static Graphics g1;
    	
    	JButton Home = new JButton("Home");
    	JPanel Info = new JPanel();
    	
    	Insets insetxy = getInsets();
    	Toolkit toolkit = getToolkit();
    	
    	int X,Y,X1,Y1,highlight1X=-10,highlight1Y=-10,highlight2X=-10,highlight2Y=-10;
    	Image highlight = toolkit.getImage(location + "highlight.png");
    	Color c;
    	int Xtemp = 99;
    	int Ytemp; 
    	boolean reset = true;
    	int turn = 0; //White = 0, Black = 1
    	int gameType = 0;
    	String details[] = null;
    	piece pieces[][] = new piece[8][8];
    	piece undo[][][] = new piece[30][8][8];
    	piece Temp [][] = new piece [8][8];
    	int undoIt = 0;
    	JButton Undo = new JButton("Undo");
    
    	String temp;
    	
    	client Client;
    	server Server;
    	
    	public board(String[] details, int gameType)
    	{
    		super("460126's Chess Game");	   
    	   
    		X1=20;
    		Y1=40;
    		setSize(800,720);
    		setVisible(true);
    		setResizable(false);
    		addMouseListener(this);
    		addMouseMotionListener(this);
    		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    		
    
    		this.setLayout(null);
    		this.add(Info);
    		Info.setLayout(new FlowLayout());
    		Info.add(Home);
    		Info.setBounds(560, 0, 240, 720);
    		Home.addActionListener(this);
    		this.addWindowListener(this);
    		this.gameType = gameType;
    		this.details = details;
    		Info.add(Undo);
    		Undo.addActionListener(this);	
    	}
    	
    	public void Board()
    	{
    		Color c;
    		int xbounds, ybounds;
    		for (int i = 0; i < 8; i++)
        	{
            	xbounds=i*70;
            	for (int j = 0; j < 8; j++)
                {
            		ybounds=j*70;
                
    	           if((i+j) % 2 == 0)
    	           {
    					c = new Color(238, 232, 170);
    				}
    				else 
    				{ 
    					c = new Color(139, 80, 30);
    				}
    	
    	           g1.setColor(c);
    	           g1.fillRect(X+xbounds, Y+ybounds, 70, 70);
    	           this.setCursor( new Cursor(Cursor.HAND_CURSOR ) ); 
                }
            }
    	}
    
    	public void paint(Graphics g )
        {
        	insetxy = getInsets();
    		Y = insetxy.top + insetxy.bottom;
    		X = insetxy.left + insetxy.right;
    		g1=g;
    		int i, j;
    		Board();
    			
    		if(reset == true)
    		{
    			pieces[0][6] = new pawn("black");
    			pieces[1][6] = new pawn("black");
    			pieces[2][6] = new pawn("black");
    			pieces[3][6] = new pawn("black");
    			pieces[4][6] = new pawn("black");
    			pieces[5][6] = new pawn("black");
    			pieces[6][6] = new pawn("black");
    			pieces[7][6] = new pawn("black");
    			pieces[0][1] = new pawn("white");
    			pieces[1][1] = new pawn("white");
    			pieces[2][1] = new pawn("white");
    			pieces[3][1] = new pawn("white");
    			pieces[4][1] = new pawn("white");
    			pieces[5][1] = new pawn("white");
    			pieces[6][1] = new pawn("white");
    			pieces[7][1] = new pawn("white");
    			
    			pieces[0][0] = new rook("white");
    			pieces[7][0] = new rook("white");
    			pieces[0][7] = new rook("black");
    			pieces[7][7] = new rook("black");
    
    			pieces[1][0] = new knight("white");
    			pieces[6][0] = new knight("white");
    			pieces[1][7] = new knight("black");
    			pieces[6][7] = new knight("black");
    
    			pieces[2][0] = new bishop("white");
    			pieces[5][0] = new bishop("white");
    			pieces[2][7] = new bishop("black");
    			pieces[5][7] = new bishop("black");	
    
    			pieces[3][0] = new queen("white");
    			pieces[3][7] = new queen("black");
    			
    			pieces[4][0] = new king("white");
    			pieces[4][7] = new king("black");
    			reset = false;
    		}
    		if(gameType == 2)
    		{
    			Server = new server(Integer.parseInt(details[0]));
    
    			Server.serverOpen();
    		}
    		
    		if(gameType == 3)
    		{
    			Client = new client(details[0], Integer.parseInt(details[1]));
    			try 
    			{
    				Client.clientOpen();
    			} 
    			catch (IOException e1) 
    			{
    				e1.printStackTrace();
    			}
    			
    			/*try 
    			{
    				temp = Client.waitForData();
    			} 
    			catch (IOException e) 
    			{
    				e.printStackTrace();
    			}*/
    		}
    		
    		for(i=0; i<8; i++)
    		{
    			for(j=0; j<8; j++)
    			{
    				if(pieces[i][j] != null)
    				{
    					Image img = toolkit.getImage(location + pieces[i][j].getColor()+pieces[i][j].getType() + ".png");
    					board.g1.drawImage(img, i*70 + X,  j*70 + Y, this);
    				}
    			}
    		}
    		
    		g1.drawImage(highlight, (highlight1X * 70) + X, (highlight1Y * 70) + Y, this);
    		g1.drawImage(highlight, (highlight2X * 70) + X, (highlight2Y * 70) + Y, this);
    	}
    
    	//public static void main(String[] args) 
    	//{
    	//	board chessBoard = new board();
    	//}
    
    	public void mouseClicked(MouseEvent e) 
    	{
    		int Xco;
      		int Yco;
      		//boolean validMove = false;
    
    		for(Xco = 0; Xco < 8; Xco++ )
      		{
      			for(Yco = 0; Yco < 8; Yco++)
      			{
      				if((e.getX() < ((Xco * 70) + X + 70)) && (e.getX() > ((Xco * 70) + X) ))
      				{
      					if((e.getY() < ((Yco * 70) + Y + 70)) && (e.getY() > ((Yco * 70) + Y) ))
      	  				{
      						if(Xtemp == 99)
      						{
      							if((pieces[Xco][Yco] != null) && (((pieces[Xco][Yco].getColor() == "white") && (turn == 0)) || 
      															  ((pieces[Xco][Yco].getColor() == "black") && (turn == 1))))
      							{
    	  	  						highlight1X = Xco;
    	  	  						highlight1Y = Yco; 
    	  	  						Xtemp = Xco;
    	  	  						Ytemp = Yco;
    	  	  						System.out.println(pieces[Xco][Yco].getColor() + " " + pieces[Xco][Yco].getType() + " selected");
      							}
      						}
      						else
      						{
      							if((Xtemp == Xco) && (Ytemp == Yco))
      							{
      								System.out.println("Piece De-Selected");
      							}
      							else
      							{
      								if(pieces[Xtemp][Ytemp].moveValidate(
      										new Point(Xtemp*70, Ytemp*70), 
      										new Point(Xco*70, Yco*70), 
      										((pieces[Xco][Yco] != null) ? true : false),
      										((pieces[Xco][Yco] != null) ? pieces[Xco][Yco].getColor() : null),
      										turn
      										) == true
      								)
    	  							{
      									undo[undoIt] = pieces; 
      									undoIt++;
      									System.out.println(Arrays.toString(undo[undoIt - 1][0])); //I am having problems here this bit returns [chessGame.rook@755c62bc, chessGame.pawn@45660d6, null, null, null, null, chessGame.pawn@42ef83d3, chessGame.rook@7ab7b3f9]
      									pieces[Xco][Yco] = pieces[Xtemp][Ytemp];
    	  								pieces[Xtemp][Ytemp] = null;
    	  								System.out.println(Arrays.toString(undo[undoIt - 1][0])); // this is after i have moved a piece [chessGame.rook@755c62bc, null, chessGame.pawn@45660d6, null, null, null, chessGame.pawn@42ef83d3, chessGame.rook@7ab7b3f9]
    	  								
    	  								turn = ((turn == 0) ? 1 : 0);
    	  							}
    	  							else
    	  							{
    	  								System.out.println("Invalid Move");
    	  								
    	  							}
      							}
      	  						highlight1X = -10;
      	  						highlight1Y = -10;
      	  						Xtemp = 99;
      						}
      						
      						repaint();
      	  				}
      				}
      			}
      		}	
    	}
    
    	public void mouseEntered(MouseEvent e) 
    	{
    		
    	}
    
    	public void mouseExited(MouseEvent e) 
    	{
    		
    	}
    
    	public void mousePressed(MouseEvent e) 
    	{
    		
    	}
    
    	public void mouseReleased(MouseEvent e) 
    	{
    	
    	}
    
    	public void mouseDragged(MouseEvent e) 
    	{
    	}
    
    	public void mouseMoved(MouseEvent e) 
    	{
    		int Xco;
      		int Yco;
    
    		for(Xco = 0; Xco < 8; Xco++ )
      		{
      			for(Yco = 0; Yco < 8; Yco++)
      			{
      				if((e.getX() < ((Xco * 70) + X + 70)) && (e.getX() > ((Xco * 70) + X) ))
      				{
      					if((e.getY() < ((Yco * 70) + Y + 70)) && (e.getY() > ((Yco * 70) + Y) ))
      	  				{
      						if((highlight2X != Xco) || (highlight2Y != Yco))
      						{
      							highlight2X = Xco;
      							highlight2Y = Yco;
      							
      							repaint();
      						}
      	  				}
      				}
      			}
      		}		
    	}
    
    	public void actionPerformed(ActionEvent e) 
    	{
    		
    		if(e.getActionCommand() == "Home")
    		{
    			processWindowEvent( new WindowEvent( this, WindowEvent.WINDOW_CLOSING) );
    		}
    		else if(e.getActionCommand() == "Undo")
    		{	
    			turn = ((turn == 0) ? 1 : 0);
    			System.out.println(Arrays.toString(undo[undoIt - 1][0]));
    			undoIt--;
    			pieces = undo[undoIt];
    			repaint();
    			
    		}
    	}
    
    	public void windowActivated(WindowEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	public void windowClosed(WindowEvent arg0) 
    	{		
    	}
    
    	public void windowClosing(WindowEvent arg0) 
    	{
    		int Quit = JOptionPane.showConfirmDialog(rootPane, "Are you sure you want to quit?", "Close", JOptionPane.YES_NO_OPTION);
    		if(Quit == 0)
    		{
    			guis.HomePageGui.setVisible(true);
    			this.setVisible(false);
    			try 
    			{
    				Client.clientClose();
    			} 
    			catch (IOException e1) 
    			{
    				e1.printStackTrace();
    			}
    		}
    	}
    
    	public void windowDeactivated(WindowEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	public void windowDeiconified(WindowEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	public void windowIconified(WindowEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	public void windowOpened(WindowEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    }

  2. #2
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    I started to make the code compiling, but I stopped. You don't follow the Java coding-conventions: Client is a variable, client a class, Board() a method, board( ... ) a constructor. Refactoring is too much trouble that way. Try to make a SSCCE (small self contained compiling example) that follows the conventions. That probably also means stripping references to Corba, client/server etc., a main() that works and dummy stubs for the pieces.
    (Often making a SSCCE is solving the problem in the process: good luck)
    Last edited by Jodokus; 05-05-2011 at 09:56 PM. Reason: spelling

  3. #3
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Instead of using a 2D array why don't you just have another variable that holds the previous step? Would be much simpler that way imo.

  4. #4
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    piece pieces[][] = new piece[8][8];
    It is not wrong what you're doing, but mostly the following syntax is used:
    Piece[][] pieces = new Piece[8][8];

Similar Threads

  1. Problems with array
    By Desmond in forum New To Java
    Replies: 1
    Last Post: 11-04-2010, 08:14 AM
  2. Array problems
    By braddy in forum New To Java
    Replies: 4
    Last Post: 10-09-2010, 01:18 PM
  3. Array problems!
    By Addez in forum New To Java
    Replies: 4
    Last Post: 08-29-2009, 06:56 PM
  4. Array problems..
    By smokeviolent in forum New To Java
    Replies: 1
    Last Post: 04-17-2009, 06:45 AM
  5. Array problems
    By Hosticus in forum New To Java
    Replies: 2
    Last Post: 01-18-2009, 02:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •