• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Trouble on simple Conways Game of Life project

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I had finally completed the project but my matrix's are generating incorrectly and I cant figure out why.
These are my specifications.
"The game is thus parameterized by the particular population density ranges that allow organ-isms to be born or to continue to thrive.  Each cell has eight neighbors (above, below, left, right, and four diagonals).  Thus, if you count the cell as well, there are nine cells in its neighborhood.  Fora birth to occur, the cell must be empty and the neighborhood density be in the birth range.  The birth range can be given as two integers from 1 to 9, birthLow and birthHigh, so that a birth will occur if the cell is empty and the neighborhood population is at least birthLow and no more than birthHigh.  Similarly, a death occurs in a cell if there is an organism there and the neighborhood has less than the minimum population for survival, or greater than the maximum.  Hence there is a live range provided as two integers from 1 to 9, liveLow and liveHigh.  The border of the area is not compatible with life, so the top and bottom rows and the left and right columns will never have organisms"

Incase there is a problem with my current code I'm going to show the entire thing.
The problem I believe lies in the updateMatrix method.



This is what it is currently printing.


Any help is appreciated!


edit: The matrix's look a little wonky but it's supposed to be in the shape of a rectangle.
 
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your neighborhood is always 3x3. No need to use .length.

 
Carey Brown
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use code tags for fixed-width text. There, doesn't that look less wonky now?
 
Lucas Carbon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Your neighborhood is always 3x3. No need to use .length.




I attempted this and it gave me an output of this.
6
8
7
=  =  =  =  =  =  =  =
=  O  O  O  =  =  =  =
=  O  O  O  O  =  O  =
=  =  =  O  O  =  O  =
=  O  =  O  =  O  O  =
=  =  =  =  =  =  =  =

=  =  =  =  =  =  =  =
=  =  =  =  =  =  =  =
=  =  =  =  =  =  =  =
=  =  =  =  =  =  =  =
=  =  =  =  =  =  =  =
=  =  =  =  =  =  =  =

=  =  =  =  =  =  =  =
=  O  O  O  O  O  O  O
=  O  O  O  O  O  O  O
=  O  O  O  O  O  O  O
=  O  O  O  O  O  O  O
=  O  O  O  O  O  O  O

=  =  =  =  =  =  =  =
=  =  =  =  =  =  =  =
=  =  =  =  =  =  =  =
=  =  =  =  =  =  =  =
=  =  =  =  =  =  =  =
=  =  =  =  =  =  =  =

=  =  =  =  =  =  =  =
=  O  O  O  O  O  O  O
=  O  O  O  O  O  O  O
=  O  O  O  O  O  O  O
=  O  O  O  O  O  O  O
=  O  O  O  O  O  O  O

=  =  =  =  =  =  =  =
=  =  =  =  =  =  =  =
=  =  =  =  =  =  =  =
=  =  =  =  =  =  =  =
=  =  =  =  =  =  =  =
=  =  =  =  =  =  =  =
 
Carey Brown
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Lucas Carbon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:



Hmm... that seems to be getting closer. I implemented that as well now and it kept the border which is perfect, but now the insides are updating wrong after the first iteration. It repeats full true for the innner, and full false for the inner consecutively. Could it possibly be a bad product of the cloneArray method?
 
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am surprised you can actually understand and discuss competitively this sort of program. I personally find it very difficult to understand for my brains.

What have you been asked as for a task?

(I need to boot my laptop to look at it better)
 
Liutauras Vilda
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Were you given some template to work on, or that is all you wrote?
 
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lucas Carbon wrote:
The problem I believe lies in the updateMatrix method.


What makes you think that?

I believe the bigger problem you have is that the code is so full of implementation detail that it drowns out your intent. In other words, all I see is nuts and bolts when I read this code that I can't even figure out how the machine works at all. It doesn't help that you use names that are not very good representations of their purpose, like count and count2 and birthLive.

If I were to use words from your requirements, I'd probably want to write this kind of code instead:

That way, I can easily understand what I'm trying to do and break each little calculation out to its own method.  

By the way, your comments are misleading:

Should be

 
Lucas Carbon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Were you given some template to work on, or that is all you wrote?



No template just written instructions.
These are the instructions:
https://imgur.com/a/VMuGdtY
 
Liutauras Vilda
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lucas Carbon wrote:No template just written instructions.
These are the instructions:...


Thank you for the instructions.

How far have you gone with your studies? Is it your first year? Covered some object oriented programming concepts yet?
 
Lucas Carbon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Lucas Carbon wrote:No template just written instructions.
These are the instructions:...


Thank you for the instructions.

How far have you gone with your studies? Is it your first year? Covered some object oriented programming concepts yet?



First year. Scratched the surface of OOP. But for this assignment it's specifically just supposed to be static methods. Unfortunately.
 
Liutauras Vilda
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP

subject line wrote:Trouble on simple Conways Game of Life project


This project isn't that simple if you think carefully. People don't get stuck on simple things. It may look simply implemented, but behind such implementation possibly are days of planning and experimenting (for me it was weeks, with help of fellow ranchers here).

In the way you wrote this program, isn't simple at all, and I personally wouldn't be able to fix this program by tracking down what index overlooked, or counter wrongly incremented or something else.

Take Junilu's advice very seriously, that will be usefull not only in solving this problem, but will be beneficial throughout your programmer's career if you ever going to specialise in it.

If I were you, I probably would open fresh project and start over. Or would start overriding this project's pieces.
 
Lucas Carbon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:@OP

subject line wrote:Trouble on simple Conways Game of Life project


This project isn't that simple if you think carefully. People don't get stuck on simple things. It may look simply implemented, but behind such implementation possibly are days of planning and experimenting (for me it was weeks, with help of fellow ranchers here).

In the way you wrote this program, isn't simple at all, and I personally wouldn't be able to fix this program by tracking down what index overlooked, or counter wrongly incremented or something else.

Take Junilu's advice very seriously, that will be usefull not only in solving this problem, but will be beneficial throughout your programmer's career if you ever going to specialise in it.

If I were you, I probably would open fresh project start over. Or would start overriding this project's pieces.



Thanks for the input, I will consider this heavily.
 
Liutauras Vilda
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read those instructions you posted. Don't know if it is fair to ask, but did you understand them well? I find them quite difficult to understand and get a picture in my head, but maybe because I got used to other wording of the instructions.

Have a look at those found on Wikipedia here, and in particular read the rules. Is your understanding the same comparing to those you were given?
Staff note (Liutauras Vilda):

Maybe OP's version isn't the standard Conway's Game of Life?

 
Lucas Carbon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:I read those instructions you posted. Don't know if it is fair to ask, but did you understand them well? I find them quite difficult to understand and get a picture in my head, but maybe because I got used to other wording of the instructions.

Have a look at those found on Wikipedia here, and in particular read the rules. Is your understanding the same comparing to those you were given?




So the way this project went is we did it in steps. The first part was getting the first iteration to print. Which didn't include an updating step, just randomizing boolean values within a border of false boolean values. And I know i had that 100% correct because I was graded for it. But now i'm at the part where I need to start updating the matrix based on the rules i was provided. And thats where it got messy. This is the point where I know things were correct and running.

https://pastebin.com/vRURz9XX

Since then I've changed a lot of stuff based on advice and help from others.
 
Liutauras Vilda
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lucas Carbon wrote:So the way this project went is we did it in steps.


Ok, understood as well as had a look at your first part's solution.

Since you were graded and according to you no mistakes were found there (I trust it does what is asked, even though some parts were questionable, but not plain wrong), i.e.:


You have inconsisten loops here, which one iterates length-1 and other whole length. I understand it is for the border, but then within if statement you as a result also have inconsistent check - and for that I'd have take few points for you, simply because you make your reader do extra thinking step, why have you done that, what's the rationale behind.

But let's leave that out, as it is submitted already

So it seems you are sort of led to implement everything that way, use booleans, and an array of arrays, and have static methods, which makes all this project less interesting and requiring minimal creativity.

So, once again, since your first assignment's part work, take that part and use as a base once again.

Now, the current task is to implement time advancement, so the cells could either die or come to life.

1. So as an input for that, you need to pass an array of arrays (let's call it 2D array) -> make a copy of it -> AND, update the each cell of copied array (excluding borders), USING an original array as a reference which you don't alter ever, so you'd know, what was the previous array's state, and in particular each element's state.

1.1 Once you done with altering copied array, from this function you need to return it.

2. Pass the returned array to a print function.

3. Repeat steps from 1 to 2 as many times as specified in the instructions.


Task 1 is the main one. So you need to know how many alive cells are within neighbourhood. So for that, given the index of row and column you need to check, how many alive cells are around (8 array's elements).

Do you get that part?

So if I'd give you indices of row and column, let them be: 3 (row index) and 4 (column index), would you be able to figure out the indices of the neighbour elements? - If yes, you just need to check within the orginal array, what the values are at those pair of indices and you'll know how many alive neighbours your cell has. And that is all, based on that, you'd update the copied array's cell.

See, the thing is, that this exercise is really that boring (probably not so for the first year student ), because of such prescribed instructions, that I'm afraid that Junilu (who gave you great tips on how to make this game's implementation more appealing) lost interest already.

Basically if you check once again what Junilu wrote, this is exactly where you could continue on the base of your first part of assignment, using similar methods he showed you.
 
Junilu Lacar
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lucas Carbon wrote:And I know i had that 100% correct because I was graded for it.
https://pastebin.com/vRURz9XX


Doesn't matter that you got 100%, it's still wrong. If you tried with different seeds that tried to put in the last column, it would do it. That's because of the inconsistency in loop indices that Liu pointed out. It makes the code confusing to read and dtherefore difficult to understand. Worse, they didn't teach you to write automated tests to verify what you wrote as you wrote it.
 
Junilu Lacar
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is the code you have. First, the seed parameter is never used so why even have it?

On line 2, you add 1 to rows and columns when you declare the array. That makes your JavaDoc @param comments wrong.

Then on line 4 you use row < rows but on line 5 you use col < columns - 1. Either way, you don't iterate over the entire array because the last indices are rows and columns, respectIvey. So for the rows, you fail to iterate over the last row, which is fine given that you leave that row empty anyway. But for columns, you don't iterate over the last two columns because your loop termination is col < columns - 1 and the largest column index is columns (see the declaration on line 2 again)

On line 7, you use Boolean.FALSE unnecessarily because you declare the array as primitive boolean. Also, it's an unnecessary operation: elements in a boolean array are already false by default so all you're doing here is re-assigning false to an element that is already false.

On line 6, you are again inconsistent, using row == rows - 1 but col == columns.

This makes for very confusing code that suggests a lack of clarity of intent/purpose by the author.
 
Junilu Lacar
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way to get clarity is to visualize what you're doing using a specific example. Something like this:

Given: rows = 5, columns = 5

  0 1 2 3 4
0 - - - - -
1 - ? ? ? -
2 - ? ? ? -
3 - ? ? ? -
4 - - - - -

Your code iterates over the entire array and uses a conditional statement to skip the border cells. That seems more complicated than it needs to be. It was that complexity of approach that led you to writing overly-complex code that turned out to be wrong anyway.

If I wanted to leave the border cells untouched, I simply would not iterate over them in the first place. Figuring out the proper loop parameters to use to do that isn't that hard, especially if you have a visual like the above to help.

Seeing that, I can easily generalize the code to take any R x C grid:

So your code becomes:

See how much simpler and straightforward that is?

 
Junilu Lacar
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing that you're overcomplicating is the calculation of the next generation. Your approach seems to be to use a new array for each new generation you calculate. You don't need to do that.

A simpler approach is to use just TWO arrays: one for the current generation and a second one for the next generation. Then, at the appropriate point in your program, you just switch references.

 
permaculture is largely about replacing oil with people. And one tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!