java - understanding a piece of code in array - java

why i need to write:
mat.length-1
in the second for loop (the loop it all conditions).
the loop:
for (int i = 0; i < mat.length-1; i++) {
for (int j = 0; j < mat.length-1; j++) {
if (i == j) {
j++;
if (i == mat.length - 1 && j == mat.length - 1) {
break;
}
}
if (i != j && mat[i][j] == mat[j][i]) {
flag = true;
} else {
flag = false;
if (flag == false) {
stop = 1;
i = mat.length - 1;
}
}
}
}
Checking program applies
Complete code:
public class test {
public static void main(String[] args) {
//int[][] mat = { { 9, 2, 4 }, { 2, 9, 7 }, { 4, 7, 9 } };
int[][]mat = { { 9, 2, 3, 4},
{ 2, 9, 6, 3},
{ 3, 6, 9 ,2},
{ 4, 3, 2 ,9}};
boolean flag = true;
int stop = 0;
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat.length; j++) {
System.out.print("[" + mat[i][j] + "]");
}
System.out.println();
}
for (int i = 0; i < mat.length-1; i++) {
for (int j = 0; j < mat.length-1; j++) {
if (i == j) {
j++;
if (i == mat.length - 1 && j == mat.length - 1) {
break;
}
}
if (i != j && mat[i][j] == mat[j][i]) {
flag = true;
} else {
flag = false;
if (flag == false) {
stop = 1;
i = mat.length - 1;
}
}
}
}
if (stop == 1) {
System.out.println("Not first folded matrix");
} else {
System.out.println("First folded matrix");
}
}
}
this is work but if i change it to
mat.length
It does not work
If I write a negative one then it stops the loop of the i before it reaches the end of the array.
Can explanation?

In your code, these lines serve no purpose as i and j would never be equal to (mat.length-1) in your code.
if (i == mat.length - 1 && j == mat.length - 1) {
break;
}
Change your code to :
for (int i = 0; i < mat.length; i++) {
for (int j = i+1; j < mat.length; j++) {
if (mat[i][j] != mat[j][i]) {
flag = false;
j = mat.length;
i = mat.length;
}
}
}
if (flag == false) {
System.out.println("Not first folded matrix");
} else {
System.out.println("First folded matrix");
}

Related

I'm getting wrong output and also I need a simplified solution

I came across a problem. The code is:
Scanner input = new Scanner(System.in);
int time = input.nextInt();
int line = input.nextInt();
input.nextline();
int[][] PrimaryArray = new int[12][11];
int[] Number = new int[12];
boolean[] dualPass = new boolean[12];
for (int i = 0; i < 12; i++) {
dualPass[i] = false;
}
for (int i = 0; i < line; i++) {
int SourceNode = input.nextInt();
int DistNode = input.nextInt();
input.nextLine();
if (SourceNode == 1) {
PrimaryArray[SourceNode][Number[1]] = DistNode;
Number[1]++;
}
if (SourceNode == 2) {
for (int j = 0; j < PrimaryArray[DistNode].length; j++) {
if (PrimaryArray[DistNode][j] == SourceNode)
dualPass[2] = true;
}
if (dualPass[2] != true) {
PrimaryArray[SourceNode][Number[2]] = DistNode;
Number[2]++;
}
dualPass[2] = false;
}
if (SourceNode == 3) {
for (int j = 0; j < PrimaryArray[DistNode].length; j++) {
if (PrimaryArray[DistNode][j] == SourceNode)
dualPass[3] = true;
}
if (dualPass[3] != true) {
PrimaryArray[SourceNode][Number[3]] = DistNode;
Number[3]++;
}
dualPass[3] = false;
}
if (SourceNode == 4) {
for (int j = 0; j < PrimaryArray[DistNode].length; j++) {
if (PrimaryArray[DistNode][j] == SourceNode)
dualPass[4] = true;
}
if (dualPass[4] != true) {
PrimaryArray[SourceNode][Number[4]] = DistNode;
Number[4]++;
}
dualPass[4] = false;
}
if (SourceNode == 5) {
for (int j = 0; j < PrimaryArray[DistNode].length; j++) {
if (PrimaryArray[DistNode][j] == SourceNode)
dualPass[5] = true;
}
if (dualPass[5] != true) {
PrimaryArray[SourceNode][Number[5]] = DistNode;
Number[5]++;
}
dualPass[5] = false;
}
if (SourceNode == 6) {
for (int j = 0; j < PrimaryArray[DistNode].length; j++) {
if (PrimaryArray[DistNode][j] == SourceNode)
dualPass[6] = true;
}
if (dualPass[6] != true) {
PrimaryArray[SourceNode][Number[6]] = DistNode;
Number[6]++;
}
dualPass[6] = false;
}
if (SourceNode == 7) {
for (int j = 0; j < PrimaryArray[DistNode].length; j++) {
if (PrimaryArray[DistNode][j] == SourceNode)
dualPass[7] = true;
}
if (dualPass[7] != true) {
PrimaryArray[SourceNode][Number[7]] = DistNode;
Number[7]++;
}
dualPass[7] = false;
}
if (SourceNode == 8) {
for (int j = 0; j < PrimaryArray[DistNode].length; j++) {
if (PrimaryArray[DistNode][j] == SourceNode)
dualPass[8] = true;
}
if (dualPass[8] != true) {
PrimaryArray[SourceNode][Number[8]] = DistNode;
Number[8]++;
}
dualPass[8] = false;
}
if (SourceNode == 9) {
for (int j = 0; j < PrimaryArray[DistNode].length; j++) {
if (PrimaryArray[DistNode][j] == SourceNode)
dualPass[9] = true;
}
if (dualPass[9] != true) {
PrimaryArray[SourceNode][Number[9]] = DistNode;
Number[9]++;
}
dualPass[9] = false;
}
if (SourceNode == 10) {
for (int j = 0; j < PrimaryArray[DistNode].length; j++) {
if (PrimaryArray[DistNode][j] == SourceNode)
dualPass[10] = true;
}
if (dualPass[10] != true) {
PrimaryArray[SourceNode][Number[10]] = DistNode;
Number[10]++;
}
dualPass[10] = false;
}
}
for (int i = 1; i < 11; i++) {
System.out.println(PrimaryArray[i][1]);
}
I have 2 problems:
about clean code that it is not and very long and has not readability. I want better solution.
about response that is false
for example when the input is :
3 14
1 2
1 5
1 9
1 3
2 8
2 6
5 9
9 4
9 6
3 10
8 11
7 11
10 7
6 7
output must be second member each. I want to say in this code that for example:
when we enter: 1 2
then 2 1
it must ignore 2 1.
output of code is wrong and I don't understand it.
For you first question, even if it's the wrong place there is an answer, just replace your
// ...
for (int i = 0; i < line; i++) {
int sourceNode = input.nextInt();
System.out.println("SourceNode: " + sourceNode);
int distNode = input.nextInt();
System.out.println("DistNode: " + distNode );
input.nextLine();
// you will get same response with following code instead all your if's:
for (int j = 0; j < primaryArray[distNode].length; j++) {
if (primaryArray[distNode][j] == sourceNode)
dualPass[sourceNode] = true;
}
if (dualPass[sourceNode] != true) {
primaryArray[sourceNode][number[sourceNode]] = distNode;
number[sourceNode]++;
}
dualPass[sourceNode] = false;
// end of code change
}
// ...
Your second answer only can be answered if we get more informations, i do not have any Idea what your code should do yet.

Check if numbers are successive

for(int i = 0; i < aLenght; i++) {
for(int j = 0; j < bLenght; j++) {
if(aChars[i] == bChars[j]) {
System.out.println(j +" == "+ i);
}
}
}
For instance the code above generates the following output:
3 == 0
5 == 0
4 == 1
6 == 1
Now I would like to check whether there exist successive numbers on both sides. For instance the example above would return true, because 3 == 0 and 4 == 1 are successive.
try this
for(int i = 0; i < aLenght - 1; i++) {
for(int j = 0; j < bLenght - 1; j++) {
if(aChars[i] == bChars[j] && aChars[i + 1] == bChars[j + 1]) {
return true;
}
}
}
notice that this covers the length of the array even when it iterates to ALenght - 1. Because when it reaches aLenght - 2, it will be compared with aLenght - 1 as well in
(aChars[i] == bChars[j] && aChars[i + 1] == bChars[j + 1]).
You can check it running this:
#Test
public void successive() {
char[] a = new char[]{'a', 'b', 'x', 'z', 'y'};
char[] b = new char[]{'r', 's', 't', 'a', 'b'};
boolean isSuccessive = false;
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < b.length - 1; j++) {
if (a[i] == b[j] && a[i + 1] == b[j + 1]) {
isSuccessive = true;
}
}
}
assertTrue(isSuccessive);
}

I can't seem to get a working main method for my project

I am programming a minesweeper game, and having a problem making the main method. I cant seem to get the text based game to output to the screen, allowing the user to interact with it. I have tried several ways, but none have worked.
public class Game {
private Board board;
boolean finish = false;
boolean win = false;
int turn = 0;
public void Jogo() {
board = new Board();
Play(board);
}
public void Play(Board board) {
do {
turn++;
System.out.println("Turn " + turn);
board.show();
finish = board.setPosition();
if (!finish) {
board.openNeighbors();
finish = board.win();
}
} while (!finish);
if (board.win()) {
System.out.println("Congratulations, you let the 10 fields with the mines in " + turn
+ " turns");
board.showMines();
} else {
System.out.println("Mine! You lost!");
board.showMines();
}
}
}
import java.util.Random;
import java.util.Scanner;
public class Board extends Game {
private int[][] mines;
private char[][] boardgame;
private int Line, Column;
Random random = new Random();
Scanner input = new Scanner(System.in);
public void Jogo() {
mines = new int[10][10];
boardgame = new char[10][10];
startMines();
randomMines();
fillTips();
startBoard();
}
public boolean win() {
int count = 0;
for (int line = 1; line < 9; line++)
for (int column = 1; column < 9; column++)
if (boardgame[line][column] == '_')
count++;
if (count == 10)
return true;
else
return false;
}
public void openNeighbors() {
for (int i = -1; i < 2; i++)
for (int j = -1; j < 2; j++)
if ((mines[Line + i][Column + j] != -1)
&& (Line != 0 && Line != 9 && Column != 0 && Column != 9))
boardgame[Line + i][Column + j] =
Character.forDigit(mines[Line + i][Column + j], 10);
}
public int getPosition(int Line, int Column) {
return mines[Line][Column];
}
public boolean setPosition() {
do {
System.out.print("\nLine: ");
Line = input.nextInt();
System.out.print("Column: ");
Column = input.nextInt();
if ((boardgame[Line][Column] != '_')
&& ((Line < 9 && Line > 0) && (Column < 9 && Column > 0)))
System.out.println("Field already shown");
if (Line < 1 || Line > 8 || Column < 1 || Column > 8)
System.out.println("Choose a number between 1 and 8");
} while ((Line < 1 || Line > 8 || Column < 1 || Column > 8)
|| (boardgame[Line][Column] != '_'));
if (getPosition(Line, Column) == -1)
return true;
else
return false;
}
public void show() {
System.out.println("\n Lines");
for (int Line = 8; Line > 0; Line--) {
System.out.print(" " + Line + " ");
for (int Column = 1; Column < 9; Column++) {
System.out.print(" " + boardgame[Line][Column]);
}
System.out.println();
}
System.out.println("\n 1 2 3 4 5 6 7 8");
System.out.println(" Columns");
}
public void fillTips() {
for (int line = 1; line < 9; line++)
for (int column = 1; column < 9; column++) {
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
if (mines[line][column] != -1)
if (mines[line + i][column + j] == -1)
mines[line][column]++;
}
}
public void showMines() {
for (int i = 1; i < 9; i++)
for (int j = 1; j < 9; j++)
if (mines[i][j] == -1)
boardgame[i][j] = '*';
show();
}
public void startBoard() {
for (int i = 1; i < mines.length; i++)
for (int j = 1; j < mines.length; j++)
boardgame[i][j] = '_';
}
public void startMines() {
for (int i = 0; i < mines.length; i++)
for (int j = 0; j < mines.length; j++)
mines[i][j] = 0;
}
public void randomMines() {
boolean raffled;
int Line, Column;
for (int i = 0; i < 10; i++) {
do {
Line = random.nextInt(8) + 1;
Column = random.nextInt(8) + 1;
if (mines[Line][Column] == -1)
raffled = true;
else
raffled = false;
} while (raffled);
mines[Line][Column] = -1;
}
}
}
public class MineSweeper extends Board {
public static void main(String[] args) {
Game game = new Game();
}
}

How to properly checkDiagonalWin in connect 4

My check vertical win and check horizontal win work perfectly fine, however i dont know what to do with my check diagonal code to make it actually check diagonal. Some guidance would be much appreciated and this is in java. Thank you.
private boolean checkVerticalWin()
{
PieceType type = myBoard[myLastPoint.x][myLastPoint.y];
System.out.println("check vert");
for(int j = 0; j < myNumColumns; j++)
{
for(int i = 0; i < myNumRows; i++)
{
if(myBoard[i][j] == type && myBoard[i][j] != null )
{
count++;
if(count == 1)
{
myWinBegin = new Point(i,j);
}
}
else
{
myWinBegin = null;
count = 0;
}
System.out.println(count);
if(count == myWinLength)
{
myWinEnd = new Point(i,j);
return true;
}
}
}
myWinBegin = null;
return false;
}
private boolean checkHorizontalWin()
{
System.out.println("test");
PieceType type = myBoard[myLastPoint.x][myLastPoint.y];
for(int i = 0; i < myNumRows; i++)
{
for(int j = 0; j < myNumColumns; j++)
{
if(myBoard[i][j] == type && myBoard[i][j] != null)
{
count++;
if (count == 1)
{
myWinBegin = new Point(i,j);
}
}
else
{
myWinBegin = null;
count = 0;
}
if(count == myWinLength)
{
myWinEnd = new Point(i,j);
return true;
}
}
}
myWinBegin = null;
return false;
}
private boolean checkDiagonalWin()
{
PieceType type = myBoard[myLastPoint.x][myLastPoint.y];
for(int i = 0; i < myNumRows; i++)
{
for (int j = 0; j < myNumColumns; j++)
{
if(myBoard[i][j] == type && myBoard[i][j] != null )
{
count++;
myWinBegin = new Point(i,j);
}
else
{
count = 0;
myWinEnd = new Point(i,j);
}
if(count == myWinLength)
{
return true;
}
}
}
for(int j = 0; j < myNumColumns; j--)
{
for (int i = 0; i < myNumRows; i--)
{
if(myBoard[i][j] == type && myBoard[i][j] != null )
{
count++;
myWinBegin = new Point(i,j);
}
else
{
count = 0;
}
if(count == myWinLength)
{
myWinEnd = new Point(i,j);
return true;
}
}
}
for(int j = 0; j < myNumColumns; j++)
{
for (int i = 0; i < myNumRows; i--)
{
if(myBoard[i][j] == type && myBoard[i][j] != null )
{
count++;
}
else
{
myWinBegin = new Point(i,j);
count = 0;
}
if(count == myWinLength)
{
myWinEnd = new Point(i,j);
return true;
}
}
}
for(int j = 0; j < myNumColumns; j--)
{
for (int i = 0; i < myNumRows; i++)
{
if(myBoard[i][j] == type && myBoard[i][j] != null )
{
count++;
myWinBegin = new Point(i,j);
}
else
{
count = 0;
}
if(count == myWinLength)
{
myWinEnd = new Point(i,j);
return true;
}
}
}
return false;
}
So basically, you need a start point, you then need to determine in which direction to move
With that idea in hand, you could use something like...
boolean win = true;
for (int count = 0; count < 4; count++) {
if (row < myNumRows && row >= 0 && col < myNumColumns && col >= 0) {
int test = myBoard[row][col];
if (test != check) {
win = false;
break;
}
} else {
break;
}
row += rowDelta;
col += colDelta;
}
As the basic algorithm. All this does is checks each cell from a start point, to a total of 4 cells, the algorithm moves by the specified delta/direction and keeps checking while each cell matches the check value.
Now, I'd wrap this in a simple method
public boolean didWin(int[][] grid, int check, int row, int col, int rowDelta, int colDelta) {
boolean win = true;
for (int count = 0; count < 4; count++) {
if (row < ROWS && row >= 0 && col < COLUMNS && col >= 0) {
int test = grid[row][col];
if (test != check) {
win = false;
break;
} else {
break;
}
}
row += rowDelta;
col += colDelta;
}
return win;
}
which makes it simpler to call, know given any point, you can do something like...
int startRow = ...;
int startCol = ...;
int player = ...;
if (didWin(myBoard, player, startRow, startCol, 1, 0) || // Vertical, down
didWin(myBoard, 1, startRow, startCol, 0, 1) || // Right
didWin(myBoard, 1, startRow, startCol, 0, -1) || // Left
didWin(myBoard, 1, startRow, startCol, 1, 1) || // Right/down
didWin(myBoard, 1, startRow, startCol, -1, -1) || // Left/Up
didWin(myBoard, 1, startRow, startCol, 1, -1) || // Down/Left
didWin(myBoard, 1, startRow, startCol, -1, 1) // Up/Right
) {
// You be the winner
}
nb: I've left out check a vertical up direction, because it's unlikely that you could actually win this way
ps: I'd be even more lazy and would just have a didWin method, which did the above checks and returned true or false, but I'm lazy
So, the startRow and startCol would represent the anchor point around which you want to check and would, in this example, represent the last drop.
This example uses a int to represent the player/token, but you could use anything, all this does is compares the token you supply with the values in the array

Check for match method for 2D array game

I am creating a game that is called "Shapeshifter" and can't figure out how to check the 2D array game board to see if the shape that the user made with the cursor is actually the shape wanted. Any amount of code works, it just needs to actually work with the rest of the game.
Here is the code that I have done:
public static final int COLS = 7;
public static final int ROWS = 7;
public static int ZEE = 0;
public static int TEE = 1;
public static int ES = 2;
public static int OH = 3;
public static int JAY = 4;
public static int EL = 5;
public static int NUM_POLYOMINOS = 6;
public static int PIECE_SIZE = 4;
public static int UP = 8;
public static int LEFT = 4;
public static int DOWN = 2;
public static int RIGHT = 6;
public static int QUIT = 5;
public static int EMPTY = 0;
public static int POLY_PIECE = 1;
public static int PLAYER = 2;
private static int[][] getRandomPoly()
{
int rint = new java.util.Random().nextInt(NUM_POLYOMINOS);
if (rint == ZEE)
return new int[][]{{1, 1, 0},
{0, 1, 1}};
else if (rint == TEE)
return new int[][]{{0, 1, 0},
{1, 1, 1}};
else if (rint == ES)
return new int[][]{{0, 1, 1},
{1, 1, 0}};
else if (rint == OH)
return new int[][]{{1, 1},
{1, 1}};
else if (rint == JAY)
return new int[][]{{1, 0, 0},
{1, 1, 1}};
else //if (rint == EL)
return new int[][]{{0, 0, 1},
{1, 1, 1}};
}
public void printCurrentPoly()
{
if (currentPoly == null)
return;
System.out.println("Current polyomino:");
System.out.println();
for (int i = 0; i < currentPoly.length; i++)
{
for (int j = 0; j < currentPoly[0].length; j++)
if (currentPoly[i][j] == 0)
System.out.print(" ");
else
System.out.print("#");
System.out.println();
}
System.out.println();
}
public void initializeBoard()
{
for (int i = 0; i < board.length; i++)
for (int j = 0; j < board[0].length; j++)
board[i][j] = 0;
currentRow = board.length / 2;
currentCol = board[0].length / 2;
board[currentRow][currentCol] = PLAYER;
for (int i = 0; i < PIECE_SIZE; i++)
{
int rrow = new java.util.Random().nextInt(ROWS),
rcol = new java.util.Random().nextInt(COLS);
while ((rrow == 0 || rrow == board.length - 1) ||
(rcol == 0 || rcol == board[0].length - 1) ||
(rrow == board.length / 2 && rcol == board[0].length / 2) ||
(board[rrow][rcol] == POLY_PIECE))
{
rrow = new java.util.Random().nextInt(ROWS);
rcol = new java.util.Random().nextInt(COLS);
}
board[rrow][rcol] = POLY_PIECE;
}
currentPoly = getRandomPoly();
}
public static void main(String args[])
{
boolean done = false;
ShapeShifter ss = new ShapeShifter();
ss.initializeBoard();
ss.printCurrentPoly();
ss.printBoard();
while (!done)
{
int move = ss.getMove();
if (move == QUIT)
done = true;
else
{
ss.executeMove(move);
if (ss.checkForMatch())
done = true;
System.out.println("\n");
ss.printCurrentPoly();
ss.printBoard();
}
}
ss.printResult();
}
private int[][] board = new int[ROWS][COLS];
private int[][] currentPoly;
private int numMoves, currentRow, currentCol;
private boolean winner;
private Scanner stdin = new Scanner(System.in);
public void printBoard()
{
System.out.println("+-------+");
for(int i = 0; i < board.length; i++)
{
System.out.print("|");
for(int j = 0; j < board[0].length; j++)
{
if(board[i][j] == POLY_PIECE)
System.out.print("#");
else if (board[i][j] == PLAYER)
System.out.print("H");
else
System.out.print(" ");
}
System.out.println("|");
}
System.out.println("+-------+");
}
public int getMove()
{
System.out.println("Were do you want to move? (4=left, 2=down, 8=up, 6=right or 5=quit)");
int move = stdin.nextInt();
while(move != LEFT && move != RIGHT && move != UP && move != DOWN && move != QUIT)
{
System.out.println("Invalid, only valid moves allowed are 4=left, 2=down, 8=up, 6=right or 5=quit");
move = stdin.nextInt();
}
return move;
}
public void executeMove(int move)
{
if(move == LEFT)
moveLeft(move);
if(move == RIGHT)
moveRight(move);
if(move == UP)
moveUp(move);
if(move == DOWN)
moveDown(move);
numMoves++;
}
private void moveLeft(int move)
{
int currentRow = 0, currentCol = 0;
for(int i = 0; i < board.length; i++)
for(int j = 0; j < board[0].length; j++)
if(board[i][j] == 2)
{ currentRow = i;
currentCol = j;
}
if(move == LEFT)
{
if(currentCol != 0 && board[currentRow][currentCol - 1] != POLY_PIECE)
{
board[currentRow][currentCol - 1] = PLAYER;
board[currentRow][currentCol] = EMPTY;
}
if(currentCol != 0 && board[currentRow][currentCol - 1] == POLY_PIECE && board[currentRow][currentCol - 2] != POLY_PIECE)
{
board[currentRow][currentCol - 1] = PLAYER;
board[currentRow][currentCol - 2] = POLY_PIECE;
board[currentRow][currentCol] = EMPTY;
}
}
}
private void moveRight(int move)
{
int currentRow = 0, currentCol = 0;
for(int i = 0; i < board.length; i++)
for(int j = 0; j < board[0].length; j++)
if(board[i][j] == 2)
{ currentRow = i;
currentCol = j;
}
if(move == RIGHT)
{ if (currentCol != 6 && board[currentRow][currentCol + 1] != POLY_PIECE)
{
board[currentRow][currentCol + 1] = PLAYER;
board[currentRow][currentCol] = EMPTY;
}
if(currentCol != 6 && board[currentRow][currentCol + 1] == POLY_PIECE && board[currentRow][currentCol + 2] != POLY_PIECE)
{
board[currentRow][currentCol + 1] = PLAYER;
board[currentRow][currentCol + 2] = POLY_PIECE;
board[currentRow][currentCol] = EMPTY;
}
}
}
private void moveUp(int move)
{
int currentRow = 0, currentCol = 0;
for(int i = 0; i < board.length; i++)
for(int j = 0; j < board[0].length; j++)
if(board[i][j] == 2)
{ currentRow = i;
currentCol = j;
}
if(move == UP)
{
if(currentRow != 0 && board[currentRow - 1][currentCol] != POLY_PIECE)
{
board[currentRow - 1][currentCol] = PLAYER;
board[currentRow][currentCol] = EMPTY;
}
if(currentRow != 0 && board[currentRow - 1][currentCol] == POLY_PIECE && board[currentRow - 2][currentCol] != POLY_PIECE)
{
board[currentRow - 1][currentCol] = PLAYER;
board[currentRow - 2][currentCol] = POLY_PIECE;
board[currentRow][currentCol] = EMPTY;
}
}
}
private void moveDown(int move)
{
int currentRow = 0, currentCol = 0;
for(int i = 0; i < board.length; i++)
for(int j = 0; j < board[0].length; j++)
if(board[i][j] == 2)
{ currentRow = i;
currentCol = j;
}
if(move == DOWN)
{ if(currentRow != 6 && board[currentRow + 1][currentCol] != POLY_PIECE)
{
board[currentRow + 1][currentCol] = PLAYER;
board[currentRow][currentCol] = EMPTY;
}
if(currentRow != 6 && board[currentRow + 1][currentCol] == POLY_PIECE && board[currentRow + 2][currentCol] != POLY_PIECE)
{
board[currentRow + 1][currentCol] = PLAYER;
board[currentRow + 2][currentCol] = POLY_PIECE;
board[currentRow][currentCol] = EMPTY;
}
}
}
public void printResult()
{
if(checkForMatch() == false)
System.out.println("You did not win. You used " + numMoves + " moves to fail.");
else
System.out.println("You won!!n/" + "It took " + numMoves + " moves to complete.");
}
}
Here is the final piece to the game that I am having trouble with:
public boolean checkForMatch()
{
}
You didn't say whether the polyomino (not polynomial) can be in any orientation. If so, I think you'll have to loop through the rotations and reflections and test each one. For each orientation there are not many positions where it can line up with the board. For each you need to check each spot and see if the player has filled them all in.
Since your polyominoes are defined in advance, for optimization you could also define constants for each one saying which ways it's symmetrical, so you can avoid redundant tests. Not that extra tests would actually matter.
You can "re-use" a lot of the code you already wrote for printCurrentPoly and printBoard to implement checkForWinner.
The idea is to go through all the cells of the board. For each cell check: if the current poly would start at this cell are all # cells in the poly matched with the player char (o or x) on the board. If yes, the player is a winner. Otherwise go to the next cell.
Here the relevant code:
/**
* Returns true when the current polyTicTacToc can be found at x,y of the
* board, with its cells filled for the given player.
*/
private boolean isPoly(int x, int y, char player) {
if (currentPoly == null)
return false;
for (int i = 0; i < currentPoly.length; i++) {
for (int j = 0; j < currentPoly[0].length; j++) {
char c = currentPoly[i][j];
if (c == '#') {
if (boardCellAt(x + i, y + j) != player) {
return false;
}
}
}
}
return true;
}
private char boardCellAt(int i, int j) {
if (i >= board.length || j >= board[0].length) {
return ' ';
}
return board[i][j];
}
/**
* Check the entire game board to see if the given player has constructed
* the polyomino.
*/
public boolean checkForWinner(char player)// Lab2
{
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (isPoly(i, j, player)) {
return true;
}
}
}
return false;
}
BTW: There is a bug in printBoard. The loop should look like this:
for(int j = 0; j < board[0].length; j++)
or you get an ArrayIndexOutOfBoundsException.
Also in printBoard you should change the first print to System.out.print("|"); otherwise it looks like there are extra cells left on the board.

Categories