Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.68 KB | None | 0 0
  1.  public boolean gameOver()
  2.     {
  3.        JFrame frame = new JFrame("Game Over");
  4.        boolean gameIsOver = false;
  5.        int filledSquares = 0;
  6.        int emptySquares = 0; // doesn't affect anything
  7.        int playerOneScore = players.get(Constants.PLAYER_ONE).getScore();
  8.        int playerTwoScore = players.get(Constants.PLAYER_TWO).getScore();
  9.        
  10.        //this checks the board to see if all of the squares are populated
  11.        for(int row = 0; row < Constants.ROWS; row++)
  12.        {
  13.             for(int col = 0; col <Constants.COLUMNS; col++)
  14.             {
  15.                 if (board[row][col].getDiscColor() != Constants.EMPTY)
  16.                     filledSquares++;
  17.                 else if(board[row][col].getDiscColor() == Constants.EMPTY)
  18.                     emptySquares++;
  19.             }
  20.        }
  21.        
  22.        if (filledSquares == Constants.MAX_DISCS)
  23.        {
  24.            if (playerOneScore > playerTwoScore )
  25.            {
  26.                JOptionPane.showMessageDialog(frame, "Game Over! Player One wins!");
  27.                //System.exit(0);
  28.            }
  29.            else if (playerTwoScore > playerOneScore)
  30.            {
  31.                JOptionPane.showMessageDialog(frame, "Game Over! Player Two wins!");
  32.                //System.exit(0);
  33.            }
  34.            else
  35.            {
  36.                JOptionPane.showMessageDialog(frame, "Game Over! It is a tie!");
  37.                //System.exit(0);
  38.            }
  39.            
  40.          
  41.        }
  42.        
  43.        //checks to see if neither player can make a valid move.
  44.        
  45.        boolean anyValidMoves = false;
  46.        Color color;
  47.        
  48.        
  49.        for(int row = 0; row < Constants.ROWS; row++)
  50.        {
  51.             for(int col = 0; col <Constants.COLUMNS; col++)
  52.          
  53.             {
  54.                 color = players.get(Constants.PLAYER_ONE).getDiscColor();
  55.                 if(checkUpValid(row, col, color))
  56.                 {
  57.                    anyValidMoves = true;
  58.                 }
  59.                 if(checkUpLeftValid(row, col, color))
  60.                 {
  61.                    anyValidMoves = true;
  62.                 }
  63.                 if(checkLeftValid(row, col, color))
  64.                 {
  65.                    anyValidMoves = true;
  66.                 }
  67.                 if(checkLeftDownValid(row, col, color))
  68.                 {
  69.                    anyValidMoves = true;
  70.                 }
  71.                 if(checkDownValid(row, col, color))
  72.                 {
  73.                    anyValidMoves = true;
  74.                 }
  75.                 if(checkDownRightValid(row, col, color))
  76.                 {
  77.                    anyValidMoves = true;
  78.                 }
  79.                 if(checkRightValid(row, col, color))
  80.                 {
  81.                    anyValidMoves = true;
  82.                 }
  83.                 if(checkUpRightValid(row, col, color))
  84.                 {
  85.                    anyValidMoves = true;
  86.                 }
  87.             }
  88.         }
  89.        
  90.        if (anyValidMoves == false)
  91.        {
  92.            if (playerOneScore > playerTwoScore )
  93.            {
  94.                JOptionPane.showMessageDialog(frame, "No available moves! Game Over! Player One wins!");
  95.                //System.exit(0);
  96.            }
  97.            else if (playerTwoScore > playerOneScore)
  98.            {
  99.                JOptionPane.showMessageDialog(frame, "No available moves! Game Over! Player Two wins!");
  100.                //System.exit(0);
  101.            }
  102.            else
  103.            {
  104.                JOptionPane.showMessageDialog(frame, "Now available moves! Game Over! It is a tie!");
  105.                //System.exit(0);
  106.            }
  107.          
  108.        }
  109.  
  110.        
  111.        for(int row = 0; row < Constants.ROWS; row++)
  112.        {
  113.             for(int col = 0; col <Constants.COLUMNS; col++)
  114.             {
  115.                 color = players.get(Constants.PLAYER_TWO).getDiscColor();
  116.                
  117.                 if(checkUpValid(row, col, color))
  118.                 {
  119.                    anyValidMoves = true;
  120.                 }
  121.                 if(checkUpLeftValid(row, col, color))
  122.                 {
  123.                    anyValidMoves = true;
  124.                 }
  125.                 if(checkLeftValid(row, col, color))
  126.                 {
  127.                    anyValidMoves = true;
  128.                 }
  129.                 if(checkLeftDownValid(row, col, color))
  130.                 {
  131.                    anyValidMoves = true;
  132.                 }
  133.                 if(checkDownValid(row, col, color))
  134.                 {
  135.                    anyValidMoves = true;
  136.                 }
  137.                 if(checkDownRightValid(row, col, color))
  138.                 {
  139.                    anyValidMoves = true;
  140.                 }
  141.                 if(checkRightValid(row, col, color))
  142.                 {
  143.                    anyValidMoves = true;
  144.                 }
  145.                 if(checkUpRightValid(row, col, color))
  146.                 {
  147.                    anyValidMoves = true;
  148.                 }
  149.             }
  150.         }
  151.        if (anyValidMoves == false)
  152.        {
  153.            if (players.get(Constants.PLAYER_ONE).getScore() > players.get(Constants.PLAYER_TWO).getScore() )
  154.            {
  155.                JOptionPane.showMessageDialog(frame, "Game Over! Player One wins!");
  156.                return gameIsOver;
  157.            }
  158.            else if (players.get(Constants.PLAYER_TWO).getScore() > players.get(Constants.PLAYER_ONE).getScore())
  159.            {
  160.                JOptionPane.showMessageDialog(frame, "Game Over! Player Two wins!");
  161.                return gameIsOver;
  162.            }
  163.            else
  164.            {
  165.                JOptionPane.showMessageDialog(frame, "Game Over! It is a tie!");
  166.                return gameIsOver;
  167.            }
  168.            
  169.          
  170.        }
  171.        
  172.     return gameIsOver;
  173.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement