Java | TicTacToe Right Diagonal Not Working - java

currently working on a tic tac toe game in java, and I have a checkWin() method that works correctly for 3 out of the 4 possible winning conditions. The one I am having an issue with is the right diagonal.
Code:
public boolean checkWin(String player){
int row = 0; // Holder to count number of player spots in row
int d1 = 0; // Holder to count number of player spots in right diag.
int d2 = 0; // Holder to count number of player spots in left diag.
int[] column = new int[squares[0].length]; /* Holder to count number
of player spots in column */
for(int i = 0; i < size; i++){
row = 0;
for(int j = 0; j < size; j++){
if(null == squares[i][j]){
continue;
}
if(squares[i][j].getText().equals(player)){
row++; /* If spot at [i][j] equals player, increase row */
column[j]++; /* If spot at [i][j] equals player, increase
col */
if(i == j){ /* If spot at i is equal to j, increase left
diag */
d1++;
} else if ((size - 1) == i + j){ /* If spot at i + j
equals board size - 1, increase right diag. */
d2++;
}
}
}
if(row == size){
/*
if spots in row is equal to size (otherwise, if it fills
the row, return win
*/
return true;
}
}
if(size == d1 || size == d2){
/*
if spots in either diag is equal to size, return win
*/
return true;
}
for(int i = 0; i < column.length; i++){
if(column[i] == size){
/*
if column is full of the same player character, return win
*/
return true;
}
}
/*
otherwise, return false
*/
return false;
}
The problem part is:
else if ((size - 1) == i + j){ /* If spot at i + j
equals board size - 1, increase right diag. */
d2++;
}
Reason for setting it up this way, is how a 2D Array works, so for a 3x3 board:
[00][01][02]
[10][11][12]
[20][21][22]
And with i + j = size - 1, it would evaluate 2 + 0, 1 + 1, 0 + 2 all equal 2, which is size - 1 if size = 3, but when I run the program and perform the right diagonal move, it doesn't return a true value for win.
Any suggestions for how to fix this will be greatly appreciated.

else if ((size - 1) == i + j)
^ This is only evaluated if the if condition above it is false.
if(i == j)
When i == 1 and j == 1, then i == j is true, and thus (size - 1) == i + j is not evaluated.
TLDR: Get rid of your else.

Related

Detecting diagonal in a row win - tic-tac-toe, gomoku

Within a game on Gomoku a player has to get 5 in a row to win. Detecting diagonal win is a problem.
I have tried the code below which searches a 2d matrix from top right until it finds a player token we are looking for e.g. 1, it then proceeds to search from that point diagonally to find a winning row. This works fine providing the first '1' the algorithm comes across is a part of the winning line. If it is not, and just a random piece, the algorithm returns false as it does not continue searching.
How would Itake the last played move of the game and only search the diagonals relating to that move? Or possibly edit provided code to search the whole board.
public boolean is_diagonal_win_left(int player) {
int i = 0;
for (int col = board_size-1; col > (win_length - 2); col--) {
for (int row = 0; row < board_size-(win_length-1); row++) {
while (board_matrix[row][col] == player) {
i++;
row++;
col--;
if (i == win_length) return true;
}
i = 0;
}
}
return false;
}
//solved
public boolean is_diagonal_win_right(int player, int r, int c) {
int count = 0;
int row = r;
int col = c;
while ((row != 0) && (col != 0)) {
row--;
col--;
}
while ((row <= board_size - 1) && (col <= board_size - 1)) {
if (board_matrix[row][col] == player) count++;
row++;
col++;
}
return count == win_length;
}
You are correct: searching the board for the first counter is invalid; searching the entire board is a waste of time. Start at the most recent move. Let's call that position (r, c); the player's token is still player. Check in each of the eight functional directions to see how long is the string of player. For instance, you check the NW-SE diagonal like this:
count = 1 // We just placed one counter
row = r-1; col = c-1
while ( (row >= 0) and (col >= 0) and
(board_matrix[row][col] == player) )
count += 1
row = r+1; col = c+1
while ( (row < board_size) and (col < board_size) and
(board_matrix[row][col] == player) )
count += 1
// Note: gomoku rules require exactly 5 in a row;
// if you're playing with a"at least 5", then adjust this to >=
if (count == win_length) {
// Process the win
}

Java Connect Four in console - Horizontal and Vertical winning conditions

I'm working on a Connect Four game for the console in Java. I have problems with the winning conditions, as I don't know how to program them. Here is my code my Main:
public class Main {
public static char[] playerNumber = new char[]{'1', '2'};
public static char[] Badge = new char[]{'X', 'O'};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int moves = 7 * 6;
int whichPlayer = 0;
for (int i = 0; i < 10; i++) {
System.out.println(" FOUR IN A ROW");
System.out.println("-------------------------------------------------------");
System.out.println("Welcome to the amazing game Four In A Row:");
System.out.println("Enter a number between 0 and 6 for choosing a column.");
System.out.println();
Board board = new Board();
board.fillBoard();
board.presentBoard();
do {
// 1. get a badge
char Player = playerNumber[whichPlayer];
char badge = Badge[whichPlayer];
// 2. make a turn
board.makeTurn(badge, Player);
board.presentBoard();
// 3. Tjek om der er vinder
if (board.checkWinHorizontal() || board.checkWinVertical()) {
System.out.println("Player " + Player + " has won!");
break;
}
// 4. change the player
whichPlayer = 1 - whichPlayer;
// 5. decrease moves
--moves;
if (moves == 0) {
System.out.println("Game over, nobody has won.");
System.out.println("Do you want to play again? 'Y' or 'N':");
String newGame = scanner.nextLine();
if (newGame.equals("Y") || newGame.equals("y")) {
break;
}
if (newGame.equals("N") || newGame.equals("n")) {
System.out.println("Thanks for the game!");
return;
}
}
// 6. repeat
} while (true);
}
}
And here is my code for my Board class:
public class Board {
char[][] board = new char[6][7];
int column;
// Fills the empty spaces
public void fillBoard() {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
board[i][j] = ' ';
}
}
}
// Prints the board
public void presentBoard() {
for (int i = 0; i < 6; i++) {
System.out.print(" | ");
for (int j = 0; j < 7; j++) {
System.out.print(board[i][j] + " | ");
}
System.out.println();
System.out.print(" -----------------------------");
System.out.println();
}
}
// Turn
public void makeTurn(char badge, char Player) {
Scanner scanner = new Scanner(System.in);
do {
// 1. Ask for a column
System.out.println("Player " + Player + " turn: ");
column = scanner.nextInt();
// 2. Check if it's between 0 and 6
if (column > 6) {
System.out.println("That is not a valid number. Please enter a number between 0 and 6: ");
continue;
}
// 3. Place a badge
for (int i = 6 - 1; i >= 0; i--) {
if (board[i][column] == ' ') {
board[i][column] = badge;
return;
}
}
// If column is full
System.out.println("Column " + column + " is full. Try another column:");
} while (true);
}
// Check for vertical win
public boolean checkWinVertical() {
return verticalWin(5, column);
}
// Check for horizontal win
public boolean checkWinHorizontal() {
return horizontalWin(5,column);
}
// Conditions for vertical win
private boolean verticalWin(int x, int y) {
char charToCheck = board[x][y];
if (board[x-1][y] == charToCheck &&
board[x-2][y] == charToCheck &&
board[x-3][y] == charToCheck) {
return true;
}
return false;
}
// Conditions for horizontal win
private boolean horizontalWin(int x, int y) {
char charToCheck = board[x][y];
if (board[x][y+1] == charToCheck &&
board[x][y+2] == charToCheck &&
board[x][y+3] == charToCheck) {
return true;
}
return false;
}
I have succeeded in getting the game recognize a win horizontally and vertically at the bottom row of my array, but I don't know how to make the game recognize for the whole array. I'm only concentrating about the horizontal and vertical, as the diagonal is too complicated for me. And I don't know if this is the right approach or there is a better one.
Thanks!
Here's another solution. It's the same general idea as previously mentioned: loop through each row/column, checking for a streak of 4 in a row. Maybe this implementation will provide some other insight. Below, I've shown an example method checking the horizontal streaks. For vertical, you would iterate over the rows in the inner for loop instead.
public boolean checkWin(char badge) {
return checkHorizontalStreaks(board, badge)
|| checkVerticalStreaks(board, badge);
}
private boolean checkHorizontalStreaks(char[][] board, char badge) {
for (int row = 0; row < board.length; row++) {
// loop throught each row
int currentStreak = 0;
for (int col = 0; col < board[row].length; col++) {
// loop through each column in the row
if (board[row][col] == badge) {
// keep the streak of 'badge' going
currentStreak++;
if (currentStreak == 4) {
// winner
return true;
}
} else {
// restart the streak
currentStreak = 0;
}
}
}
return false;
}
And then update your Main class with
if (board.checkWin(badge)) {
System.out.println("Player " + Player + " has won!");
break;
}
I'd wager there is a more efficient way to determine a winner (perhaps by treating the grid as a graph and traversing it with some special logic). However, I suspect this may be enough for what you need. I'll spare you the output, but it worked with a few different test cases.
Possibly you could check all the adjacent fields around the last played field, so after the user did his turn. So for checking upwards you could do this:
public boolean checkUp(int rowPlayed, int columnPlayed){
boolean checked = false;
if(rowplayed + 1 <= maxrows){ //Checks if you didn't hit the top
if(board[rowPlayed+1][columnPlayed] != null){
if(board[rowPlayed+1][columnPlayed].getPlayer() == currentPlayer){
checked = true;
}
}
}
return checked;
}
and for example implemented like this:
public void checkWin(int rowPlayed, int columnPlayed){
boolean checkingWin = true;
int countWin = 0;
while(checkingWin){
if(checkUp(rowPlayed + countWin, columnPlayed)){
countWin++;
}
else{
checkingWin = false;
}
if(countWin == 4){
checkinWin = false;
//Insert win confirmation here
}
}
}
It's partially pseudo code because I don't know exactly how you handle things in your code, nor do I know if this is the best way to do it. But I hope it was of help for you.
This is a long answer and I'll go around the houses a bit so you can see how I reached my solution (which also expands to diagonal checking at the end).
I would use the last piece added as a starting point and work from there since checking all combinations is exhaustive and unnecessary.
Given the row and column of the last piece added I need to decide what I need to achieve.
I already know that the current row and column has the piece of the colour I'm looking for so I can ignore that.
For horizontal matching, I want to check I want to checking pieces to left and right in the same row have the same colour, and stop if the colour is different or there is no piece.
So imagine the following board (# = empty, R = Red piece, Y = Yellow piece:
6 # # # # # # # #
5 # # # # # # # #
4 # # # # # # # #
3 # # # # # # # #
2 # # # # # # # #
1 # # # # # # # #
0 Y R R R Y Y Y R
0 1 2 3 4 5 6 7
The last move was Yellow, row 0, col 4.
So I want to check left and right from [0][4] and see if the total number of consecutive pieces of the colour is 3, (not 4) since I know [0][4] is Yellow and can be discounted.
Based on this I can take a recursive approach where I check the adjacent to one side, then recursively do the same thing as long as I keep matching pieces of the same colour or do not encounter an empty slot.
I'll start of with a check to the right (to demonstrate):
private static final int COLS = 7;
private static final int ROWS = 6;
public enum Piece {RED, YELLOW}; // null is empty
private Piece[][] board = new Piece[ROWS][COLS]; // the board
private int checkRight(Piece piece, int row, int col) {
// assume valid row for now
col++; // moving col to the right
if (col >= COLS || board[row][col] != piece) {
// We're outside the limits of the column or the Piece doesn't match
return 0; // So return 0, nothing to add
} else {
// otherwise return 1 + the result of checkRight for the next col
return 1 + checkRight(piece, row, col);
}
}
Now I can perform the same to the left.
private int checkLeft(Piece piece, int row, int col) {
// assume valid row for now
col--; // moving col to the left
if (col < 0 || board[row][col] != piece) {
// We're outside the limits of the column or the Piece doesn't match
return 0; // So return 0, nothing to add
} else {
// otherwise return 1 + the result of checkLeft for the next col
return 1 + checkLeft(piece, row, col);
}
}
And to check a winner for horizontal, I could do this:
public boolean checkWinner(Piece piece, int row, int col) {
// if the sum is 3, we have a winner (horizontal only).
return checkRight(piece, row, col) + checkLeft(piece, row, col) == 3;
}
Ugh, there's a lot of repetition isn't there?
We can condense the two methods into one by introducing a new parameter direction which can change if we move col positive or negative through the values 1 and -1 respectively:
private int check(Piece piece, int row, int col, int direction) {
col += direction; // direction is either 1 (right) or -1 (left)
if (col < 0 || col >= COLS || board[row][col] != piece) {
return 0;
} else {
return 1 + check(piece, row, col);
}
}
Update checkWinner() for this new parameter:
private static final int POSITIVE = 1; // right at the moment
private static final int NEGATIVE = -1; // left at the moment
public boolean checkWinner(Piece piece, int row, int col) {
// if the sum is 3, we have a winner (horizontal only).
return check(piece, row, col, POSITIVE) + check(piece, row, col, NEGATIVE) == 3;
}
Now I could implement the same sort of logic for vertical, but instead stay on the same col and change the row. I will skip this part in detail and move onto a solution which includes this and diagonal checking.
This has been done using an enum called CheckType storing values for which row and col should change and is used by the check() method. e.g. for HORIZONTAL the column changes by 1 or -1 (depending upon the direction specified when check() is called) and the row remains 0.
public class Board {
public enum Piece {
RED, YELLOW
};
private enum CheckType {
HORIZONTAL(0, 1), VERTICAL(1, 0), DIAGNONAL_UP(1, 1), DIAGNONAL_DOWN(-1, 1);
int row;
int col;
CheckType(int row, int col) {
this.row = row;
this.col = col;
}
}
private static final int POSITIVE = 1;
private static final int NEGATIVE = -1;
private static final int ROWS = 6;
private static final int COLS = 7;
private Piece[][] board = new Piece[ROWS][COLS];
private boolean hasWinner = false;
public boolean hasWinner() {
return hasWinner;
}
private void checkWinner(Piece piece, int row, int col) {
// check all values of enum CheckType for a winner
// so HORIZONTAL, VERTICAL, etc..
int enumIndex = 0;
while (!hasWinner && enumIndex < CheckType.values().length) {
hasWinner = check(piece, row, col, POSITIVE, CheckType.values()[enumIndex])
+ check(piece, row, col, NEGATIVE, CheckType.values()[enumIndex]) == 3;
enumIndex++;
}
}
private int check(Piece piece, int row, int col, int direction, CheckType type) {
row += type.row * direction;
col += type.col * direction;
if (row >= ROWS || row < 0 || col >= COLS || col < 0 || board[row][col] != piece) {
return 0;
} else {
return 1 + check(piece, row, col, direction, type);
}
}
// for completeness, adding a Piece
public boolean add(Piece piece, int col) {
int row = 0;
while (row < ROWS && board[row][col] != null) {
row++;
}
if (row < ROWS) {
board[row][col] = piece;
// check for winner after successful add
checkWinner(piece, row, col);
}
return row < ROWS;
}
}
Hope this helps.

Why won't the loop stop iterating?

I am a beginner java student writing a gui tic-tac-toe program for my class. (No players, just computer generated).
Everything in my program works as expected, except for one thing; it seems that the placement of my method call for checkWinner is not place correctly, because the assignment for the X's and O's always finish. Why won't the loop end as soon as there is a winner?
It will return the correct winner based on the method call, but the for-loop will continue to iterate and fill in the rest (so sometimes it looks like both the x and o win or one wins twice). I've been going crazy, thinking it might be the placement of my checkWinner method call and if statement. When I set the winner = true; shouldn't that cancel the loop? I have tried putting it between, inside and outside each for-loop with no luck :(
I have marked the area I think is the problem //What is wrong here?// off to the right of that part the code. Thank you for any input!! :)
public void actionPerformed(ActionEvent e)
{
int total = 0, i = 0;
boolean winner = false;
//stop current game if a winner is found
do{
// Generate random # 0-1 for the labels and assign
// X for a 0 value and O for a 1 value
for (int row = 0; row < gameboard.length; row++) //rows
{
for (int col = 0; col < gameboard[row].length; col++) //columns
{
//Generate random number
gameboard[row][col] = (int)(Math.random() * 2);
//Assign proper values
if(gameboard[row][col] == 0)
{
labels[i].setText("X");
gameboard[row][col] = 10; //this will help check for the winner
}
else if(gameboard[row][col] == 1)
{
labels[i].setText("O");
gameboard[row][col] = 100; //this will help check for winner
}
/**Send the array a the method to find a winner
The x's are counted as 10s
The 0s are counted as 100s
if any row, column or diag = 30, X wins
if any row, column or diag = 300, Y wins
else it will be a tie
*/
total = checkWinner(gameboard); **//Is this okay here??//**
if(total == 30 || total == 300) //
winner = true; //Shouldn't this cancel the do-while?
i++; //next label
}
}//end for
}while(!winner);//end while
//DISPLAY WINNER
if(total == 30)
JOptionPane.showMessageDialog(null, "X is the Winner!");
else if(total == 300)
JOptionPane.showMessageDialog(null, "0 is the Winner!");
else
JOptionPane.showMessageDialog(null, "It was a tie!");
}
The easiest way would be to break all loops at once. (Even if some people dont like this)
outerwhile: while(true){
// Generate random # 0-1 for the labels and assign
// X for a 0 value and O for a 1 value
for (int row = 0; row < gameboard.length; row++) //rows
{
for (int col = 0; col < gameboard[row].length; col++) //columns
{
total = checkWinner(gameboard);
if(total == 30 || total == 300)
break outerwhile; //leave outer while, implicit canceling all inner fors.
i++; //next label
}
}//end for
}//end while
This However would not allow for the "tie" option, because the while will basically restart a game, if no winner has been found. To allow tie, you dont need the outer while at all, and can leave both fors at once, when a winner is found:
Boolean winner = false;
outerfor: for (int row = 0; row < gameboard.length; row++) //rows
{
for (int col = 0; col < gameboard[row].length; col++) //columns
{
total = checkWinner(gameboard);
if(total == 30 || total == 300){
winner = true;
break outerfor; //leave outer for, implicit canceling inner for.
}
i++; //next label
}
}//end for
if (winner){
//winner
}else{
//tie.
}
First of all, your code iterates through a board and generates random marks of X and O. This leads to some very odd board states, being always filled row-by-row, and possibly with unbalanced number of X and O marks.
IMHO you should organize your code in opposite manner to fill a board similary to a true game. I mean a series of 9 marks 'XOXOXOXOX' spreaded over the board.
Let Labels labels be a nine-character array, initialized to 9 spaces.
public int doGame( Labels labels)
{
labels = " ";
int itisXmove = true; // player X or O turn
for( int movesLeft = 9; movesLeft > 0; movesLeft --)
{
int position = // 0 .. movesLeft-1
(int) Math.floor(Math.random() * movesLeft);
for( int pos = 0; pos < 9; pos ++) // find position
if( labels[ pos] == " ") // unused pos?
if( position-- == 0) // countdown
{
if( itisXmove) // use the pos
labels[ pos] = "X"; // for current player
else
labels[ pos] = "O";
break;
}
int result = checkWinner( labels); // who wins (non-zero)?
if( result != 0)
return result;
itisXmove = ! itisXmove; // next turn
}
return 0; // a tie
}
then
public void actionPerformed(ActionEvent e)
{
Labels labels;
int result = doGame( labels);
if( result == valueForX)
JOptionPane.showMessageDialog(null, "X is the Winner!");
else if( result == valueForO)
JOptionPane.showMessageDialog(null, "O is the Winner!");
else
JOptionPane.showMessageDialog(null, "It's a tie!");
for( int rowpos = 0; rowpos < 9; rowpos += 3)
{
for( int colpos = 0; colpos < 3; colpos ++)
/* output (char)label[ rowpos + colpos] */;
/* output (char)newline */;
}
}
I think you should change your loop condition and add one more bool.
You have a "tie" condition but currently you only check for winner. The only explanation without the checkWinner code is that you are encountering a tie every time.
So...
boolean tie;
boolean winner;
do {
//your stuff
}
while(!(tie || winner))
Edit: I didn't realize you put the while loop outside your for loop, you will need to break out of your for loops in order for the while condition to be checked.
//stop current game if a winner is found
do{
for (int row = 0; row < gameboard.length; row++) //rows
{
for (int col = 0; col < gameboard[row].length; col++) //columns
{
if(winner || tie)
break;
}//end for
if(winner || tie)
break;
}//end for
}while(!(winner || tie));//end while
//the rest of your stuff here
You're not checking the value of winner until both for loops complete. Add a break right after you set winner = true, and add an
if (winner)
{
break;
}
to the beginning or end of your outer for loop.
Your issue is that your do/while statement is wrapped around the for statements. So the for statements end up running their entire cycle before it ever reaches the while statement. A solution to get around this is checking for a winner in the for statements and breaking:
//stop current game if a winner is found
do {
for (int row = 0; row < gameboard.length; row++) //rows
{
for (int col = 0; col < gameboard[row].length; col++) //columns
{
// ... your other code ...
total = checkWinner(gameboard);
if(total == 30 || total == 300) {
winner = true;
break; // end current for-loop
}
i++; //next label
}
if (winner) break; // we have a winner so we want to kill the for-loop
} //end for
} while(!winner); //end while
So you should be able to just loop through the two for-statements and break upon a winner. Your code also does not seem to handle a tied case, but I am guessing you already know that.

dynamic programming, two player matrix game

I am programming a matrix game which is played by two player. The objective is to say whether player 1 can win the game return 1, if not return 0.
player can delete either the last row or the last column of the matrix, but only if the sum of the numbers in that row/column is even. example:
original matrix:
[1 2]
[2 2]
then we can subtract these matrices from this:
sum row, and this becomes:
[1 3] [0 0]
[2 4] [1 1]
sum col, and this becomes:
[1 2] [0 1]
[3 4] [0 1]
now we can play the game, and its clear that player 1 can remove from either the row or the column matrix. then player two can make a move, and finally we can see that player 1 cant win in this example.
here is my implementation for this program:
I only provide the method that is suppose to use the row and the column matrices and should return a 1 if player 1 wins and a 0 if player 1 ant win.
public static int play(int[][] rowM, int[][] colM){
int player = 1;
int[][] matrix = new int[rowM.length][colM[0].length];
for (int i = 0; i < rowM.length; i++) {
for (int j = 0; j < colM[0].length; j++) {
//if no possible move
if(rowM[i][j] == 0 && colM[i][j] == 0){
//if player 1 is playing, and losing
if(player % 2 == 1){
matrix[i][j] = 0;
player++;
}
//player 2 is losing
else{
matrix[i][j] = 1;
player++;
}
continue;
}
//if player 1 is playing
if(player % 2 == 1){
player++;
//the value in this position is even
if(rowM[i][j] == 1){
//if there is only 1 row left,
if(i == 0){
matrix[i][j] = 1;
}
else if(matrix[i-1][j] == 1){
matrix[i][j] = 1;
}
else{
matrix[i][j] = 0;
}
}
//the value in the col matrix this position is even
else if(colM[i][j] == 1){
//if there is only 1 column left,
if(j == 0){
//matrix[i][j] = 1;
matrix[i][j] = 1;
//player++;
}
else if (matrix[i][j-1] == 1){
matrix[i][j] = 1;
}
else{
matrix[i][j] = 0;
}
}
}
//player 2 is playing
else{
player++;
//the value in this position is even
if(rowM[i][j] == 1){
if(i == 0){
matrix[i][j] = 1;
}
else if(matrix[i-1][j] == 1){
matrix[i][j] = 0;
}
else{
matrix[i][j] = 1;
}
}
//the value in the col matrix this position is even
else if(colM[i][j] == 1){
if(j == 0){
matrix[i][j] = 1;
}
else if(matrix[i][j-1] == 1){
matrix[i][j] = 0;
}
else{
matrix[i][j] = 1;
}
}
}
}
}
return matrix[rowM.length-1][colM[0].length-1];
}
rowM and colM are matrices which have 1's and 0's inside, them, I didnt put my method that makes those matrices. this algorithm works with almost everything but I have found few examples that wont work with my implementation. I think some where in my code, specially one of the if statements must not be correct. I use debug to find it but I couldnt find out why it goes wrong.
here is an example that wot work with my code:
a matrix 2*1:
[2]
[1]
this gives me a 1, but its wrong it should be 0. Can some one help me to find my problem. this is my last option to see what am doing wrong. Please help

TicTacToe using two dimensional arrays

I am trying to create a program that does the game TicTacToe. I have finished creating
all the methods and I just need to create the driver program. Before creating the
driver program, I tried to just print the board along with a character but I don't
think my methods are correct. Here is what my error looks like:
java.lang.ArrayIndexOutOfBoundsException: 3
at TicTacToeBoard.move(TicTacToeBoard.java:75)
at TicTacToe.main(TicTacToe.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)
Here are my two programs:
This is my driver program that I can't seem to complete. The last thing that will be shown is the
template so that you have the idea of how each program works.
class TicTacToe
{
public static void main(String [] args)
{
//System.out.println("Welcome! Tic-Tac-Toe is a two player game.");
//System.out.println("Enter player one's name: ");
TicTacToeBoard game = new TicTacToeBoard();
System.out.println(game.toString());
//int count = 0;
game.move('x', 1, 3);
// game.move('o', 1, 1);
/* while (game.gameWon() !true || count != 9)
{
System.out.print(game.move());
System.out.print(game.isEmpty());
}*/
}
}
This is where all the methods are......
class TicTacToeBoard
{
private char [][] board = new char[3][3];
String b;
// This a new constructor that creates a tic-tac-toe board
public TicTacToeBoard()
{
for (int rows = 0; rows < board.length; rows++)// creates rows
{
for (int columns = 0; columns <board[rows].length;columns++)// creates columns
{
//System.out.println("| ");
board[rows][columns] = ' ';
//System.out.println(" |\n" );
}
}
}
// creates a string form of the tic-tac-toe board and allows the user
// to access it during the game.
public String toString()
{
String b = "";
// creates a vertical bar at the beginning and the end of each row
for (int rows = 0; rows < board.length; rows++)
{
b += "| ";
// adds a space for each row and column character in tic-tac-toe board.
for (int columns = 0; columns < board[rows].length; columns++)
{
b += board[rows][columns] + " ";
}
b += "|\n";// prints a | space space space | and breaks off to create two new lines.
}
return b; // prints the tic-tac-toe board to be accessed by the user.
}
String move(char x, int rows, int columns)
{
String b = "";
// creates a vertical bar at the beginning and the end of each row
for (int r = 0; r < board.length; r++)
{
b += "| ";
for (int c = 0; c < board[r].length; c++)
{
b += board[r][c] + " "; //prints 3 spaces on each line.
// prints string character from user input if row and column not equal to zero
if (board[rows - 1][columns - 1] >= 0 && board[rows - 1][columns - 1] <= 2 )
{
board[rows - 1][columns - 1] = x;// prints character in the specified index from user input
b += board[rows - 1][columns - 1];// prints out the board and the new character in specified space.
}
else if (board[rows - 1][columns - 1] < 0) // makes user pick another choice
return "ILLEGAL MOVE, TRY AGAIN!";
// adds a space for each row and column character in tic-tac-toe board.
}
b += "|\n";// prints a | space space space | and breaks off to create two new lines.
}
return b; // prints the tic-tac-toe board to be accessed by the user.
}
// checks if a space character is empty
void isEmpty(char x, int row, int col)
{
if (board [row - 1][col - 1] == ' ')
board[row - 1][col - 1] = x;
else // makes user pick another row and column if space character is not empty
System.out.println("ILLEGAL CHOICE, PICK AGAIN!");
}
// checks if game is won
public boolean gameWon(int row, int col)
{
if ((board[2][0] == board[1][1]) && (board[2][0] == board[0][2]))
return true;
else if ((board[2][0] != board[1][1]) && (board[2][0] != board[0][2]))
return false;
if ((board[2][2] == board[1][1])&& (board[2][2] == board[0][0]))
return true;
else if ((board[2][2] != board[1][1])&& (board[2][2] != board[0][0]))
return false;
if ((board[0][0] == board[1][0]) && (board[0][0] == board[2][0]))
return true;
else if ((board[0][0] != board[1][0]) && (board[0][0] != board[2][0]))
return false;
if ((board[0][1] == board[1][1]) && (board[0][1] == board[2][1]))
return true;
else if ((board[0][1] != board[1][1]) && (board[0][1] != board[2][1]))
return false;
if ((board[0][2] == board[1][2]) && (board[0][2] == board[2][2]))
return true;
else if ((board[0][2] != board[1][2]) && (board[0][2] != board[2][2]))
return false;
if ((board[0][0] == board[0][1]) && (board[0][0] == board[0][2]))
return true;
else if ((board[0][0] != board[0][1]) && (board[0][0] != board[0][2]))
return false;
if ((board[1][0] == board[1][1]) && (board[1][0] == board[1][2]))
return true;
else if ((board[1][0] != board[1][1]) && (board[1][0] != board[1][2]))
return false;
if ((board[2][0] == board[2][1]) && (board[2][0] == board[2][2]))
return true;
else
return false;
}
}
Here is the template for the whole thing!!!!!
class TicTacToe
{
public static void main (String [] args)
{
TicTacToeBoard b = new TicTacToeBoard();
while (game not over)
{
swtich player
increment turn counter
until user enters a valid move
{
prompt for move
}
make move
b.makeMove (player, row, col);
print board
System.out.println(b);
}
print outcome
}
}
class TicTacToeBoard
{
private char [][] board = ...;
public TicTacToeBoard()
{
initialize board with spaces
}
public void makeMove (char c, int row, int col)
{
store symbol in specified position
}
public boolean isEmpty(int row, int col)
{
return true if square is unfilled
}
public boolean gameWon()
{
check board for a win
}
public String toString ()
{
return String representation of board
}
}
Programming languages are unforgiving for errors and force rigor and care on us.
Your code is quite difficult for us to read and thus for both us and you to debug, starting with your indentation which is all over the place, but there are also careless errors, especially this one:
for (int r = 0; r < board.length; rows++)
Do you see what is wrong here? r is not the same as rows, and you can't use one as the index for the loop and then increment the other. You're using both of these variables inside of the loop. There are several other careless errors in the code as well.
I recommend that you start over but be much more careful with your code and be especially careful with your indentation. If you don't line up your curly braces correctly, you will not see when one code block ends and another begins (nor will we!).
Oh, and next time, please let us know which lines of your code are causing your error. It will be much easier to help you if we don't have to guess this information.
Edit
Your new code indentation is some better, but still is off. This is what you have:
String move(char x, int rows, int columns)
{
String b = "";
// creates a vertical bar at the beginning and the end of each row
for (int r = 0; r < board.length; r++)
{
b += "| ";
for (int c = 0; c < board[r].length; c++)
{
b += board[rows][columns] + " ";
and this is what I recommend:
String move(char x, int rows, int columns) {
String b = "";
// creates a vertical bar at the beginning and the end of each row
for (int r = 0; r < board.length; r++) {
b += "| ";
for (int c = 0; c < board[r].length; c++) {
// let's check to see what the variables hold!
System.out.printf("rows: %d, columns %d, r: %d, c: %d%n", rows, columns, r, c);
b += board[rows][columns] + " "; // **** the offending line ****
Even more important, note the result from the printf statement followed immediately by the exception:
rows: 1, columns 3, r: 0, c: 0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
So here it is the column variable that holds the value of 3 and causes your array to explode. You will need to track back to see how you call this method and why it is passing a 3 into the column parmaeter.
Edit 2
On re-review of your latest post, you're still hard coding your move method to accept a 3 as the column parameter:
game.move('x', 1, 3);
Fix that first and foremost. That parameter can't be 3.

Categories