M2G Posted January 16, 2011 Report Posted January 16, 2011 (edited) Am facut un mic joculet in Java.Jocul are dimensiuni variabile si are un scor.Regulile sunt ca si la jocul clasic doar ca daca si 4 de x intr-o linie se pun 2 puncte la scor. Daca ai 5 de x intr-o linie se pun 3 puncte etc...Cam asa arata: Puteti descarca fisierul jar de aici: FileShare Download X-0.jarCodul sursa:import java.awt.*;import java.awt.event.*;import javax.swing.*;public class GUI extends JFrame { //declaratii variabile, butoane, frame... static final String dimensiune[] = {"3", "5", "7", "9"}; public int dimbuton = 3; JComboBox Dimensiunea; JTextField scorx; JTextField scor0; private int count, xwins, owins = 0; private String letter = ""; private JFrame fereastra = new JFrame("X_0"); public JButton buttons[][]= new JButton[10][10]; JButton okButton = new JButton("OK"); JButton verificaButon = new JButton("Calculeaza Scor"); GridLayout tablaLayout = new GridLayout(); public GUI() { //setare ComboBox Dimensiunea = new JComboBox(dimensiune); final JPanel tabla = new JPanel(); tabla.setLayout(tablaLayout); //setare layout pentru afisarea scorului JPanel scor = new JPanel(); scor.setLayout(new GridLayout(2,1)); scorx= new JTextField(5); scor0= new JTextField(5); //adaugare componente pe layoutul scor scor.add(new Label("Player X:")); scor.add(scorx); scor.add(new Label("Player 0:")); scor.add(scor0); //setare layout controale JPanel control = new JPanel(); control.setLayout(new GridLayout(1,1)); //adaugare controale control.add(new Label("Selectati Dimensiunea:")); control.add(Dimensiunea); control.add(okButton); control.add(verificaButon); //Actiuni asupra butonului "OK" okButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ //setare dimensiuni String dim = (String)Dimensiunea.getSelectedItem(); //stergerea butonelor pentru a putea fi adaugate altele tabla.removeAll(); //initializarea scorului cu 0 pentru ambii jucatori String zerox = Integer.toString(xwins); scorx.setText(zerox); String zeroy = Integer.toString(xwins); scor0.setText(zeroy); dimbuton=Integer.parseInt(dim); //inserare butoane pe tabla in functie de valoarea aleasa din Combo box for(int i=1; i<=dimbuton ; i++){ for (int j=1; j<=dimbuton; j++){ buttons[i][j] = new JButton(); tabla.add(buttons[i][j]); //adaugare "ascultatori" pentru butoanele create buttons[i][j].addActionListener(new ActionListener() { //setare actiuni la apasarea unui buton de pe tabla public void actionPerformed(ActionEvent e) { //setarea ordinii jucatorilor count++; if(count % 2 == 0){ letter = "0"; } else { letter = "X"; } for(int i=1; i<=dimbuton ; i++){ for (int j=1; j<=dimbuton; j++){ if (e.getSource() == buttons[i][j]){ //pentru fiecare buton de pe tabla apasat butonul se seteaza cu textul playerului respectiv si se blocheaza buttons[i][j].setText(letter); buttons[i][j].setEnabled(false); } } } } }); } } //Setare coloane si randuri |revalidare si repaint| tablaLayout.setColumns(dimbuton); tablaLayout.setRows(dimbuton); tabla.revalidate(); tabla.repaint(); //setare layout tablaLayout.layoutContainer(tabla); } }); //actiuni asupra butonului care verifica scorul verificaButon.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ //aduce scor la 0 pe urma verifica noul scor xwins=0; owins=0; String zerox = Integer.toString(xwins); scorx.setText(zerox); String zeroy = Integer.toString(xwins); scor0.setText(zeroy); verifiaScor(); } }); //adaugare elemente in fereastra si setare dimensiuni fereastra.add(control, BorderLayout.SOUTH); fereastra.add(tabla, BorderLayout.CENTER); fereastra.add(scor, BorderLayout.NORTH); fereastra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); fereastra.setBounds(400, 20, 700, 700); fereastra.setVisible(true); setResizable(true); fereastra.getContentPane(); }//metoda care verifica combinatiile castigatoare public void verifiaScor(){ //verificare pe orizontala for (int i=1;i<=dimbuton;i++){ for(int j=1;j<=dimbuton-2;j++){ //verificare pe orizontala pentru "X" if( buttons[i][j].getText().equals("X") && buttons[i][j+1].getText().equals("X") && buttons[i][j+2].getText().equals("X")) { xwins++; String x = Integer.toString(xwins); scorx.setText(x); } //verificare pe orizontala pentru "0" else if (buttons[i][j].getText().equals("0") && buttons[i][j + 1].getText().equals("0") && buttons[i][j + 2].getText().equals("0")) { owins++; String o = Integer.toString(owins); scor0.setText(o); } } } //verificare pe verticalafor (int i=1;i<=dimbuton-2;i++){ for(int j=1;j<=dimbuton;j++){ //verificare pe verticala pentru "X" if (buttons[i][j].getText().equals("X") && buttons[i+1][j].getText().equals("X") && buttons[i+2][j].getText().equals("X")) { xwins++; String x = Integer.toString(xwins); scorx.setText(x); } //verificare pe verticala pentru "0" if (buttons[i][j].getText().equals("0") && buttons[i+1][j].getText().equals("0") && buttons[i+2][j].getText().equals("0")) { owins++; String o = Integer.toString(owins); scor0.setText(o); } } } //verificare diagonala principala for (int i=1;i<=dimbuton-2;i++){ for(int j=1;j<=dimbuton-2;j++){ //verificare diagonala pentru "X" if (buttons[i][j].getText().equals("X") && buttons[i+1][j+1].getText().equals("X") && buttons[i+2][j+2].getText().equals("X")) { xwins++; String x = Integer.toString(xwins); scorx.setText(x); } if (buttons[i][j].getText().equals("0") && buttons[i+1][j+1].getText().equals("0") && buttons[i+2][j+2].getText().equals("0")) { owins++; String o = Integer.toString(owins); scor0.setText(o); } } } //verificare diagonala secundara for (int i = 2; i <= dimbuton; i++) { for (int j = 1 ;j <=dimbuton - 2; j++) { if (buttons[i][j].getText().equals("X") && buttons[i-1][j+1].getText().equals("X") && buttons[i-2][j+2].getText().equals("X")) { xwins++; String x = Integer.toString(xwins); scorx.setText(x); } if (buttons[i][j].getText().equals("0") && buttons[i-1][j+1].getText().equals("0") && buttons[i-2][j+2].getText().equals("0")) { owins++; String o = Integer.toString(owins); scor0.setText(o); } } } }}Si main-ul:public class Principala{ public static void main(String[] args) {GUI starter = new GUI(); }}Have fun! Edited January 16, 2011 by M2G Quote
M2G Posted January 16, 2011 Author Report Posted January 16, 2011 (edited) E facut in NetBeans dar poti sa folosesti ce vrei...//LEOh da..presupun ca te referi la IDE... Edited January 26, 2011 by M2G Quote