Question

public void processCell(int rowIndex, int colIndex) {        if (board[rowIndex][colIndex] != BoardCell.EMPTY) {       ...

public void processCell(int rowIndex, int colIndex) {
       if (board[rowIndex][colIndex] != BoardCell.EMPTY) {
           for (int col = colIndex; col < getMaxCols(); col++) {
               if (board[rowIndex][col] == board[rowIndex][colIndex]){
                   board[rowIndex][col] = BoardCell.EMPTY;
                   score++;
               }
           }
           for (int col = colIndex; col > 0; col--) {
               if (board[rowIndex][col] == board[rowIndex][colIndex]) {
                   board[rowIndex][col] = BoardCell.EMPTY;
                   score++;
               }
           }
           for (int row = rowIndex; row < getMaxRows(); row++) {
               if (board[row][colIndex] == board[rowIndex][colIndex]) {
                   board[row][colIndex] = BoardCell.EMPTY;
                   score++;
               }
           }
           for (int row = rowIndex; row > 0; row--) {
               if (board[row][colIndex] == board[rowIndex][colIndex]) {
                   board[row][colIndex] = BoardCell.EMPTY;
                   score++;
               }
           }
       }

This is a game where I click on a point (board[rowindex][colindex] ) and the horizontal, vertical, and diagonal points with the same color are going to be empty. but my for loop is not working, every time when I click on a point, only that single point is empty. I'm not sure why.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

At looking code it is not clear that what matrix you are taking. Is it a Global matrix ? if not then we have to pass it in function.

what is score variable and why you are taking it ?

Anyways I have reviewed the code and find some missing point which I have added.We have to add some validation for corner cases here. diagonal case is missing in the code.

Find the modified code below,

public void processCell(int rowIndex, int colIndex) {
// First make check for valid indexes
if(rowIndex >= 0 && rowIndex < getMaxRows() && colIndex >=0 && colIndex < getMaxCols() ){ // I am assuming getMaxRows(),getMaxCols will return correct values
if (board[rowIndex][colIndex] != BoardCell.EMPTY) {
   //setting same column to empty
for (int col = colIndex; col < getMaxCols(); col++) {
if (board[rowIndex][col] == board[rowIndex][colIndex]){
board[rowIndex][col] = BoardCell.EMPTY;
score++;
}
}
for (int col = colIndex; col >= 0; col--) { // here we have to include 0 also
if (board[rowIndex][col] == board[rowIndex][colIndex]) {
board[rowIndex][col] = BoardCell.EMPTY;
score++;
}
}
       //setting same row values to empty
for (int row = rowIndex; row < getMaxRows(); row++) {
if (board[row][colIndex] == board[rowIndex][colIndex]) {
board[row][colIndex] = BoardCell.EMPTY;
score++;
}
}
for (int row = rowIndex; row >= 0; row--) { // here we have to include 0 also
if (board[row][colIndex] == board[rowIndex][colIndex]) {
board[row][colIndex] = BoardCell.EMPTY;
score++;
}
}
       //setting diagonal values to EMPTY
       int col=colIndex;
       int row=rowIndex;
       while(col < getMaxCols() && row < getMaxRows()){
           if (board[row][col] == board[rowIndex][colIndex]){
board[row][col] = BoardCell.EMPTY;
score++;
}
           col++;
           row++;
       }
         
       col=colIndex;
       row=rowIndex;
       while(col >= 0 && row > 0){
           if (board[row][col] == board[rowIndex][colIndex]){
board[row][col] = BoardCell.EMPTY;
score++;
}
           col--;
           row--;
       }
}
     
     
   }
}

Add a comment
Know the answer?
Add Answer to:
public void processCell(int rowIndex, int colIndex) {        if (board[rowIndex][colIndex] != BoardCell.EMPTY) {       ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner...

    Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner method, it declares a winner for horizontal wins, but not for vertical. if anyone can see what im doing wrong. public class ConnectFour { /** Number of columns on the board. */ public static final int COLUMNS = 7; /** Number of rows on the board. */ public static final int ROWS = 6; /** Character for computer player's pieces */ public static final...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • Please modify the following code to NOT have "TicTacToe extends Board" (I don't want any extending)....

    Please modify the following code to NOT have "TicTacToe extends Board" (I don't want any extending). Please ensure the resulting code completes EACH part of the following prompt: "Your task is to create a game of Tic-Tac-Toe using a 2-Dimensional String array as the game board. Start by creating a Board class that holds the array. The constructor should use a traditional for-loop to fill the array with "blank" Strings (eg. "-"). You may want to include other instance data......

  • This is my code for a TicTacToe game. However, I don't know how to write JUnit...

    This is my code for a TicTacToe game. However, I don't know how to write JUnit tests. Please help me with my JUnit Tests. I will post below what I have so far. package cs145TicTacToe; import java.util.Scanner; /** * A multiplayer Tic Tac Toe game */ public class TicTacToe {    private char currentPlayer;    private char[][] board;    private char winner;       /**    * Default constructor    * Initializes the board to be 3 by 3 and...

  • Write the following function: const int MIN_SIZE = 2; const int MAX_SIZE = 8; const int...

    Write the following function: const int MIN_SIZE = 2; const int MAX_SIZE = 8; const int UNKNOWN = 0; const int RED = 1; const int BLUE = 2; const char UNKNOWN_LETTER = '-'; const char RED_LETTER = 'X'; const char BLUE_LETTER = 'O'; const string UNKNOWN_STRING = "unknown"; const string RED_STRING = "X"; const string BLUE_STRING = "O"; /** * Requires: size <= MAX_SIZE and size is a positive even integer, *           0 <= row && row < size....

  • Hi i need heeeeelllllp on this assignment i need to print the numbers diagonal top right...

    Hi i need heeeeelllllp on this assignment i need to print the numbers diagonal top right corner to the bottom left corner and i dont know how to do it please help me thank you dd another method to the bottom of the "TwoDimArraysMethods.java" file called "printDiagonalRL()"                                         public static void printDiagonalRL(int[][] matrix) 4. Call this method in the main file ("TwoDimArraysAsParam.java") passing it "board." e.g. TwoDimArraysMethods.printDiagonal(board); 5. Your new method should print any numbers along the diagonal from...

  • PLEASE COMPLETE IN C++ LANGUAGE Location row : int = -1 col : int = -1...

    PLEASE COMPLETE IN C++ LANGUAGE Location row : int = -1 col : int = -1 setLocation(row : int, col : int) getRow(): int getColl): int isEmpty(): bool Details Write all the code necessary to implement the Location class as shown in the UML Class Diagram to the right. Do this in a project named snake (we'll continue adding files to this project as the term progresses). Your class definition and implementation should be in separate files. When complete, you...

  • I need help with this method (public static int computer_move(int board[][])) . When I run it...

    I need help with this method (public static int computer_move(int board[][])) . When I run it and play the compute.The computer enters the 'O' in a location that the user has all ready enter it sometimes. I need to fix it where the computer enters the 'O' in a location the user has not enter the "X' already package tictactoe; import java.util.Scanner; public class TicTacToeGame { static final int EMPTY = 0; static final int NONE = 0; static final...

  • JAVA JAR HELP...ASAP I have the code that i need for my game Connect 4, but...

    JAVA JAR HELP...ASAP I have the code that i need for my game Connect 4, but i need to create a jar file . the instructions are below. It has to pass some parameters. I am really confused on doing so.l I dont know what to do next. Can someone help me and give detailed descritiopm opn how you ran the jar file in CMD import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Connect4 {               ...

  • Can someone please complete the "allTheQueensAreSafe" function? #include <stdio.h> #include <stdlib.h> void printBoard(int *whichRow, int n)...

    Can someone please complete the "allTheQueensAreSafe" function? #include <stdio.h> #include <stdlib.h> void printBoard(int *whichRow, int n) {    int row, col;    for (row = 0; row < n; row++)    {        for (col = 0; col < n; col++)        {            printf("%c", (whichRow[col] == row) ? 'Q' : '.');        }        printf("\n");    }    printf("\n"); } int allTheQueensAreSafe(int *whichRow, int n, int currentCol) {    // TODO: Write a function that returns 1 if all the queens represented by    // this array are safe (i.e., none...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT