Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.48 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. public class Reversi extends JApplet implements ActionListener
  5. {
  6.     private Display display;
  7.     private JLabel player;      //accessed in init() and actionPerformed()
  8.     private JButton newGame;
  9.    
  10.     int pressRow = 0;
  11.     int pressCol = 0;
  12.    
  13.     boolean up;
  14.     boolean down;
  15.     boolean left;
  16.     boolean right;
  17.    
  18.     public void init()
  19.     {
  20.         Container window = getContentPane();
  21.        
  22.         //North is a label with the title of the game
  23.         JLabel title = new JLabel("Reversi Challenge!", SwingConstants.CENTER);
  24.         title.setFont(new Font("ComicSans", Font.BOLD, 28));
  25.         window.add(title, BorderLayout.NORTH);
  26.        
  27.         //South is label for which color moves next
  28.         player = new JLabel("BLUE to move", SwingConstants.CENTER);     //Changes color with player
  29.         player.setForeground(Color.BLUE);                               //Changes color with player
  30.         player.setFont(new Font("ComicSans",Font.BOLD, 20));
  31.         window.add(player, BorderLayout.SOUTH);
  32.        
  33.         //What color scheme do you want?
  34.        
  35.         //East is nothing
  36.        
  37.         //West is a JPanel with 2 buttons vertically
  38.         JPanel menu = new JPanel();
  39.         menu.setLayout(new GridLayout(2,1));
  40.        
  41.         newGame = new JButton("New Game");
  42.         newGame.addActionListener(this);
  43.         menu.add(newGame);
  44.        
  45.         JButton quit = new JButton("Quit");
  46.         quit.addActionListener(this);
  47.         menu.add(quit);
  48.        
  49.         window.add(menu, BorderLayout.WEST);
  50.        
  51.         //Center is the array of buttons
  52.         display = new Display();
  53.         window.add(display, BorderLayout.CENTER);
  54.        
  55.         setSize(500,500);
  56.     }
  57.    
  58.    
  59.     public void actionPerformed(ActionEvent e)
  60.     {
  61.         JButton button = (JButton)e.getSource();
  62.         if (button == newGame) display.newGame();
  63.         else System.exit(0);
  64.    
  65.     }  
  66.    
  67.     class Display extends JPanel implements ActionListener
  68.     {
  69.         private final int ROWS = 6;     //What size?
  70.         private final int COLS = 6;
  71.         private JButton[][]buttons = new JButton[ROWS][COLS];
  72.         private Color currentColor;
  73.        
  74.         public Display()
  75.         {
  76.             setLayout(new GridLayout(ROWS,COLS));
  77.        
  78.             for (int r = 0; r < ROWS; r++)
  79.                 for (int c = 0; c < COLS; c++)
  80.                 {
  81.                     buttons[r][c] = new JButton("");
  82.                     buttons[r][c].addActionListener(this);
  83.                     buttons[r][c].setBackground(Color.WHITE);  
  84.                     add(buttons[r][c]);
  85.                 }
  86.             currentColor = Color.BLUE;
  87.            
  88.             buttons[2][2].setBackground(Color.BLUE);
  89.             buttons[2][3].setBackground(Color.GREEN);
  90.             buttons[3][3].setBackground(Color.BLUE);
  91.             buttons[3][2].setBackground(Color.GREEN);
  92.            
  93.             //Set any original marks you want in the center
  94.         }
  95.        
  96.         public void newGame()
  97.         {
  98.             for (int r = 0; r < ROWS; r++)
  99.                 for (int c = 0; c < COLS; c++)
  100.                     buttons[r][c].setBackground(Color.WHITE);
  101.            
  102.             buttons[2][2].setBackground(Color.BLUE);
  103.             buttons[2][3].setBackground(Color.GREEN);
  104.             buttons[3][3].setBackground(Color.BLUE);
  105.             buttons[3][2].setBackground(Color.GREEN);
  106.         }
  107.        
  108.         public void actionPerformed(ActionEvent e)
  109.         {
  110.             JButton button = (JButton)e.getSource();
  111.             for (int r = 0; r < ROWS; r++)
  112.                 for (int c = 0; c < COLS; c++)
  113.                 {
  114.                     if (button == buttons[r][c])
  115.                     {
  116.                         pressRow = r;
  117.                         pressCol = c;
  118.                     }
  119.                 }
  120.            
  121.             // White cells turn to players color
  122.            
  123.             if(buttons[pressRow][pressCol].getBackground() == Color.WHITE)
  124.             {
  125.                 buttons[pressRow][pressCol].setBackground(currentColor);
  126.                
  127.                 if(pressRow != 0){
  128.                     for (int row = pressRow - 1; row >= 0; row--)
  129.                         if(buttons[row][pressCol].getBackground() == currentColor)
  130.                             up = true;
  131.                 }
  132.                 if(pressRow != ROWS - 1){
  133.                     for (int row = pressRow + 1; row < ROWS; row++)
  134.                         if(buttons[row][pressCol].getBackground() == currentColor)
  135.                             down = true;
  136.                 }  
  137.                
  138.                
  139.                 if(pressCol != 0){
  140.                     for (int col = pressCol - 1; col >= 0; col--)
  141.                         if(buttons[pressRow][col].getBackground() == currentColor)
  142.                             left = true;
  143.                 }
  144.                
  145.                 if(pressCol != COLS - 1){
  146.                     for (int col = pressCol + 1; col < COLS; col++)
  147.                         if(buttons[pressRow][col].getBackground() == currentColor)
  148.                             right = true;
  149.                 }
  150.                
  151.                 int cr = pressRow;
  152.                 int cc = pressCol;
  153.                 if (down)
  154.                 {
  155.                     cr++;
  156.                     while(buttons[cr][pressCol].getBackground() != currentColor){
  157.                         buttons[cr][pressCol].setBackground(currentColor);
  158.                         cr++;
  159.                     }
  160.                     cr = pressRow;
  161.                 }
  162.                
  163.                 if (up)
  164.                 {
  165.                     cr--;
  166.                     while(buttons[cr][pressCol].getBackground() != currentColor){
  167.                         buttons[cr][pressCol].setBackground(currentColor);
  168.                         cr--;
  169.                     }
  170.                     cr = pressRow;
  171.                 }
  172.                
  173.                 if (left)
  174.                 {
  175.                     cc--;
  176.                     while(buttons[pressRow][cc].getBackground() != currentColor){
  177.                         buttons[pressRow][cc].setBackground(currentColor);
  178.                         cc--;
  179.                     }
  180.                     cc = pressCol;
  181.                 }
  182.                
  183.                 if (right)
  184.                 {
  185.                     cc++;
  186.                     while(buttons[pressRow][cc].getBackground() != currentColor){
  187.                         buttons[pressRow][cc].setBackground(currentColor);
  188.                         cc++;
  189.                     }
  190.                     cc = pressCol;
  191.                 }
  192.                
  193.                 if(currentColor == Color.BLUE)
  194.                 {
  195.                     player.setForeground(Color.GREEN);
  196.                     player.setText("Green to move");
  197.                     currentColor = Color.GREEN;
  198.                 }
  199.                 else
  200.                 {
  201.                     player.setForeground(Color.BLUE);
  202.                     player.setText("BLUE to move");
  203.                     currentColor = Color.BLUE;
  204.                 }
  205.             }
  206.         }
  207.    
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement