Don't really have time to help but heres my version i made my 1st year with java.
Code:/** * ConnectFour [Version 1.50] * @author Brian SonniE * @Source: FluidCoding.com * @version 1.50 2009/9/12 */ import javax.swing.*; import java.awt.*; import java.io.*; import javax.imageio.*; import java.awt.event.*; import java.net.*; import java.awt.image.*; import javax.swing.ImageIcon; public class ConnectFour extends JFrame{ private JPanel mainPane; private JButton[] slots; private JButton[] selector; // Set Images ImageIcon emptySpace = new ImageIcon(ConnectFour.class.getResource("images/whitecon.png")); ImageIcon blackSpace = new ImageIcon(ConnectFour.class.getResource("images/blackcon.png")); ImageIcon redSpace = new ImageIcon(ConnectFour.class.getResource("images/redcon.png")); ImageIcon selectIcon = new ImageIcon(ConnectFour.class.getResource("images/selectcon.png")); int[] board; int turn = 1; int tieCounter = 0; boolean gameRunning = true; /** *Constructor */ public ConnectFour() { setTitle("SonniE's ConnectFour"); setVisible(true); setSize(600,550); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buildMainPane(); } /** * Setup Gui */ public void buildMainPane() { mainPane = new JPanel(new GridLayout(7,6)); mainPane.setBackground(Color.black); board = new int[42]; slots = new JButton[42]; selector = new JButton[7]; // Init Buttons/Board for(int ct=0; ct< 7; ct++) { selector[ct] = new JButton(selectIcon); selector[ct].addActionListener(new SelectListener()); mainPane.add(selector[ct]); } for(int ct=0; ct<42; ct++) { board[ct] = 0; slots[ct] = new JButton(emptySpace); mainPane.add(slots[ct]); } add(mainPane); } private class SelectListener implements ActionListener { public void actionPerformed(ActionEvent e) { boolean validMove = true; int activeColumn = 0; int activePiece = 0; ImageIcon activeIcon = redSpace; if(turn == 1) { activeIcon = blackSpace; } Object obj = e.getSource(); // Get Column for(int i=0; i<7; i++) { if(obj == selector[i]) activeColumn = i; } // Get Row activePiece = 5*7+activeColumn; for(int i = 0; i<6; i++) { if(board[activePiece] == 0) { board[activePiece] = turn; break; } else { activePiece-=7; } } // Check if Valid if(activePiece<0) validMove=false; else{ slots[activePiece].setIcon(activeIcon); // Check 4 Win // Check Horizontal Win for(int ct=0; ct<39; ct++) { if(ct%7<5) { if(board[ct] != 0 && board[ct] == board[ct+1] && board[ct] == board[ct+2] && board[ct] == board[ct+3]) { gameRunning = false; } } } // Check Vertical Win for(int ct=0; ct<22; ct++) { if(board[ct] != 0 && board[ct] == board[ct+7] && board[ct] == board[ct+14] && board[ct] == board[ct+21]) { gameRunning = false; } } // Check Diagnol Win // L to R for (int ct = 0; ct < 18; ct++) { if (ct % 7 < 4) { if(board[ct] != 0 && board[ct] == board[ct+8] && board[ct] == board[ct+16] && board[ct] == board[ct+24]) { gameRunning = false; } } } for(int ct=0; ct<24; ct++) { if(ct % 7 > 2) { if(board[ct] != 0 && board[ct] == board[ct+6] && board[ct] == board[ct+12] && board[ct] == board[ct+18]) { gameRunning = false; } } } // Display Winner Message if(!gameRunning) { if(turn==1) JOptionPane.showMessageDialog(null, "Player 1 Wins!!!"); else JOptionPane.showMessageDialog(null, "Player 2 Wins!!!"); for(int i = 0; i<7; i++) selector[i].setEnabled(false); } tieCounter++; if(tieCounter==42) { JOptionPane.showMessageDialog(null, "Tie You Both Lose!!!"); gameRunning = false; for(int i = 0; i<7; i++) selector[i].setEnabled(false); } // Update Player Turn if(turn==1) turn = 2; else turn = 1; if(!gameRunning) { int n = JOptionPane.showConfirmDialog(null, "Would You Like a Rematch", "New Game?", JOptionPane.YES_NO_OPTION); if(n == 0) restartGame(); else System.exit(0); } } } } public void restartGame() { for(int i = 0; i<42; i++) { slots[i].setIcon(emptySpace); board[i] = 0; turn = 1; tieCounter = 0; gameRunning = true; } for(int i =0; i<7; i++) selector[i].setEnabled(true); } public static void main(String[] args) { ConnectFour window = new ConnectFour(); } }




Reply With Quote