📄 TicTacToe.java .java dosyası

⬇️ İndir
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class TicTacToe implements ActionListener { Random random = new Random(); JFrame frame = new JFrame("Tic Tac Toe"); JFrame playerFrame = new JFrame("Player Frame"); JLabel baslikLabel = new JLabel(); JPanel baslikPaneli = new JPanel(); JPanel butonPaneli = new JPanel(); JPanel scorePanel = new JPanel(); String birinciOyuncu = ""; String ikinciOyuncu = ""; JButton[] butonlar = new JButton[9]; boolean player1_turn; TicTacToe() { playerframe(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(680,840); frame.getContentPane().setBackground(new Color(50,50,50)); frame.setLocationRelativeTo(null); frame.setLayout(new BorderLayout()); // frame'in layout'u BorderLayout. baslikLabel.setBackground(new Color(242, 75, 89)); baslikLabel.setForeground(new Color(242, 223, 167)); baslikLabel.setFont(new Font("Helvetica", Font.BOLD, 60)); baslikLabel.setHorizontalAlignment(JLabel.CENTER); baslikLabel.setText("Tic-Tac-Toe"); baslikLabel.setOpaque(true); baslikPaneli.setLayout(new BorderLayout()); // baslikPaneli'nin Layout'u BorderLayout. baslikPaneli.add(baslikLabel); scorePanel.setLayout(new BorderLayout()); // scorePanel'de de BorderLayout kullaniyoruz scorePanel.add(score(0,0,birinciOyuncu, ikinciOyuncu)); butonPaneli.setLayout(null); // butonPaneli'nin layout'unu null kullaniyoruz. Onu frame'e ekliyoruz. butonPaneli.setBackground(new Color(200,150,11)); // butonPaneli'nin arka fon rengi. int point = 30; int koordinatX = point; int koordinatY = point; int genislik = 200; int yukseklik = 200; int f = 210; for(int i = 0; i<9; i++) { butonlar[i] = new JButton(); butonlar[i].setBackground(new Color(242, 242, 206)); if (i<3 ) { koordinatX = (f*i)+point; // birinci sira koordinatY = point; } else if (i<=3 || i<6 ) { koordinatX = (f*(i%3))+point; // ikinci sira koordinatY = f+point; } else if (i>=6 ) { koordinatX = (f*(i%3))+point; // üçüncü sira koordinatY = (f*2)+point; } butonlar[i].setBounds(koordinatX, koordinatY, genislik, yukseklik); butonPaneli.add(butonlar[i]); butonlar[i].setFont(new Font("Helvetica", Font.BOLD, 150)); butonlar[i].setFocusable(false); butonlar[i].addActionListener(this); } frame.add(baslikPaneli, BorderLayout.NORTH); frame.add(scorePanel, BorderLayout.SOUTH); ilkSira(); } public void playerframe() { int x = 50; int y = 25; int f = 40; int w = 250; int h = 40; JLabel isimLabel = new JLabel("Oyuncu X : "); isimLabel.setFont(new Font("Helvetica", Font.BOLD, 30)); isimLabel.setBounds(x,y,w,h); y = y + f; JTextField isimField = new JTextField(); isimField.setBounds(x,y,w,h); y = y + f; JLabel isimLabel2 = new JLabel("Oyuncu O : "); isimLabel2.setFont(new Font("Helvetica", Font.BOLD, 30)); isimLabel2.setBounds(x,y,w,h); y = y + f; JTextField isimField2 = new JTextField(); isimField2.setBounds(x,y,w,h); y = y + f; JButton kaydet = new JButton("Basla"); kaydet.setBounds(x+130,y+20,120,40); playerFrame.add(isimLabel); playerFrame.add(isimField); playerFrame.add(isimLabel2); playerFrame.add(isimField2); playerFrame.add(kaydet); kaydet.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ birinciOyuncu = isimField.getText(); ikinciOyuncu = isimField2.getText(); // scorePanel'e score() metodu ekliyoruz. // score() metodu JLabel döndürür unutmayalim!!! scorePanel.add(score(0,0,birinciOyuncu, ikinciOyuncu)); playerFrame.setVisible(false); frame.setVisible(true); } }); playerFrame.setSize(400,300); playerFrame.setLocationRelativeTo(null); playerFrame.setLayout(null); playerFrame.getContentPane().setBackground(new Color(194, 210, 242)); playerFrame.setVisible(true); } public static JLabel score(int scoreX, int scoreO, String oyuncuX, String oyuncuO) { JLabel scoreLabel = new JLabel(oyuncuX + " : " + scoreX + " | " + oyuncuO + " : " + scoreO); scoreLabel.setFont(new Font("Helvetica", Font.PLAIN, 32)); scoreLabel.setForeground(new Color(125,50,75)); scoreLabel.setHorizontalAlignment(JLabel.CENTER); scoreLabel.setOpaque(true); return scoreLabel; } public void ilkSira() { // Oyuna kimin baslayacagina karar verilir. // 2 saniye bekler ve butonlar görünür. try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } frame.add(butonPaneli); // random 0 ve 1 gelir. // 0 gelirse player_1 1 gelirse player_2 oyuna baslar. if(random.nextInt(2) == 0) { player1_turn = true; baslikLabel.setText("X turn"); } else { player1_turn = false; baslikLabel.setText("O turn"); } } public void kontrol() { // üçlü siralar kontrol ediliyor. //X'in kazandığı durumlar //yatay if((butonlar[0].getText() == "X") && (butonlar[1].getText() == "X") && (butonlar[2].getText() == "X")) { xKazanir(0,1,2); } if((butonlar[3].getText() == "X") && (butonlar[4].getText() == "X") && (butonlar[5].getText() == "X")) { xKazanir(3,4,5); } if((butonlar[6].getText() == "X") && (butonlar[7].getText() == "X") && (butonlar[8].getText() == "X")) { xKazanir(6,7,8); } // dikey if((butonlar[0].getText() == "X") && (butonlar[3].getText() == "X") && (butonlar[6].getText() == "X")) { xKazanir(0,3,6); } if((butonlar[1].getText() == "X") && (butonlar[4].getText() == "X") && (butonlar[7].getText() == "X")) { xKazanir(1,4,7); } if((butonlar[2].getText() == "X") && (butonlar[5].getText() == "X") && (butonlar[8].getText() == "X")) { xKazanir(2,5,8); } //çapraz if((butonlar[0].getText() == "X") && (butonlar[4].getText() == "X") && (butonlar[8].getText() == "X")) { xKazanir(0,4,8); } if((butonlar[2].getText() == "X") && (butonlar[4].getText() == "X") && (butonlar[6].getText() == "X")) { xKazanir(2,4,6); } //O'nun kazandığı durumlar //yatay if((butonlar[0].getText() == "O") && (butonlar[1].getText() == "O") && (butonlar[2].getText() == "O")) { oKazanir(0,1,2); } if((butonlar[3].getText() == "O") && (butonlar[4].getText() == "O") && (butonlar[5].getText() == "O")) { oKazanir(3,4,5); } if((butonlar[6].getText() == "O") && (butonlar[7].getText() == "O") && (butonlar[8].getText() == "O")) { oKazanir(6,7,8); } // dikey if((butonlar[0].getText() == "O") && (butonlar[3].getText() == "O") && (butonlar[6].getText() == "O")) { oKazanir(0,3,6); } if((butonlar[1].getText() == "O") && (butonlar[4].getText() == "O") && (butonlar[7].getText() == "O")) { oKazanir(1,4,7); } if((butonlar[2].getText() == "O") && (butonlar[5].getText() == "O") && (butonlar[8].getText() == "O")) { oKazanir(2,5,8); } //çapraz if((butonlar[0].getText() == "O") && (butonlar[4].getText() == "O") && (butonlar[8].getText() == "O")) { oKazanir(0,4,8); } if((butonlar[2].getText() == "O") && (butonlar[4].getText() == "O") && (butonlar[6].getText() == "O")) { oKazanir(2,4,6); } } public void xKazanir(int a, int b, int c) { butonlar[a].setBackground(new Color(15, 242, 49)); butonlar[b].setBackground(new Color(15, 242, 49)); butonlar[c].setBackground(new Color(15, 242, 49)); for(int i = 0; i<9; i++) { butonlar[i].setEnabled(false); } baslikLabel.setText("X WIN"); scorePanel.add(score(1,0,birinciOyuncu, ikinciOyuncu)); } public void oKazanir(int a, int b, int c) { butonlar[a].setBackground(new Color(15, 242, 49)); butonlar[b].setBackground(new Color(15, 242, 49)); butonlar[c].setBackground(new Color(15, 242, 49)); for(int i = 0; i<9; i++) { butonlar[i].setEnabled(false); } baslikLabel.setText("O WIN"); scorePanel.add(score(0,1,birinciOyuncu, ikinciOyuncu)); } public void actionPerformed(ActionEvent e) { for(int i = 0; i<9; i++) { if(e.getSource() == butonlar[i]) { if(player1_turn) { // yukarida boolean tanimlamistik true/false olur palyer1_turn if(butonlar[i].getText() == "") { butonlar[i].setForeground(new Color(173, 204, 153)); butonlar[i].setText("X"); player1_turn = false; baslikLabel.setText("O Turn"); kontrol(); // her butona basildiginda kontrol() metoduna bakilir. } } else { if(butonlar[i].getText() == "") { butonlar[i].setForeground(new Color(242, 170, 97)); butonlar[i].setText("O"); player1_turn = true; baslikLabel.setText("X Turn"); kontrol(); // her butona basildiginda kontrol() metoduna bakilir. } } } } } }