Help with 2D Arrays of Objects - Static refs from non static method
This is a little project I'm just doing myself, and I'm stuck.
I'm basically trying to create a program, that, some time later, when I learn GUIs properly, will allow the user to play chess. For now, I'm just trying to set up the board and everything, but I'm having trouble with static (error I'm getting is non-static method cannot be referenced from a static context, or similiar), which we haven't really covered in class. Here's the relevant code:
Code:
public void printBoard()
{
for(int i=0; i<8; i++)
{
for(int j=0; j<8; j++)
{
if(Piece.Board[i][j] == null)
System.out.print(" ");
else
System.out.print(Piece.getAbbrevType(getPieceOnPosition(i,j)));
}
System.out.println("");
}
}
Basically I want to go through the array of Pieces (chess piece objects, taking a String type, int player, and then a position) and, if a piece doesn't occupy the space, then print out a space, otherwise, print out the first letter of the Type. From my limited understanding, the problem here is that I'm trying to make it generally take any Piece object, but because I have set it up as such, the Piece needs to be initialized before I can use it like this. Is this code salvagable? If so, can someone point me in the right direction? Thanks. Also, I have listed the rest of the code below, in case it's useful. Note that some of the methods under the pieces class may or may not be useful, I was experimenting with stuff and haven't gotten around to removing things that didn't. Edit: Could this be fixed by having get method below in red, getAbbrevType, ask for a row and column instead of a piece?
Code:
public Piece getPieceOnPosition(int row, int col){
return Piece.Board[row-1][col-1];
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package chess;
/**
*
* @author Rashmi
*/
public class Piece{
private String pieceType;
private int owner;
public static Piece[][] Board = new Piece[8][8];
static int[] boardPosition = new int[2];
public Piece(String type, int player, int row, int col){
pieceType = type;
owner = player;
boardPosition[0] = row;
boardPosition[1] = col;
}
public int getOwner(){
return owner;
}
public String getType(){
return pieceType;
}
[COLOR="Red"]public char getAbbrevType(Piece piece){
if(piece == null){
return ' ';
}
else
{
if(piece.getType().equalsIgnoreCase("Knight"))
{
return 'N';
}
else
{
return piece.getType().toUpperCase().charAt(0);
}
}
}[/COLOR]
public int getRow(){
return boardPosition[0];
}
public int getCol(){
return boardPosition[1];
}
public void placeOnBoard(Piece piece){
Board[boardPosition[0]-1][boardPosition[1]-1] = piece;
}
public void autoPlace(String type, int player, int row, int col){
Piece piece = new Piece(type, player, row, col);
placeOnBoard(piece);
}
public boolean onBoardTest(Piece piece){
if(Board[piece.getRow()][piece.getCol()] != null)
{
return true;
}
else return false;
}
public void move(int row, int col, Piece piece){
int newRow, newCol;
newRow = piece.getRow()+row;
newCol = piece.getCol()+col;
if(Board[newRow][newCol]==null && piece.onBoardTest(piece)==true){
Board[piece.getRow()][piece.getCol()] = null;
Board[newRow][newCol] = piece;
}
}
public void capture(int row, int col, Piece piece){
int newRow, newCol;
newRow = piece.getRow()+row;
newCol = piece.getCol()+col;
if(Board[newRow][newCol]!=null && piece.onBoardTest(piece)==true){
Board[piece.getRow()][piece.getCol()] = null;
Board[newRow][newCol] = piece;
}
}
}