big w

 avatar
unknown
java
4 years ago
2.6 kB
4
Indexable
public Akari(String inFilename)
{
    // TODO 3
    filename = inFilename;
	String[] lineSplit;
	char[] blockSplit;
	int x, y;
    FileIO fio = new FileIO(filename);
	// loop through each line in file
    for (int k = 0; k < fio.getLines().size(); k++)
    {
		// if its the first line in the file
        if (k == 0) {
			size = Integer.parseInt(fio.getLines().get(k)); // get int in the first line
			board = new Space[size][size]; // initialise board using the size
		} else {
			lineSplit = fio.getLines().get(k).split(" "); // splits the current line into the spaces
			// loops through each space in each line\
			for (int l = 0; l < lineSplit.length; l++) {
				blockSplit = lineSplit[l].toCharArray(); // each space gets split, 23 turns into [2, 3]
				if (blockSplit.length > 0) {
					x = Character.getNumericValue(blockSplit[0]);
					if (blockSplit.length > 2) {
						// if the space has 3 numbers in it i.e. 233 ([2, 3, 3])
						// loop through each number after the first number so if its 233 ([2, 3, 4]) it will loop through [3, 4]
						for (int p = 1; p < blockSplit.length; p++) {
							y = Character.getNumericValue(blockSplit[p]);
							boardSpace(k, x, y);
						}
					} else {
						y = Character.getNumericValue(blockSplit[1]);
						boardSpace(k, x, y);
					}
				}
			}
		}
	}

	for (int a = 0; a < size; a++) {
		for (int b = 0; b < size; b++) {
			if (board[a][b] == null) {
				board[a][b] = Space.EMPTY;
			}
		}
	}
}

private void boardSpace(int k, int x, int y) {
	switch (k) {
		case 1:
			board[x][y] = Space.BLACK;
			break;
		case 2:
			board[x][y] = Space.ZERO;
			break;
		case 3:
			board[x][y] = Space.ONE;
			break;
		case 4:
			board[x][y] = Space.TWO;
			break;
		case 5:
			board[x][y] = Space.THREE;
			break;
		case 6:
			board[x][y] = Space.FOUR;
			break;
		default:
			break;
	}
}

public boolean isLegal(int k)
{
    // TODO 5
	if (k <= size && k >= 0) {
		return true;
	} else {
		return false; 
	}
}

/**
 * Returns true iff r and c are both legal indices into the board. 
 */
public boolean isLegal(int r, int c)
{
    if (isLegal(r) && isLegal(c)) {
		return true;
	} else {
		return false; 
	}
}

/**
 * Returns the contents of the square at r,c if the indices are legal, 
 * o/w throws an illegal argument exception. 
 */
public Space getBoard(int r, int c)
{
    // TODO 7
	return board[r][c];
    // if (isLegal(r, c)) {
	// 	return board[r][c];
	// } else {
	// 	// throw error
	// }
}
Editor is loading...