Tic Tac Toe in Java printing error - java

I've almost got this homework assignment finished, but I'm having some printing errors with the output.
The game is functional, but there's a problem with methods printOBoard and printXBoard where it messes up the game board when a move is made. The problem becomes worse the longer the game goes on. Thanks for your help.
import java.util.Scanner;
public class TicTacToe {
private enum Tiles {
X, O, EMPTY;
}
public static void main(String[] args) {
int i;
int j;
//initialize 2d array of enum types called "board"
Tiles[][] board = new Tiles[3][3];
for (i=0; i<board.length; i++)
for (j=0; j<board.length; j++)
board[i][j] = Tiles.EMPTY;
//print out an empty board as a 2d array, with each tile set as "EMPTY"
printBoard(board);
int row, col;
int countEmpty=0;
//initial countEmpty count, if it's less than 1, the board is full and the game is over.
for (i=0; i<board.length; i++)
for (j=0; j<board.length; j++)
if (board[i][j] == Tiles.EMPTY)
countEmpty++;
while (countEmpty > 0) {
//Player O enters the row coordinate
System.out.println("Player O's turn.");
System.out.println("Player O: Enter row (0, 1, or 2):");
Scanner stdin1 = new Scanner(System.in);
row = stdin1.nextInt();
//Player O enters the column coordinate
System.out.println("Player O: Enter column (0, 1, or 2):");
Scanner stdin2 = new Scanner(System.in);
col = stdin2.nextInt();
//If the tile is empty, it was a valid move, and an 'O' is placed on the spot.
if (board[row][col] == Tiles.EMPTY) {
board[row][col] = Tiles.O;
//MOVE FOR O ********************
printOBoard(board, row, col);
checkWin(board);
if (checkWin(board) == 2)
;
else
break;
}
//If the tile is not empty, it was not a valid move, and Player O is prompted to try again.
else {
System.out.println("Space already occupied. Please choose another.");
System.out.println("Player O's turn.");
//Player 0 enters the row coordinate
System.out.println("Player O: Enter row (0, 1, or 2):");
stdin1 = new Scanner(System.in);
row = stdin1.nextInt();
//Player O enters the column coordinate
System.out.println("Player O: Enter column (0, 1, or 2):");
stdin2 = new Scanner(System.in);
col = stdin2.nextInt();
//ERROR MOVE FOR O********************
board[row][col] = Tiles.O;
printOBoard(board, row, col);
checkWin(board);
if (checkWin(board) == 2)
;
else
break;
}
//Player X enters the row coordinate
System.out.println("Player X's turn.");
System.out.println("Player X: Enter row (0, 1, or 2):");
Scanner stdin3 = new Scanner(System.in);
row = stdin3.nextInt();
//Player X enters the column coordinate
System.out.println("Player X: Enter column (0, 1, or 2):");
Scanner stdin4 = new Scanner(System.in);
col = stdin4.nextInt();
if (board[row][col] == Tiles.EMPTY) {
board[row][col] = Tiles.X;
printXBoard(board, row, col);
//MOVE FOR X *************************************************
checkWin(board);
if (checkWin(board) == 2)
;
else
break;
}
else {
System.out.println("Space already occupied. Please choose another.");
System.out.println("Player O's turn.");
System.out.println("Player O: Enter row (0, 1, or 2):");
stdin3 = new Scanner(System.in);
row = stdin3.nextInt();
//Player O enters the column coordinate
System.out.println("Player O: Enter column (0, 1, or 2):");
stdin4 = new Scanner(System.in);
col = stdin4.nextInt();
board[row][col] = Tiles.O;
//ERROR MOVE FOR X ****************************************
printXBoard(board, row, col);
checkWin(board);
if (checkWin(board) == 2)
;
else
break;
}
//After both players move, we check to see if the board is full.
countEmpty = 0;
for (i=0; i<board.length; i++)
for (j=0; j<board.length; j++)
if (board[i][j] == Tiles.EMPTY)
countEmpty++;
}
}
//method printBoard prints out a grid of EMPTY's and returns nothing
private static void printBoard(Tiles board[][]) {
int i, j;
System.out.println(" -----------------------------");
System.out.println("| | | |");
for (i=0; i<board.length; i++){
for (j=0; j<board.length; j++){
System.out.printf("| " + board[i][j] + " ");
}
System.out.println("|");
System.out.println("| | | |");
System.out.println(" -----------------------------");
if (i<2)
System.out.println("| | | |");
}
return;
}
//method printXBoard prints out the grid modified with the addition of an X after Player X's turn
private static void printXBoard(Tiles board[][], int curRow, int curCol) {
int i, j;
System.out.println(" -----------------------------");
System.out.println("| | | |");
for (i=0; i<board.length; i++){
for (j=0; j<board.length; j++){
if (i == curRow && j == curCol)
board[i][j] = Tiles.X;
else
;
System.out.printf("| " + board[i][j] + " ");
}
System.out.println("|");
System.out.println("| | | |");
System.out.println(" -----------------------------");
if (i<2)
System.out.println("| | | |");
}
return;
}
//method printOBoard prints out the grid modified with the addition of an X after Player X's turn
private static void printOBoard(Tiles board[][], int curRow, int curCol) {
int i, j;
System.out.println(" -----------------------------");
System.out.println("| | | |");
for (i=0; i<board.length; i++){
for (j=0; j<board.length; j++){
if (i == curRow && j == curCol)
board[i][j] = Tiles.O;
else
;
System.out.printf("| " + board[i][j] + " ");
}
System.out.println("|");
System.out.println("| | | |");
System.out.println(" -----------------------------");
if (i<2)
System.out.println("| | | |");
}
return;
}
//method checkWin checks all possible winning combinations for both players.
private static int checkWin(Tiles board[][]) {
if (board[0][0] == Tiles.X && board[0][1] == Tiles.X && board[0][2] == Tiles.X){
System.out.println("Player X wins!");
return 1;
}
else if (board[0][0] == Tiles.X && board[1][1] == Tiles.X && board[2][2] == Tiles.X){
System.out.println("Player X wins!");
return 1;
}
else if (board[0][0] == Tiles.X && board[1][0] == Tiles.X && board[2][0] == Tiles.X){
System.out.println("Player X wins!");
return 1;
}
else if (board[1][0] == Tiles.X && board[1][1] == Tiles.X && board[1][2] == Tiles.X){
System.out.println("Player X wins!");
return 1;
}
else if (board[2][0] == Tiles.X && board[2][1] == Tiles.X && board[2][2] == Tiles.X){
System.out.println("Player X wins!");
return 1;
}
else if (board[0][1] == Tiles.X && board[1][1] == Tiles.X && board[2][1] == Tiles.X){
System.out.println("Player X wins!");
return 1;
}
else if (board[0][2] == Tiles.X && board[1][2] == Tiles.X && board[2][2] == Tiles.X){
System.out.println("Player X wins!");
return 1;
}
else if (board[2][0] == Tiles.X && board[1][1] == Tiles.X && board[0][2] == Tiles.X){
System.out.println("Player X wins!");
return 1;
}
//check if player O wins
else if (board[0][0] == Tiles.O && board[0][1] == Tiles.O && board[0][2] == Tiles.O){
System.out.println("Player O wins!");
return 0;
}
else if (board[0][0] == Tiles.O && board[1][1] == Tiles.O && board[2][2] == Tiles.O){
System.out.println("Player O wins!");
return 0;
}
else if (board[0][0] == Tiles.O && board[1][0] == Tiles.O && board[2][0] == Tiles.O){
System.out.println("Player O wins!");
return 0;
}
else if (board[1][0] == Tiles.O && board[1][1] == Tiles.O && board[1][2] == Tiles.O){
System.out.println("Player O wins!");
return 0;
}
else if (board[2][0] == Tiles.O && board[2][1] == Tiles.O && board[2][2] == Tiles.O) {
System.out.println("Player O wins!");
return 0;
}
else if (board[0][1] == Tiles.O && board[1][1] == Tiles.O && board[2][1] == Tiles.O) {
System.out.println("Player O wins!");
return 0;
}
else if (board[0][2] == Tiles.O && board[1][2] == Tiles.O && board[2][2] == Tiles.O){
System.out.println("Player O wins!");
return 0;
}
else if (board[2][0] == Tiles.O && board[1][1] == Tiles.O && board[0][2] == Tiles.O) {
System.out.println("Player O wins!");
return 0;
}
else
return 2;
}
}

There is a problem in 2nd attempt to place X (if space is occupied by O), you copy pasted code from O and haven't changed player & tile.
System.out.println("Player O's turn.");
board[row][col] = Tiles.O;
It was pain to read this code -_- but you'll get better eventually ;) .

The reason as to why this is happening when printing out the Letters onto your board(which I've run on my mac) is that when you're printing the letters, you're using println...since you're accompanying a certain amount of space per block you should be using printf...for example:
System.out.printf("%10s", LetterVariableForTurn);
This will give the LetterVariableForTurn 10 spaces of text to write within.
here is a link to help you more: printf
Hope this helps!

Related

TicTacToe using 2D array in java

Hello Everyone I need help.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[][] board = {
{'?','?','?'},
{'?','?','?'},
{'?','?','?'}
};
System.out.print("Type any key to play the game and type 'n' to stop the game: ");
String Stop = sc.nextLine();
while(true){
if(Stop.equals("n"))break;
System.out.print("Player" + "[" + "X" + "]: ");
int PlayerX = sc.nextInt();
if(PlayerX == 1){
board[2][0] = 'x';
}
if(PlayerX == 2){
board[2][1] = 'x';
}
if(PlayerX == 3){
board[2][2] = 'x';
}
if(PlayerX == 4){
board[1][0] = 'x';
}
if(PlayerX == 5){
board[1][1] = 'x';
}
if(PlayerX == 6){
board[1][2] = 'x';
}
if(PlayerX == 7){
board[0][0] = 'x';
}
if(PlayerX == 8){
board[0][1] = 'x';
}
if(PlayerX == 9){
board[0][2] = 'x';
}
for(char[] x1 : board){
for(char x2 : x1){
System.out.print(x2 + "\t");
}
System.out.println();
}
System.out.print("Player" + "[" + "O" + "]: ");
int PlayerO = sc.nextInt();
if(PlayerO == 1){
board[2][0] = 'o';
}
if(PlayerO == 2){
board[2][1] = 'o';
}
if(PlayerO == 3){
board[2][2] = 'o';
}
if(PlayerO == 4){
board[1][0] = 'o';
}
if(PlayerO == 5){
board[1][1] = 'o';
}
if(PlayerO == 6){
board[1][2] = 'o';
}
if(PlayerO == 7){
board[0][0] = 'o';
}
if(PlayerO == 8){
board[0][1] = 'o';
}
if(PlayerO == 9){
board[0][2] = 'o';
}
for(char[] x1 : board){
for(char x2 : x1){
System.out.print(x2 + "\t");
}
System.out.println();
}
}
}
}
I am trying to make a simple tictactoes program in java. I am already done in placing the X and O, but I am struggling on checking of wether there is a winner or not.
I am confused on what code I will type o check the winner of the program.
You simply just need to write some code to check if there are 3 matches in each row column and diagonal.
You can utilise for loops to do this more efficiently
public static char checkWinner(char[][] board) {
// Check rows
for (int i = 0; i < 3; i++) {
if (board[i][0] != '?' && board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
return board[i][0];
}
}
// Check columns
for (int j = 0; j < 3; j++) {
if (board[0][j] != '?' && board[0][j] == board[1][j] && board[1][j] == board[2][j]) {
return board[0][j];
}
}
// Check diagonal
if (board[0][0] != '?' && board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
return board[0][0];
}
// Check anti-diagonal
if (board[0][2] != '?' && board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
return board[0][2];
}
// No winner
return '?';
}
If you havent come accross for loops yet, there are plenty of good tutorials out there such as https://www.w3schools.com/java/java_for_loop.asp
(Note, you can also use loops to clean up some of your pre-existing code)

Sorting through 2D array for Tic Tac Toe winner

I have made this very obnoxious way of finding the winner in a tic tac toe game and I was just wondering if there is a simpler way of doing this.
This is the method I currently have, and as you can see it is very redundant and repetitive. Any tips to narrow this down would be amazing. I'm thinking maybe a nested for-loop might work but not entirely sure how to set that up within this.
public static boolean isGameOver(char[][] gameBoard) {
//Testing for Horizontal Win
if(gameBoard[0][0] == 'X' && gameBoard[0][2] == 'X' && gameBoard [0][4] == 'X') {
System.out.println("Player Wins!\n");
playerScore++;
return true;
}
if(gameBoard[0][0] == 'O' && gameBoard[0][2] == 'O' && gameBoard [0][4] == 'O') {
System.out.println("CPU Wins!\n");
cpuScore++;
return true;
}
if(gameBoard[2][0] == 'X' && gameBoard[2][2] == 'X' && gameBoard [2][4] == 'X') {
System.out.println("Player Wins!\n");
playerScore++;
return true;
}
if(gameBoard[2][0] == 'O' && gameBoard[2][2] == 'O' && gameBoard [2][4] == 'O') {
System.out.println("CPU Wins!\n");
cpuScore++;
return true;
}
if(gameBoard[4][0] == 'X' && gameBoard[4][2] == 'X' && gameBoard [4][4] == 'X') {
System.out.println("Player Wins!\n");
playerScore++;
return true;
}
if(gameBoard[4][0] == 'O' && gameBoard[4][2] == 'O' && gameBoard [4][4] == 'O') {
System.out.println("CPU Wins!\n");
cpuScore++;
return true;
}
//Testing for Vertical Win
if(gameBoard[0][0] == 'X' && gameBoard[2][0] == 'X' && gameBoard [4][0] == 'X') {
System.out.println("Player Wins!\n");
playerScore++;
return true;
}
if(gameBoard[0][0] == 'O' && gameBoard[2][0] == 'O' && gameBoard [4][0] == 'O') {
System.out.println("CPU Wins!\n");
cpuScore++;
return true;
}
if(gameBoard[0][2] == 'X' && gameBoard[2][2] == 'X' && gameBoard [4][2] == 'X') {
System.out.println("Player Wins!\n");
playerScore++;
return true;
}
if(gameBoard[0][2] == 'O' && gameBoard[2][2] == 'O' && gameBoard [4][2] == 'O') {
System.out.println("CPU Wins!\n");
cpuScore++;
return true;
}
if(gameBoard[0][4] == 'X' && gameBoard[2][4] == 'X' && gameBoard [4][4] == 'X') {
System.out.println("Player Wins!\n");
playerScore++;
return true;
}
if(gameBoard[0][4] == 'O' && gameBoard[2][4] == 'O' && gameBoard [4][4] == 'O') {
System.out.println("CPU Wins!\n");
cpuScore++;
return true;
}
//Testing for Diagonal Win
if(gameBoard[0][0] == 'X' && gameBoard[2][2] == 'X' && gameBoard [4][4] == 'X') {
System.out.println("Player Wins!\n");
playerScore++;
return true;
}
if(gameBoard[0][0] == 'O' && gameBoard[2][2] == 'O' && gameBoard [4][4] == 'O') {
System.out.println("CPU Wins!\n");
cpuScore++;
return true;
}
if(gameBoard[4][0] == 'X' && gameBoard[2][2] == 'X' && gameBoard [0][4] == 'X') {
System.out.println("Player Wins!\n");
playerScore++;
return true;
}
if(gameBoard[4][0] == 'O' && gameBoard[2][2] == 'O' && gameBoard [0][4] == 'O') {
System.out.println("CPU Wins!\n");
cpuScore++;
return true;
}
//Testing for Tie
if(gameBoard[0][0] != ' ' && gameBoard[0][2] != ' ' && gameBoard[0][4] != ' ' &&
gameBoard[2][0] != ' ' && gameBoard[2][2] != ' ' && gameBoard[2][4] != ' ' &&
gameBoard[4][0] != ' ' && gameBoard[4][2] != ' ' && gameBoard[4][4] != ' ') {
System.out.println("It's a tie!!!\n");
numOfTies++;
return true;
}
return false;
}
Ok, I got a little carried away. But perhaps you could use some ideas presented here. My main goal was to make it so the entire board need not be checked after each move. This is the type of issue that is best considered during the design phase of a program.
I created a TriGroup class (which is essentially a mutable string to hold successive moves.
Then a map is used to hold all the groupings which have a common coordinate.
when a move is made, those groupings have the current player appended.
and check is done to see if that player won.
this program will run by itself using random moves resulting in a victory or a tie.
Some border cases may have been overlooked.
public class TicTacToeCheck {
int moveCount = 0;
static int MAX_MOVES = 27;
class TriGroup {
public String group = "";
#Override
public String toString() {
return group;
}
}
TriGroup row1 = new TriGroup();
TriGroup row2 = new TriGroup();
TriGroup row3 = new TriGroup();
TriGroup col1 = new TriGroup();
TriGroup col2 = new TriGroup();
TriGroup col3 = new TriGroup();
TriGroup diag1 = new TriGroup();
TriGroup diag2 = new TriGroup();
Map<String, List<TriGroup>> commonGroupings = new HashMap<>();
{
commonGroupings.put("00", List.of(row1, col1, diag1));
commonGroupings.put("02", List.of(row1, col2));
commonGroupings.put("04", List.of(row1, col3));
commonGroupings.put("20", List.of(row2, col1));
commonGroupings.put("22", List.of(row2, col2, diag1, diag2));
commonGroupings.put("24", List.of(row2, col3));
commonGroupings.put("40", List.of(row3, col1, diag1));
commonGroupings.put("42", List.of(row3, col2));
commonGroupings.put("44", List.of(row3, col3));
}
public static void main(String[] args) {
new TicTacToeCheck().start();
}
public void start() {
char player = 'X';
Random r = new Random();
outer: while (moveCount < MAX_MOVES) {
commonGroupings.entrySet().forEach(System.out::println);
System.out.println();
int row = r.nextInt(3) * 2;
int col = r.nextInt(3) * 2;
System.out.println("Move: " + row + ", " + col);
player = player == 'X' ? 'O' : 'X';
char val;
switch (val = recordMove(row, col, player)) {
case 'X' -> {
System.out.println("X wins!");
break outer;
}
case 'O' -> {
System.out.println("O wins!");
break outer;
}
case 0 -> {
System.out.println("Tie!");
break outer;
}
default -> {
}
}
}
commonGroupings.entrySet().forEach(System.out::println);
}
public char recordMove(int row, int col, char c) {
moveCount++;
for (TriGroup tri : commonGroupings.get(row + "" + col)) {
if (tri.group.length() > 2) {
// just ignore the row/col and try the next
continue;
}
// update group
tri.group += c;
if (tri.group.equals(c + "" + c + "" + c)) {
return c;
}
}
if (moveCount == MAX_MOVES) {
return 0;
}
return '#';
}
}
This version uses 2 auxiliary, private methods to make the code less repetitive, without changing the behavior of calling isGameOver. The main observation is that there is very little difference between checking for a win by O or checking for one by X - only a single character. So that becomes checkWins. The next observation is that there is a lot of repetition involved in checking 3 positions of the board that are next to each other. If you give me the char to expect, where to start, and where to look next (dcol and drow), that becomes allEqual.
Your code skips over uneven positions of the board; my code does not. I feel that it is an error to mix presentation ("how you show things to users") with model ("how you represent things internally"); so my code is not currently a drop-in replacement for yours, but can quickly be fixed to be such a replacement by tweaking values in checkWins (diagonal down would be 0, 0, 2, 2, and so on).
Note that, efficiency-wise, your code is probably faster. But I find this version to be much shorter, more readable, and therefore easier to debug & maintain.
private static boolean allEqual(char expected, char[][] b,
int row, int col, int drow, int dcol) {
for (int i=0; i<b[0].length; i++) {
if (b[row][col] != expected) return false;
row += drow;
col += dcol;
}
return true;
}
private static boolean checkWins(char playerChar, char[][]b) {
boolean win = allEqual(playerChar, b, 0, 0, 0, 1) // 1st row
|| allEqual(playerChar, b, 1, 0, 0, 1)
|| allEqual(playerChar, b, 2, 0, 0, 1) // 3rd row
|| allEqual(playerChar, b, 0, 0, 1, 0) // 1st col
|| allEqual(playerChar, b, 0, 1, 1, 0)
|| allEqual(playerChar, b, 0, 2, 1, 0) // 3rd col
|| allEqual(playerChar, b, 0, 0, 1, 1) // diagonal down
|| allEqual(playerChar, b, 2, 0, 1,-1); // diagonal up
return win;
}
public static boolean isGameOver(char[][] gameBoard) {
if (checkWins('X', gameBoard)) {
System.out.println("Player Wins!\n");
playerScore ++;
return true;
} else if (checkWins('O', gameBoard)) {
System.out.println("CPU Wins!\n");
cpuScore ++;
return true;
} else {
return false;
}
}
Take a look at this:
https://www.geeksforgeeks.org/tic-tac-toe-game-in-java/
Add the strings of gameBoard[x][y] and check them in a switch statement.
If the compound strings are equals to XXX or OOO, you can return the winner.
For your code something like this:
for (int a = 0; a < 8; a++) {
String line = null;
switch (a) {
case 0:
line = gameBoard[0][0] + gameBoard[0][1] + gameBoard[0][2];
break;
case 1:
line = gameBoard[1][0] + gameBoard[1][1] + gameBoard[1][2];
break;
case 2:
line = gameBoard[2][0] + gameBoard[2][1] + gameBoard[2][2];
break;
case 3:
line = gameBoard[0][0] + gameBoard[1][0] + gameBoard[2][0];
break;
case 4:
line = gameBoard[0][1] + gameBoard[1][1] + gameBoard[2][1];
break;
case 5:
line = gameBoard[0][2] + gameBoard[1][2] + gameBoard[2][2];
break;
case 6:
line = gameBoard[0][0] + gameBoard[1][1] + gameBoard[2][2];
break;
case 7:
line = gameBoard[0][2] + gameBoard[1][1] + gameBoard[2][0];
break;
}
//For X winner
if (line.equals("XXX")) {
return "X";
}
// For O winner
else if (line.equals("OOO")) {
return "O";
}
}

Nested loop infinitely looping after random interval?

I am programming a connect 4 game using Java for an assignment. However, whenever player 2 makes a move about 5 moves in, the player 2 loop will infinitely loop. There is some sort of logic error that I cannot find, and it is frustrating. What is the logic error, and what is a good way to avoid future mistakes of the same vain?
I have tried changing the variables for the do > while loop where player 1 and player two attempt their moves. However that has no affect on it.
import java.util.Arrays;
public class Lab6Shell {
public static void main(String args[]) {
// variables
Scanner input = new Scanner(System.in);
char[][] board = new char[7][8];
boolean finished = false;
boolean gameOver = false;
int width = 7;
int height = 8;
char currentPlayer = 'X';
int numMoves = 0;
int bottom_row = width - 1;
// loop until user wants to stop
for (int row = 0; row < board.length; row++) {
java.util.Arrays.fill(board[row], 0, board[row].length, '*');
}
do {
// display the board
DisplayBoard(board);
// loop until this game is over
do {
// get the next move for the current player
int columnChosen = 0;
do {
if (currentPlayer == 'X') {
int counter = 1;
System.out.println("Player 1 turn");
System.out.println("Enter the column you want to place your piece.");
columnChosen = input.nextInt();
input.nextLine();
while (true) {
if (columnChosen > width) {
System.out.println("That's not a valid column");
break;
}
if ((board[bottom_row][columnChosen] == '*')) {
board[bottom_row][columnChosen] = 'X';
break;
} else if ((board[bottom_row][columnChosen] == 'X')
|| (board[bottom_row][columnChosen] == 'O')) {
if (board[bottom_row - counter][columnChosen] == '*') { // puts X if blank
board[bottom_row - counter][columnChosen] = 'X';
break;
}
counter += 1;
if (counter == width) {
System.out.println("That column is full");
break;
}
}
}
}
if (currentPlayer == 'O') {
int counter = 1;
System.out.println("Player 2's turn");
System.out.println("Enter the column you want to place your piece.");
columnChosen = input.nextInt();
input.nextLine();
while (true) {
if (columnChosen > width) {
System.out.println("That's not a valid column");
break;
}
if ((board[bottom_row][columnChosen] == '*')) {
board[bottom_row][columnChosen] = 'O';
break;
} else if ((board[bottom_row][columnChosen] == 'X')
|| (board[bottom_row][columnChosen] == 'O')) {
if (board[bottom_row - counter][columnChosen] == '*') { // puts O
board[bottom_row - counter][columnChosen] = 'O';
break;
}
counter += 1;
if (counter == width) {
System.out.println("That column is full");
break;
}
}
}
}
} while (columnChosen < 0 || columnChosen > 8 || board[1][columnChosen] != '*');
// place piece
// increment number of moves
numMoves++;
// display the board
DisplayBoard(board);
// check for win
if (checkWin(board)) {
// if winner, display congratulations and set gameOver true
System.out.println("Congratulations! You won!");
gameOver = true;
} else if (numMoves == 42) {
// if tie, display result and set gameOver true
DisplayBoard(board);
System.out.println("Tie Game! Game over");
gameOver = true;
} else if (checkWin(board) == false) {
if (currentPlayer == ('X')) {
currentPlayer = ('O');
} else {
currentPlayer = ('X');
}
}
} while (!gameOver);
// ask if user wants to play again, set finished accordingly
System.out.println("Would you like to play again?");
input.nextLine();
String decision = input.nextLine();
if (decision.toLowerCase().equals("yes")) {
finished = false;
}
else if (decision.toLowerCase().equals("no")) {
finished = true;
}
} while (finished == false);
}
// this method displays the board passed in
public static void DisplayBoard(char[][] board) {
for (int i = 0; i < board.length; i++) {
System.out.print("|");
for (int j = 0; j < board[i].length; j++) {
System.out.print(" " + board[i][j] + "|");
}
System.out.println("");
}
}
public static boolean checkWin(char[][] board) {
final int HEIGHT = board.length;
final int WIDTH = board[0].length;
final int EMPTY_SLOT = '*';
for (int r = 0; r < HEIGHT; r++) { // iterate rows, bottom to top
for (int c = 0; c < WIDTH; c++) { // iterate columns, left to right
char player = board[r][c];
if (player == EMPTY_SLOT)
continue; // don't check empty slots
if (c + 3 < WIDTH && player == board[r][c + 1] && // look right
player == board[r][c + 2] && player == board[r][c + 3])
return true;
if (r + 3 < HEIGHT) {
if (player == board[r + 1][c] && // look up
player == board[r + 2][c] && player == board[r + 3][c])
return true;
if (c + 3 < WIDTH && player == board[r + 1][c + 1] && // look up & right
player == board[r + 2][c + 2] && player == board[r + 3][c + 3])
return true;
if (c - 3 >= 0 && player == board[r + 1][c - 1] && // look up & left
player == board[r + 2][c - 2] && player == board[r + 3][c - 3])
return true;
}
}
}
return false; // no winner found
}
}
The expected result is that each player will play a piece until four of the same piece are in a row. Then the first to reach four in a row is declared the winner, and the game ends. However, once the game gets in about 5 loops, the player 2 loop infinitely loops until a column is full, and does not print out the board.
Your infinite loop is caused by checking the condition board[1][columnChosen] != '*' in your do ... while loop. The program will continue to ask the current user for a new move as long as the second to top row of the selected column is occupied.
Replace:
do
{
...
} while (columnChosen < 0 || columnChosen > 8 || board[1][columnChosen] != '*');
With:
do
{
...
} while (columnChosen < 0 || columnChosen > 8)
This should get you to a point where you can tackle the remaining issues.

(Java) Tic-Tac-Toe game using 2 dimensional Array

In class, our assignment is to create a two-dimensional array and create a tic-tac-toe game around it. I have everything done except displaying when the whole board is full and the game is a draw. I have tried a few things but I have not found the solution and I need some help... Here is my code:
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int row, column;
char player = 'X';
//create 2 dimensional array for tic tac toe board
char[][] board = new char[3][3];
char ch = '1';
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++) {
board[i][j] = ch++;
}
}
displayBoard(board);
while(!winner(board) == true){
//get input for row/column
System.out.println("Enter a row and column (0, 1, or 2); for player " + player + ":");
row = in.nextInt();
column = in.nextInt();
//occupied
while (board[row][column] == 'X' || board[row][column] == 'O') {
System.out.println("This spot is occupied. Please try again");
}
//place the X
board[row][column] = player;
displayBoard(board);
if (winner(board)){
System.out.println("Player " + player + " is the winner!");
}
//time to swap players after each go.
if (player == 'O') {
player = 'X';
}
else {
player = 'O';
}
if (winner(board) == false) {
System.out.println("The game is a draw. Please try again.");
}
}
private static void displayBoard(char[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (j == board[i].length - 1) System.out.print(board[i][j]);
else System.out.print( board[i][j] + " | ");
}
System.out.println();
}
}
//method to determine whether there is an x or an o in the spot
public static Boolean winner(char[][] board){
for (int i = 0; i< board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == 'O' || board[i][j] == 'X') {
return false;
}
}
}
return (board[0][0] == board [0][1] && board[0][0] == board [0][2]) ||
(board[0][0] == board [1][1] && board[0][0] == board [2][2]) ||
(board[0][0] == board [1][0] && board[0][0] == board [2][0]) ||
(board[2][0] == board [2][1] && board[2][0] == board [2][2]) ||
(board[2][0] == board [1][1] && board[0][0] == board [0][2]) ||
(board[0][2] == board [1][2] && board[0][2] == board [2][2]) ||
(board[0][1] == board [1][1] && board[0][1] == board [2][1]) ||
(board[1][0] == board [1][1] && board[1][0] == board [1][2]);
}
}
I want output saying that the board is full when it's full but I get nothing. This is the last line of my output and as you can see, my current strategy is not working as it continues to ask for input. -->
Enter a row and column (0, 1, or 2); for player X:
2 0
X | O | X
O | O | X
X | X | O
Enter a row and column (0, 1, or 2); for player O:
First off:
while (board[row][column] == 'X' || board[row][column] == 'O') {
System.out.println("This spot is occupied. Please try again");
}
This will create a infinite loop because row and column shouldn't change you should ask for new input!
Also
public static Boolean winner(char[][] board){
for (int i = 0; i< board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == 'O' || board[i][j] == 'X') {
return false;
}
}
}
As soon you hit 'O' or 'X' you will exit the Method with a false (no winner)
What you probably want to check is if every spot is occupied
public static Boolean winner(char[][] board){
//Boolean which is true until there is a empty spot
boolean occupied = true;
//loop and check if there is empty space or if its a draw
for (int i = 0; i< board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
//Check if spot is not 'O' or not 'X' => empty
if (board[i][j] != 'O' || board[i][j] != 'X') {
occupied = false;
}
}
}
if(occupied)
return false;
//Check if someone won
return (board[0][0] == board [0][1] && board[0][0] == board [0][2]) ||
(board[0][0] == board [1][1] && board[0][0] == board [2][2]) ||
(board[0][0] == board [1][0] && board[0][0] == board [2][0]) ||
(board[2][0] == board [2][1] && board[2][0] == board [2][2]) ||
(board[2][0] == board [1][1] && board[0][0] == board [0][2]) ||
(board[0][2] == board [1][2] && board[0][2] == board [2][2]) ||
(board[0][1] == board [1][1] && board[0][1] == board [2][1]) ||
(board[1][0] == board [1][1] && board[1][0] == board [1][2]);
}
This would now check if there is a winner or its a tie
Occupied == true == tie == return false
Winner == return true
But you have three states:
Win
Tie
NotFinished
With the changed Method you will NOT finish the game until you win.
Reason:
while(!winner(board) == true)
This makes the game run as long as there is NO winner
(winner() will be false because everything is occupied or there is no winner)
while(!false==true) => while(true)
You could write a method similar to winner but it only checks if the board has empty spots:
public static Boolean hasEmptySpot(char[][] board){
//loop and check if there is empty space
for (int i = 0; i< board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] != 'O' && board[i][j] != 'X') {
return true;
}
}
}
return false;
}
//New code
while(hasEmptySpot(board) || !winner(board)){
//Your code for the game here
....
}
this would end the game when there is no empty spot left
After you finished the game you can call winner(board) and it will return if you tied or won!
By creating hasEmptySpot() you could change your winner method to
public static Boolean winner(char[][] board){
return (board[0][0] == board [0][1] && board[0][0] == board [0][2]) ||
(board[0][0] == board [1][1] && board[0][0] == board [2][2]) ||
(board[0][0] == board [1][0] && board[0][0] == board [2][0]) ||
(board[2][0] == board [2][1] && board[2][0] == board [2][2]) ||
(board[2][0] == board [1][1] && board[0][0] == board [0][2]) ||
(board[0][2] == board [1][2] && board[0][2] == board [2][2]) ||
(board[0][1] == board [1][1] && board[0][1] == board [2][1]) ||
(board[1][0] == board [1][1] && board[1][0] == board [1][2]);
}
Why?
Because you finished the game and you know there are only two possible outcomes Win or Tie.
I hope this helped you a little bit.
EDIT
Had a logic error myself!
First mistake:
you still need to check if there is a winner while the game is running forgot that point!
while(hasEmptySpot(board) || !winner(board)){
}
Now this will quit the game loop when there is a winner or no empty spots is left
Second mistake:
In hasEmptySpot()
if (board[i][j] != 'O' && board[i][j] != 'X') {
return true;
not
if (board[i][j] != 'O' || board[i][j] != 'X') {
return true;
Fixed it in the upper examples.
I'm sorry for the inconvenience!
The most efficient way to do this is to keep a running count of how many spaces have been filled previously and increment that count each time a space is occupied. The board can be considered full when that count reaches 9.
If you're familiar with object-oriented programming, I think you'll find this easier to implement if you wrap your 2D array in a Board class.
Example:
public static class Board {
private char[][] spaces = new char[3][3];
private int numMoves = 0;
public void makeMove(int row, int col, char player) {
if (spaces[row][col] == 'X' || spaces[row][col] == 'O') {
System.out.println("This spot is occupied. Please try again");
} else {
spaces[row][col] = player;
numMoves++;
}
}
public boolean isFull() {
return numMoves == 9;
}
public boolean hasWinner() {
...
}
public void display() {
...
}
}
You could try to incorporate a new method such as the following:
public Boolean boardFull()
{
short count = 0;
for(short i = 0; i < 3; i++){
for(short j = 0; j < 3; j++){
if(board[i][j] == ‘O’ || board[i][j] == ’X’){
count++;
} else {
continue;
}
}
}
if(count == 9){
return true;
} else {
return false;
}
}
You could use an if statement to see if it returns true and then print something out if it does.
Solution
The code that's not working is your winner() method. It is always returning false if there is at least one cell occupied. You could proceed based on the last part of Nordiii's answer.
Extra problems
Cell-checking loop
Your code to check if a cell is occupied is going infinitely. You need to use an 'if' statement instead of a 'while' loop:
if(board[row][column] == 'X' || board[row][column] == 'O'){
System.out.println("This spot is occupied. Please try again");
continue;
}
Your old code got stuck always checking if 1 cell was occupied and it always returned true, which kept the loop alive and flooded your console. The continue statement will exit the current iteration of your other 'while' loop and start a new iteration, thus asking for new input.
Exceptions
Man, that's a lot of uncaught exceptions! If I mess up my input, pow! The whole thing fails. Just put a try block for your input-checking code:
try {
row = in.nextInt();
column = in.nextInt();
// Attempt to place player (an ArrayOutOfBoundsException could be thrown)
if(board[row][column] == 'X' || board[row][column] == 'O'){
System.out.println("This spot is occupied. Please try again");
continue;
}
board[row][column] = player;
} catch(Exception e){
System.out.println("I'm sorry, I didn't get that.");
continue;
}
This attempts to execute the code inside the try statement, and if someone inputs something incorrect, the exception gets 'caught' and a new iteration is created. Genius!
Although there are already some great answers I'd like to post another solution that is more generic in its logic to determine the winner. Currently you've hard-coded your some of the possible winning scenarios when you could write more generic logic for this.
As other answers have pointed out you want a method to check for unoccupied spaces in the board and this will tell you if there is a tie. I have implemented such a method in the code below along with the more generic winner logic.
Note that some methods are public to make it easier to test, they do not necessarily have to remain public.
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int row, column;
char player = 'X';
//create 2 dimensional array for tic tac toe board
char[][] board = new char[3][3];
char ch = '1';
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++) {
board[i][j] = ch++;
}
}
displayBoard(board);
while(!winner(board) == true){
//get input for row/column
System.out.println("Enter a row and column (0, 1, or 2); for player " + player + ":");
row = in.nextInt();
column = in.nextInt();
//occupied
while (board[row][column] == 'X' || board[row][column] == 'O') {
System.out.println("This spot is occupied. Please try again");
}
//place the X
board[row][column] = player;
displayBoard(board);
if (winner(board)){
System.out.println("Player " + player + " is the winner!");
}
//time to swap players after each go.
if (player == 'O') {
player = 'X';
}
else {
player = 'O';
}
if (winner(board) == false && !hasFreeSpace(board)) {
System.out.println("The game is a draw. Please try again.");
}
}
//Don't forget to close the scanner.
in.close();
}
public static void displayBoard(char[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (j == board[i].length - 1) System.out.print(board[i][j]);
else System.out.print( board[i][j] + " | ");
}
System.out.println();
}
}
/**
* Determines whether the board is completely occupied by X and O characters
* #param board the board to search through
* #return true if entire board is populated by X or O, false otherwise.
*/
public static boolean hasFreeSpace(char[][] board){
for (int i = 0; i< board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] != 'O' && board[i][j] != 'X') {
return true;
}
}
}
return false;
}
//method to determine whether there is a winner
public static boolean winner(char[][] board){
return isHorizontalWin(board) || isVerticalWin(board) || isDiagonalWin(board);
}
/**
* Determines if there is a winner by checking each row for consecutive
* matching tokens.
* #return true if there is a winner horizontally, false otherwise.
*/
private static boolean isHorizontalWin(char[][] board) {
for(int row = 0; row < board.length; row++){
if(isWin(board[row]))
return true;
}
return false;
}
/**
* Determines whether all of the buttons in the specified array have the
* same text and that the text is not empty string.
* #param lineToProcess an array of buttons representing a line in the grid
* #return true if all buttons in the array have the same non-empty text, false otherwise.
*/
private static boolean isWin(char[] lineToProcess) {
boolean foundWin = true;
char prevChar = '-';
for(char character: lineToProcess) {
if(prevChar == '-')
prevChar = character;
if ('O' != character && 'X' != character) {
foundWin = false;
break;
} else if (prevChar != character) {
foundWin = false;
break;
}
}
return foundWin;
}
/**
* Determines whether there is a winner by checking column for consecutive
* matching tokens.
* #return true if there is a vertical winner, false otherwise.
*/
private static boolean isVerticalWin(char[][] board) {
char[] column = null;
//assuming all rows have same legnth (same number of cols in each row), use first row
for(int col = 0; col < board[0].length; col++){
column = new char[board[0].length];
for(int row = 0; row < column.length; row++){
column[row] = board[row][col];
}
if(isWin(column))
return true;
}
return false;
}
/**
* Determines if there is a winner by checking each diagonal for consecutive
* matching tokens.
* #return true if a diagonal winner exists, false otherwise.
*/
private static boolean isDiagonalWin(char[][] board) {
int row = 0, col = 0;
int cols = board.length;
int rows = board[0].length; //assuming all rows are equal length so just use the first one
//Create a one-dimensional array to represent the diagonal. Use the lesser
// of the rows or columns to set its size. If the grid is rectangular then
// a diagonal will always be the size of the lesser of its two dimensions.
int size = rows < cols ? rows : cols;
char[] diagonal = new char[size];
//Since we know the grid is a square we really could just check one of
// these - either row or col, but I left both in here anyway.
while (row < rows && col < cols) {
diagonal[col] = board[row][col];
row++;
col++;
}
if (isWin(diagonal)) {
return true;
}
row = rows - 1;
col = 0;
diagonal = new char[size];
while (row >=0 && col < cols) {
diagonal[col] = board[row][col];
row--;
col++;
}
return isWin(diagonal);
}
}

Connect4 game with 2D arrays diagonal check

So Im pretty new at java and i have an assignment to create a connect 4 game. I create a board 6 rows and 7 columns full of characters like this one -> '-', when the user inputs the desire column it replaces the '-' with a B or R (Red or black checker) well whatever this is just a background. Everything works except for the part that my code for checking diagonals its awfully long, i couldnt figure out a way to go through all the possible 4 sets of diagonals in which a player can win, except by doing each one in a different for loop... I know its horrible hope you can help me to do it shorter :(
this is the code for diagonal check: (lol only looking at it makes me feel sad)
public class Connect4 {
public static void main(String[] args) {
//Create board
Scanner input = new Scanner(System.in);
char[][] grid = new char[6][7];
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
grid[i][j] = '-';
}
}
public static void checkWinner(char[][] grid) {
try{
//A LOT OF FOR LOOPS FOR DIAGONAL CHECKS
for (int i = 5; i > 1; i-- ) {
for(int j = 0; j < 4; j++) {
if ( grid[i][j] == 'R' &&
grid[i-1][j+1] == 'R' &&
grid[i-2][j+2] == 'R' &&
grid[i-3][j+3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if ( grid[i][j] == 'B' &&
grid[i-1][j+1] == 'B' &&
grid[i-2][j+2] == 'B' &&
grid[i-3][j+3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}
for (int i = 5; i > 1; i--) {
for (int j = 1; j < 4; j++) {
if (grid[i][j] == 'R' &&
grid[i-1][j+1] == 'R' &&
grid[i-2][j+2] == 'R' &&
grid[i-3][j+3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i-1][j+1] == 'B' &&
grid[i-2][j+2] == 'B' &&
grid[i-3][j+3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}
for (int i = 0; i < 4; i++) {
for (int j = 4; j < 7; j++) {
if ( grid[i][j] == 'R' &&
grid[i+1][j-1] == 'R' &&
grid[i+2][j-2] == 'R' &&
grid[i+3][j-3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i+1][j-1] == 'B' &&
grid[i+2][j-2] == 'B' &&
grid[i+3][j-3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}
for (int i = 1; i < 2; i++) {
for (int j = 6; j > 5; j--) {
if ( grid[i][j] == 'R' &&
grid[i+1][j-1] == 'R' &&
grid[i+2][j-2] == 'R' &&
grid[i+3][j-3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i+1][j-1] == 'B' &&
grid[i+2][j-2] == 'B' &&
grid[i+3][j-3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}
for (int i = 4; i < 5; i++){
for (int j = 2; j < 3; j++){
if (grid[i][j] == 'R' &&
grid[i-1][j+1] == 'R' &&
grid[i-2][j+2] == 'R' &&
grid[i-3][j+3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i-1][j+1] == 'B' &&
grid[i-2][j+2] == 'B' &&
grid[i-3][j+3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}
for (int i = 0; i < 4; i++) {
for (int j = 3; j > 0; j--) {
if (grid[i][j] == 'R' &&
grid[i+1][j+1] == 'R' &&
grid[i+2][j+2] == 'R' &&
grid[i+3][j+3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i+1][j+1] == 'B' &&
grid[i+2][j+2] == 'B' &&
grid[i+3][j+3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}
for(int i =0; i < 1; i++) {
for (int j = 0; j <1; j++) {
if (grid[i][j] == 'R' &&
grid[i+1][j+1] == 'R' &&
grid[i+2][j+2] == 'R' &&
grid[i+3][j+3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i+1][j+1] == 'B' &&
grid[i+2][j+2] == 'B' &&
grid[i+3][j+3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}
for (int j = 0; j < 1; j++) {
for(int i =1 ; i < 3; i++) {
if (grid[i][j] == 'R' &&
grid[i+1][j+1] == 'R' &&
grid[i+2][j+2] == 'R' &&
grid[i+3][j+3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i+1][j+1] == 'B' &&
grid[i+2][j+2] == 'B' &&
grid[i+3][j+3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}
for (int j = 3; j < 4; j++) {
for (int i = 0; i < 3; i++) {
if (grid[i][j] == 'R' &&
grid[i+1][j+1] == 'R' &&
grid[i+2][j+2] == 'R' &&
grid[i+3][j+3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i+1][j+1] == 'B' &&
grid[i+2][j+2] == 'B' &&
grid[i+3][j+3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}
for (int i = 2; i < 3; i++) {
for (int j = 2; j > 0; j--) {
if (grid[i][j] == 'R' &&
grid[i+1][j+1] == 'R' &&
grid[i+2][j+2] == 'R' &&
grid[i+3][j+3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i+1][j+1] == 'B' &&
grid[i+2][j+2] == 'B' &&
grid[i+3][j+3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}
for (int i = 1; i < 2; i++) {
for (int j = 2; j < 3; j++){
if (grid[i][j] == 'R' &&
grid[i+1][j+1] == 'R' &&
grid[i+2][j+2] == 'R' &&
grid[i+3][j+3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i+1][j+1] == 'B' &&
grid[i+2][j+2] == 'B' &&
grid[i+3][j+3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}
for (int i = 1; i < 2; i++) {
for (int j = 1; j < 2; j++){
if (grid[i][j] == 'R' &&
grid[i+1][j+1] == 'R' &&
grid[i+2][j+2] == 'R' &&
grid[i+3][j+3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i+1][j+1] == 'B' &&
grid[i+2][j+2] == 'B' &&
grid[i+3][j+3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Exception thrown :" + e);
}
}
Instead of checking every possible bit on the board, play the game based on how humans play it - It's only connect 4 when someone drops in a piece that then forms a line of four tiles. So: don't check every possible tile, use the tile that just got dropped in, and only check for lines involving that tile:
does this new tile form a horizontal? that means checking column-3 through column+3 to cover all possible horizontals.
does this new tile form a vertical? that means only checking the three tiles below it.
does this new tile form a diagonal? that means checking {column-3, row-2} through {column+3, row+3} to cover all possible diagonals. We could even check for this at the same time we're checking for horizontals, because it traverses the same column-3 through column+3 range.
You already know what "color" the tile has, so your checks (in fake code) would simply be of the form:
boolean matched = false;
int stretch = 0;
do {
checkTile = ...;
matched = checkedTile.color.equals(droppedTile.color);
if (matched) {
if (stretch == 3) playerWins()
stretch++;
}
} while(matched);
If the largest stretch of tiles you find that have the same color is 4, done. If not, no connect-4.
That said, this is a homework assignment: S.O. is here for you when you have a problem while programming, but we're not here to do your homework for you. If you get stuck, ask your fellow students or even your teacher. The internet isn't the only place to ask help, especially when you're taking a course.
Instead of coding it in a if-statement, I would e.g for the \ diagonal simply start at the position of the coin and count in direction right-lower, how many of the same color are there until the color changes, or the border is reached. Same in the left-upper direction.
At the end I would simply check if the sum of both counts plus one is greater or equal 4.
Same method for the / diagonal.
Warning: Your implementation of - and | have no array-boundary checks yet. I would recommend the counting approach for them too.
P.s. And to make your code more clear, put each check in a separate methood:
isHorizonallWin(x,y,color,grid)
isVerticalWin(x,y,color,grid)
isLdiagonalLeftUpper2RightLowerWin(x,y,color,grid)
isLdiagonalRightUpper2leftLowerWin(x,y,color,grid)

Categories