Nesne Tabanlı Programlama-2 (Bahar-2016) Hafta-4/2

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class xox extends JFrame implements ActionListener {
	
	JButton[] jbtn= new JButton[9];
	String sira="X";
	int skor_x,skor_o,sayac;
	JLabel jlab_siradaki,jlab_skor;
	public xox(){
		
		this.setTitle("X-O-X");
		this.setSize(185,250);
		this.setLayout(new FlowLayout());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel jpn_main = new JPanel();
		jpn_main.setLayout(new FlowLayout());
		jpn_main.setPreferredSize(new Dimension(165, 210));
		
		JPanel jpn1 = new JPanel();
		jpn1.setLayout(new GridLayout(3, 3));
		jpn1.setPreferredSize(new Dimension(165, 165));
		
		for(int i=0;i<jbtn.length;i++)
		{
			jbtn[i]=new JButton("?");
			jbtn[i].setPreferredSize(new Dimension(50, 50));
			
			JPanel jpn = new JPanel();
			jpn.add(jbtn[i]);
			
			jpn1.add(jpn);
			
			jbtn[i].addActionListener(this);
			jbtn[i].setActionCommand(i+"");
		}
		
		JPanel jpn2 = new JPanel();
		jpn2.setLayout(new GridLayout(1, 2));
		jpn2.setPreferredSize(new Dimension(165, 50));
		
		jlab_siradaki = new JLabel("Sıradaki: X");
		jlab_skor = new JLabel("X:0 O:0");
		
		jpn2.add(jlab_siradaki);
		jpn2.add(new JPanel().add(jlab_skor));
		
		jpn_main.add(jpn1);
		jpn_main.add(jpn2);
		
		this.add(jpn_main);
		this.setVisible(true);
	}
	

	private void siradaki()
	{
		if(sira.equals("X"))
			sira="O";
		else
			sira="X";
		// label siradakini yaz
		jlab_siradaki.setText("Sıradaki: "+sira);
	}
	
	
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		
		JButton jbtn = (JButton) arg0.getSource();
		jbtn.setText(sira);
		jbtn.setEnabled(false);
		
		if(++sayac>4){
		int durum = kontrol();
		
						
			if(durum>0)
				temizle();
			
			if(durum==1){
				JOptionPane.showMessageDialog(null, sira +" kullanıcısı oyunu kazandı");
				return;
				}
				
			if(durum==2)
				JOptionPane.showMessageDialog(null, "Oyun Berabere");		
		
		}
		siradaki();
		
	}
	
	private void temizle()
	{
		
		for(int i=0;i<jbtn.length;i++)
		{
			jbtn[i].setText("?");
			jbtn[i].setEnabled(true);
		}
		
		sayac=0;
		
	}
	
	private void skoryaz()
	{
		if(sira.equals("X"))
			skor_x++;
		else
			skor_o++;
		
		jlab_skor.setText("X: "+skor_x +" O:"+skor_o);
	}
	
	private int kontrol()
	{
		int say=0;
		for(int i=0;i<3;i++)
		{
			// satir
			if(!jbtn[3*i].getText().equals("?") && 
				jbtn[3*i].getText().equals( jbtn[3*i+1].getText()) &&
				jbtn[3*i].getText().equals( jbtn[3*i+2].getText()))
					say++;
			
			// satir
			if(!jbtn[i].getText().equals("?") && 
				jbtn[i].getText().equals( jbtn[i+3].getText()) &&
				jbtn[i].getText().equals( jbtn[i+6].getText()))
					say++;
			
			// capraz
			if(i==0)
			{
				// sol->sag
				if(!jbtn[0].getText().equals("?") && 
					jbtn[0].getText().equals( jbtn[4].getText()) &&
					jbtn[0].getText().equals( jbtn[8].getText()))
						say++;
				
				if(!jbtn[2].getText().equals("?") && 
					jbtn[2].getText().equals( jbtn[4].getText()) &&
					jbtn[2].getText().equals( jbtn[6].getText()))
						say++;
				
			}
			
			if(say>0)
				break;
		}
		
		
		if(say>0){
			skoryaz();
			return 1; // kazanan var
		}
		
		if(sayac==9)
			return 2; // berabere 
		
		return 0;
	}
	

	public static void main(String[] args) {
		new xox();

	}

}

 

Share