All pastes #384390 Raw Edit

Something

public text v1 · immutable
·published 2007-03-07 01:13 UTC
rendered paste body
import javax.swing.JOptionPane;

public class Puissance4
{
    public static void initialisation(char tab[][])
    {
	//remplissage
	//la premiere dimension du tableau est le nombre de rangees, 
	//en faisant tab.length, on obtient le nombre de rangees.
	//on boucle sur chacune des rangees.
	for( int i = 0 ; i< tab.length; i++)
	    {
		//on boucle sur chacun des elements de la rangees i.
		//en faisant tab[i].length, on obtient le nombre de colonnes
		for(int j = 0; j< tab[i].length; j++)
		    {
			//on met un caractere '.' a la ieme rangee, 
			//jieme colonne du tableau tab
			tab[i][j] = '.';
		    }
	    }
    }

    public static void afficher(char tab[][])
    {
	System.out.println("  1 2 3 4 5 6 7");
	for(int i = 0; i< tab.length; i++)
	    {
		System.out.print(i+1 + " ");
		for(int j = 0; j< tab[i].length; j++)
		    System.out.print( tab[i][j]+ " " );
		System.out.println();
	    }  
	System.out.println();
    }

    public static void joueur1(char tab[][],char j1)
    {
	//System.out.print("Joueur 1 colonne? ");
	//int pos = Keyboard.readInt();
	int pos = Integer.parseInt(JOptionPane.showInputDialog(null, "Joueur 1 colonne?"));
	if(!(pos == 1 || pos == 2 || pos == 3 || pos == 4
	     || pos == 5 || pos == 6 || pos == 7))
	    {
		System.out.println("Colonne hors du tableau. " +
				   "Choisissez une autre colonne.");
		joueur1(tab,j1);
		return;
	    }
	if(tab[0][pos-1] != '.')
	    {
		System.out.println("Colonne pleine. " +
				   "Choisissez une autre colonne.");
		joueur1(tab,j1);
		return;
	    }
	for(int i = tab.length - 1; i >= 0; i--)
	    {
		if(tab[i][pos-1] == '.')
		    {		    
			tab[i][pos-1] = 'X';
			System.out.println(">> X " + pos + " " + (i+1));
			break;
		    }
	    }
    }

    public static void joueur2(char tab[][],char j2)
    {
	//System.out.print("Joueur 2 colonne? ");
	//int pos = Keyboard.readInt();
	int pos = Integer.parseInt(JOptionPane.showInputDialog(null, "Joueur 2 colonne?"));
	if(!(pos == 1 || pos == 2 || pos == 3 || pos == 4
	     || pos == 5 || pos == 6 || pos == 7))
	    {
		System.out.println("Colonne hors du tableau. " + 
				   "Choisissez une autre colonne.");
		joueur2(tab,j2);
		return;
	    }
	if(tab[0][pos-1] != '.')
	    {
		System.out.println("Colonne pleine. " +
				   "Choisissez une autre colonne.");
		joueur2(tab,j2);
		return;
	    }
	for(int i = tab.length - 1; i >= 0; i--)
	    {
		if(tab[i][pos-1] == '.')
		    {		    
			tab[i][pos-1] = 'O';
			System.out.println(">> O " + pos + " " + (i+1));
			break;
		    }
	    }
    }
    
    public static final int nombreDeLignes = 6;
    public static final int nombreDeColonnes = 7;

    public static void main(String [] args)
    {
	char j1 = 'h';
	char j2 = 'h';
	char[][] tab = new char[nombreDeLignes][nombreDeColonnes];
	initialisation(tab);
	afficher(tab);
	while(1==1)
	    {
		joueur1(tab,j1);
		afficher(tab);
		joueur2(tab,j2);
		afficher(tab);
	    }
    }
}