Advertisement
mixeila

Untitled

Apr 9th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. package tetris;
  2.  
  3. import java.awt.Dimension;
  4.  
  5. import javax.swing.JCheckBox;
  6. import javax.swing.JComponent;
  7. import javax.swing.JFrame;
  8. import javax.swing.JLabel;
  9. import javax.swing.JPanel;
  10. import javax.swing.JSlider;
  11. import javax.swing.SwingUtilities;
  12. import javax.swing.UIManager;
  13.  
  14. public class JBrainTetris extends JTetris {
  15.  
  16. protected static DefaultBrain brain;
  17. protected JCheckBox brainMode;
  18. protected JSlider slider;
  19. protected JLabel ok;
  20. protected Brain.Move move;
  21. protected static int numPiece;
  22.  
  23. JBrainTetris(int pixels) {
  24. super(pixels);
  25. }
  26.  
  27. public static void main(String[] args) {
  28. try {
  29. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  30. } catch (Exception ignored) { }
  31.  
  32. JBrainTetris tetris = new JBrainTetris(16);
  33. JFrame frame = JBrainTetris.createFrame(tetris);
  34. frame.setVisible(true);
  35. numPiece = -1;
  36. brain = new DefaultBrain();
  37. }
  38.  
  39. @Override
  40. public JComponent createControlPanel() {
  41. JPanel panel = (JPanel)super.createControlPanel();
  42. brainMode = new JCheckBox("Brain active");
  43. panel.add(brainMode);
  44. panel.add(new JLabel("Adversary:"));
  45. slider = new JSlider(0, 100, 0);
  46. slider.setPreferredSize(new Dimension(100, 15));
  47. panel.add(slider);
  48. ok = new JLabel(" ok ");
  49. panel.add(ok);
  50. return panel;
  51. }
  52. @Override
  53. public void tick(int verb) {
  54. if(numPiece!=super.count || move==null) {
  55. numPiece = super.count;
  56. board.undo();
  57. move = brain.bestMove(board, currentPiece, currentY, null);
  58. }
  59. if(verb==DOWN) {
  60. if(brainMode.isSelected()) {
  61. if(move != null) {
  62. if(!move.piece.equals(currentPiece)) super.tick(ROTATE);
  63. if(move.x == currentX && move.y<currentY) {
  64. if(move.piece.equals(currentPiece)) {
  65. super.tick(DROP);
  66. return;
  67. }
  68. }else {
  69. if(move.x>currentX) super.tick(RIGHT);
  70. if(move.x<currentX) super.tick(LEFT);
  71. }
  72. }
  73. }
  74. super.tick(DOWN);
  75. }else {
  76. super.tick(verb);
  77. }
  78. }
  79. @Override
  80. public Piece pickNextPiece() {
  81. int rand = random.nextInt(100);
  82. if(slider.getValue()>rand) {
  83. ok.setText("*ok*");
  84. return getWorstPiece();
  85. }else {
  86. ok.setText(" ok ");
  87. return super.pickNextPiece();
  88. }
  89. }
  90.  
  91. private Piece getWorstPiece() {
  92. Brain.Move result = brain.bestMove(board, pieces[0], board.getHeight(), null);
  93. for(int i=1; i<pieces.length; i++) {
  94. Brain.Move temp = brain.bestMove(board, pieces[i], board.getHeight(), null);
  95. if(temp.score > result.score) result = temp;
  96. }
  97. return result.piece;
  98. }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement