Skip to content

Instantly share code, notes, and snippets.

@hotchkiss
Last active December 16, 2015 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hotchkiss/5499894 to your computer and use it in GitHub Desktop.
Save hotchkiss/5499894 to your computer and use it in GitHub Desktop.
Java compiler is throwing an error for not implementing an abstract class's method, even though the method is implemented.
/*
* Version:
* $Id$
*
* Revisions:
* $Log$
*/
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
/**
* ChessBoard extends Puzzle
* Model for a game of solitaire chess
* @author Collin Hotchkiss
*/
public class ChessBoard extends Puzzle{
private ArrayList<JButton> pieceButtons;
private ChessPiece[][] board;
private ChessPiece[][] start;
private int rowCount;
private int colCount;
public ChessBoard(String filename) throws FileNotFoundException, IOException{
filename = "test.txt";
BufferedReader fileReader = new BufferedReader(new FileReader(filename));
String l = fileReader.readLine();
String[] dimensions = l.split("\\s+");
this.rowCount = Integer.parseInt(dimensions[0]);
this.colCount = Integer.parseInt(dimensions[1]);
int count = 0;
while((l = fileReader.readLine()) != null){
for(int i = 0; i < l.length(); i++){
String current = l.substring(i, i);
if(current == "\\s+"){
continue;
} else{
if(current.equals("K")){
this.board[count][i] = new King(count, i);
} else if(current.equals("N")){
this.board[count][i] = new Knight(count, i);
} else if(current.equals("B")){
this.board[count][i] = new Bishop(count, i);
} else if(current.equals("P")){
this.board[count][i] = new Pawn(count, i);
} else if(current.equals("R")){
this.board[count][i] = new Rook(count, i);
} else if(current.equals("Q")){
this.board[count][i] = new Queen(count, i);
}
else this.board[count][i] = null;
System.out.println(this.board[count][i]);
}
}
count++;
System.out.println("\n");
}
}
public ArrayList<ChessPiece> getPieces(){
ArrayList<ChessPiece> pieces = new ArrayList<ChessPiece>();
for(int i = 0; i < rowCount; i++){
for(int j = 0; j < colCount; j++){
if(board[i][j] != null){
pieces.add(board[i][j]);
}
}
}
return pieces;
}
public boolean isSolved(){
return (this.getPieces().size() == 1);
}
public ArrayList<Integer> getNeighbors(Integer config){
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(config);
return temp;
}
public Integer getGoal(){
return 1;
}
}
ChessBoard.java:19: ChessBoard is not abstract and does not override abstract method getNeighbors(java.lang.Object) in Puzzle
public class ChessBoard extends Puzzle{
^
1 error
/*
* Puzzle.java
*
* Version:
* $Id: Puzzle.java,v 1.2 2013/04/19 03:02:56 cah3490 Exp $
* Revisions:
* $Log: Puzzle.java,v $
* Revision 1.2 2013/04/19 03:02:56 cah3490
* Done and commented. Ready for initial submission
*
* Revision 1.1 2013/04/19 02:53:05 cah3490
* Done and needing comments
*
* Revision 1.2 2013/04/04 22:38:11 cxh9923
* Added comments.
*
*/
import java.util.*;
/**
* Abstract to represent a Puzzle.
* @author Collin Hotchkiss
* @author Chance Henderson
*/
public abstract class Puzzle<E> {
/**
* Determines if the candidate is the solution. Is overridden
* @param candidate the possible solution
* @return whether or not it's the solution
*/
public abstract boolean isSolved(E candidate);
/**
* Returns the starting config
* @return the starting config
*/
public abstract E getStart();
/**
* Returns the neighbors of the current config
* @param config the current setup
* @return a list of next steps
*/
public abstract ArrayList<E> getNeighbors(E config);
/**
* returns the goal
* @return the goal
*/
public abstract E getGoal();
/**
* Uses BFS to solve the problem
* @return a history of configurations, the shortest path
*/
public ArrayList<E> solve(){ //then here
ArrayList<ArrayList<E>> myQueue = new ArrayList<ArrayList<E>>();
// Declare visited
HashMap<E, E> predecessors = new HashMap<E, E>();
ArrayList<E> element = new ArrayList<E>();
element.add(this.getStart());
myQueue.add(element);
boolean found = (this.isSolved(this.getStart()));
//Current is what's being returned, it's the current history
ArrayList<E> current = new ArrayList<E>();
predecessors.put(this.getStart(), this.getStart());
while(myQueue.size() > 0 && !found){
//Somewhere in here we need to do stuff with visited
current = myQueue.remove(0);
ArrayList<E> neighbors = this.getNeighbors(current.get(current.size() - 1));
for(int i = 0; i < neighbors.size(); i++){
ArrayList<E> next = new ArrayList<E>();
if(!predecessors.containsKey(neighbors.get(i))){
for(int j = 0; j < current.size(); j++){
next.add(current.get(j));
}
next.add(neighbors.get(i));
// If valid, add to predecessors
predecessors.put(neighbors.get(i), current.get(current.size()-1));
if(this.isSolved(next.get(next.size() - 1))){
current = next;
found = true;
break;
} else{
myQueue.add(next);
}
}
}
}
if(found){
return current;
} else{
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment