Skip to content
Snippets Groups Projects
Commit 8aae25f2 authored by MAUGEZ Emeline's avatar MAUGEZ Emeline
Browse files

Morpion repris d'un autre code à revoir

parent 35aa3711
No related branches found
No related tags found
No related merge requests found
package com.example.robotise.morpion;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.robotise.R;
//from sandipapps
public class MorpionActivity extends AppCompatActivity{
int currentPlayer = 0;
boolean gameActive = true;
int[] gameState = {-1,-1,-1,-1,-1,-1,-1,-1,-1};
int[][] winningPositions = {
{0,1,2}, {3,4,5}, {6,7,8},
{0,3,6}, {1,4,7}, {2,5,8},
{0,4,8}, {2,4,6}
};
String winner = "X";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.morpion);
}
public void play(View view) {
ImageView ivClicked = (ImageView) view;
ivClicked.setAlpha(1.0f);
ivClicked.setVisibility(View.VISIBLE);
int clickedImageView = Integer.parseInt(ivClicked.getTag().toString());
if(gameState[clickedImageView] == -1 && gameActive){
gameState[clickedImageView] = currentPlayer;
if(currentPlayer == 0){
ivClicked.setImageResource(R.drawable.o);
currentPlayer = 1;
} else {
ivClicked.setImageResource(R.drawable.x);
currentPlayer = 0;
}
ivClicked.setVisibility(View.VISIBLE);
for (int[] winningPosition: winningPositions
) {
if(gameState[winningPosition[0]] == gameState[winningPosition[1]] &&
gameState[winningPosition[1]] == gameState[winningPosition[2]] &&
gameState[winningPosition[0]] != -1){
gameActive = false;
if(gameState[winningPosition[0]] == 0){
winner = "0";
}
TextView tvMessage = findViewById(R.id.tvMessage);
tvMessage.setText(winner + " has won!");
LinearLayout layout = findViewById(R.id.restartLayout);
layout.setVisibility(View.VISIBLE);
}else{
boolean gameOver = true;
for (int currentPlayer: gameState
) {
if(currentPlayer == -1){
gameOver = false;
}
}
if(gameOver){
TextView tvMessage = findViewById(R.id.tvMessage);
tvMessage.setText("It's a draw");
LinearLayout layout = findViewById(R.id.restartLayout);
layout.setVisibility(View.VISIBLE);
}
}
}
}
}
public void playAgain(View view) {
winner = "X";
gameActive = true;
currentPlayer = 0;
LinearLayout layout = findViewById(R.id.restartLayout);
layout.setVisibility(View.INVISIBLE);
for (int i=0; i< gameState.length; i++){
gameState[i] = -1;
}
LinearLayout gameLayout = findViewById(R.id.gameLayout);
for (int i=0; i < gameLayout.getChildCount(); i++){
View subView = gameLayout.getChildAt(i);
if(subView instanceof LinearLayout){
LinearLayout linearLayout = (LinearLayout) subView;
for(int j=0; j < linearLayout.getChildCount(); j++){
View linearSubView = linearLayout.getChildAt(j);
if(linearSubView instanceof ImageView){
linearSubView.setAlpha(0.0f);
}
}
}
}
}
}
app/src/main/res/drawable/o.png

23.8 KiB

app/src/main/res/drawable/x.png

26.9 KiB

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".morpion.MorpionActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/gameLayout"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="6dp"
android:src="@mipmap/o"
android:onClick="play"
android:tag="0"
android:alpha="0.0" />
<View
android:layout_width="6dp"
android:layout_height="match_parent"
android:background="@color/black" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="6dp"
android:src="@mipmap/o"
android:onClick="play"
android:tag="1"
android:alpha="0.0" />
<View
android:layout_width="6dp"
android:layout_height="match_parent"
android:background="@color/black" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="6dp"
android:src="@mipmap/o"
android:onClick="play"
android:tag="2"
android:alpha="0.0" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="@color/black" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="6dp"
android:src="@mipmap/o"
android:onClick="play"
android:tag="3"
android:alpha="0.0" />
<View
android:layout_width="6dp"
android:layout_height="match_parent"
android:background="@color/black" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="6dp"
android:src="@mipmap/o"
android:onClick="play"
android:tag="4"
android:alpha="0.0" />
<View
android:layout_width="6dp"
android:layout_height="match_parent"
android:background="@color/black" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="6dp"
android:src="@mipmap/o"
android:onClick="play"
android:tag="5"
android:alpha="0.0" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="@color/black" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="6dp"
android:src="@mipmap/o"
android:onClick="play"
android:tag="6"
android:alpha="0.0" />
<View
android:layout_width="6dp"
android:layout_height="match_parent"
android:background="@color/black" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="6dp"
android:src="@mipmap/o"
android:onClick="play"
android:tag="7"
android:alpha="0.0" />
<View
android:layout_width="6dp"
android:layout_height="match_parent"
android:background="@color/black" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="6dp"
android:src="@mipmap/o"
android:onClick="play"
android:tag="8"
android:alpha="0.0" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/restartLayout"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical"
android:padding="30dp"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvMessage"
android:layout_gravity="center_horizontal"
android:textSize="30sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnPlayAgain"
android:layout_gravity="center_horizontal"
android:onClick="playAgain"
android:text="Play Again" />
</LinearLayout>
</RelativeLayout>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment