Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.04 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;
  3.  
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.border.EmptyBorder;
  7. import javax.swing.JOptionPane;
  8. import javax.swing.JTextArea;
  9. import javax.swing.JButton;
  10. import javax.swing.JTextField;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.Font;
  14. import java.util.Scanner;
  15. import javax.swing.JLabel;
  16. import javax.swing.JComboBox;
  17.  
  18.  
  19. public class C4GUI extends JFrame {
  20.  
  21.     /**
  22.      *
  23.      */
  24.     private JPanel contentPane;
  25.     private JTextField input;
  26.     private JTextArea output;
  27.     private ConnectFour game;
  28.     private char[][] board;
  29.     private JPanel panel_1;
  30.     private JLabel lblNewLabel;
  31.         private JButton newGame;
  32.     private JComboBox playerOne;
  33.     private JLabel lblNewLabel_1;
  34.     private JComboBox playerTwo;
  35.    
  36.         private String[] players;
  37.         private Player currentPlayer;
  38.     private Player firstPlayer;
  39.     private Player secondPlayer;
  40.  
  41.  
  42.     /**
  43.      * Launch the application.
  44.      */
  45.     public static void main(String[] args) {
  46.         EventQueue.invokeLater(new Runnable() {
  47.             public void run() {
  48.                 try {
  49.                     C4GUI frame = new C4GUI();
  50.                     frame.setVisible(true);
  51.                 } catch (Exception e) {
  52.                     e.printStackTrace();
  53.                 }
  54.             }
  55.         });
  56.     }
  57.  
  58.     /**
  59.      * Create the frame.
  60.      */
  61.     public C4GUI() {
  62.         setTitle("Connect Four");
  63.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  64.         setBounds(100, 100, 500, 370);
  65.         contentPane = new JPanel();
  66.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  67.         contentPane.setLayout(new BorderLayout(0, 0));
  68.         setContentPane(contentPane);
  69.  
  70.         output = new JTextArea();
  71.         output.setFont(new Font("Monospaced", Font.BOLD, 18));
  72.         output.setEditable(false);
  73.         contentPane.add(output, BorderLayout.CENTER);
  74.  
  75.         JPanel panel = new JPanel();
  76.         contentPane.add(panel, BorderLayout.SOUTH);
  77.  
  78.         newGame = new JButton("New Game");
  79.         newGame.addActionListener(new ActionListener() {
  80.             public void actionPerformed(ActionEvent arg0) {
  81.                 newGameButtonClicked();
  82.             }
  83.         });
  84.         panel.add(newGame);
  85.  
  86.         input = new JTextField();
  87.         input.addActionListener(new ActionListener() {
  88.             public void actionPerformed(ActionEvent e) {
  89.                 inputEntered();
  90.             }
  91.         });
  92.         panel.add(input);
  93.         input.setColumns(20);
  94.        
  95.         panel_1 = new JPanel();
  96.         contentPane.add(panel_1, BorderLayout.NORTH);
  97.        
  98.         lblNewLabel = new JLabel("Player X:");
  99.         panel_1.add(lblNewLabel);
  100.        
  101.                 players = new String[] {"Human", "Dumb Robot", "Good Robot"};
  102.        
  103.         playerOne = new JComboBox(players);
  104.         panel_1.add(playerOne);
  105.        
  106.         lblNewLabel_1 = new JLabel("Player O:");
  107.         panel_1.add(lblNewLabel_1);
  108.        
  109.         playerTwo = new JComboBox(players);
  110.         panel_1.add(playerTwo);
  111.        
  112.        
  113.     }
  114.         /**
  115.          * Manages entered input
  116.          */
  117.     protected void inputEntered() {
  118.         String line = input.getText();
  119.         input.setText("");
  120.                
  121.                 //if (currentPlayer instanceof HumanPlayer) {
  122.         Scanner sc = new Scanner(line);
  123.                 if (!sc.hasNextInt()) {
  124.                     //Invalid input
  125.                     JOptionPane.showMessageDialog(this, "Invalid input! Please try again.");
  126.                     return;
  127.                 }
  128.                 //} else {
  129.                    
  130.                 //}
  131.         int col = sc.nextInt();
  132.                 board = new char[ConnectFour.ROWS][ConnectFour.COLS];
  133.                
  134.         //board = new char[ConnectFour.ROWS][ConnectFour.COLS];
  135.  
  136. //      for(int j = 0; j <= ConnectFour.COLS; j++){
  137. //          for(int n = 0; n < ConnectFour.ROWS; n++){
  138. //              if(board[j][n] == 0){
  139. //                  game.setMove(game.getTurn(), col);
  140. //                  game.isGameOver();
  141. //                  game.getTurn();
  142. //                  output.setText(game.toString());
  143. //              }
  144. //          }          
  145. //      }
  146.              
  147.                
  148. //                if ((result == false) || (game.findEmptySpace(col) < -1)) { // if (!result)
  149. //                    JOptionPane.showMessageDialog(this, "Invalid input! Please try again.");
  150. //                    return;
  151. //                }
  152.         output.setText(game.toString());
  153.  
  154.        
  155.     }
  156.         /**
  157.          * Begins new game
  158.          */
  159.     protected void newGameButtonClicked() {
  160.         //      System.out.println("New Game Button clicked!");
  161.         game = new ConnectFour();
  162.         output.setText(game.toString());
  163.                 assignPlayers();
  164.                 currentPlayer = firstPlayer;
  165.                 //System.out.println(currentPlayer == null);
  166.                 makeMoves();
  167. //                while (currentPlayer instanceof HumanPlayer) {
  168. //                    inputEntered();
  169. //                }
  170. //                newGame.setEnabled(false);
  171.         }
  172.        
  173.         /**
  174.          * Assigns players based on JComboBox values
  175.          */
  176.         protected void assignPlayers() {
  177.             if (playerOne.getSelectedItem() == players[0]){
  178.             firstPlayer = new HumanPlayer();
  179.         }
  180.        
  181.         else if (playerOne.getSelectedItem() == players[1]){
  182.             firstPlayer = new DumbRobot();
  183.         }
  184.        
  185.         else if (playerOne.getSelectedItem() == players[2]){
  186.             firstPlayer = new GoodRobot();
  187.         }
  188.        
  189.         if (playerTwo.getSelectedItem() == players[0]){
  190.             secondPlayer = new HumanPlayer();
  191.         }
  192.        
  193.         else if (playerTwo.getSelectedItem() == players[1]){
  194.             secondPlayer = new DumbRobot();
  195.         }
  196.        
  197.         else if (playerTwo.getSelectedItem() == players[2]){
  198.             secondPlayer = new GoodRobot();
  199.         }
  200.         }
  201.        
  202.         protected void makeMoves() {
  203.            
  204.             while(game.getWinner() == ConnectFour.EMPTY) {
  205.                 //output.setText(game.toString());
  206.                 int move = 1;
  207.                 if (currentPlayer instanceof HumanPlayer) {
  208.                     move = 3;
  209.                 } else {
  210.                     move = currentPlayer.makeMove(ConnectFour.COLS, game.getTurn(), board);
  211.                 }
  212.                 game.setMove(game.getTurn(), move);
  213.                 if (game.getTurn() == ConnectFour.PLAYER_X) {
  214.                     currentPlayer = secondPlayer;
  215.                 } else {
  216.                     currentPlayer = firstPlayer;
  217.                 }            
  218.             }
  219.         }
  220.  
  221.    
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement