Add files via upload
This commit is contained in:
		
							parent
							
								
									56eef89161
								
							
						
					
					
						commit
						d8a578f029
					
				
					 2 changed files with 106 additions and 109 deletions
				
			
		
										
											Binary file not shown.
										
									
								
							| 
						 | 
				
			
			@ -12,67 +12,74 @@ public class Programme {
 | 
			
		|||
	char O = 'O';
 | 
			
		||||
	Scanner scan = new Scanner(System.in);
 | 
			
		||||
	char joueurActuel;
 | 
			
		||||
	String saisir;
 | 
			
		||||
	// Dessiner une grille
 | 
			
		||||
	char[]grid= 
 | 
			
		||||
	{'1','2','3',
 | 
			
		||||
	'4','5','6',
 | 
			
		||||
	'7','8','9',
 | 
			
		||||
	};
 | 
			
		||||
	char[] grid = { '1', '2', '3', '4', '5', '6', '7', '8', '9', };
 | 
			
		||||
 | 
			
		||||
	public void montrerLaGrille(char[] grid) {
 | 
			
		||||
		System.out.println("[" + grid[0] + '|' + grid[1] + '|' + grid[2] + ']');
 | 
			
		||||
		System.out.println("[" + grid[3] + '|' + grid[4] + '|' + grid[5] + ']');
 | 
			
		||||
		System.out.println("[" + grid[6] + '|' + grid[7] + '|' + grid[8] + ']');
 | 
			
		||||
		System.out.println("");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void gagnant(char[] grid, char joueurActuel) {
 | 
			
		||||
		montrerLaGrille(grid);
 | 
			
		||||
		System.out.println("Le joueur qui a les " + joueurActuel + " a gagné!");
 | 
			
		||||
		 System.exit(0);
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public boolean isGagnant () {
 | 
			
		||||
	public void isGagnant() {
 | 
			
		||||
		
 | 
			
		||||
		// Vérifier s'il y a 3 elements alignés en vertical horizontal ou diagonale
 | 
			
		||||
		// On verifie les lignes
 | 
			
		||||
		for (int i = 0; i < 3; ++i) {
 | 
			
		||||
			if (grid[i * 3 + 0] == joueurActuel && grid[i * 3 + 1] == joueurActuel && grid[i * 3 + 2] == joueurActuel)
 | 
			
		||||
				gagnant = true;
 | 
			
		||||
				gagnant(grid, joueurActuel);
 | 
			
		||||
				break;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
           
 | 
			
		||||
		// on verifie les colonnes
 | 
			
		||||
		for (int i = 0; i < 3; i++) {
 | 
			
		||||
			if (grid[i] == joueurActuel && grid[i + 3] == joueurActuel && grid[i + 6] == joueurActuel)
 | 
			
		||||
				gagnant = true;
 | 
			
		||||
				gagnant(grid, joueurActuel);
 | 
			
		||||
				break;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
       
 | 
			
		||||
		// on verifie 1ere diagonale
 | 
			
		||||
		for (int i = 0; i < 3; ++i) {
 | 
			
		||||
            if (grid[0]==joueurActuel && grid[4]==joueurActuel && grid[8]==joueurActuel) {
 | 
			
		||||
			if (grid[0] == joueurActuel && grid[4] == joueurActuel && grid[8] == joueurActuel)
 | 
			
		||||
				gagnant = true;
 | 
			
		||||
				gagnant(grid, joueurActuel);
 | 
			
		||||
				break;
 | 
			
		||||
            }
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// on verifie 2eme diagonale
 | 
			
		||||
		for (int j = 0; j < 3; ++j) {
 | 
			
		||||
            if (grid[2]==joueurActuel && grid[4]==joueurActuel && grid[6]==joueurActuel) {
 | 
			
		||||
			if (grid[2] == joueurActuel && grid[4] == joueurActuel && grid[6] == joueurActuel)
 | 
			
		||||
				gagnant = true;
 | 
			
		||||
				gagnant(grid, joueurActuel);
 | 
			
		||||
				break;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// si toutes les cases sont pleines et qu'iln'y a pas 3 pions d'alligné match
 | 
			
		||||
		// nul
 | 
			
		||||
		for (int k = 0; k < 8; ++k) {
 | 
			
		||||
			if (grid[k] == X || grid[k] == O)
 | 
			
		||||
				gagnant = true;
 | 
			
		||||
				System.out.println("Match nul!!");
 | 
			
		||||
				break;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
            return gagnant;
 | 
			
		||||
		
 | 
			
		||||
	}
 | 
			
		||||
		
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
	public Programme() {
 | 
			
		||||
 | 
			
		||||
					
 | 
			
		||||
		// début du jeu
 | 
			
		||||
 | 
			
		||||
// tirer au hazard quel joueur commence
 | 
			
		||||
| 
						 | 
				
			
			@ -84,46 +91,40 @@ public class Programme {
 | 
			
		|||
 | 
			
		||||
		// imprimer la grille
 | 
			
		||||
 | 
			
		||||
	do{
 | 
			
		||||
		montrerLaGrille(grid);
 | 
			
		||||
	do {	montrerLaGrille(grid);
 | 
			
		||||
		// le joueur 1 choisit une case : entre 1 et 9 et non cochée.
 | 
			
		||||
		// si caractere incorrect reposer la question
 | 
			
		||||
 | 
			
		||||
		System.out.println(joueurActuel+ " choisis un numéro de case libre entre 1 et 9");
 | 
			
		||||
		String cocher=scan.nextLine();
 | 
			
		||||
		while (true) {
 | 
			
		||||
			System.out.print(joueurActuel + " choisis un numéro de case libre entre 1 et 9");
 | 
			
		||||
			saisir = scan.nextLine();
 | 
			
		||||
			try {
 | 
			
		||||
				cases =Integer.parseInt(cocher);
 | 
			
		||||
				if ((1<=cases) && (cases<=9) && (grid[cases-1]!=X) && (grid[cases-1]!=O));
 | 
			
		||||
				cases = Integer.parseInt(saisir);
 | 
			
		||||
				if (cases >= 1 && cases <= 9 && grid[cases - 1] != X && grid[cases - 1] != O)
 | 
			
		||||
					break;
 | 
			
		||||
				}catch (Exception cases){cases.printStackTrace();}
 | 
			
		||||
			} catch (Exception e) {
 | 
			
		||||
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		// Mettre le bon caractere dans la case choisie
 | 
			
		||||
		grid[cases - 1] = joueurActuel;
 | 
			
		||||
 | 
			
		||||
		isGagnant();
 | 
			
		||||
			// si toutes les cases sont pleines et qu'iln'y a pas 3 pions d'alligné match nul
 | 
			
		||||
			for (int k=0;k<9;++k){
 | 
			
		||||
				if (grid[k]==X || grid[k]==O)
 | 
			
		||||
				System.out.println("Match nul!!");
 | 
			
		||||
				break;		
 | 
			
		||||
	}
 | 
			
		||||
	}while(gagnant==false);// si non demander à l'autre joueur de choisir une case libre
 | 
			
		||||
 | 
			
		||||
	while(gagnant == false); // si non demander à l'autre joueur de choisir une case libre
 | 
			
		||||
			if (joueurActuel == X)
 | 
			
		||||
				joueurActuel = O;
 | 
			
		||||
			else
 | 
			
		||||
				joueurActuel = X;
 | 
			
		||||
		
 | 
			
		||||
	}	
 | 
			
		||||
 | 
			
		||||
           
 | 
			
		||||
         
 | 
			
		||||
			
 | 
			
		||||
           
 | 
			
		||||
	            		
 | 
			
		||||
	/**
 | 
			
		||||
	 * Main
 | 
			
		||||
	 */
 | 
			
		||||
	public static void main(String[] args) {
 | 
			
		||||
	/**TODO Auto-generated method stub*/
 | 
			
		||||
			Programme morpion=new Programme();
 | 
			
		||||
		/** TODO Auto-generated method stub */	/**TODO Auto-generated method stub*/
 | 
			
		||||
			Programme programme = new Programme();
 | 
			
		||||
		}}	
 | 
			
		||||
 | 
			
		||||
		
 | 
			
		||||
| 
						 | 
				
			
			@ -136,7 +137,3 @@ public static void main(String[] args) {
 | 
			
		|||
	
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue