0
 package edu.bsu.cs121.mamurphy;

    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;

// Maurice Murphy
// CS121
// 10/17/15

public class GameOfLifeMain extends JFrame {

    // Intitial reading and printing of the world

    public GameOfLifeMain() {
        super("Game of Life");
        setSize(600, 445);
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1, 2));
        Test test1 = new Test();
        test1.setBackground(Color.LIGHT_GRAY);
        panel.add(test1);
        setContentPane(panel);
        setVisible(true);
        Temp one = new Temp(test1);
        one.start();
    }

    public static void main(String[] asd) {
        new GameOfLifeMain();
        System.out.println();

    }
}

class Temp extends Thread {
    Test anim;

    public Temp(Test anim) {
        this.anim = anim;

    }

    public void run()// for each instance of test begin will be executed
    {
        anim.begin();
    }
}

class Test extends JPanel

{
    final static int numOfRow = 25, numOfCol = 75;
    final static char DOT = '.';
    static char[][] grid = new char[numOfRow + 2][numOfCol + 2];
    static char[][] nextgrid = new char[numOfRow + 2][numOfCol + 2];
    boolean sameFlag;
    boolean blankFlag;

    public static void init(char[][] grid, char[][] nextgrid) {
        for (int numOfRow = 0; numOfRow <= numOfRow + 1; numOfRow++) {
            for (int numOfCol = 0; numOfCol <= numOfCol + 1; numOfCol++) {
                grid[numOfRow][numOfCol] = DOT;
                nextgrid[numOfRow][numOfCol] = DOT;
            }
        }
    }

    public static void pause() {
        try {
            Thread.currentThread();
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
        }
    }

    public void begin() {
        init(grid, nextgrid);
        read(grid);
        repaint(); // calls paintComponent
        pause();
        while (sameFlag == true && blankFlag == false) {
            nextGen(grid, nextgrid);
        }
    }

    public static void read(char[][] grid) {
        Scanner world = new Scanner(System.in);
        System.out.println("Type the file name of the world you'd like to create.");
        String fileName = world.nextLine();
        {
            try {
                world = new Scanner(new File(fileName));
            } catch (Exception ex) {
                System.out.println("Please insert a valid file name.");

            }
            ;

            for (int numOfRow = 1; numOfRow <= numOfRow; numOfRow++) {
                String s = world.next();
                for (int numOfCol = 1; numOfCol <= numOfCol; numOfCol++) {
                    grid[numOfRow][numOfCol] = s.charAt(numOfCol - 1);
                }

            }
        }
    }

    public void print(Graphics g) {
        int x, y;
        y = 20;
        for (int numOfRow = 1; numOfRow <= numOfRow; numOfRow++) {
            x = 20;
            for (int numOfCol = 1; numOfCol <= numOfCol; numOfCol++) {
                g.drawString("" + grid[numOfRow][numOfCol], x, y);
                x = x + 7;
            }
            y = y + 15;
        }
    }

    public static int neighbors(char[][] grid, int r, int c) {
        // counts the # of closest neighbors that are X's
        int count = 0;
        for (int numOfRow = r - 1; numOfRow <= r + 1; numOfRow++) {
            for (int numOfCol = c - 1; numOfCol <= c + 1; numOfCol++) {
                if (grid[numOfRow][numOfCol] == 'X') {
                    count++;
                }
            }
        }
        if (grid[r][c] == 'X') {
            count = count - 1;
        }
        return count;
    }

    public static void nextGen(char[][] grid, char[][] nextgrid) {
        for (int numOfRow = 1; numOfRow <= numOfRow; numOfRow++) {
            for (int numOfCol = 1; numOfCol <= numOfCol; numOfCol++) {
                if (grid[numOfRow][numOfCol] == 'X') {
                    int count = 0;
                    {
                        if (grid[numOfRow][numOfCol - 1] == 'X')
                            count = count + 1;
                        if (grid[numOfRow][numOfCol + 1] == 'X')
                            count = count + 1;
                        if (grid[numOfRow - 1][numOfCol] == 'X')
                            count = count + 1;
                        if (grid[numOfRow - 1][numOfCol - 1] == 'X')
                            count = count + 1;
                        if (grid[numOfRow - 1][numOfCol + 1] == 'X')
                            count = count + 1;
                        if (grid[numOfRow + 1][numOfCol - 1] == 'X')
                            count = count + 1;
                        if (grid[numOfRow + 1][numOfCol] == 'X')
                            count = count + 1;
                        if (grid[numOfRow + 1][numOfCol + 1] == 'X')
                            count = count + 1;
                    }

                    if (count == 2 || count == 3) {
                        nextgrid[numOfRow][numOfCol] = 'X';
                    } else
                        nextgrid[numOfRow][numOfCol] = DOT;
                }
                if (grid[numOfRow][numOfCol] == DOT) {
                    int count1 = 0;
                    {
                        if (grid[numOfRow][numOfCol - 1] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow][numOfCol + 1] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow - 1][numOfCol] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow - 1][numOfCol - 1] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow - 1][numOfCol + 1] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow + 1][numOfCol - 1] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow + 1][numOfCol] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow + 1][numOfCol + 1] == 'X')
                            count1 = count1 + 1;
                    }
                    if (count1 == 3)
                        nextgrid[numOfRow][numOfCol] = 'X';
                }
            }
        }
    }

    public static void copy(char[][] grid, char[][] nextgrid) {
        for (int i = 0; i < numOfRow + 1; i++) {
            for (int j = 0; j < numOfCol + 1; j++) {
                grid[i][j] = nextgrid[i][j];
            }
        }
    }

    public static boolean isEmpty(char[][] grid, char[][] nextgrid) {
        boolean blankFlag = true;
        for (int i = 0; i < numOfRow + 1; i++) {
            for (int j = 0; j < numOfCol + 1; j++) {
                if (grid[i][j] != DOT) {
                    blankFlag = false;
                }
            }
        }
        return blankFlag;
    }

    public static boolean isSame(char[][] grid, char[][] nextgrid) {
        boolean sameFlag = false;
        for (int i = 0; i < numOfRow + 1; i++) {
            for (int j = 0; j < numOfCol + 1; j++) {
                if (grid[i][j] == nextgrid[i][j]) {
                    sameFlag = true;
                    break;
                }
            }
        }
        return sameFlag;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);// erases panel Contents
        g.setColor(Color.black);
        if (sameFlag == false && blankFlag == false) {
            print(g);// or whatever method you use to print the world
        } else {
            if (sameFlag == true) {
                g.drawString("The worlds are repeating!", 10, 250);
            }
            if (blankFlag == true) {
                g.drawString("The world is blank!", 10, 250);
            }
        }
    }

}

Sorry about not putting in all the code.

Any help is greatly appreciated.

I have figured out the issue thanks to the help of all your answers. For whatever reason, Eclipse wasn't reading that the files were in the folder with the project, so I simply deleted and readded the files (in the same place mind you) and all of a sudden it started working again.

Now my current issue is to figure out why my program isn't proceeding onto the next generation of life.

Specifically, I need to be able to ask the user if they want to continue onto the next generation of life.

The output should be Do you want to go to the next generation? Type y for yes, type n to quit the program.

  • Some code is missing here. Post the full code. – fge Oct 21 '15 at 19:18
  • are you giving the full path to the file? – John Kane Oct 21 '15 at 19:18
  • 1
    Break out your code and add some debugging. Instead of `world = new Scanner(new File(fileName));`, break it out into `File file = new File(fileName);`, then `System.out.println(file.getCanonicalPath());`, then `world = new Scanner(file);`. That way it will print out the path to where it is looking for the file. – Andrew Mairose Oct 21 '15 at 19:27

3 Answers3

1

Part of your problem is that you are catching a general Exception, so anything could be going wrong. You should print the stack trace from your Exception to help in debugging this issue, as it will give you more precise info on where the error actually is. I.e., is it in opening the File or is it in creating a Scanner from the open File. Hopefully you are seeing why should not catch general exceptions.

Note this is only for debugging, though, as printing the stack trace is not good general practice.

Community
  • 1
  • 1
J.R. Owens
  • 536
  • 3
  • 4
1

File must located in directory project. You see error message replace your source:

   } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
Fortran
  • 2,218
  • 2
  • 27
  • 33
0

you probably need to provide the full path of the file. meaning the exact location of where the file is on your system.

also, it would be easier to have the file as a resource in your project. this way, you can easily load it as a resource by name as

ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("test.txt");
AbtPst
  • 7,778
  • 17
  • 91
  • 172
  • 1
    If I have the files in the same folder as my actual project in Eclipse, should that matter? I mean? I have done programs with file readers and writers before and have never had this issue. – MoeMarmalade Oct 21 '15 at 19:37
  • that should be just fine – AbtPst Oct 21 '15 at 19:37
  • actually even better as all you would need to do is specify the file name – AbtPst Oct 21 '15 at 19:38
  • can you try printing Ex.StackTrace() ? – AbtPst Oct 21 '15 at 19:40
  • 1
    Just print it out anywhere or do I need to do it within my try catch? – MoeMarmalade Oct 21 '15 at 19:50
  • in your catch. that way we'll know which exception is occurring – AbtPst Oct 21 '15 at 19:53
  • 1
    I don't know what I did, but adding in `ex.printStackTrace();` actually made it read the file. Now I need to figure out how to make it go to the next generation. I believe I have all my rules coded in correctly but it isn't going to any of the new generations. – MoeMarmalade Oct 21 '15 at 20:00
  • then clearly, the problem was not with file read. what do you mean next generation? – AbtPst Oct 21 '15 at 20:01
  • You do know what Conway's Game of Life is? Basically my code is reading text files that have a series of periods and x's in a grid formation. An x is a living cell, and a period is a dead cell. Basically, following the rules of the game of life (I wont type them all out here), the grid is supposed to change with each generation. Specifically, I need the program to ask the user if they want the grid to go on to the next generation. It is reading the text file and is popping up the grid, but I need to figure out how to make it so it asks the user to keep going. – MoeMarmalade Oct 21 '15 at 20:04
  • you should probably use different Scanners for reading the file and user input – AbtPst Oct 21 '15 at 20:07