From 7a25ed35eddf68d19e6b516a6cc14dc90b4a6b26 Mon Sep 17 00:00:00 2001 From: toussain Date: Fri, 24 Feb 2023 19:30:54 +0100 Subject: [PATCH] ameliorations: code + options --- PJ1_Morpion/Morpion.java | 261 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 261 insertions(+) create mode 100644 PJ1_Morpion/Morpion.java diff --git a/PJ1_Morpion/Morpion.java b/PJ1_Morpion/Morpion.java new file mode 100644 index 0000000..ed03438 --- /dev/null +++ b/PJ1_Morpion/Morpion.java @@ -0,0 +1,261 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +public class Morpion extends JFrame +{ + private JButton box1, box2, box3, box4, box5, box6, box7, box8, box9, replay; + private int size = 250, player = 0, i, csJouee[] = { 0,0,0,0,0,0,0,0,0 }; + private Color coulWin = new Color(250,164,1), coulBase = new Color(0,0,0); // orange, noir + + public static void main(String[] args) { + new Morpion(); + } + + public Morpion() + { + Color coul1 = new Color(206,206,206); // Argent + Color coul2 = new Color(76,166,107); // vert clair + + // on configure la fenetre + this.setSize(760, 835); + this.setLocation(500, 150); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + Dimension minSize = new Dimension(300, 150); + this.setMinimumSize(minSize); + this.setLayout(null); + + Clicklistener click = new Clicklistener(); + + box1 = new JButton(""); + box2 = new JButton(""); + box3 = new JButton(""); + box4 = new JButton(""); + box5 = new JButton(""); + box6 = new JButton(""); + box7 = new JButton(""); + box8 = new JButton(""); + box9 = new JButton(""); + replay = new JButton("Restart"); + box1.setFont(new Font("Montserrat", Font.BOLD, 100)); + box2.setFont(new Font("Montserrat", Font.BOLD, 100)); + box3.setFont(new Font("Montserrat", Font.BOLD, 100)); + box4.setFont(new Font("Montserrat", Font.BOLD, 100)); + box5.setFont(new Font("Montserrat", Font.BOLD, 100)); + box6.setFont(new Font("Montserrat", Font.BOLD, 100)); + box7.setFont(new Font("Montserrat", Font.BOLD, 100)); + box8.setFont(new Font("Montserrat", Font.BOLD, 100)); + box9.setFont(new Font("Montserrat", Font.BOLD, 100)); + replay.setFont(new Font("Montserrat", Font.BOLD, 20)); + box1.setBackground(coul2); + box2.setBackground(coul1); + box3.setBackground(coul2); + box4.setBackground(coul1); + box5.setBackground(coul2); + box6.setBackground(coul1); + box7.setBackground(coul2); + box8.setBackground(coul1); + box9.setBackground(coul2); + box1.setSize(size,size); + box2.setSize(size,size); + box2.setLocation(size,0); + box3.setSize(size,size); + box3.setLocation(size*2,0); + box4.setSize(size,size); + box4.setLocation(0,size); + box5.setSize(size,size); + box5.setLocation(size,size); + box6.setSize(size,size); + box6.setLocation(size*2,size); + box7.setSize(size,size); + box7.setLocation(0,size*2); + box8.setSize(size,size); + box8.setLocation(size,size*2); + box9.setSize(size,size); + box9.setLocation(size*2,size*2); + replay.setSize(140,30); + replay.setLocation(size*3-150,size*3+10); + box1.addActionListener(click); + box2.addActionListener(click); + box3.addActionListener(click); + box4.addActionListener(click); + box5.addActionListener(click); + box6.addActionListener(click); + box7.addActionListener(click); + box8.addActionListener(click); + box9.addActionListener(click); + replay.addActionListener(click); + this.add(box1); + this.add(box2); + this.add(box3); + this.add(box4); + this.add(box5); + this.add(box6); + this.add(box7); + this.add(box8); + this.add(box9); + this.add(replay); + + this.setVisible(true); + } + + private class Clicklistener implements ActionListener + { + public void actionPerformed(ActionEvent e) + { + if (e.getSource() == box1 && csJouee[0] == 0) + { + if (player%2 == 0) { + box1.setText("X"); + csJouee[0] = 1; + } else { box1.setText("O"); csJouee[0] = 2; } + player++; + } + + if (e.getSource() == box2 && csJouee[1] == 0) + { + if (player%2 == 0) { + box2.setText("X"); + csJouee[1] = 1; + } else { box2.setText("O"); csJouee[1] = 2; } + player++; + } + + if (e.getSource() == box3 && csJouee[2] == 0) + { + if (player%2 == 0) { + box3.setText("X"); + csJouee[2] = 1; + } else { box3.setText("O"); csJouee[2] = 2; } + player++; + } + + if (e.getSource() == box4 && csJouee[3] == 0) + { + if (player%2 == 0) { + box4.setText("X"); + csJouee[3] = 1; + } else { box4.setText("O"); csJouee[3] = 2; } + player++; + } + + if (e.getSource() == box5 && csJouee[4] == 0) + { + if (player%2 == 0) { + box5.setText("X"); + csJouee[4] = 1; + } else { box5.setText("O"); csJouee[4] = 2; } + player++; + } + + if (e.getSource() == box6 && csJouee[5] == 0) + { + if (player%2 == 0) { + box6.setText("X"); + csJouee[5] = 1; + } else { box6.setText("O"); csJouee[5] = 2; } + player++; + } + + if (e.getSource() == box7 && csJouee[6] == 0) + { + if (player%2 == 0) { + box7.setText("X"); + csJouee[6] = 1; + } else { box7.setText("O"); csJouee[6] = 2; } + player++; + } + + if (e.getSource() == box8 && csJouee[7] == 0) + { + if (player%2 == 0) { + box8.setText("X"); + csJouee[7] = 1; + } else { box8.setText("O"); csJouee[7] = 2; } + player++; + } + + if (e.getSource() == box9 && csJouee[8] == 0) + { + if (player%2 == 0) { + box9.setText("X"); + csJouee[8] = 1; + } else { box9.setText("O"); csJouee[8] = 2; } + player++; + } + + if (e.getSource() == replay) + { + player = 0; + for (i=0;i