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.
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--;
}
}
}
}
public void processCell(int rowIndex, int colIndex) { if (board[rowIndex][colIndex] != BoardCell.EMPTY) { ...
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...
how to randomly generate moves until the whole board is filled?. If the board size is c*r, you can only call the random function rand() c*r times (excluding the random function rand() that have been used in the provided code). Below is the code I need to convert. It is manual right now (user inputs numbers until whole board filled). I also have a piece of algorithmn below..but am unsure of how I can use it in the code: //Algorithm...
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 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 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...
For this exercise, you will complete the TicTacToe Board that we started in the 2D Arrays Lesson. We will add a couple of methods to the TicTacToe class. To track whose turn it is, we will use a counter turn. This is already declared as a private instance variable. Create a getTurn method that returns the value of turn. Other methods to implement: printBoard()- This method should print the TicTacToe array onto the console. The board should include numbers that...
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 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...
Programming Language : JAVA Write a class named Ship. Its purpose is to model a ship in the BattleShip game and its placement on the Battleship board. Ships exist on a 10 x 10 battleship board with ten rows labeled A through J and columns labeled 1 through 9 and the final column labeled 0 to indicate the tenth column. We will refer to the Ship placed on the board at the origin which the pair (r,c) where in the...
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...