Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Check values in an array (Java)?
Lets say I have a 3x3 array that looks like this:
1 2 3
4 5 6
7 8 9
Now, the program is running, placing letter values in the array and checking the array in whole every iteration. When the array looks like this:
A 2 3
A 5 6
A 8 9
or
A A A
4 5 6
7 8 9
etc, for any arrangement that has 3 matching values in a row. I need to be able to recognize this and perform an action. I was thinking the compareto method would work, but I haven't figured out a way to do this. I am not sure how to check values in specific array positions against a variable. Any suggestions?
It needs to: check for a tictactoe type conditon. If it exists, reset those positions back to their original values. If it doesn't, continue the method. It also needs to check for a catsgame type condition. Meaning, if all the spaces are filled but no tictactoe exists. Or enough of the spaces are filled that no tictactoe conditon *could* exist.
Please help!
2 Answers
- husoskiLv 710 years agoFavorite Answer
Write a low-level check function. You didn't say what you were using to represent the cells. I'm using String in the following.
static boolean CheckTTT( String[][] board, int r1, int c1, int r2, int c1, int r3, int c3)
{
boolean result = board[r1][c1].equals( board[r2][c2] ) && board[r1][r2].equals( board[r3][c3] );
if (result) // If "tictactoe" found
{
// reset the cells at board[r1][c1], board[r2][c2] and board[r3][c3]
}
return result.
}
This will check *any* three positions in the board for equality. It's the caller's duty to insure that these are in straight lines. For example:
for (row=0; row<3; ++row)
{ checkTTT(board, row, 0, row, 1, row, 2); }
...will check all three rows. You can add some logic if you want an indication of whether or how many rows were reset. CheckTTT() returns true if it found an reset a match.
- ?Lv 44 years ago
do one loop with the aid of, like this // hand do the 1st one that is index 0 - constantly get a streak of one, top? =) char currentChar = funds[0]; int currentStreak = a million; int longestStreak = a million; // did first one, initiate on 2d - it fairly is index a million for(int i = a million; i < funds.length; i++) { // ascertain if we are nevertheless in a streak if(funds[i] == currentChar) { currentStreak++; // we are - amplify the streak // because of the fact that we proceed the streak, ascertain if it fairly is the recent checklist! if(currentStreak > longestStreak) { longestStreak = currentStreak; // set to new checklist } // end if new checklist } // end if eq else { // no longer equivalent, making a sparkling streak currentChar = funds[i]; // keep the recent streak's coin currentStreak = a million; // constantly a minimum of a streak of one :) } // end else } // end for funds device.out.println("The longest streak replaced into " + longestStreak); you are able to adapt this code to keep which coin the longest streak replaced into, or you will desire to adapt it to maintain the longest heads streak, and the longest tails streak. stable success!