Advertisement
CSenshi

OOP - HW2 Milestone: C (JBrainTetris.java)

Apr 18th, 2020
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3.  
  4. public class JBrainTetris extends JTetris {
  5.     private JCheckBox brainMode;
  6.     private Brain brain;
  7.     private Piece lastPiece;
  8.     private Brain.Move move;
  9.     private JLabel status;
  10.     private JPanel little;
  11.     private JSlider adversary;
  12.  
  13.     /**
  14.      * Creates a new JTetris where each tetris square
  15.      * is drawn with the given number of pixels.
  16.      *
  17.      * @param pixels
  18.      */
  19.     JBrainTetris(int pixels) {
  20.         super(pixels);
  21.  
  22.         this.brain = new DefaultBrain();
  23.         this.lastPiece = null;
  24.  
  25.     }
  26.  
  27.     @Override
  28.     public JComponent createControlPanel() {
  29.         JPanel panel = (JPanel) super.createControlPanel();
  30.  
  31.         // Brain checkbox
  32.         panel.add(new JLabel("Brain:"));
  33.         brainMode = new JCheckBox("Brain active");
  34.         panel.add(brainMode);
  35.  
  36.         // make a little panel, put a JSlider in it. JSlider responds to getValue()
  37.         little = new JPanel();
  38.         little.add(new JLabel("Adversary:"));
  39.         adversary = new JSlider(0, 100, 0); // min, max, current
  40.         adversary.setPreferredSize(new Dimension(100, 15));
  41.         little.add(adversary);
  42.  
  43.         // now add little to panel of controls
  44.         panel.add(little);
  45.  
  46.         // Status label
  47.         status = new JLabel("OK");
  48.         panel.add(status);
  49.  
  50.         return panel;
  51.     }
  52.  
  53.     @Override
  54.     public Piece pickNextPiece() {
  55.         int randVal = 1 + random.nextInt(99);
  56.  
  57.         if (adversary.getValue() <= randVal) {
  58.             status.setText("OK");
  59.             return super.pickNextPiece();
  60.         }
  61.  
  62.         double score = Integer.MIN_VALUE;
  63.         Piece resPiece = null;
  64.         for (Piece piece : super.pieces) {
  65.             board.undo();
  66.             Brain.Move testMove = brain.bestMove(this.board, piece, board.getHeight(), null);
  67.             if (testMove.score > score) {
  68.                 score = testMove.score;
  69.                 resPiece = piece;
  70.             }
  71.         }
  72.         status.setText("*OK*");
  73.         return resPiece;
  74.     }
  75.  
  76.     @Override
  77.     public void tick(int verb) {
  78.  
  79.         if (brainMode.isSelected()) {
  80.             if (this.currentPiece != lastPiece) {
  81.                 board.undo();
  82.                 move = brain.bestMove(this.board, this.currentPiece, board.getHeight(), null);
  83.                 lastPiece = this.currentPiece;
  84.             }
  85.  
  86.             if (move != null) {
  87.                 if (!move.piece.equals(this.currentPiece)) {
  88.                     super.tick(ROTATE);
  89.                 }
  90.  
  91.                 int dx = move.x - this.currentX;
  92.                 if (dx != 0) {
  93.                     super.tick((dx > 0) ? RIGHT : LEFT);
  94.                 }
  95.             }
  96.         }
  97.         super.tick(verb);
  98.     }
  99.  
  100.     public static void main(String[] args) {
  101.         // Set GUI Look And Feel Boilerplate.
  102.         // Do this incantation at the start of main() to tell Swing
  103.         // to use the GUI LookAndFeel of the native platform. It's ok
  104.         // to ignore the exception.
  105.         try {
  106.             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  107.         } catch (Exception ignored) {
  108.         }
  109.  
  110.         JTetris tetris = new JBrainTetris(16);
  111.         JFrame frame = JTetris.createFrame(tetris);
  112.         frame.setVisible(true);
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement