Question
JAVA programming, Dear SoAndSo, we are working on a ConnectFour game in java right now and I have everything working as per the requirements, all
JAVA programming,
Dear SoAndSo, we are working on a ConnectFour game in java right now and I have everything working as per the requirements, all except for the major and minor diagonals; something is seriously whopperjawed with them and I was seeing if someone could diagnose my problem (because I can't). The diagonals literally works 25% of the time.
I know there are ways to condense this code, but I'm not looking to condense it I am simply looking for the errors in my logic, if you need to see my complete code I can provide it too. I know this is a common issue with this popular assignment.
package connectFour;
import java.util.Scanner;
public class ConnectFourClient
{
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
boolean replayGame = false;
char[][] connectFourGridFirstGame = new char[6][7];
displayGrid(connectFourGridFirstGame);
playConnectFour(connectFourGridFirstGame);
do{
System.out.println();
System.out.print("Play again? (Y/N)");
if (keyboard.next().toUpperCase().equals("Y"))
{
char[][] connectFourGridAdditionalGame = new char[6][7];
displayGrid(connectFourGridAdditionalGame);
playConnectFour(connectFourGridAdditionalGame);
replayGame = true;
}
else
{
replayGame = false;
keyboard.close();
}
}
while(replayGame == true);
}
//displayGrid prints to the console a 6 x 7 grid seperated by vertical lines
public static void displayGrid(char[][] connectFourGrid)
{
System.out.println();
System.out.println(" 1 2 3 4 5 6 7");
for (int i = 0; i < connectFourGrid.length; i++)
{
for (int j = 0; j < connectFourGrid[i].length; j++)
{
System.out.print("|" + connectFourGrid[i][j]);
}
System.out.println("|");
}
for (int i = 0; i < connectFourGrid.length - 1; i++)
{
System.out.print("---");
}
System.out.println();
System.out.println();
}
//playConnectFour is the engine that determines who's turn it is
public static void playConnectFour(char[][] connectFourGrid)
{
boolean gameOver = false;
boolean playerOneTurn = true;
String userInput = "";
int columnPosition = 0;
char chipLetter;
//boolean isUserInputInt = false;
//true = Player One's turn (X) false = Player Two's turn (O)
while(!gameOver)
{
if (playerOneTurn)
{
System.out.print("Player One( X ) may pick column (1 - 7) to drop an 'X' >> ");
chipLetter = 'X';
}
else
{
System.out.print("Player Two ( O ) may pick column (1 - 7) to drop an 'O' >> ");
chipLetter = 'O';
}
try
{
userInput = keyboard.next();
//adjusts it to 0 based indexing from user input
columnPosition = Integer.parseInt(userInput) - 1;
//isUserInputInt = true;
}
catch (Exception Ex)
{
System.out.println("ERROR, please type a number between 1 - 7, please try again");
System.out.println();
//isUserInputInt = false;
continue;
}
if (columnPosition < 0 || columnPosition > 6)
{
System.out.println("ERROR, please type a number between 1 - 7, please try again");
System.out.println();
continue;
}
playerOneTurn = !playerOneTurn;
if (dropChip(connectFourGrid, columnPosition, chipLetter))
{
playerOneTurn = !playerOneTurn;
}
else
{
displayGrid(connectFourGrid);
if (gameStatus(connectFourGrid, columnPosition, chipLetter))
{
gameOver = true;
System.out.print("Player " + chipLetter + " won! Game Over");
}
}
}
}
//gameStatus checks the connectFourGrid for a winner using the four methods: checkColumn, checkRow, checkDiagonal1, checkDiagonal2
public static boolean gameStatus(char[][] connectFourGrid, int columnPosition, char chipLetter)
{
int rowPosition = 0;
for (int i = 0; i < connectFourGrid.length; i++)
{
if (connectFourGrid[i][columnPosition] != 0)
{
rowPosition = i;
break;
}
}
if (checkColumn(connectFourGrid, columnPosition, chipLetter, rowPosition))
{
return true;
}
if (checkRow(connectFourGrid, columnPosition, chipLetter, rowPosition))
{
return true;
}
if (checkDiagonal1(connectFourGrid, columnPosition, chipLetter, rowPosition))
{
return true;
}
if (checkDiagonal2(connectFourGrid, columnPosition, chipLetter, rowPosition))
{
return true;
}
return false;
}
//checkDiagonal1 checks for 4 in a row in the major diagonal direction (upper left to lower right).
public static boolean checkDiagonal1(char[][] connectFourGrid, int columnPosition, char chipLetter, int rowPosition)
{
int chipCounter = 1;
for (int i = rowPosition - 1; i >= 0; i--)
{
for (int j = columnPosition - 1; j >= 0; j--)
{
if (chipLetter == connectFourGrid[i][j])
{
chipCounter++;
}
else
{
break;
}
if (chipCounter >= 4)
{
return true;
}
}
}
for (int i = rowPosition + 1; i < connectFourGrid.length; i++)
{
for (int j = columnPosition; j < connectFourGrid[0].length; j++)
{
if (chipLetter == connectFourGrid[i][j])
{
chipCounter++;
}
else
{
break;
}
if (chipCounter >= 4)
{
return true;
}
}
}
return false;
}
//checkDiagonal2 checks for 4 in a row in the minor diagonal direction (upper right to lower left).
public static boolean checkDiagonal2(char[][] connectFourGrid, int columnPosition, char chipLetter, int rowPosition)
{
int chipCounter = 1;
for (int i = rowPosition + 1; i < connectFourGrid.length; i++)
{
for (int j = columnPosition - 1; j >= 0; j--)
{
if (chipLetter == connectFourGrid[i][j])
{
chipCounter++;
}
else
{
break;
}
if (chipCounter >= 4)
{
return true;
}
}
}
for (int i = rowPosition - 1; i >= 0; i--)
{
for (int j = columnPosition + 1; j < connectFourGrid[0].length; j++)
{
if (chipLetter == connectFourGrid[i][j])
{
chipCounter++;
}
else
{
break;
}
if (chipCounter >= 4)
{
return true;
}
}
}
return false;
}
//checkRow checks each horizontal row for 4 in a row (left to right)
public static boolean checkRow (char[][] connectFourGrid, int columnPosition, char chipLetter, int rowPosition)
{
int chipCounter = 1;
for (int i = columnPosition - 1; i >= 0; i--)
{
if (chipLetter == connectFourGrid[rowPosition][i])
{
chipCounter++;
}
else
{
break;
}
if (chipCounter >= 4)
{
return true;
}
}
for (int i = columnPosition + 1; i < connectFourGrid[0].length; i++)
{
if (chipLetter == connectFourGrid[rowPosition][i])
{
chipCounter++;
}
else
{
break;
}
if (chipCounter >= 4)
return true;
}
return false;
}
//checkColumn check each vertical column for 4 in a row (bottom to top)
public static boolean checkColumn (char[][] connectFourGrid, int columnPosition, char chipLetter, int rowPosition)
{
int chipCounter = 1;
if ((rowPosition + 4) <= 6)
{
for (int i = rowPosition + 1; i <= (rowPosition + 3); i++)
{
if (chipLetter == connectFourGrid[i][columnPosition])
{
chipCounter++;
}
else
{
break;
}
if (chipCounter == 4)
{
return true;
}
}
}
return false;
}
public static boolean dropChip (char[][] connectFourGrid, int columnPosition, char chipLetter)
{
for (int i = connectFourGrid.length - 1; i >= 0; i--)
{
if (connectFourGrid[i][columnPosition] == 0)
{
connectFourGrid[i][columnPosition] = chipLetter;
return false;
}
}
System.out.println("Column already full, please try again");
System.out.println();
return true;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started