I am creating a game called SOS. It is a 3x3 board game and it has the same concept as Tic-Tac-Toe but in this game, the players cannot choose whether they will play as X or O, the only rule in this game is to form "SOS".
Our program should be terminated after all the positions are filled, and each formed "SOS" will be added to the player who formed "SOS".
My problem is about scoring. After entering SOS in the first row which is (- - -), I tried to input "O" in the second row first column, and the player 2 will increment. It should not increment because it didn't satisfy the second else if in my program. Why is it happening?
Here is my code:
import java.util.Scanner;
public class SOS
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String ar[] = {"-","-","-","-","-","-","-","-","-"};
int player1 = 0;
int player2 = 0;
int index = 0;
for(int l = 0; l<3; l++)
{
for(int j = 0; j<3; j++)
{
System.out.print(ar[j]);
}
System.out.print("\n");
}
for(int j = 0;j < 9;j++)
{
//Input position and letter
System.out.println("Enter the position number: ");
index = input.nextInt();
input.nextLine();
System.out.println("Enter (S or O): ");
ar[index - 1] = input.nextLine();
//Output for the game
for(int l = 0; l<9; l++)
{
System.out.print(ar[l]);
if(l == 2)
{
System.out.println();
}
else if(l == 5)
{
System.out.println();
}
else if(l == 8)
{
System.out.println();
}
}
//condition
if((ar[0]+ar[1]+ar[2]).equals("SOS"))
{
if(j%2 == 0)
{
player1++;
System.out.println("Player 1: "+player1+" Player 2: "+player2);
}
else if( j % 2 != 0)
{
player2++;
System.out.println("Player 1: "+player1+" Player 2: "+player2);
}
}
else if((ar[3]+ar[4]+ar[5]).equals("SOS"))
{
if(j%2 == 0)
{
player1++;
System.out.println("Player 1: "+player1+" Player 2: "+player2);
}
else if( j % 2 != 0)
{
player2++;
System.out.println("Player 1: "+player1+" Player 2: "+player2);
}
}
else
{
System.out.println("Player 1: "+player1+" Player 2: "+player2);
}
//end of condition
}
}
}
The bug is occurring in this section of your code:
if((ar[0]+ar[1]+ar[2]).equals("SOS"))
After the first player correctly enters SOS into these positions, this statement will always be true. Which means that your nested if/else is running on every turn:
if(j%2 == 0)
{
player1++;
}
else if( j % 2 != 0)
{
player2++;
}
During the 4th turn, j will be equal to 3. (j%2 != 0) is true, which increments player2.
If you continue running the program, you will see player1 and player2 continually incrementing on every turn because the initial if will always be true.
Related
So I created a checkWinner method, using 'row' and 'col' private variables so I can locate the 'curPlayer' position in the 2D array.
import java.util.Scanner;
public class TicTacBoard
{
private char[][] board; // 2-D array of characters
private char curPlayer; // the player whose turn it is (X or O)
// added so I can locate the current player location in the board
private int row;
private int col;
// Constructor: board will be size x size
public TicTacBoard(int size)
{
board = new char[size][size];
// initialize the board with all spaces:
for(row=0; row < board.length; row++)
for(col=0; col < board[row].length; col++)
board[row][col] = ' ';
curPlayer = 'X'; // X gets the first move
}
public void playGame()
{
display();
do
{
takeTurn();
display();
}while(!checkWinner(row, col));
}
/////// display ////////
// Display the current status of the board on the
// screen, using hyphens (-) for horizontal lines
// and pipes (|) for vertical lines.
public void display()
{
System.out.println();
dispRow(0);
System.out.println("-----");
dispRow(1);
System.out.println("-----");
dispRow(2);
System.out.println();
}
// Display the current status of row r of the board
// on the screen, using hyphens (-) for horizontal
// lines and pipes (|) for vertical lines.
private void dispRow(int r)
{
System.out.println(board[r][0] + "|" + board[r][1]
+ "|" + board[r][2]);
}
/////// takeTurn ////////
// Allow the curPlayer to take a turn.
// Send output to screen saying whose turn
// it is and specifying the format for input.
// Read user's input and verify that it is a
// valid move. If it's invalid, make them
// re-enter it. When a valid move is entered,
// put it on the board.
public void takeTurn()
{
Scanner scan = new Scanner(System.in);
int row, col;
boolean invalid;
do{
invalid = false; // assume correct entry
System.out.println("It is now " + curPlayer + "'s turn.");
System.out.println("Please enter your move in the form row column.");
System.out.println("So 0 0 would be the top left, and 0 2 would be the top right.");
row = scan.nextInt();
col = scan.nextInt();
if(row < 0 || col < 0 || row > 2 || col > 2)
{
System.out.println("Invalid entry: row and column must both be between 0 and 2 (inclusive).");
System.out.println("Please try again.");
invalid = true;
}
else if(board[row][col] != ' ')
{
System.out.println("Invalid entry: Row " + row + " at Column " + col
+ " already contains: " + board[row][col]);
System.out.println("Please try again.");
invalid = true;
}
}while(invalid);
// Now that input validation loop is finished, put the move on the board:
board[row][col] = curPlayer;
// Switch to the other player (take turns):
if(curPlayer == 'X')
curPlayer = 'O';
else
curPlayer = 'X';
}
// If the game is over, print who won (if anyone),
// and return true. If the game is not over, return false.
public boolean checkWinner(int row, int col)
{
// YOUR CODE GOES HERE
int x = row;
int y = col;
// board length is always 3 here
// check winner on column
for (int i = 0; i < board.length; i++) {
if (board[x][i] != curPlayer)
break;
if (i == board.length - 1)
System.out.println("Player " + curPlayer + " wins!");
return true;
}
//check winner on row
for (int i = 0; i < board.length; i++) {
if (board[i][y] != curPlayer)
break;
if (i == board.length - 1)
System.out.println("Player " + curPlayer + " wins!");
return true;
}
// checks winner on diagonal up
if (x == y) {
for (int i = 0; i < board.length; i++) {
if (board[i][i] != curPlayer)
break;
if (i == board.length - 1)
System.out.println("Player " + curPlayer + " wins!");
return true;
}
}
// check winner on diagonal down
if (x + y == board.length - 1){
for (int i = 0; i < board.length; i++) {
if (board[i][(board.length-1)-i] != curPlayer)
break;
if (i == board.length - 1)
System.out.println("Player " + curPlayer + " wins!");
return true;
}
}
// checks if board is full
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
if (board[i][j] == '-')
System.out.println("Nobody won, game ends in a draw!");
return true;
}
}
return false;
}
}
The code works but I while I was checking I got this:
| |
-----
| |
-----
| |
It is now X's turn.
Please enter your move in the form row column.
So 0 0 would be the top left, and 0 2 would be the top right.
2 0
| |
-----
| |
-----
X| |
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
at TicTacBoard.checkWinner(TicTacBoard.java:126)
at TicTacBoard.playGame(TicTacBoard.java:43)
at Main.main(Main.java:14)
I thought the board length is always 3 with the location ranging from 0 to 3. Any solutions to this error? Any more efficient ways to do this? Please let me know!
You have a "shadowing" problem - that is, you're shadowing the instance fields row and col with local variables in your takeTurn method.
In it's current state...
// Constructor: board will be size x size
public TicTacBoard(int size) {
board = new char[size][size];
// initialize the board with all spaces:
for (row = 0; row < board.length; row++) {
for (col = 0; col < board[row].length; col++) {
board[row][col] = ' ';
}
}
curPlayer = 'X'; // X gets the first move
}
after the constructor has run, row and col will be 3, but in takeTurn, you define row and col as local variables...
public void takeTurn() {
Scanner scan = new Scanner(System.in);
int row, col;
boolean invalid;
This means, that when you call checkWinner in the playGame method...
public void playGame() {
display();
do {
takeTurn();
display();
} while (!checkWinner(row, col));
}
You're passing the instance field values (of 3/3) and everything breaks.
So, the "quick" solution would be to remove the local declaration of row/col from takeTurn
public void takeTurn() {
Scanner scan = new Scanner(System.in);
//int row, col;
boolean invalid;
You could also fix this in the constructor, but making row/col local variables
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
board[row][col] = ' ';
}
}
but at some point, you need to update the row/col value for the player, but I might consider passing this information back from takeTurn rather than trying to use instance fields.
You also have a subtle, but common bug in your if statements. Without brackets, { and }, only the line IMMEDIATELY following the if statement will be executed when the conditional statement above is true. Your INDENTATION, however, indicates that you expected a different behavior.
For instance, your very first for loop is:
for (int i = 0; i < board.length; i++) {
if (board[x][i] != curPlayer)
break;
if (i == board.length - 1)
System.out.println("Player " + curPlayer + " wins!");
return true;
}
Here, only the System.out.println() line is executed when the if statement is true. The indentation of the return true; statement indicates that you expect it to only run with the println(), only when the conditional is true.
The return true; line is NOT dependent upon the preceding if statement, though, because it is not within brackets and the if statement only runs the line immediately following it. This means that the for loop is only ever running ONE ITERATION because the return line is STAND-ALONE and executes every single time, regardless of how those if statements evaluate.
You should ALWAYS, ALWAYS, ALWAYS add brackets to your if statements, even if they are "one-liners". With that in mind, I'd expect it to look more like:
for (int i = 0; i < board.length; i++) {
if (board[x][i] != curPlayer) {
break;
}
if (i == board.length - 1) {
System.out.println("Player " + curPlayer + " wins!");
return true;
}
}
Now the return line is only executed when the preceding if statement is true.
boolean onGoing = true;
do {
String p1 = "playing";
while (p1.equals("playing")) {
System.out.println("Player 1, enter hit row/column:");
int a = sc.nextInt();
int b = sc.nextInt();
if (a >= 0 && a < 5 && b >= 0 && b < 5) {
if (history1[a][b] == '-') {
p1 += " no more";
if (board2[a][b] == '#') {
history1[a][b] = 'X';
board2[a][b] = 'X';
String status = check(board2);
if (status.equals("win")) {
System.out.println("PLAYER 1 WINS! YOU SUNK ALL OF YOUR OPPONENT'S SHIPS!");
onGoing = false;
printBattleShip(board1);
printBattleShip(board2);
break;
} else {
System.out.println("PLAYER 1 HIT PLAYER 2's SHIP!");
}
} else if (board2[a][b] == '-') {
System.out.println("PLAYER 1 MISSED!");
history1[a][b] = 'O';
board2[a][b] = 'O';
}
} else {
System.out.println("You already fired on this spot. Choose different coordinates.");
}
} else {
System.out.println("Invalid coordinates. Choose different coordinates.");
}
}
String p2 = "playing";
while (p2.equals("playing")) {
System.out.println("Player 2, enter hit row/column:");
int c = sc.nextInt();
int d = sc.nextInt();
if (c >= 0 && c < 5 && d >= 0 && d < 5) {
if (history2[c][d] == '-') {
p2 += " no more";
if (board1[c][d] == '#') {
history2[c][d] = 'X';
board1[c][d] = 'X';
String status = check(board1);
if (status.equals("win")) {
System.out.println("PLAYER 2 WINS! YOU SUNK ALL OF YOUR OPPONENT'S SHIPS!");
onGoing = false;
printBattleShip(board2);
printBattleShip(board1);
break;
} else {
System.out.println("PLAYER 2 HIT PLAYER 1's SHIP!");
}
} else if (board1[c][d] == '-') {
history2[c][d] = 'O';
board1[c][d] = 'O';
System.out.println("PLAYER 2 MISSED!");
}
} else {
System.out.println("You already fired on this spot. Choose different coordinates.");
}
} else {
System.out.println("Invalid coordinates. Choose different coordinates.");
}
}
} while (onGoing);
private static String check(char[][] arr1) {
int sum = 0;
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr1.length; j++) {
if (arr1[i][j] == '#') {
sum += 1;
}
}
}
if (sum == 0) {
return "win";
} else {
return "keep playing";
}
}
// I declared and initialized a boolean variable called ongoing before a do-while loop. Then I create a while loop inside the do while loop. And inside the while loop, there are a series of conditional statement. Eventually, I changed the value of the boolean variable and feed it back to the while statement, letting it evaluate it. But it doesn't seem to change the value. Is it because the scope of the variable? How can I fix it?
edit: check method added
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.
I've been working on for awhile but i can't seem to fix it and get it to wrong correctly. It tells me where the exception is but I don't see any problems.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at TicTacToe.displayWinner(TicTacToe.java:243)
at TicTacToe.playerMove(TicTacToe.java:151)
at TicTacToe.playGame(TicTacToe.java:303)
at TicTacToeTester.main(TicTacToeTester.java:20)
Java Result: 1
import java.util.Scanner; //Used for player's input in game
public class TicTacToe
{
//instance variables
private char[][] board; //Tic Tac Toe Board, 2d array
private boolean xTurn; // true when X's turn, false if O's turn
private Scanner input; // Scanner for reading input from keyboard
//Constants for creation of gameboard
public final int ROWS = 3; //total rows
public final int COLS = 3; //total columns
public final int WIN = 3; //amount needed to win
public TicTacToe()
{
//creates the board
board = new char[ROWS][COLS];
for(int r = 0; r < ROWS; r++)
{
for(int c = 0; c < COLS; c++)
{
board[r][c] = ' ';
}
}
//X's turn when game starts
xTurn = true;
//creates our input object for the turn player
input = new Scanner(System.in);
}
//shows game board
public void displayBoard()
{
int colNum = 0; //number of columns
int rowNum = 0; //number of rows
//creates column labels
System.out.println(" \n");
System.out.println(" Columns ");
for (int num = 0; num < COLS; num++)
{
System.out.print(" " + colNum);
colNum++;
}
//creates vertical columns and spaces between each spot
System.out.println(" \n");
for (int row = 0; row < ROWS; row++)
{
//numbers rows
System.out.print(" " + rowNum + " ");
rowNum++;
for (int col = 0; col < COLS; ++col)
{
System.out.print(board[row][col]); // print each of the cells
if (col != COLS - 1)
{
System.out.print(" | "); // print vertical partition
}
}
System.out.println();
//creates seperation of rows
if (row != ROWS - 1)
{
System.out.println(" ------------"); // print horizontal
partition
}
}
//labels row
System.out.println("Rows \n");
}
//displays turn player
public void displayTurn()
{
if (xTurn)
{
System.out.println("X's Turn");
}
else
{
System.out.println("O's Turn");
}
}
//allows you to make move
public boolean playerMove()
{
boolean invalid = true;
int row = 0;
int column = 0;
while(invalid)
{
System.out.println("Which row (first) then column (second)
would you like to \n"
+ "play this turn? Enter 2 numbers between 0-2 as \n"
+ "displayed on the board, seperated by a space to
\n"
+ "choose your position.");
row = input.nextInt();
column = input.nextInt();
//checks if spot is filled
if (row >= 0 && row <= ROWS - 1 &&
column >= 0 && column <= COLS - 1)
{
if (board[row][column] != ' ')
{
System.out.println("Spot is taken \n");
}
else
{
invalid = false;
}
}
else
{
System.out.println("Invalid position");
}
//fills spot if not taken
if (xTurn)
{
board[row][column] = 'X';
}
else
{
board[row][column] = 'O';
}
}
return displayWinner(row,column);
}
public boolean displayWinner(int lastR, int lastC)
{
boolean winner = false;
int letter = board[lastR][lastC];
//checks row for win
int spotsFilled = 0;
for (int c = 0; c < COLS; c++)
{
if(board[lastR][c] == letter)
{
spotsFilled++;
}
}
if (spotsFilled == WIN)
{
winner = true;
if (xTurn)
{
System.out.println("X won");
displayBoard();
}
else
{
System.out.println("O won");
displayBoard();
}
}
//checks columns for win
spotsFilled = 0;
for (int r = 0; r < ROWS; r++)
{
if(board[r][lastC] == letter)
{
spotsFilled++;
}
}
if (spotsFilled == WIN)
{
winner = true;
if (xTurn)
{
System.out.println("X won");
displayBoard();
}
else
{
System.out.println("O won");
displayBoard();
}
}
//checks diagonals for win
spotsFilled = 0;
for (int i = 0; i < WIN; i++)
{
if(board[i][i] == letter)
{
spotsFilled++;
}
}
if(spotsFilled == WIN)
{
winner = true;
if (xTurn)
{
System.out.println("X won");
displayBoard();
}
else
{
System.out.println("O won");
displayBoard();
}
}
//checks other diagonal
spotsFilled = 0;
for(int i = 0; i < WIN; i++)
{
if(board[i][COLS-i] == letter)
{
spotsFilled++;
}
}
if(spotsFilled == WIN)
{
winner = true;
if (xTurn)
{
System.out.println("X won");
displayBoard();
}
else
{
System.out.println("O won");
displayBoard();
}
}
return winner;
}
//checks if board is full
public boolean fullBoard()
{
int filledSpots = 0;
for(int r = 0; r < ROWS; r++)
{
for (int c = 0; c < COLS; c++)
{
if (board[r][c] == 'X' || board[r][c] == 'O')
{
filledSpots++;
}
}
}
return filledSpots == ROWS*COLS;
}
//plays game
public void playGame()
{
boolean finish = true;
System.out.println("Are your ready to start?");
System.out.println("1 for Yes or 0 for No? : ");
int choice = input.nextInt();
if (choice == 1)
{
while (finish)
{
displayBoard();
displayTurn();
if (playerMove())
{
displayBoard();
}
else if (fullBoard())
{
displayBoard();
System.out.println("Draw");
}
else
{
xTurn=!xTurn;
}
}
}
}
}
my tester
public class TicTacToeTester {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
TicTacToe tictactoe = new TicTacToe();
tictactoe.playGame();
}
}
Your Player Move method has an issue take a look at this if you notice you have commented saying "// fills spot when not taken" but you misplaced the while loop bracket and included move part inside it so it threw that exception, just close the while loop bracket before moving as follows (also follow #oblivion Creations Answer these are the two issues with your code) :-
public boolean playerMove()
{
boolean invalid = true;
int row = 0;
int column = 0;
while (invalid)
{
System.out.println("Which row (first) then column (second)" + "would you like to \n"
+ "play this turn? Enter 2 numbers between 0-2 as \n"
+ "displayed on the board, seperated by a space to" + "\n" + "choose your position.");
row = input.nextInt();
column = input.nextInt();
// checks if spot is filled
if (row >= 0 && row <= ROWS - 1 && column >= 0 && column <= COLS - 1)
{
if (board[row][column] != ' ')
{
System.out.println("Spot is taken \n");
}
else
{
invalid = false;
}
}
else
{
System.out.println("Invalid position");
}
} // close while loop here
// fills spot if not taken
if (xTurn)
{
board[row][column] = 'X';
}
else
{
board[row][column] = 'O';
}
return displayWinner(row, column);
}
The reason you are getting your exception is this section of code inside your displayWinner method:
//checks other diagonal
spotsFilled = 0;
for(int i = 0; i < WIN; i++)
{
if(board[i][COLS-i] == letter)
{
spotsFilled++;
}
}
When i = 0 then COLS-i will be 3. This is outside the bounds of your array.
There are multiple ways to solve this, one would be to increase i by 1 during comparison.
//checks other diagonal
spotsFilled = 0;
for(int i = 0; i < WIN; i++)
{
if(board[i][COLS-(i+1)] == letter)
{
spotsFilled++;
}
}
Edit: also make sure you check out Null Saints answer, as it will cause you headaches later if you don't address it now.
My brother offered me a challege to help my studies by making a game that only runs in the main method instead of using other classes to make sure I still remember my old stuff. Bases off that I went with a cat and mouse game where the player is the mouse looking for the cheese while avoiding all the cats. When you enter an empty "room" (cell) the game is supposed to give you a clue at how far you are from the cheese. Now the game runs but my clues just keep going higher and higher to the point where it over the amount of rooms in the maze. I am stumpped on where the problem is.
Here's the code
import java.util.Scanner;
import java.util.Random;
public class CatAndMouse
{
public static final int MAX = 10;
public static void main(String args[ ])
{
Scanner mouse = new Scanner(System.in);
Random placement = new Random();
boolean check = true, gameOver = false, win = false, lose = false;
final int row = MAX;
final int col = MAX;
final int page = MAX;
int cheeseX, cheeseY, cheeseZ;
int cheese = 1;
int catX, catY, catZ;
int cat = 2;
int mouseRow;
int mouseCol;
int mousePage;
int mouseMove;
int empty = 0;
int clue = 0;
int clueCount = 0;
int winQuotes;
int loseQuotes;
int [][][]maze = new int [row][col][page];
for(int i = 0; i < MAX; i++)
{
for(int j = 0; j < MAX; j++)
{
for(int k = 0; k < MAX; k++)
{
maze[i][j][k] = empty;
}//page
}//col
}//row
cheeseX = placement.nextInt(row);
cheeseY = placement.nextInt(col);
cheeseZ = placement.nextInt(page);
maze[cheeseX][cheeseY][cheeseZ] = cheese;
for (int i = 0; i < 500; i++)
{
catX = placement.nextInt(row);
catY = placement.nextInt(col);
catZ = placement.nextInt(page);
maze[catX][catY][catZ] = cat;
if ((maze[catX][catY][catZ]) == (maze[cheeseX][cheeseY][cheeseZ]))
{
catX = placement.nextInt(row);
catY = placement.nextInt(col);
catZ = placement.nextInt(page);
maze[catX][catY][catZ] = cat;
}//if place with cheese
}//cat placement loop
System.out.println("Hello there, my name is Q, do you like it? it's short for Q. So you're probably asking yourself \"why am I now a mouse?\"");
System.out.println("The answer is simple, I was bored and you humans are so much fun to play with, but don't worry I can change you back.");
System.out.println("All you have to do is win my little game and you'll be back to your old self again, loose and...well just don't lose.");
System.out.println("In this maze there is a piece of cheese, find it and you win. But be careful now, I added a \'few\' cats to hunt you.");
System.out.println("Can't make this too easy now can we? But don't worry, you'll be given clues if you're close to the cheese or not");
System.out.println("The maze itself is 10*10*10 and to move through it enter an integer between 0-9.");
System.out.println("Now then, let the game begin.");
System.out.println();
do
{
System.out.print("Enter row: ");
mouseRow = mouse.nextInt();
if((mouseRow < 0) || (mouseRow > 9))
{
while (check == true)
{
System.out.print("I said, it needs to be an integer between 0-9. Try again: ");
mouseRow = mouse.nextInt();
if((mouseRow >= 0) && (mouseRow <= 9))
check = false;
}//while closer
}//row check
check = true;
System.out.print("Enter column: ");
mouseCol = mouse.nextInt();
if((mouseCol < 0) || (mouseCol > 9))
{
while (check == true)
{
System.out.print("I said, it needs to be an integer between 0-9. Try again: ");
mouseCol = mouse.nextInt();
if((mouseCol >= 0) && (mouseCol <= 9))
check = false;
}//while closer
}//column check
check = true;
System.out.print("Enter page: ");
mousePage = mouse.nextInt();
if((mousePage < 0) || (mousePage > 9))
{
while (check == true)
{
System.out.print("I said, it needs to be an integer between 0-9. Try again: ");
mousePage = mouse.nextInt();
if((mousePage >= 0) && (mousePage <= 9))
check = false;
}//while closer
}//page check
check = true;
mouseMove = maze[mouseRow][mouseCol][mousePage];
System.out.println();
/*================[Win/Lose]===============*/
if (mouseMove == 2)
{
gameOver = true;
lose = true;
}//loser
if (mouseMove == 1)
{
gameOver = true;
win = true;
}//winner
/*===============[Win/Lose]===============*/
/*=================[Clue]=================*/
if(mouseRow == cheeseX)
{
System.out.println("In same row as cheese!");
}//if same row
else if (mouseRow > cheeseX)
{
for(int i = cheeseX; i <= mouseRow; i++)
{
clueCount++;
}//for loop closer
}//if mouse is larger
else
{
for(int i = mouseRow; i <= cheeseX; i++)
{
clueCount++;
}//for loop closer
}//else cheese is larger
clue = clue + clueCount;
if(mouseCol == cheeseY)
{
System.out.println("In same column as cheese!");
}//if same colum
if (mouseCol > cheeseY)
{
for(int i = cheeseY; i <= mouseCol; i++)
{
clueCount++;
}//for loop closer
}//if mouse is larger
else
{
for(int i = mouseCol; i <= cheeseY; i++)
{
clueCount++;
}//for loop closer
}//else cheese is larger
clue = clue + clueCount;
if(mousePage == cheeseZ)
{
System.out.println("In same page as cheese!");
}//if same page
if (mousePage > cheeseZ)
{
for(int i = cheeseZ; i <= mousePage; i++)
{
clueCount++;
}//for loop closer
}//if mouse is larger
else
{
for(int i = mousePage; i <= cheeseZ; i++)
{
clueCount++;
}//for loop closer
}//else cheese is larger
clue = clue + clueCount;
System.out.println("You are " + clue + " cells away from the cheese.");
System.out.println();
/*=================[Clue]=================*/
}while (gameOver == false);
if (win == true)
{
winQuotes = (int)(3 * Math.random()) + 1;
switch (winQuotes)
{
case 1:
System.out.println("You found the cheese! Now it's time to send you back, but don't worry. I'm sure we'll meet again soon.");
break;
case 2:
System.out.println("An excellent job, maybe you were meant to be a mouse all long. What, change you back? Oh fine.");
break;
default:
System.out.println("Congradulation, I don't think Captian Picard couldn't have done it better. Maybe I should pay him a visit.");
break;
}//win switch
}//if you won
if (lose == true)
{
loseQuotes = (int)(3 * Math.random()) + 1;
switch(loseQuotes)
{
case 1:
System.out.println("Well at least you fed a hungry cat right? Star Fleet would be so proud to have you on one of their ships.");
break;
case 2:
System.out.println("Oh come on, don't tell me you wore a red shirt before I brought you here.");
break;
default:
System.out.println("Maybe I should have brought Captian Janeway here instead, I still owe her for that punch to my face.");
break;
}//lose switch
}//if you lose
}//main closer
} //class closer
You never reinitialize your clue variable to 0 in your do/while loop. So, everytime you run through the loop, instead of setting clue to the current clue value, you keep adding to it (clue = clue + clueCount).
You aren't resetting clue to zero in the big do-while loop
So it's seems logical to me that the clues can't do anything but go higher, because it keep adding the new count to itself (clue = clue + clueCount)