Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JOptionPane;
- import javax.swing.JTextArea;
- import javax.swing.JButton;
- import javax.swing.JTextField;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.awt.Font;
- import java.util.Scanner;
- import javax.swing.JLabel;
- import javax.swing.JComboBox;
- public class C4GUI extends JFrame {
- /**
- *
- */
- private JPanel contentPane;
- private JTextField input;
- private JTextArea output;
- private ConnectFour game;
- private char[][] board;
- private JPanel panel_1;
- private JLabel lblNewLabel;
- private JButton newGame;
- private JComboBox playerOne;
- private JLabel lblNewLabel_1;
- private JComboBox playerTwo;
- private String[] players;
- private Player currentPlayer;
- private Player firstPlayer;
- private Player secondPlayer;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- C4GUI frame = new C4GUI();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public C4GUI() {
- setTitle("Connect Four");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 500, 370);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- contentPane.setLayout(new BorderLayout(0, 0));
- setContentPane(contentPane);
- output = new JTextArea();
- output.setFont(new Font("Monospaced", Font.BOLD, 18));
- output.setEditable(false);
- contentPane.add(output, BorderLayout.CENTER);
- JPanel panel = new JPanel();
- contentPane.add(panel, BorderLayout.SOUTH);
- newGame = new JButton("New Game");
- newGame.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- newGameButtonClicked();
- }
- });
- panel.add(newGame);
- input = new JTextField();
- input.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- inputEntered();
- }
- });
- panel.add(input);
- input.setColumns(20);
- panel_1 = new JPanel();
- contentPane.add(panel_1, BorderLayout.NORTH);
- lblNewLabel = new JLabel("Player X:");
- panel_1.add(lblNewLabel);
- players = new String[] {"Human", "Dumb Robot", "Good Robot"};
- playerOne = new JComboBox(players);
- panel_1.add(playerOne);
- lblNewLabel_1 = new JLabel("Player O:");
- panel_1.add(lblNewLabel_1);
- playerTwo = new JComboBox(players);
- panel_1.add(playerTwo);
- }
- /**
- * Manages entered input
- */
- protected void inputEntered() {
- String line = input.getText();
- input.setText("");
- //if (currentPlayer instanceof HumanPlayer) {
- Scanner sc = new Scanner(line);
- if (!sc.hasNextInt()) {
- //Invalid input
- JOptionPane.showMessageDialog(this, "Invalid input! Please try again.");
- return;
- }
- //} else {
- //}
- int col = sc.nextInt();
- board = new char[ConnectFour.ROWS][ConnectFour.COLS];
- //board = new char[ConnectFour.ROWS][ConnectFour.COLS];
- // for(int j = 0; j <= ConnectFour.COLS; j++){
- // for(int n = 0; n < ConnectFour.ROWS; n++){
- // if(board[j][n] == 0){
- // game.setMove(game.getTurn(), col);
- // game.isGameOver();
- // game.getTurn();
- // output.setText(game.toString());
- // }
- // }
- // }
- // if ((result == false) || (game.findEmptySpace(col) < -1)) { // if (!result)
- // JOptionPane.showMessageDialog(this, "Invalid input! Please try again.");
- // return;
- // }
- output.setText(game.toString());
- }
- /**
- * Begins new game
- */
- protected void newGameButtonClicked() {
- // System.out.println("New Game Button clicked!");
- game = new ConnectFour();
- output.setText(game.toString());
- assignPlayers();
- currentPlayer = firstPlayer;
- //System.out.println(currentPlayer == null);
- makeMoves();
- // while (currentPlayer instanceof HumanPlayer) {
- // inputEntered();
- // }
- // newGame.setEnabled(false);
- }
- /**
- * Assigns players based on JComboBox values
- */
- protected void assignPlayers() {
- if (playerOne.getSelectedItem() == players[0]){
- firstPlayer = new HumanPlayer();
- }
- else if (playerOne.getSelectedItem() == players[1]){
- firstPlayer = new DumbRobot();
- }
- else if (playerOne.getSelectedItem() == players[2]){
- firstPlayer = new GoodRobot();
- }
- if (playerTwo.getSelectedItem() == players[0]){
- secondPlayer = new HumanPlayer();
- }
- else if (playerTwo.getSelectedItem() == players[1]){
- secondPlayer = new DumbRobot();
- }
- else if (playerTwo.getSelectedItem() == players[2]){
- secondPlayer = new GoodRobot();
- }
- }
- protected void makeMoves() {
- while(game.getWinner() == ConnectFour.EMPTY) {
- //output.setText(game.toString());
- int move = 1;
- if (currentPlayer instanceof HumanPlayer) {
- move = 3;
- } else {
- move = currentPlayer.makeMove(ConnectFour.COLS, game.getTurn(), board);
- }
- game.setMove(game.getTurn(), move);
- if (game.getTurn() == ConnectFour.PLAYER_X) {
- currentPlayer = secondPlayer;
- } else {
- currentPlayer = firstPlayer;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement