Projet_Java_Perso/PJ1_Morpion/Morpion.java
2023-02-24 19:30:54 +01:00

261 lines
7.5 KiB
Java

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<csJouee.length;i++) {
csJouee[i] = 0;
}
box1.setText("");
box2.setText("");
box3.setText("");
box4.setText("");
box5.setText("");
box6.setText("");
box7.setText("");
box8.setText("");
box9.setText("");
box1.setForeground(coulBase);
box2.setForeground(coulBase);
box3.setForeground(coulBase);
box4.setForeground(coulBase);
box5.setForeground(coulBase);
box6.setForeground(coulBase);
box7.setForeground(coulBase);
box8.setForeground(coulBase);
box9.setForeground(coulBase);
}
if (csJouee[0]!=0 && csJouee[0]==csJouee[1] && csJouee[0]==csJouee[2]) {
box1.setForeground(coulWin);
box2.setForeground(coulWin);
box3.setForeground(coulWin);
}
if (csJouee[3]!=0 && csJouee[3]==csJouee[4] && csJouee[3]==csJouee[5]) {
box4.setForeground(coulWin);
box5.setForeground(coulWin);
box6.setForeground(coulWin);
}
if (csJouee[6]!=0 && csJouee[6]==csJouee[7] && csJouee[6]==csJouee[8]) {
box7.setForeground(coulWin);
box8.setForeground(coulWin);
box9.setForeground(coulWin);
}
if (csJouee[0]!=0 && csJouee[0]==csJouee[3] && csJouee[0]==csJouee[6]) {
box1.setForeground(coulWin);
box4.setForeground(coulWin);
box7.setForeground(coulWin);
}
if (csJouee[1]!=0 && csJouee[1]==csJouee[4] && csJouee[1]==csJouee[7]) {
box2.setForeground(coulWin);
box5.setForeground(coulWin);
box8.setForeground(coulWin);
}
if (csJouee[2]!=0 && csJouee[2]==csJouee[5] && csJouee[2]==csJouee[8]) {
box3.setForeground(coulWin);
box6.setForeground(coulWin);
box9.setForeground(coulWin);
}
if (csJouee[0]!=0 && csJouee[0]==csJouee[4] && csJouee[0]==csJouee[8]) {
box1.setForeground(coulWin);
box5.setForeground(coulWin);
box9.setForeground(coulWin);
}
if (csJouee[2]!=0 && csJouee[2]==csJouee[4] && csJouee[2]==csJouee[6]) {
box3.setForeground(coulWin);
box5.setForeground(coulWin);
box7.setForeground(coulWin);
}
}
}
}