4 Replies - 837 Views - Last Post: 25 March 2009 - 04:01 PM Rate Topic: -----

#1 ghaws  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-March 09

Evaluating multi-dimensional arrays whithin an 'if' statement

Posted 24 March 2009 - 10:26 PM

Hello there, this is the first time I've posted but I've been browsing the forum for a few days and seen that there a lot of knowledgeable and helpful folks around. I'm new to programming, and I'm having some trouble on a simple, command line tic-tac-toe game (I guess it's more of an efficiency problem). Unfortunately I do not have another class session for another two weeks, and I can't seem to find a solution in the book.

I set up a two-dimensional array to hold the string values, either X or O, entered by the users.
private static String [][] ghettoBoard = new String [3][3];


My problem is in setting up the win/draw conditions. The only way I can think of to detect a win or a draw is (just the first example):
if ((ghettoBoard [0][0] == "X") && (ghettoBoard[0][1] == "X") && (ghettoBoard[0][2] == "X"))


This seems to work just fine. However, the thought of creating a similar if statement for every possible permutation of wins or draws is daunting. First of all, I'm lazy. Second, it seems horribly inefficient, and will lead to a big chunk of ungainly code. My problem is I simply don't know enough about accessing arrays and the information stored inside them. If anyone can direct me to the docs or methods that might help me accomplish this task, I would be HUGELY grateful. Alternatively, as I secretly suspect/fear, if this is the only way to get it done, that would be great to know too.

Is This A Good Question/Topic? 0
  • +

Replies To: Evaluating multi-dimensional arrays whithin an 'if' statement

#2 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Evaluating multi-dimensional arrays whithin an 'if' statement

Posted 25 March 2009 - 05:58 AM

Quote

Unfortunately I do not have another class session for another two weeks

Spring Break??? Unfortunately we don't have one this year. :(

As for the checking for win conditions, you can use a loop to loop over the array to check each row column and diagonal. Since the grid is only 3*3 you can really have no inefficient way to check as it will go so fast you won't tell unless you add intentional delays and such in.
Was This Post Helpful? 1
  • +
  • -

#3 ghaws  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-March 09

Re: Evaluating multi-dimensional arrays whithin an 'if' statement

Posted 25 March 2009 - 11:50 AM

No spring break?? That's a travesty.

Thanks for the advice. I hadn't thought of being able to use a loop to check the conditions (like I said, I'm very new to programming). It might take me a while to figure out the exact method, but I'm going to try using nested loops, one each for rows, columns, and diagonals. Ideally, they will count the indexes until they find three in a row, or an empty index. If neither is true, it will return a draw. As soon as I figure it out I will post it here.
Was This Post Helpful? 0
  • +
  • -

#4 ghaws  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-March 09

Re: Evaluating multi-dimensional arrays whithin an 'if' statement

Posted 25 March 2009 - 12:08 PM

So I cobbled this together (this is FAR from the finished method, but it helps me to figure out the problem if I have a visual to work with).
public static boolean checkXwin()
		{	
			for(int x=0; x < ghettoBoard.length; x++)
			{
				for(int y = 0; y < ghettoBoard.length; y++)
				{
					if(ghettoBoard[x][y] == "X")
					{
						
						return pOneWins;
					}	
					else if(ghettoBoard[x][y] == " ")
					{
						return !scratch;
					}	
				}
			}	
			return scratch; 

}
Am I at least on the right track?

Edited to add what I had come up with. I've made a little progress, but now it returns a win if there is an X in any of the indexes, rather than consecutive X's, which is the goal. I think the problem is I just don't know the proper syntax.

This post has been edited by ghaws: 25 March 2009 - 01:09 PM

Was This Post Helpful? 0
  • +
  • -

#5 ghaws  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-March 09

Re: Evaluating multi-dimensional arrays whithin an 'if' statement

Posted 25 March 2009 - 04:01 PM

I may have actually figured it out. We'll see how it goes once it's written.

EDIT: Yep, I figured it out. Turns out it was much simpler than I thought (much like pretty much every problem I've encountered in programming so far). Thanks BigAnt, for providing my initial inspiration.

This post has been edited by ghaws: 25 March 2009 - 05:10 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1