Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class Reversi extends JApplet implements ActionListener
- {
- private Display display;
- private JLabel player; //accessed in init() and actionPerformed()
- private JButton newGame;
- int pressRow = 0;
- int pressCol = 0;
- boolean up;
- boolean down;
- boolean left;
- boolean right;
- public void init()
- {
- Container window = getContentPane();
- //North is a label with the title of the game
- JLabel title = new JLabel("Reversi Challenge!", SwingConstants.CENTER);
- title.setFont(new Font("ComicSans", Font.BOLD, 28));
- window.add(title, BorderLayout.NORTH);
- //South is label for which color moves next
- player = new JLabel("BLUE to move", SwingConstants.CENTER); //Changes color with player
- player.setForeground(Color.BLUE); //Changes color with player
- player.setFont(new Font("ComicSans",Font.BOLD, 20));
- window.add(player, BorderLayout.SOUTH);
- //What color scheme do you want?
- //East is nothing
- //West is a JPanel with 2 buttons vertically
- JPanel menu = new JPanel();
- menu.setLayout(new GridLayout(2,1));
- newGame = new JButton("New Game");
- newGame.addActionListener(this);
- menu.add(newGame);
- JButton quit = new JButton("Quit");
- quit.addActionListener(this);
- menu.add(quit);
- window.add(menu, BorderLayout.WEST);
- //Center is the array of buttons
- display = new Display();
- window.add(display, BorderLayout.CENTER);
- setSize(500,500);
- }
- public void actionPerformed(ActionEvent e)
- {
- JButton button = (JButton)e.getSource();
- if (button == newGame) display.newGame();
- else System.exit(0);
- }
- class Display extends JPanel implements ActionListener
- {
- private final int ROWS = 6; //What size?
- private final int COLS = 6;
- private JButton[][]buttons = new JButton[ROWS][COLS];
- private Color currentColor;
- public Display()
- {
- setLayout(new GridLayout(ROWS,COLS));
- for (int r = 0; r < ROWS; r++)
- for (int c = 0; c < COLS; c++)
- {
- buttons[r][c] = new JButton("");
- buttons[r][c].addActionListener(this);
- buttons[r][c].setBackground(Color.WHITE);
- add(buttons[r][c]);
- }
- currentColor = Color.BLUE;
- buttons[2][2].setBackground(Color.BLUE);
- buttons[2][3].setBackground(Color.GREEN);
- buttons[3][3].setBackground(Color.BLUE);
- buttons[3][2].setBackground(Color.GREEN);
- //Set any original marks you want in the center
- }
- public void newGame()
- {
- for (int r = 0; r < ROWS; r++)
- for (int c = 0; c < COLS; c++)
- buttons[r][c].setBackground(Color.WHITE);
- buttons[2][2].setBackground(Color.BLUE);
- buttons[2][3].setBackground(Color.GREEN);
- buttons[3][3].setBackground(Color.BLUE);
- buttons[3][2].setBackground(Color.GREEN);
- }
- public void actionPerformed(ActionEvent e)
- {
- JButton button = (JButton)e.getSource();
- for (int r = 0; r < ROWS; r++)
- for (int c = 0; c < COLS; c++)
- {
- if (button == buttons[r][c])
- {
- pressRow = r;
- pressCol = c;
- }
- }
- // White cells turn to players color
- if(buttons[pressRow][pressCol].getBackground() == Color.WHITE)
- {
- buttons[pressRow][pressCol].setBackground(currentColor);
- if(pressRow != 0){
- for (int row = pressRow - 1; row >= 0; row--)
- if(buttons[row][pressCol].getBackground() == currentColor)
- up = true;
- }
- if(pressRow != ROWS - 1){
- for (int row = pressRow + 1; row < ROWS; row++)
- if(buttons[row][pressCol].getBackground() == currentColor)
- down = true;
- }
- if(pressCol != 0){
- for (int col = pressCol - 1; col >= 0; col--)
- if(buttons[pressRow][col].getBackground() == currentColor)
- left = true;
- }
- if(pressCol != COLS - 1){
- for (int col = pressCol + 1; col < COLS; col++)
- if(buttons[pressRow][col].getBackground() == currentColor)
- right = true;
- }
- int cr = pressRow;
- int cc = pressCol;
- if (down)
- {
- cr++;
- while(buttons[cr][pressCol].getBackground() != currentColor){
- buttons[cr][pressCol].setBackground(currentColor);
- cr++;
- }
- cr = pressRow;
- }
- if (up)
- {
- cr--;
- while(buttons[cr][pressCol].getBackground() != currentColor){
- buttons[cr][pressCol].setBackground(currentColor);
- cr--;
- }
- cr = pressRow;
- }
- if (left)
- {
- cc--;
- while(buttons[pressRow][cc].getBackground() != currentColor){
- buttons[pressRow][cc].setBackground(currentColor);
- cc--;
- }
- cc = pressCol;
- }
- if (right)
- {
- cc++;
- while(buttons[pressRow][cc].getBackground() != currentColor){
- buttons[pressRow][cc].setBackground(currentColor);
- cc++;
- }
- cc = pressCol;
- }
- if(currentColor == Color.BLUE)
- {
- player.setForeground(Color.GREEN);
- player.setText("Green to move");
- currentColor = Color.GREEN;
- }
- else
- {
- player.setForeground(Color.BLUE);
- player.setText("BLUE to move");
- currentColor = Color.BLUE;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement