Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

HELLO. PLEASE TELL ME AND FIX THE ISSUE WITH MY CODE. THERE IS A NULL POINTER EXCEPTION. Below I have added all the code files.

HELLO. PLEASE TELL ME AND FIX THE ISSUE WITH MY CODE. THERE IS A NULL POINTER EXCEPTION. Below I have added all the code files.

import java.lang.reflect.Array;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Random;

import java.util.Scanner;

public class GameBoard {

private char[][] gameBoard;

private boolean gameOngoing = true;

private HashedDictionary dictionary;

/**

* This is the constructor for the GameBoard class

*/

public GameBoard() {

gameBoard = new char[3][3];

dictionary = new HashedDictionary();

for (int row = 0; row < gameBoard.length; row++) {

Arrays.fill(gameBoard[row], ' ');

}

} // end of constructor

/**

* This method will display the gameBoard to the screen

*/

public void displayBoard() {

for (int row = 0; row < gameBoard.length; row++) {

for (int col = 0; col < gameBoard[0].length; col++) {

System.out.print("\t" + gameBoard[row][col] + "\t");

if (col == 0 || col == 1) {

System.out.print("|");

}

if ((row == 0 && col == 2) || (row == 1 && col == 2)) {

System.out.print(" ------------------------------------------------- ");

}

}

}

System.out.println();

System.out.println();

System.out.println();

} // end displayBoard method

/**

* This method will validate if a players move is allowed and return true if the

* move was completed

*/

public boolean makeMove(char player, int row, int col) {

if (row >= 0 && row <= 2 && col >= 0 && col <= 2) {

if (gameBoard[row][col] != ' ') {

return false;

} else {

gameBoard[row][col] = player;

return true;

}

} else {

return false;

}

} // end of makeMove method

/**

* Will return true if the game is still active

*/

public boolean gameActive() {

return gameOngoing;

} // end of gameActive method

/**

* This method will ask the user to pick a row and column, validate the inputs

* and call the method makeMove()

*/

public void askPlayer(char player) {

Scanner sc = new Scanner(System.in);

int row, col;

do {

System.out.printf("Player %s please enter a row (1-3): ", player);

row = sc.nextInt();

System.out.printf("Player %s Please enter a column (1-3): ", player);

col = sc.nextInt();

} while (notValid(row, col));

makeMove(player, row - 1, col - 1);

} // end of askPlayer method

// Getters for gameBoard and gameOngoing

public char[][] getGameBoard() {

return gameBoard;

}

/**

* This method will validate if the row and column are between 1-3 and if the

* position is currently empty

*/

public boolean notValid(int row, int col) {

if (row > 3 || row < 1 || col > 3 || col < 1 || !isEmpty(row, col)) {

return true;

} else {

return false;

}

} // end notValid method

/**

* This method will check if a position is empty

*

* @return true if the position is empty, false otherwise

*/

public boolean isEmpty(int row, int col) {

if (gameBoard[row - 1][col - 1] == ' ') {

return true;

} else {

System.out.println("That postion is taken.");

return false;

}

}// end isEmpty method

/**

* This method will check to see if there are 3 X's or O's in a row

*

* @return true if there is a winner, false otherwise

*/

public boolean checkForWinner() {

for (int row = 0; row < gameBoard.length; row++) {

// checking horizontal

if (gameBoard[row][0] == gameBoard[row][1] && gameBoard[row][1] == gameBoard[row][2]

&& gameBoard[row][0] != ' ') {

System.out.println("The winner is " + gameBoard[row][0]);

gameOngoing = false;

}

}

for (int col = 0; col < gameBoard[0].length; col++) {

// checking vertical

if (gameBoard[0][col] == gameBoard[1][col] && gameBoard[1][col] == gameBoard[2][col]

&& gameBoard[0][col] != ' ') {

System.out.println("The winner is " + gameBoard[0][col]);

gameOngoing = false;

}

}

if (gameBoard[0][0] == gameBoard[1][1] && gameBoard[1][1] == gameBoard[2][2] && gameBoard[0][0] != ' ') {

System.out.println("The winner is " + gameBoard[0][0]);

gameOngoing = false;

}

if (gameBoard[2][0] == gameBoard[1][1] && gameBoard[1][1] == gameBoard[0][2] && gameBoard[0][2] != ' ') {

System.out.println("The winner is " + gameBoard[2][0]);

gameOngoing = false;

}

return false;

} // end checkForWinner method

/**

* Checks status of game

*

* @return if the game continues

*/

public boolean isGameOngoing() {

return gameOngoing;

}

/**

* Turns the gameBoard into a string

*/

@Override

public String toString() {

String str = "";

for (int row = 0; row < gameBoard.length; row++) {

for (int col = 0; col < gameBoard[0].length; col++) {

str = str + gameBoard[row][col];

}

}

return str;

} // end toString method

/**

* Converts board of char[][] to a string

*/

public String convertBoardToString() {

String strBoard = "";

for (int row = 0; row < gameBoard.length; row++) {

for (int col = 0; col < gameBoard[0].length; col++) {

strBoard = strBoard + gameBoard[row][col];

}

}

strBoard = strBoard.replace(' ', '-');

System.out.println(strBoard);

return strBoard;

}

/**

* This method converts coordinates to a single digit

*/

public int convertCoordinateToSingle(int row, int col) {

if (row == 1 && col == 1) {

return 0;

}

if (row == 1 && col == 2) {

return 1;

}

if (row == 1 && col == 3) {

return 2;

}

if (row == 2 && col == 1) {

return 3;

}

if (row == 2 && col == 2) {

return 4;

}

if (row == 2 && col == 3) {

return 5;

}

if (row == 3 && col == 1) {

return 6;

}

if (row == 3 && col == 2) {

return 7;

}

if (row == 3 && col == 3) {

return 8;

}

return 0;

} // end convertCoordinatesToSingle method

/**

* This method converts coordinates to a row and col

*/

public int[] convertCoordinateToDouble(int dig) {

int[] rowcol = new int[2];

if (dig == 0) {

rowcol[0] = 1;

rowcol[1] = 1;

return rowcol;

}

if (dig == 1) {

rowcol[0] = 1;

rowcol[1] = 2;

return rowcol;

}

if (dig == 2) {

rowcol[0] = 1;

rowcol[1] = 3;

return rowcol;

}

if (dig == 3) {

rowcol[0] = 2;

rowcol[1] = 1;

return rowcol;

}

if (dig == 4) {

rowcol[0] = 2;

rowcol[1] = 2;

return rowcol;

}

if (dig == 5) {

rowcol[0] = 2;

rowcol[1] = 3;

return rowcol;

}

if (dig == 6) {

rowcol[0] = 3;

rowcol[1] = 1;

return rowcol;

}

if (dig == 7) {

rowcol[0] = 3;

rowcol[1] = 2;

return rowcol;

}

if (dig == 8) {

rowcol[0] = 3;

rowcol[1] = 3;

return rowcol;

}

return null;

} // end convertCoordinatesToDouble method

public HashedDictionary getDictionary() {

return dictionary;

}

/**

* Generates all possible TicTacToe boards that are valid

*/

public void generateBoard(String board, int emptySpaces, char currentPlayer) {

if (emptySpaces == 0) {

return;

}

for (int i = 0; i < 9; i++) {

if (board.charAt(i) == '-') {

String newBoard = board.substring(0, i) + currentPlayer + board.substring(i + 1);

// System.out.println(newBoard);

// System.out.println(dictionary.getValue(convertStringToArrayRep(newBoard)));

// System.out.println(getBestMove(newBoard));

// System.out.println(convertStringToArrayRep(newBoard));

ArrayList ch = convertStringToArrayRep(newBoard);

BoardPosition bp = new BoardPosition(ch);

if(bp != null) {

dictionary.add(bp, getBestMove(newBoard));

}

generateBoard(newBoard, emptySpaces - 1, determineTurn(currentPlayer));

newBoard = board.substring(0, i) + '-' + board.substring(i + 1);

}

}

// dictionary.remove(null);

} // end generateBoard method

/**

* Takes in a string and returns an array representation

*/

public ArrayList convertStringToArrayRep(String board) {

ArrayList arrayBoard = new ArrayList();

for (int i = 0; i < board.length(); i++) {

arrayBoard.add(board.charAt(i));

}

return arrayBoard;

} // end convertStringToArrayRep method

public int getBestMove(String board) {

for (int i = 0; i < board.length(); i++) {

if (board.charAt(i) == '-') {

String nextMoveBoardX = board.substring(0, i) + 'X' + board.substring(i + 1);

if (winExists(nextMoveBoardX) == 'X') {

return i;

}

}

}

for (int i = 0; i < board.length(); i++) {

if (board.charAt(i) == '-') {

String nextMoveBoardO = board.substring(0, i) + 'O' + board.substring(i + 1);

if (winExists(nextMoveBoardO) == 'O') {

return i;

}

}

}

Random rand = new Random();

int n = rand.nextInt(8) + 0;

return n;

} // end getBestMove method

/**

* Determines if there is a win within the string

*

* @param board

* @return

*/

private char winExists(String board) {

char winner = ' ';

String firstRow = board.substring(0, 3);

String secondRow = board.substring(3, 6);

String thirdRow = board.substring(6, 9);

// Check first row

if (firstRow.charAt(0) == firstRow.charAt(1) && firstRow.charAt(2) == firstRow.charAt(0)

&& firstRow.charAt(0) != '-') {

winner = firstRow.charAt(0);

}

// Check second row

if (secondRow.charAt(0) == secondRow.charAt(1) && secondRow.charAt(2) == secondRow.charAt(0)

&& secondRow.charAt(0) != '-') {

winner = secondRow.charAt(0);

}

// Check third row

if (thirdRow.charAt(0) == thirdRow.charAt(1) && thirdRow.charAt(2) == thirdRow.charAt(0)

&& thirdRow.charAt(0) != '-') {

winner = thirdRow.charAt(0);

}

// Check diagonal

if (firstRow.charAt(0) == secondRow.charAt(1) && firstRow.charAt(0) == thirdRow.charAt(2)

&& firstRow.charAt(0) != '-') {

winner = firstRow.charAt(0);

}

// Check opposite diagonal

if (firstRow.charAt(2) == secondRow.charAt(1) && firstRow.charAt(2) == thirdRow.charAt(0)

&& firstRow.charAt(2) != '-') {

winner = firstRow.charAt(2);

}

return winner;

} // end winExists method

/**

* Determines whos turn it is

*

* @param currentPlayer

* @return

*/

private char determineTurn(char currentPlayer) {

if (currentPlayer == 'X') {

return 'O';

} else {

return 'X';

}

} // end determineTurn method

} // end class

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

import java.util.ArrayList;

import java.util.Scanner;

public class PlayTicTacToe {

public static void main(String[] args) {

// GameBoard myGame = new GameBoard();

// for(int i = 0; i < 12; i++) {

// System.out.println((i%2)+1);

// }

// myGame.generateBoard("---------", 9, 'X');

/*

* HashedDictionary dictionary = new

* HashedDictionary(); Character[] sA= new Character[9];

* for(int i = 0; i<9; i++) { sA[i] = 'X'; } dictionary.add(sA, 0);

*

* dictionary.contains(sA); dictionary.getValue(sA);

*

* myGame.displayDictionary();

*/

/*

* String board = "aaadefghi"; String firstRow = board.substring(0,3); String

* secondRow = board.substring(3,6); String thirdRow = board.substring(6,9);

*

* System.out.println(firstRow); System.out.println(secondRow);

* System.out.println(thirdRow);

*

* char winner = ' ';

*

* if(firstRow.charAt(0) == firstRow.charAt(1) && firstRow.charAt(2) ==

* firstRow.charAt(0) && firstRow.charAt(0) != '-') { winner =

* firstRow.charAt(0); }

*

* System.out.println(winner);

*/

/*

* String str = "abcde"; String s = str.substring(0, 1); System.out.println(s);

* String ss = str.substring(2); System.out.println(ss);

*/

/*

* System.out.println("Should return 8:");

* System.out.println(myGame.getBestMove("X--XXOOO-"));

*

* System.out.println("Should return 0:");

* System.out.println(myGame.getBestMove("--OXXO--X"));

*

* System.out.println("Should return 0:");

* System.out.println(myGame.getBestMove("--XOOX--O"));

*

* System.out.println("Should return 7:");

* System.out.println(myGame.getBestMove("--X-X-O-O"));

*

*

*/

String b = "XX-O-O---";

ArrayList ch = convertStringToArrayRep(b);

/*

* for(int i = 0; i < 9; i++) { System.out.println(ch[i]); }

*/

GameBoard myGame = new GameBoard();

myGame.displayBoard();

HashedDictionary dic;// = new HashedDictionary();

myGame.generateBoard("---------", 9, 'X');

dic = myGame.getDictionary();

BoardPosition bp = new BoardPosition(ch);

// myGame.makeMove('O',0,0); // myGame.makeMove('X', 1, 1);

//System.out.println("gedgrergegerg" + myGame.getDictionary().getValue(bp));

int counter = 1;

while (myGame.gameActive() && counter < 10) {

if (counter % 2 == 0) {

myGame.askPlayer('O');

} else {

System.out.println("Computers turn...");

String boardStr = myGame.convertBoardToString();

// System.out.println(boardStr);

ArrayList boardArr = myGame.convertStringToArrayRep(boardStr);

// for(int i = 0; i < 9; i++) { System.out.println(boardArr[i]); }

// myGame.generateBoard("---------", 9, 'X');

// dic = myGame.getDictionary();

BoardPosition boardBP = new BoardPosition(boardArr);

int bestMove = dic.getValue(boardBP); // dictionary is null //boardArr has value

int[] rowcol = new int[2];

rowcol = myGame.convertCoordinateToDouble(bestMove);

int row = rowcol[0];

int col = rowcol[1];

myGame.makeMove('X', row, col);

}

counter++;

System.out.println();

myGame.displayBoard();

myGame.checkForWinner();

System.out.println(myGame.toString());

if (counter == 10) {

System.out.println("Stale mate! ");

}

}

} // end main

public static ArrayList convertStringToArrayRep(String board) {

ArrayList arrayBoard = new ArrayList();

for (int i = 0; i < board.length(); i++) {

arrayBoard.add(board.charAt(i));

}

return arrayBoard;

} // end convertStringToArrayRep method

}// end class

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

import java.util.Arrays;

import java.util.Iterator;

import java.util.NoSuchElementException;

/**

A class that implements the ADT dictionary by using hashing and

linear probing to resolve collisions.

The dictionary is unsorted and has distinct search keys.

Notes: Uses probe for add, but locate for remove and getValue.

(HashedDictionary2 uses probe instead of locate, as described in the

answer to Self-Test Question 4 in Chapter 19.)

Uses linear probing, but includes code for quadratic probing.

Has a display method for illustration and testing.

@author Frank M. Carrano

@author Timothy M. Henry

@version 4.0

*/

public class HashedDictionary implements DictionaryInterface

{

// The dictionary:

private int numberOfEntries;

private static final int DEFAULT_CAPACITY = 5; // Must be prime

private static final int MAX_CAPACITY = 10000;

// The hash table:

private TableEntry[] hashTable;

private int tableSize; // Must be prime

private static final int MAX_SIZE = 2 * MAX_CAPACITY;

private boolean initialized = false;

private static final double MAX_LOAD_FACTOR = 0.5; // Fraction of hash table that can be filled

public HashedDictionary()

{

this(DEFAULT_CAPACITY); // Call next constructor

} // end default constructor

public HashedDictionary(int initialCapacity)

{

checkCapacity(initialCapacity);

numberOfEntries = 0; // Dictionary is empty

// Set up hash table:

// Initial size of hash table is same as initialCapacity if it is prime;

// otherwise increase it until it is prime size

int tableSize = getNextPrime(initialCapacity);

checkSize(tableSize);

// The cast is safe because the new array contains null entries

@SuppressWarnings("unchecked")

TableEntry[] temp = (TableEntry[])new TableEntry[tableSize];

hashTable = temp;

initialized = true;

} // end constructor

// -------------------------

// We've added this method to display the hash table for illustration and testing

// -------------------------

public void displayHashTable()

{

checkInitialization();

for (int index = 0; index < hashTable.length; index++)

{

if (hashTable[index] == null)

System.out.println("null ");

else if (hashTable[index].isRemoved())

System.out.println("removed state");

else

System.out.println(hashTable[index].getKey() + " " + hashTable[index].getValue());

} // end for

System.out.println();

} // end displayHashTable

// -------------------------

public V add(K key, V value)

{

checkInitialization();

if ((key == null) || (value == null))

throw new IllegalArgumentException("Cannot add null to a dictionary.");

else

{

V oldValue; // Value to return

int index = getHashIndex(key);

index = probe(index, key); // Check for and resolve collision

// Assertion: index is within legal range for hashTable

assert (index >= 0) && (index < hashTable.length);

if ( (hashTable[index] == null) || hashTable[index].isRemoved())

{ // Key not found, so insert new entry

hashTable[index] = new TableEntry<>(key, value);

numberOfEntries++;

oldValue = null;

}

else

{ // Key found; get old value for return and then replace it

oldValue = hashTable[index].getValue();

hashTable[index].setValue(value);

} // end if

// Ensure that hash table is large enough for another add

if (isHashTableTooFull())

enlargeHashTable();

return oldValue;

} // end if

} // end add

public V remove(K key)

{

checkInitialization();

V removedValue = null;

int index = getHashIndex(key);

index = locate(index, key);

if (index != -1)

{

// Key found; flag entry as removed and return its value

removedValue = hashTable[index].getValue();

hashTable[index].setToRemoved();

numberOfEntries--;

} // end if

// Else not found; result is null

return removedValue;

} // end remove

public V getValue(K key)

{

checkInitialization();

V result = null;

int index = getHashIndex(key);

index = locate(index, key);

if (index != -1)

result = hashTable[index].getValue(); // Key found; get value

// Else not found; result is null

return result;

} // end getValue

public boolean contains(K key)

{

return getValue(key) != null;

} // end contains

public boolean isEmpty()

{

return numberOfEntries == 0;

} // end isEmpty

public int getSize()

{

return numberOfEntries;

} // end getSize

public final void clear()

{

checkInitialization();

for (int index = 0; index < hashTable.length; index++)

hashTable[index] = null;

numberOfEntries = 0;

/// locationsUsed = 0;

} // end clear

public Iterator getKeyIterator()

{

return new KeyIterator();

} // end getKeyIterator

public Iterator getValueIterator()

{

return new ValueIterator();

} // end getValueIterator

private int getHashIndex(K key)

{

int hashIndex = key.hashCode() % hashTable.length;

if (hashIndex < 0)

{

hashIndex = hashIndex + hashTable.length;

} // end if

return hashIndex;

} // end getHashIndex

// Precondition: checkInitialization has been called.

private int probe(int index, K key)

{

boolean found = false;

int removedStateIndex = -1; // Index of first location in removed state

// int increment = 1; // For quadratic probing **********

while ( !found && (hashTable[index] != null) )

{

if (hashTable[index].isIn())

{

if (key.equals(hashTable[index].getKey()))

found = true; // Key found

else // Follow probe sequence

index = (index + 1) % hashTable.length; // Linear probing

// index = (index + increment) % hashTable.length; // Quadratic probing **********

// increment = increment + 2; // Odd values for quadratic probing **********

}

else // Skip entries that were removed

{

// Save index of first location in removed state

if (removedStateIndex == -1)

removedStateIndex = index;

index = (index + 1) % hashTable.length; // Linear probing

// index = (index + increment) % hashTable.length; // Quadratic probing **********

// increment = increment + 2; // Odd values for quadratic probing **********

} // end if

} // end while

// Assertion: Either key or null is found at hashTable[index]

if (found || (removedStateIndex == -1) )

return index; // Index of either key or null

else

return removedStateIndex; // Index of an available location

} // end probe

// Precondition: checkInitialization has been called.

private int locate(int index, K key)

{

boolean found = false;

// int increment = 1; // Quadratic probing **********

while ( !found && (hashTable[index] != null) )

{

if ( hashTable[index].isIn() && key.equals(hashTable[index].getKey()) )

found = true; // Key found

else // Follow probe sequence

index = (index + 1) % hashTable.length; // Linear probing

// index = (index + increment) % hashTable.length; // Quadratic probing **********

// increment = increment + 2; // Odd values for quadratic probing **********

} // end while

// Assertion: Either key or null is found at hashTable[index]

int result = -1;

if (found)

result = index;

return result;

} // end locate

// Increases the size of the hash table to a prime >= twice its old size.

// In doing so, this method must rehash the table entries.

// Precondition: checkInitialization has been called.

private void enlargeHashTable()

{

TableEntry[] oldTable = hashTable;

int oldSize = hashTable.length;

int newSize = getNextPrime(oldSize + oldSize);

checkSize(newSize);

// The cast is safe because the new array contains null entries

@SuppressWarnings("unchecked")

TableEntry[] tempTable = (TableEntry[])new TableEntry[newSize]; // Increase size of array

hashTable = tempTable;

numberOfEntries = 0; // Reset number of dictionary entries, since

// it will be incremented by add during rehash

// Rehash dictionary entries from old array to the new and bigger array;

// skip both null locations and removed entries

for (int index = 0; index < oldSize; index++)

{

if ( (oldTable[index] != null) && oldTable[index].isIn() )

add(oldTable[index].getKey(), oldTable[index].getValue());

} // end for

} // end enlargeHashTable

// Returns true if lambda > MAX_LOAD_FACTOR for hash table;

// otherwise returns false.

private boolean isHashTableTooFull()

{

return numberOfEntries > MAX_LOAD_FACTOR * hashTable.length;

} // end isHashTableTooFull

// Returns a prime integer that is >= the given integer.

private int getNextPrime(int integer)

{

// if even, add 1 to make odd

if (integer % 2 == 0)

{

integer++;

} // end if

// test odd integers

while (!isPrime(integer))

{

integer = integer + 2;

} // end while

return integer;

} // end getNextPrime

// Returns true if the given intege is prime.

private boolean isPrime(int integer)

{

boolean result;

boolean done = false;

// 1 and even numbers are not prime

if ( (integer == 1) || (integer % 2 == 0) )

{

result = false;

}

// 2 and 3 are prime

else if ( (integer == 2) || (integer == 3) )

{

result = true;

}

else // integer is odd and >= 5

{

assert (integer % 2 != 0) && (integer >= 5);

// a prime is odd and not divisible by every odd integer up to its square root

result = true; // assume prime

for (int divisor = 3; !done && (divisor * divisor <= integer); divisor = divisor + 2)

{

if (integer % divisor == 0)

{

result = false; // divisible; not prime

done = true;

} // end if

} // end for

} // end if

return result;

} // end isPrime

// Throws an exception if this object is not initialized.

private void checkInitialization()

{

if (!initialized)

throw new SecurityException ("HashedDictionary object is not initialized properly.");

} // end checkInitialization

// Ensures that the client requests a capacity

// that is not too small or too large.

private void checkCapacity(int capacity)

{

if (capacity < DEFAULT_CAPACITY)

capacity = DEFAULT_CAPACITY;

else if (capacity > MAX_CAPACITY)

throw new IllegalStateException("Attempt to create a dictionary " +

"whose capacity is larger than " +

MAX_CAPACITY);

} // end checkCapacity

// Throws an exception if the hash table becomes too large.

private void checkSize(int size)

{

if (tableSize > MAX_SIZE)

throw new IllegalStateException("Dictionary has become too large.");

} // end checkSize

private class KeyIterator implements Iterator

{

private int currentIndex; // Current position in hash table

private int numberLeft; // Number of entries left in iteration

private KeyIterator()

{

currentIndex = 0;

numberLeft = numberOfEntries;

} // end default constructor

public boolean hasNext()

{

return numberLeft > 0;

} // end hasNext

public K next()

{

K result = null;

if (hasNext())

{

// Skip table locations that do not contain a current entry

while ( (hashTable[currentIndex] == null) || hashTable[currentIndex].isRemoved() )

{

currentIndex++;

} // end while

result = hashTable[currentIndex].getKey();

numberLeft--;

currentIndex++;

}

else

throw new NoSuchElementException();

return result;

} // end next

public void remove()

{

throw new UnsupportedOperationException();

} // end remove

} // end KeyIterator

private class ValueIterator implements Iterator

{

private int currentIndex;

private int numberLeft;

private ValueIterator()

{

currentIndex = 0;

numberLeft = numberOfEntries;

} // end default constructor

public boolean hasNext()

{

return numberLeft > 0;

} // end hasNext

public V next()

{

V result = null;

if (hasNext())

{

// Skip table locations that do not contain a current entry

while ( (hashTable[currentIndex] == null) || hashTable[currentIndex].isRemoved() )

{

currentIndex++;

} // end while

result = hashTable[currentIndex].getValue();

numberLeft--;

currentIndex++;

}

else

throw new NoSuchElementException();

return result;

} // end next

public void remove()

{

throw new UnsupportedOperationException();

} // end remove

} // end ValueIterator

private static class TableEntry

{

private S key;

private T value;

private States state; // Flags whether this entry is in the hash table

private enum States {CURRENT, REMOVED} // Possible values of state

private TableEntry(S searchKey, T dataValue)

{

key = searchKey;

value = dataValue;

state = States.CURRENT;

} // end constructor

private S getKey()

{

return key;

} // end getKey

private T getValue()

{

return value;

} // end getValue

private void setValue(T newValue)

{

value = newValue;

} // end setValue

// Returns true if this entry is currently in the hash table.

private boolean isIn()

{

return state == States.CURRENT;

} // end isIn

// Returns true if this entry has been removed from the hash table.

private boolean isRemoved()

{

return state == States.REMOVED;

} // end isRemoved

// Sets the state of this entry to removed.

private void setToRemoved()

{

key = null;

value = null;

state = States.REMOVED; // Entry not in use, ie deleted from table

} // end setToRemoved

// Sets the state of this entry to current.

private void setToIn() // Not used

{

state = States.CURRENT; // Entry in use

} // end setToIn

} // end TableEntry

} // end HashedDictionary

XXXXXXXXXXXXXXXXXXXXXXXXXXXXX

import java.util.ArrayList;

public class BoardPosition {

ArrayList board;

public BoardPosition(ArrayList board) {

this.board = board;

}

@Override

public boolean equals(Object o) {

if (o instanceof BoardPosition) {

BoardPosition anotherBoard = (BoardPosition) o;

String b = board.toString();

String other = anotherBoard.toString();

if (b.contentEquals(other)) {

return true;

}

}

return false;

}

}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions