Note: this is from a COMPLETED course. This is not a ploy to get help with homework, as I have already received a 100% on this assignment (last year). I just want to become a neater programmer. Can you please provide me with some stylistic feedback? Note, the assignment did not allow for the use of objects at the time and the dat files contained X's and .'s. X's = life, .'s = absence of life. It was an exercise to provide us practice with multi-dimensional arrays.
package java_labs.GameOfLife;
import java.util.Scanner;
import java.io.*;
public class GameOfLife {
final static int M = 25;
final static int N = 75;
public static void main(String[] args) {
char[][] oldBoard = new char[M + 2][N + 2];
char[][] newBoard = new char[M + 2][N + 2];
char[][] tempBoard = new char[M + 2][N + 2];
buildBoardFromFile(oldBoard, newBoard, tempBoard);
int generationCount = 0;
printBoard(oldBoard, generationCount); /* prints Generation #0 every time*/
boolean continueGame = true;
while (continueGame) {
if (isEmpty(oldBoard)) {
continueGame = false;
} else {
newBoard = getNextGeneration(oldBoard, newBoard);
if (isGenerationChanged(oldBoard, newBoard)) {
tempBoard = copyArray(newBoard, tempBoard);
Scanner input = new Scanner(System.in);
String inputValue = " ";
while ((!(inputValue.equalsIgnoreCase("Y")))
&& (!(inputValue.equalsIgnoreCase("Q")))) {
System.out.print("\nWould you like to see the next generation?\n"
+ "Enter 'Y' for yes or 'Q' to quit: ");
inputValue = input.next();
if (inputValue.equalsIgnoreCase("Q")) {
continueGame = false;
System.out.println("\nYou opted to quit. "
+ "Game Over!\n");
} else if (inputValue.equalsIgnoreCase("Y")) {
generationCount++;
printBoard(newBoard, generationCount);
oldBoard = tempBoard;
} else {
System.out.println("\nInvalid option. Try again.");
}
}
} else {
continueGame = false;
generationCount++;
System.out.println("\n\nGAME OVER! Generation #"
+ (generationCount)
+ " is the same as Generation #"
+ (generationCount - 1)
+ ".\nBelow is proof that Generation #"
+ (generationCount)
+ " is the same as Generation #"
+ (generationCount - 1)
+ "\nfor your reference:\n");
printBoard(newBoard, generationCount);
System.out.println("\n\n\nYou will now exit the program!\n\n\n");
}
}
}
}
static void buildBoardFromFile(char[][] oldBoard, char[][] newBoard,
char[][] tempBoard) {
boolean inputInvalid = true;
Scanner fileReader = null;
File file = null;
String line = "";
boolean invalidNumberOfRowCol = false;
while (inputInvalid) {
Scanner consoleReader = new Scanner(System.in);
System.out.print("\nWhich file do you want to open?");
String filename = consoleReader.next();
file = new File(filename);
try {
fileReader = new Scanner(file);
inputInvalid = false;
}
catch (Exception e) {
System.out.println("\nError: File " + file
+ " does not exist! Try again:");
}
}
for (int row = 0; row < M; row++) {
try {
line = fileReader.nextLine();
}/* Accounts for files with too few rows */
catch (java.util.NoSuchElementException err) {
System.out.println("\nError: " + file
+ " is not compatible with"
+ " this program! Try adding " + (M - (row))
+ " rows to\n" + file + " to fix this issue.");
buildBoardFromFile(oldBoard, newBoard, tempBoard);
break;
}
for (int col = 0; col < N; col++) {
/* Added this code to account for files with too few columns */
try {
oldBoard[row + 1][col + 1] = line.charAt(col);
}
catch (StringIndexOutOfBoundsException err) {
System.out.println("\nError: " + file
+ " is not compatible with this program!"
+ " Try adding " + (N - line.length())
+ " characters to\nrow #" + (row + 1) + " in "
+ file + " to fix this issue.");
invalidNumberOfRowCol = true;
break;
}
}
}/*
* Note this program will ignore data in file greater outside the
* maximum column size and row size. For example, if a row in the .dat
* file contains 76 characters and the maximum board size is 75, the
* 76th character in that row is ignored by the program's for loop. This
* assignment did not ask the user to address this issue. I did create
* additional error checking functionality outside the scope of the
* assignment for practice. See above.
*/
if (invalidNumberOfRowCol)
buildBoardFromFile(oldBoard, newBoard, tempBoard);
setBoarder(oldBoard, newBoard, tempBoard);
}
/* Note boarder will always be . and will not change */
static void setBoarder(char[][] oldBoard, char[][] newBoard,
char[][] tempBoard) {
for (int row = 0; row <= M + 1; row++) {
for (int col = 0; col <= N + 1; col++) {
if (row == 0 || col == 0 || row == M + 1 || col == N + 1) {
oldBoard[row][col] = '.';
newBoard[row][col] = '.';
tempBoard[row][col] = '.';
}
}
}
}
/* Checks for life on game board */
static boolean isEmpty(char[][] oldBoard) {
boolean noLife = true;
for (int row = 1; row <= M; row++) {
for (int col = 1; col <= N; col++) {
if (oldBoard[row][col] == 'X')
noLife = false;
}
}
return noLife;
}
/* Identifies if a generation is different from its successor */
static boolean isGenerationChanged(char[][] oldBoard, char[][] newBoard) {
boolean continueGame = false;
for (int row = 1; row <= M; row++) {
for (int col = 1; col <= N; col++) {
if (oldBoard[row][col] != newBoard[row][col])
continueGame = true;
}
}
return continueGame;
}
static void printBoard(char[][] board, int generationCount) {
if (isEmpty(board))
System.out.println("\nEveryone is dead! Game over!\n");
System.out.println("\nGeneration #" + generationCount + ":");
for (int row = 1; row <= M; row++) {
for (int col = 1; col <= N; col++) {
System.out.print(board[row][col]);
}
System.out.print("\n");
}
}
static char[][] getNextGeneration(char[][] oldBoard, char[][] newBoard) {
for (int row = 1; row <= M; row++) {
for (int col = 1; col <= N; col++) {
if ((oldBoard[row][col] == 'X' && (countNeighbors(oldBoard,
row, col) == 2 || countNeighbors(oldBoard, row, col) == 3))
|| (oldBoard[row][col] == '.' && countNeighbors(
oldBoard, row, col) == 3)) {
newBoard[row][col] = 'X';
} else {
newBoard[row][col] = '.';
}
}
}
return newBoard;
}
static char[][] copyArray(char[][] newBoard, char[][] tempBoard) {
for (int row = 0; row <= M + 1; row++) {
for (int col = 0; col <= N + 1; col++) {
tempBoard[row][col] = newBoard[row][col];
}
}
return tempBoard;
}
static int countNeighbors(char[][] board, int row, int col) {
int count = 0;
int tempRow = row;
int tempCol = col;
for (col = tempCol - 1; col <= tempCol + 1; col++) {
if (board[row - 1][col] == 'X')
count++;
if (board[row + 1][col] == 'X')
count++;
}
for (col = tempCol - 1; col <= tempCol + 1; col += 2) {
if (board[row][col] == 'X')
count++;
}
return count;
}
}