tp1 partie 1 et partie 2.1/2.2
This commit is contained in:
3
TTT_java/.idea/.gitignore
generated
vendored
Normal file
3
TTT_java/.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
6
TTT_java/.idea/misc.xml
generated
Normal file
6
TTT_java/.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
8
TTT_java/.idea/modules.xml
generated
Normal file
8
TTT_java/.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/TTT_java.iml" filepath="$PROJECT_DIR$/TTT_java.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
TTT_java/.idea/vcs.xml
generated
Normal file
6
TTT_java/.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
19
TTT_java/AIPlayer.java
Normal file
19
TTT_java/AIPlayer.java
Normal file
@@ -0,0 +1,19 @@
|
||||
import java.util.Random;
|
||||
|
||||
public class AIPlayer extends Player {
|
||||
private Random random;
|
||||
|
||||
public AIPlayer(char mark) {
|
||||
super(mark);
|
||||
random = new Random();
|
||||
}
|
||||
|
||||
public void makeMove(Board board) {
|
||||
int row, col;
|
||||
do {
|
||||
row = random.nextInt(3);
|
||||
col = random.nextInt(3);
|
||||
} while (!board.isCellEmpty(row, col));
|
||||
board.placeMark(row, col, getMark());
|
||||
}
|
||||
}
|
||||
64
TTT_java/Board.java
Normal file
64
TTT_java/Board.java
Normal file
@@ -0,0 +1,64 @@
|
||||
public class Board {
|
||||
private char[][] grid;
|
||||
private static final int SIZE = 3;
|
||||
|
||||
public Board() {
|
||||
grid = new char[SIZE][SIZE];
|
||||
for (int i = 0; i < SIZE; i++) {
|
||||
for (int j = 0; j < SIZE; j++) {
|
||||
grid[i][j] = '-';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void printBoard() {
|
||||
for (int i = 0; i < SIZE; i++) {
|
||||
for (int j = 0; j < SIZE; j++) { //IL FAUT METTRE I ET J INFERIEUR!!! SINON PAS D'AFFICHAGE!!!
|
||||
System.out.print(grid[i][j] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isCellEmpty(int row, int col) {
|
||||
return grid[row][col] == '-';
|
||||
}
|
||||
|
||||
public void placeMark(int row, int col, char mark) {
|
||||
grid[row][col] = mark;
|
||||
}
|
||||
|
||||
public boolean hasWon(char mark) {
|
||||
// Vérifie les lignes
|
||||
for (int i = 0; i < SIZE; i++) {
|
||||
if (grid[i][0] == mark && grid[i][1] == mark && grid[i][2] == mark) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// Vérifie les colonnes
|
||||
for (int j = 0; j < SIZE; j++) {
|
||||
if (grid[0][j] == mark && grid[1][j] == mark && grid[2][j] == mark) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// Vérifie les diagonales
|
||||
if (grid[0][0] == mark && grid[1][1] == mark && grid[2][2] == mark) {
|
||||
return true;
|
||||
}
|
||||
if (grid[0][2] == mark && grid[1][1] == mark && grid[2][0] == mark) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isFull() {
|
||||
for (int i = 0; i < SIZE; i++) {
|
||||
for (int j = 0; j < SIZE; j++) {
|
||||
if (grid[i][j] == '-') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
60
TTT_java/Game.java
Normal file
60
TTT_java/Game.java
Normal file
@@ -0,0 +1,60 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Game {
|
||||
private Board board;
|
||||
private Player human;
|
||||
private Player ai= new AIPlayer('O');
|
||||
private Scanner scanner;
|
||||
|
||||
public Game() {
|
||||
board = new Board(); // ATTENTION IL FALLAIT INITIALISER LE BOARD!!!! sinon erreur exit code 1
|
||||
human = new Player('X');
|
||||
scanner = new Scanner(System.in);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
System.out.println("Bienvenue dans Tic-Tac-Toe !");
|
||||
board.printBoard();
|
||||
|
||||
while (true) {
|
||||
humanTurn();
|
||||
if (isGameOver(human)) break;
|
||||
|
||||
aiTurn();
|
||||
if (isGameOver(ai)) break;
|
||||
}
|
||||
}
|
||||
|
||||
private void humanTurn() {
|
||||
System.out.println("Votre tour ! Entrez la ligne et la colonne (ex: 2 2 pour le centre) :"); //ATTENTION !! LE MILIEU C'EST 2 2 PAS 1 1 !!
|
||||
int row, col;
|
||||
while (true) {
|
||||
row = scanner.nextInt() - 1;
|
||||
col = scanner.nextInt() - 1;
|
||||
if (board.isCellEmpty(row, col)) {
|
||||
board.placeMark(row, col, human.getMark());
|
||||
board.printBoard();
|
||||
break;
|
||||
} else {
|
||||
System.out.println("Cette case est déjà prise. Essayez à nouveau.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void aiTurn() {
|
||||
System.out.println("Tour de l'IA...");
|
||||
((AIPlayer) ai).makeMove(board);
|
||||
board.printBoard();
|
||||
}
|
||||
|
||||
private boolean isGameOver(Player player) {
|
||||
if (board.hasWon(player.getMark())) {
|
||||
System.out.println("Le joueur " + player.getMark() + " a gagné !"); //PRENDRE LA MARK DU PLAYER!!! SINON AFFICHE TOUJOURS VICTOIRE IA!!
|
||||
return true;
|
||||
} else if (board.isFull()) {
|
||||
System.out.println("Égalité ! La grille est pleine.");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
11
TTT_java/Player.java
Normal file
11
TTT_java/Player.java
Normal file
@@ -0,0 +1,11 @@
|
||||
public class Player {
|
||||
private char mark;
|
||||
|
||||
public Player(char mark) {
|
||||
this.mark = mark;
|
||||
}
|
||||
|
||||
public char getMark() {
|
||||
return mark;
|
||||
}
|
||||
}
|
||||
11
TTT_java/TTT_java.iml
Normal file
11
TTT_java/TTT_java.iml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
6
TTT_java/TicTacToeGame.java
Normal file
6
TTT_java/TicTacToeGame.java
Normal file
@@ -0,0 +1,6 @@
|
||||
public class TicTacToeGame {
|
||||
public static void main(String[] args) {
|
||||
Game game = new Game();
|
||||
game.start();
|
||||
}
|
||||
}
|
||||
3
TTT_java/out/production/TTT_java/.idea/.gitignore
generated
vendored
Normal file
3
TTT_java/out/production/TTT_java/.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
6
TTT_java/out/production/TTT_java/.idea/misc.xml
generated
Normal file
6
TTT_java/out/production/TTT_java/.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
8
TTT_java/out/production/TTT_java/.idea/modules.xml
generated
Normal file
8
TTT_java/out/production/TTT_java/.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/TTT_java.iml" filepath="$PROJECT_DIR$/TTT_java.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
TTT_java/out/production/TTT_java/.idea/vcs.xml
generated
Normal file
6
TTT_java/out/production/TTT_java/.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
BIN
TTT_java/out/production/TTT_java/AIPlayer.class
Normal file
BIN
TTT_java/out/production/TTT_java/AIPlayer.class
Normal file
Binary file not shown.
BIN
TTT_java/out/production/TTT_java/Board.class
Normal file
BIN
TTT_java/out/production/TTT_java/Board.class
Normal file
Binary file not shown.
BIN
TTT_java/out/production/TTT_java/Game.class
Normal file
BIN
TTT_java/out/production/TTT_java/Game.class
Normal file
Binary file not shown.
BIN
TTT_java/out/production/TTT_java/Player.class
Normal file
BIN
TTT_java/out/production/TTT_java/Player.class
Normal file
Binary file not shown.
11
TTT_java/out/production/TTT_java/TTT_java.iml
Normal file
11
TTT_java/out/production/TTT_java/TTT_java.iml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
BIN
TTT_java/out/production/TTT_java/TicTacToeGame.class
Normal file
BIN
TTT_java/out/production/TTT_java/TicTacToeGame.class
Normal file
Binary file not shown.
Reference in New Issue
Block a user