Skip to content

Instantly share code, notes, and snippets.

@stefanhuber
Last active March 9, 2019 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanhuber/25c498091ce86b0f4f3b0e8d0fc571d5 to your computer and use it in GitHub Desktop.
Save stefanhuber/25c498091ce86b0f4f3b0e8d0fc571d5 to your computer and use it in GitHub Desktop.
Tic Tac Toe Android XML Layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toTopOf="@+id/button3"
app:layout_constraintDimensionRatio="w,1:1"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toBottomOf="@+id/button2" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.3333" />
<android.support.constraint.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.6666" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintDimensionRatio="h,1:1"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="h,1:1"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toBottomOf="@+id/button" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toTopOf="@+id/button6"
app:layout_constraintDimensionRatio="w,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toBottomOf="@+id/button5" />
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toTopOf="@+id/button4"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<Button
android:id="@+id/button6"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toBottomOf="@+id/button4" />
<Button
android:id="@+id/button7"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toTopOf="@+id/button9"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button8" />
<Button
android:id="@+id/button8"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:onClick="onClick"
app:layout_constraintBottom_toTopOf="@+id/button7"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<Button
android:id="@+id/button9"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button7" />
</android.support.constraint.ConstraintLayout>
package at.stefanhuber.tictactoe;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
protected TextView gameState;
protected TTTGameService gameService = new TTTGameService();
protected int X = 0xFFFF0000;
protected int O = 0xFF00FF00;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
gameState = findViewById(R.id.game_state);
ViewGroup layout = findViewById(R.id.tictactoe_layout);
int count = layout.getChildCount();
for (int i = 0; i < count; i++) {
View v = layout.getChildAt(i);
v.setOnClickListener(this);
}
showGameState();
}
public void showGameState() {
String player = gameService.getCurrentPlayer();
int turn = gameService.getTurnCount();
gameState.setText("Zug Nr. " + turn + ", Spieler: " + player);
}
@Override
public void onClick(View v) {
int index = Integer.parseInt(v.getTag().toString());
Log.i("TICTACTOE", "Index: " + index);
int color = gameService.getCurrentPlayer().equals("X") ? X : O;
if (gameService.turn(index)) {
ObjectAnimator colorAnimator = ObjectAnimator.ofArgb(v, "backgroundColor", color);
colorAnimator.setDuration(1500);
colorAnimator.start();
showGameState();
}
}
}
package at.stefanhuber.tictactoe;
import java.util.Arrays;
public class TTTGameService {
protected String[] game = {"", "", "", "", "", "", "", "", ""};
/**
*
* Firstly the current player is derived and then it is checked
* if the turn is valid, if it is valid it is executed (and true
* is returned) otherwise false is returned
*
* @param index
* @return
*/
public boolean turn(int index) {
String player = getCurrentPlayer();
if (game[index].equals("")) {
game[index] = player;
return true;
}
return false;
}
public String[] getGame() {
return game;
}
public void setGame(String[] game) {
this.game = game;
}
public int getTurnCount() {
int count = 0;
for (int i = 0; i < game.length; i++) {
if (game[i].equals("")) {
count++;
}
}
return 10 - count;
}
public String getCurrentPlayer() {
return getTurnCount() % 2 == 1 ? "X" : "O";
}
public String checkWinner() {
for (int i = 0; i < 3; i++)
if (!game[i*3].equals("") &&
game[i*3].equals(game[i*3+1]) &&
game[i*3].equals(game[i*3+2])) {
return game[i*3];
}
for (int i = 0; i < 3; i++)
if (!game[i].equals("") &&
game[i].equals(game[i+3]) &&
game[i].equals(game[i+6])) {
return game[i];
}
if (!game[0].equals("") &&
game[0].equals(game[4]) &&
game[0].equals(game[8])) {
return game[4];
}
if (!game[2].equals("") &&
game[2].equals(game[4]) &&
game[2].equals(game[6])) {
return game[4];
}
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment