המודל
//Model.java
package com.example.seminarfirstdemoapp;
import static com.example.seminarfirstdemoapp.AppConsts.*;
public class Model
{
// 1 Board - 2D Array
private int[][] board;
private int turnCounter = 0;
public Model() {
board = new int[ROWS][COLUMNS];
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
board[i][j] = EMPTY;
}
}
}
// check Win
// update Board (row,column)
// is valid move (row,column)
// TIE - turns are over and no one has won
// reset Board
// 2 currentPlayer
}
the activity DemoActivity.java
package com.example.seminarfirstdemoapp;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class DemoActivity extends AppCompatActivity {
private Button button;
private Model model;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_demo);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
initUI();
model = new Model();
}
private void initUI() {
button = findViewById(R.id.button00);
/*
button.setOnClickListener(view -> {
int viewId = view.getId();
String idAsString = getResources().getResourceEntryName(viewId);
Log.d("ID AS STRING", "initUI: " + idAsString);
String tag = view.getTag().toString();
Log.d("TAG", "initUI: " + tag);
// Handle button click
// For example, you can show a Toast or start another activity
// Toast.makeText(DemoActivity.this, "Button clicked!", Toast.LENGTH_SHORT).show();
});
*/
}
public void onButtonClick(View view) {
String tag = view.getTag().toString();
int row = tag.charAt(0)-'0';
int column = tag.charAt(1)-'0';
Button clickedButton = (Button) view;
clickedButton.setText("X");
Log.d("Android Seminar", "row: " + row + "column: " + column );
}
}
xml activity_demo.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DemoActivity">
<Button
android:id="@+id/button00"
android:layout_width="91dp"
android:layout_height="69dp"
android:layout_marginStart="52dp"
android:layout_marginTop="72dp"
android:onClick="onButtonClick"
android:tag="00"
android:text="Click Me"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>