Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public boolean gameOver()
- {
- JFrame frame = new JFrame("Game Over");
- boolean gameIsOver = false;
- int filledSquares = 0;
- int emptySquares = 0; // doesn't affect anything
- int playerOneScore = players.get(Constants.PLAYER_ONE).getScore();
- int playerTwoScore = players.get(Constants.PLAYER_TWO).getScore();
- //this checks the board to see if all of the squares are populated
- for(int row = 0; row < Constants.ROWS; row++)
- {
- for(int col = 0; col <Constants.COLUMNS; col++)
- {
- if (board[row][col].getDiscColor() != Constants.EMPTY)
- filledSquares++;
- else if(board[row][col].getDiscColor() == Constants.EMPTY)
- emptySquares++;
- }
- }
- if (filledSquares == Constants.MAX_DISCS)
- {
- if (playerOneScore > playerTwoScore )
- {
- JOptionPane.showMessageDialog(frame, "Game Over! Player One wins!");
- //System.exit(0);
- }
- else if (playerTwoScore > playerOneScore)
- {
- JOptionPane.showMessageDialog(frame, "Game Over! Player Two wins!");
- //System.exit(0);
- }
- else
- {
- JOptionPane.showMessageDialog(frame, "Game Over! It is a tie!");
- //System.exit(0);
- }
- }
- //checks to see if neither player can make a valid move.
- boolean anyValidMoves = false;
- Color color;
- for(int row = 0; row < Constants.ROWS; row++)
- {
- for(int col = 0; col <Constants.COLUMNS; col++)
- {
- color = players.get(Constants.PLAYER_ONE).getDiscColor();
- if(checkUpValid(row, col, color))
- {
- anyValidMoves = true;
- }
- if(checkUpLeftValid(row, col, color))
- {
- anyValidMoves = true;
- }
- if(checkLeftValid(row, col, color))
- {
- anyValidMoves = true;
- }
- if(checkLeftDownValid(row, col, color))
- {
- anyValidMoves = true;
- }
- if(checkDownValid(row, col, color))
- {
- anyValidMoves = true;
- }
- if(checkDownRightValid(row, col, color))
- {
- anyValidMoves = true;
- }
- if(checkRightValid(row, col, color))
- {
- anyValidMoves = true;
- }
- if(checkUpRightValid(row, col, color))
- {
- anyValidMoves = true;
- }
- }
- }
- if (anyValidMoves == false)
- {
- if (playerOneScore > playerTwoScore )
- {
- JOptionPane.showMessageDialog(frame, "No available moves! Game Over! Player One wins!");
- //System.exit(0);
- }
- else if (playerTwoScore > playerOneScore)
- {
- JOptionPane.showMessageDialog(frame, "No available moves! Game Over! Player Two wins!");
- //System.exit(0);
- }
- else
- {
- JOptionPane.showMessageDialog(frame, "Now available moves! Game Over! It is a tie!");
- //System.exit(0);
- }
- }
- for(int row = 0; row < Constants.ROWS; row++)
- {
- for(int col = 0; col <Constants.COLUMNS; col++)
- {
- color = players.get(Constants.PLAYER_TWO).getDiscColor();
- if(checkUpValid(row, col, color))
- {
- anyValidMoves = true;
- }
- if(checkUpLeftValid(row, col, color))
- {
- anyValidMoves = true;
- }
- if(checkLeftValid(row, col, color))
- {
- anyValidMoves = true;
- }
- if(checkLeftDownValid(row, col, color))
- {
- anyValidMoves = true;
- }
- if(checkDownValid(row, col, color))
- {
- anyValidMoves = true;
- }
- if(checkDownRightValid(row, col, color))
- {
- anyValidMoves = true;
- }
- if(checkRightValid(row, col, color))
- {
- anyValidMoves = true;
- }
- if(checkUpRightValid(row, col, color))
- {
- anyValidMoves = true;
- }
- }
- }
- if (anyValidMoves == false)
- {
- if (players.get(Constants.PLAYER_ONE).getScore() > players.get(Constants.PLAYER_TWO).getScore() )
- {
- JOptionPane.showMessageDialog(frame, "Game Over! Player One wins!");
- return gameIsOver;
- }
- else if (players.get(Constants.PLAYER_TWO).getScore() > players.get(Constants.PLAYER_ONE).getScore())
- {
- JOptionPane.showMessageDialog(frame, "Game Over! Player Two wins!");
- return gameIsOver;
- }
- else
- {
- JOptionPane.showMessageDialog(frame, "Game Over! It is a tie!");
- return gameIsOver;
- }
- }
- return gameIsOver;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement