• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do i search a 2D array diagonally?  RSS feed

 
brian watkins
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator







Ok i have this 2D array here I understand how to search it row by row and column by column. I don't really understand how to search it diagonally.
The point of the array is the basic connect 4 game i have to search the array rows and columns to see if either user has 4 in a row. I set it up to look like player 1 has won but I cant seem to get the code to find it.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We know what you mean by "2D" but technically, Java doesn't have multidimensional arrays; it has arrays of arrays (... of arrays of arrays...).

To figure out how to traverse diagonally, it helps to walk through the process with pen and paper, observing the change pattern in the indices you're using. In this case, you have two patterns. Assuming you always start from the left side and move to the right, you can go diagonal up or diagonal down. Note that in a for-loop, you can have multiple initializers and multiple increments.

That is, just as you can declare and initialize multiple variables on one line, like on line 2 below, you can also declare and initialize multiple variables in the first part of a for-loop header (the initialization expression). You can also have multiple increment/decrement expressions in the third part of the loop header.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just thinking "out loud"/online, but it would be nice if they taught students how to solve these kinds of problems in an object-oriented way after they've solved it in a procedural way.

It would be so great if we could see someone in the future post code that looks like this:

And I would be really excited if someone asked if it made more sense to name those methods like this instead:

I would be very impressed. One can always hope and dream...

 
brian watkins
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Learning object code writing is the class after this one. I am in the basic java class where I am learning how to write code. So it is very basic and we are not allowed to use things that are built into java on most project/homework.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
brian watkins wrote:Learning object code writing is the class after this one. I am in the basic java class where I am learning how to write code. So it is very basic and we are not allowed to use things that are built into java on most project/homework.

Well, let's hope they teach you well. BTW, none of that code is "built into" Java. It's code that a programmer who was thinking in terms of objects and encapsulated behaviors might hypothetically come up with himself/herself.
 
brian watkins
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry if you thought I meant the code you wrote was built in. I was just saying it just to let you know how the class is formatted. We cant use things that help us build arrays and calenders things like that.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't even have to wait until you learn about object-oriented programming to use the same ideas I showed in that example. If you've learned about methods already, you can apply the same kind of decomposition you see in that example, even in a procedural-style solution.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the board is square, then I don't think you need two indices. You can do it all with one index.
If the board isn't square, do you have diagonals at all?
You shou‍ld be able to work it all out if you draw it on paper, as Junilu suggested.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
brian watkins wrote:We cant use things that help us build arrays and calenders things like that.

I understand. That's good to the extent that it allows you to understand how those things work under the covers. Hopefully, after you learn what it's like to do things the hard way, they'll let you start using the tools that are already available instead of making you write your own little things that make it easier to write more complex programs.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If the board isn't square, do you have diagonals at all?

As shown, the board he has represented has 6 rows and 7 columns. It still has diagonals though. And you're right about not needing two indices; you can get away with one index for one diagonal and one index and some simple math for the other diagonal. Two index variables just makes it a little bit easier to visualize if you're a beginner, IMO.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!