Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package itaGUI;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JButton;
- import java.awt.Font;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.util.ArrayList;
- import javax.swing.JLabel;
- import java.awt.Color;
- public class TrisPl extends JFrame {
- public static int who = 0;
- public static int win1=0;
- public static int win2=0;
- public static int turn=0;
- public static ArrayList<String> list = new ArrayList<String>();
- public static int whoWin(ArrayList<String> num) {
- if (num.get(0) == "X" && num.get(1) == "X" && num.get(2) == "X") {
- return 1;
- } else if (num.get(3) == "X" && num.get(4) == "X"
- && num.get(5) == "X") {
- return 1;
- } else if (num.get(6) == "X" && num.get(7) == "X"
- && num.get(8) == "X") {
- return 1;
- } else if (num.get(6) == "X" && num.get(3) == "X"
- && num.get(0) == "X") {
- return 1;
- } else if (num.get(7) == "X" && num.get(4) == "X"
- && num.get(1) == "X") {
- return 1;
- } else if (num.get(8) == "X" && num.get(5) == "X"
- && num.get(2) == "X") {
- return 1;
- } else if (num.get(6) == "X" && num.get(4) == "X"
- && num.get(2) == "X") {
- return 1;
- } else if (num.get(8) == "X" && num.get(4) == "X"
- && num.get(0) == "X") {
- return 1;
- } else if (num.get(0)!=" "&&num.get(1)!=" "&&num.get(2)!=" "&&num.get(3)!=" "&&num.get(4)!=" "&&num.get(5)!=" "&&num.get(6)!=" "&&num.get(7)!=" "&&num.get(8)!=" ") {
- return 3;
- } else if (num.get(0) == "O" && num.get(1) == "O"
- && num.get(2) == "O") {
- return 2;
- } else if (num.get(3) == "O" && num.get(4) == "O"
- && num.get(5) == "O") {
- return 2;
- } else if (num.get(6) == "O" && num.get(7) == "O"
- && num.get(8) == "O") {
- return 2;
- } else if (num.get(6) == "O" && num.get(3) == "O"
- && num.get(0) == "O") {
- return 2;
- } else if (num.get(7) == "O" && num.get(4) == "O"
- && num.get(1) == "O") {
- return 2;
- } else if (num.get(8) == "O" && num.get(5) == "O"
- && num.get(2) == "O") {
- return 2;
- } else if (num.get(6) == "O" && num.get(4) == "O"
- && num.get(2) == "O") {
- return 2;
- } else if (num.get(8) == "O" && num.get(4) == "O"
- && num.get(0) == "O") {
- return 2;
- } else if (num.get(0)!=" "&&num.get(1)!=" "&&num.get(2)!=" "&&num.get(3)!=" "&&num.get(4)!=" "&&num.get(5)!=" "&&num.get(6)!=" "&&num.get(7)!=" "&&num.get(8)!=" ") {
- return 3;
- } else {
- return 0;
- }
- }
- private static final long serialVersionUID = 6584630614505902562L;
- private JPanel contentPane;
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- TrisPl frame = new TrisPl();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- public TrisPl() {
- setForeground(Color.BLUE);
- setBackground(Color.BLACK);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 350, 300);
- contentPane = new JPanel();
- contentPane.setForeground(Color.BLUE);
- contentPane.setBackground(Color.BLACK);
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- JLabel label_1 = new JLabel("");
- JButton btnRincomincia = new JButton("Rincomincia");
- btnRincomincia.setForeground(Color.BLUE);
- btnRincomincia.setBackground(Color.BLACK);
- JButton pos1 = new JButton("");
- pos1.setForeground(Color.BLUE);
- pos1.setBackground(Color.BLACK);
- JButton pos2 = new JButton("");
- pos2.setForeground(Color.BLUE);
- pos2.setBackground(Color.BLACK);
- JButton pos3 = new JButton("");
- pos3.setForeground(Color.BLUE);
- pos3.setBackground(Color.BLACK);
- JButton pos4 = new JButton("");
- pos4.setForeground(Color.BLUE);
- pos4.setBackground(Color.BLACK);
- JButton pos5 = new JButton("");
- pos5.setForeground(Color.BLUE);
- pos5.setBackground(Color.BLACK);
- JButton pos6 = new JButton("");
- pos6.setForeground(Color.BLUE);
- pos6.setBackground(Color.BLACK);
- JButton pos7 = new JButton("");
- pos7.setForeground(Color.BLUE);
- pos7.setBackground(Color.BLACK);
- JButton pos8 = new JButton("");
- pos8.setForeground(Color.BLUE);
- pos8.setBackground(Color.BLACK);
- JButton pos9 = new JButton("");
- pos9.setForeground(Color.BLUE);
- pos9.setBackground(Color.BLACK);
- JLabel label = new JLabel("");
- label.setForeground(Color.BLUE);
- label.setBackground(Color.BLACK);
- pos1.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent arg0) {
- if(pos1.isEnabled()){
- if (list.size() != 9) {
- for (int a = 0; a < 10; a++) {
- list.add(" ");
- }
- }
- turn++;
- if(turn%2==1){label_1.setText("Tocca al giocatore 2");pos1.setText("X");list.set(0, "X");}
- if(turn%2==0){label_1.setText("Tocca al giocatore 1");pos1.setText("O");list.set(0, "O");}
- who=whoWin(list);
- if(who!=0){pos1.setEnabled(false);pos2.setEnabled(false);pos3.setEnabled(false);pos4.setEnabled(false);pos5.setEnabled(false);pos6.setEnabled(false);pos7.setEnabled(false);pos8.setEnabled(false);pos9.setEnabled(false);btnRincomincia.setVisible(true);}
- if(who==1){win1++;label.setText("Vince il giocatore 1");}
- if(who==2){win2++;label.setText("Vince il giocatore 2");}
- }
- }
- });
- pos1.setFont(new Font("Tahoma", Font.PLAIN, 20));
- pos1.setBounds(10, 11, 48, 48);
- contentPane.add(pos1);
- pos2.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- if(pos2.isEnabled()){
- if (list.size() != 9) {
- for (int a = 0; a < 10; a++) {
- list.add(" ");
- }
- }
- turn++;
- if(turn%2==1){label_1.setText("Tocca al giocatore 2");pos2.setText("X");list.set(1, "X");}
- if(turn%2==0){label_1.setText("Tocca al giocatore 1");pos2.setText("O");list.set(1, "O");}
- who=whoWin(list);
- if(who!=0){pos2.setEnabled(false);pos2.setEnabled(false);pos3.setEnabled(false);pos4.setEnabled(false);pos5.setEnabled(false);pos6.setEnabled(false);pos7.setEnabled(false);pos8.setEnabled(false);pos9.setEnabled(false);btnRincomincia.setVisible(true);}
- if(who==1){win1++;label.setText("Vince il giocatore 1");}
- if(who==2){win2++;label.setText("Vince il giocatore 2");}
- }
- }
- });
- pos2.setFont(new Font("Tahoma", Font.PLAIN, 20));
- pos2.setBounds(68, 11, 48, 48);
- contentPane.add(pos2);
- pos3.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- if(pos3.isEnabled()){
- if (list.size() != 9) {
- for (int a = 0; a < 10; a++) {
- list.add(" ");
- }
- }
- turn++;
- if(turn%2==1){label_1.setText("Tocca al giocatore 2");pos3.setText("X");list.set(2, "X");}
- if(turn%2==0){label_1.setText("Tocca al giocatore 1");pos3.setText("O");list.set(2, "O");}
- who=whoWin(list);
- if(who!=0){pos3.setEnabled(false);pos2.setEnabled(false);pos3.setEnabled(false);pos4.setEnabled(false);pos5.setEnabled(false);pos6.setEnabled(false);pos7.setEnabled(false);pos8.setEnabled(false);pos9.setEnabled(false);btnRincomincia.setVisible(true);}
- if(who==1){win1++;label.setText("Vince il giocatore 1");}
- if(who==2){win2++;label.setText("Vince il giocatore 2");}
- }
- }
- });
- pos3.setFont(new Font("Tahoma", Font.PLAIN, 20));
- pos3.setBounds(126, 11, 48, 48);
- contentPane.add(pos3);
- pos4.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- if(pos4.isEnabled()){
- if (list.size() != 9) {
- for (int a = 0; a < 10; a++) {
- list.add(" ");
- }
- }
- turn++;
- if(turn%2==1){label_1.setText("Tocca al giocatore 2");pos4.setText("X");list.set(3, "X");}
- if(turn%2==0){label_1.setText("Tocca al giocatore 1");pos4.setText("O");list.set(3, "O");}
- who=whoWin(list);
- if(who!=0){pos4.setEnabled(false);pos2.setEnabled(false);pos3.setEnabled(false);pos4.setEnabled(false);pos5.setEnabled(false);pos6.setEnabled(false);pos7.setEnabled(false);pos8.setEnabled(false);pos9.setEnabled(false);btnRincomincia.setVisible(true);}
- if(who==1){win1++;label.setText("Vince il giocatore 1");}
- if(who==2){win2++;label.setText("Vince il giocatore 2");}
- }
- }
- });
- pos4.setFont(new Font("Tahoma", Font.PLAIN, 20));
- pos4.setBounds(10, 70, 48, 48);
- contentPane.add(pos4);
- pos5.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- if(pos5.isEnabled()){
- if (list.size() != 9) {
- for (int a = 0; a < 10; a++) {
- list.add(" ");
- }
- }
- turn++;
- if(turn%2==1){label_1.setText("Tocca al giocatore 2");pos5.setText("X");list.set(4, "X");}
- if(turn%2==0){label_1.setText("Tocca al giocatore 1");pos5.setText("O");list.set(4, "O");}
- who=whoWin(list);
- if(who!=0){pos5.setEnabled(false);pos2.setEnabled(false);pos3.setEnabled(false);pos4.setEnabled(false);pos5.setEnabled(false);pos6.setEnabled(false);pos7.setEnabled(false);pos8.setEnabled(false);pos9.setEnabled(false);btnRincomincia.setVisible(true);}
- if(who==1){win1++;label.setText("Vince il giocatore 1");}
- if(who==2){win2++;label.setText("Vince il giocatore 2");}
- }
- }
- });
- pos5.setFont(new Font("Tahoma", Font.PLAIN, 20));
- pos5.setBounds(126, 70, 48, 48);
- contentPane.add(pos5);
- pos6.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent arg0) {
- if(pos6.isEnabled()){
- if (list.size() != 9) {
- for (int a = 0; a < 10; a++) {
- list.add(" ");
- }
- }
- turn++;
- if(turn%2==1){label_1.setText("Tocca al giocatore 2");pos6.setText("X");list.set(5, "X");}
- if(turn%2==0){label_1.setText("Tocca al giocatore 1");pos6.setText("O");list.set(5, "O");}
- who=whoWin(list);
- if(who!=0){pos6.setEnabled(false);pos2.setEnabled(false);pos3.setEnabled(false);pos4.setEnabled(false);pos5.setEnabled(false);pos6.setEnabled(false);pos7.setEnabled(false);pos8.setEnabled(false);pos9.setEnabled(false);btnRincomincia.setVisible(true);}
- if(who==1){win1++;label.setText("Vince il giocatore 1");}
- if(who==2){win2++;label.setText("Vince il giocatore 2");}
- }
- }
- });
- pos6.setFont(new Font("Tahoma", Font.PLAIN, 20));
- pos6.setBounds(126, 70, 48, 48);
- contentPane.add(pos6);
- pos7.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- if(pos7.isEnabled()){
- if (list.size() != 9) {
- for (int a = 0; a < 10; a++) {
- list.add(" ");
- }
- }
- turn++;
- if(turn%2==1){label_1.setText("Tocca al giocatore 2");pos7.setText("X");list.set(6, "X");}
- if(turn%2==0){label_1.setText("Tocca al giocatore 1");pos7.setText("O");list.set(6, "O");}
- who=whoWin(list);
- if(who!=0){pos7.setEnabled(false);pos2.setEnabled(false);pos3.setEnabled(false);pos4.setEnabled(false);pos5.setEnabled(false);pos6.setEnabled(false);pos7.setEnabled(false);pos8.setEnabled(false);pos9.setEnabled(false);btnRincomincia.setVisible(true);}
- if(who==1){win1++;label.setText("Vince il giocatore 1");}
- if(who==2){win2++;label.setText("Vince il giocatore 2");}
- }
- }
- });
- pos7.setFont(new Font("Tahoma", Font.PLAIN, 20));
- pos7.setBounds(10, 129, 48, 48);
- contentPane.add(pos7);
- pos8.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- if(pos8.isEnabled()){
- if (list.size() != 9) {
- for (int a = 0; a < 10; a++) {
- list.add(" ");
- }
- }
- turn++;
- if(turn%2==1){label_1.setText("Tocca al giocatore 2");pos8.setText("X");list.set(7, "X");}
- if(turn%2==0){label_1.setText("Tocca al giocatore 1");pos8.setText("O");list.set(7, "O");}
- who=whoWin(list);
- if(who!=0){pos8.setEnabled(false);pos2.setEnabled(false);pos3.setEnabled(false);pos4.setEnabled(false);pos5.setEnabled(false);pos6.setEnabled(false);pos7.setEnabled(false);pos8.setEnabled(false);pos9.setEnabled(false);btnRincomincia.setVisible(true);}
- if(who==1){win1++;label.setText("Vince il giocatore 1");}
- if(who==2){win2++;label.setText("Vince il giocatore 2");}
- }
- }
- });
- pos8.setFont(new Font("Tahoma", Font.PLAIN, 20));
- pos8.setBounds(68, 129, 48, 48);
- contentPane.add(pos8);
- pos9.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- if(pos9.isEnabled()){
- if (list.size() != 9) {
- for (int a = 0; a < 10; a++) {
- list.add(" ");
- }
- }
- turn++;
- if(turn%2==1){label_1.setText("Tocca al giocatore 2");pos9.setText("X");list.set(8, "X");}
- if(turn%2==0){label_1.setText("Tocca al giocatore 1");pos9.setText("O");list.set(8, "O");}
- who=whoWin(list);
- if(who!=0){pos9.setEnabled(false);pos2.setEnabled(false);pos3.setEnabled(false);pos4.setEnabled(false);pos5.setEnabled(false);pos6.setEnabled(false);pos7.setEnabled(false);pos8.setEnabled(false);pos9.setEnabled(false);btnRincomincia.setVisible(true);}
- if(who==1){win1++;label.setText("Vince il giocatore 1");}
- if(who==2){win2++;label.setText("Vince il giocatore 2");}
- }
- }
- });
- pos9.setFont(new Font("Tahoma", Font.PLAIN, 20));
- pos9.setBounds(126, 129, 48, 48);
- contentPane.add(pos9);
- label.setBounds(10, 237, 164, 14);
- contentPane.add(label);
- JLabel lblComputer = new JLabel("Giocatore 2 = 0");
- lblComputer.setForeground(Color.BLUE);
- lblComputer.setBackground(Color.BLACK);
- lblComputer.setBounds(215, 89, 133, 14);
- contentPane.add(lblComputer);
- JLabel lblGiocatore = new JLabel("Giocatore 1 = 0");
- lblGiocatore.setForeground(Color.BLUE);
- lblGiocatore.setBackground(Color.BLACK);
- lblGiocatore.setBounds(215, 28, 133, 14);
- contentPane.add(lblGiocatore);
- btnRincomincia.setVisible(false);
- btnRincomincia.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent arg0) {
- btnRincomincia.setVisible(false);
- list.set(0, " ");
- list.set(1, " ");
- list.set(2, " ");
- list.set(3, " ");
- list.set(4, " ");
- list.set(5, " ");
- list.set(6, " ");
- list.set(7, " ");
- list.set(8, " ");
- list.set(9, " ");
- pos1.setText("");
- pos2.setText("");
- pos3.setText("");
- pos4.setText("");
- pos5.setText("");
- pos6.setText("");
- pos7.setText("");
- pos8.setText("");
- pos9.setText("");
- pos1.setEnabled(true);
- pos2.setEnabled(true);
- pos3.setEnabled(true);
- pos4.setEnabled(true);
- pos5.setEnabled(true);
- pos6.setEnabled(true);
- pos7.setEnabled(true);
- pos8.setEnabled(true);
- pos9.setEnabled(true);
- label.setText("");
- lblGiocatore.setText("Giocatore 1 = "+Integer.toString(win1));
- lblComputer.setText("Giocatore 2 = "+Integer.toString(win2));
- turn=0;
- }
- });
- btnRincomincia.setBounds(20, 188, 140, 23);
- contentPane.add(btnRincomincia);
- pos5.setForeground(Color.BLUE);
- pos5.setFont(new Font("Tahoma", Font.PLAIN, 20));
- pos5.setBackground(Color.BLACK);
- pos5.setBounds(68, 70, 48, 48);
- contentPane.add(pos5);
- label_1.setForeground(Color.BLUE);
- label_1.setBackground(Color.BLACK);
- label_1.setBounds(184, 147, 164, 14);
- contentPane.add(label_1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement