Android : Jeux X-O

Générer le jeux Croix -O

1.Layout

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
android:background="#ff0"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="0"
android:id="@+id/xscore"
android:gravity="center"
android:textSize="25dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="X"
android:background="#f00"
android:gravity="center"
android:textSize="25dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Vs"
android:gravity="center"
android:textSize="25dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="O"
android:gravity="center"
android:textSize="25dp"
android:background="#0f0"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="0"
android:id="@+id/oscore"
android:gravity="center"
android:textSize="25dp"/>

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#D3D332"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#E4E498"
android:orientation="horizontal">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D3D332"
android:layout_weight="1"
android:id="@+id/c00"
android:layout_margin="2dp"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D3D332"
android:layout_weight="1"
android:layout_margin="2dp"
android:id="@+id/c01"/>
<ImageButton
android:id="@+id/c02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D3D332"
android:layout_weight="1"
android:layout_margin="2dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#E4E498"
android:orientation="horizontal">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D3D332"
android:layout_weight="1"
android:id="@+id/c10"
android:layout_margin="2dp"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D3D332"
android:layout_weight="1"
android:id="@+id/c11"
android:layout_margin="2dp"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D3D332"
android:layout_weight="1"
android:id="@+id/c12"
android:layout_margin="2dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#E4E498"
android:orientation="horizontal">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D3D332"
android:layout_weight="1"
android:id="@+id/c20"
android:layout_margin="2dp"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D3D332"
android:layout_weight="1"
android:id="@+id/c21"
android:layout_margin="2dp"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D3D332"
android:layout_weight="1"
android:id="@+id/c22"
android:layout_margin="2dp"/>
</LinearLayout>

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
android:background="#ff0"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="22dp"
android:id="@+id/bstart"
android:text="Start"/>

</LinearLayout>
</LinearLayout>


2.Java



import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class Test2_Croix_O extends AppCompatActivity implements View.OnClickListener{
//les objets java
TextView xscore,oscore;
ImageButton[][]LesCases=new ImageButton[3][3];
String[][]LesCasesint=new String[3][3];
Button bstart;
String tour="o";
int xpoints=0,opoints=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test2__croix__o);
//liaison entre les objets java et les objets graphiques
xscore=findViewById(R.id.xscore);
oscore=findViewById(R.id.oscore);
LesCases[0][0]=findViewById(R.id.c00);
LesCases[0][1]=findViewById(R.id.c01);
//.... 9 fois OU on utilise une boucle
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
//retourner les id des objets Graphiques
int id = getResources().getIdentifier("c"+i+""+j, "id", getPackageName());
LesCases[i][j]=findViewById(id);
LesCases[i][j].setOnClickListener(this);

LesCasesint[i][j]="";
}
}

bstart=findViewById(R.id.bstart);

//ecouter les objets
bstart.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if(bstart.getId()==view.getId()) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
LesCasesint[i][j] = "";
//set l'image vide dans la src du bouton
LesCases[i][j].setImageResource(R.drawable.vvide);
//activer le bouton
LesCases[i][j].setEnabled(true);
}
}
}
//chercher sur quelle bouton on a cliqué
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++) {
if (view.getId() == LesCases[i][j].getId()) {

if (LesCasesint[i][j] == "") {
if (tour == "O") {
LesCasesint[i][j] = "O";
LesCases[i][j].setImageResource(R.drawable.oo);
tour = "X";
} else {
LesCasesint[i][j] = "X";
LesCases[i][j].setImageResource(R.drawable.xx);
tour = "O";

}
}
}
}

}

if(verifierGagnant())
{

for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
//désactiver les boutons
LesCases[i][j].setEnabled(false);
}
}
}

}

boolean verifierGagnant(){

//vérifier les lignes
for(int i=0;i<3;i++){
if(LesCasesint[i][0]!="")
if( (LesCasesint[i][0]==LesCasesint[i][1])
&& (LesCasesint[i][0]==LesCasesint[i][2]))
{
if(LesCasesint[i][0]=="O"){
opoints++;
oscore.setText(""+opoints);
}
else
{
xpoints++;
xscore.setText(""+xpoints);
}
return true;
}




}


//vérifier les colonnes
for(int i=0;i<3;i++) {
if (LesCasesint[0][i] != "")
if ((LesCasesint[0][i] == LesCasesint[1][i])
&& (LesCasesint[0][i] == LesCasesint[2][i])) {
if (LesCasesint[0][i] == "O") {
opoints++;
oscore.setText("" + opoints);
} else {
xpoints++;
xscore.setText("" + xpoints);
}
return true;
}
}

//diagonale à droite
if(LesCasesint[0][0]!="")
{

if( (LesCasesint[0][0]==LesCasesint[1][1])
&& (LesCasesint[0][0]==LesCasesint[2][2]))
{
if(LesCasesint[0][0]=="O"){
opoints++;
oscore.setText(""+opoints);
}
else
{
xpoints++;
xscore.setText(""+xpoints);
}
return true;
}
}



//diagonale à gauche
if(LesCasesint[0][2]!="")
{

if( (LesCasesint[0][2]==LesCasesint[1][1])
&& (LesCasesint[0][2]==LesCasesint[2][0]))
{
if(LesCasesint[0][2]=="O"){
opoints++;
oscore.setText(""+opoints);
}
else
{
xpoints++;
xscore.setText(""+xpoints);
}
return true;
}
}
return false;
}
}