Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.myxogame;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.TableLayout;
- import android.widget.TableRow;
- import android.widget.TextView;
- import android.widget.Toast;
- public class GameActivity extends AppCompatActivity implements View.OnClickListener {
- int rows, cols, winCount;
- String player;
- TableLayout tblBoard;
- TextView tvPlayerTurn;
- int counter = 0;
- Button[][] btns;
- boolean gameOver = false;
- int clickedRow, clickedCol;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Intent intent = getIntent();
- rows = intent.getIntExtra("ROWS_COUNT", -1);
- cols = intent.getIntExtra("COLS_COUNT", -1);
- winCount = intent.getIntExtra("WIN_COUNT", -1);
- if (rows == -1 || cols == -1 || winCount == -1) {
- // handle exception
- Toast.makeText(this, "Error! missing or error data.", Toast.LENGTH_LONG).show();
- finish();
- }
- setContentView(R.layout.activity_game);
- //Toast.makeText(this, rows + "," + cols + "," + winCount, Toast.LENGTH_LONG).show();
- btns = new Button[rows][cols];
- tblBoard = findViewById(R.id.tblBoard);
- tvPlayerTurn = findViewById(R.id.tvPlayerTurn);
- for (int r = 0; r < rows; r++) {
- TableRow newRow = new TableRow(this);
- TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 0, 1);
- newRow.setLayoutParams(rowParams);
- for (int c = 0; c < cols; c++) {
- Button newButton = new Button(this);
- TableRow.LayoutParams buttonParams = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT, 1);
- newButton.setLayoutParams(buttonParams);
- newButton.setOnClickListener(this);
- newRow.addView(newButton);
- btns[r][c] = newButton;
- }
- tblBoard.addView(newRow);
- }
- player = "X";
- tvPlayerTurn.setText(player + " : playing");
- }
- @Override
- public void onClick(View v) {
- if (!gameOver) {
- Button btn = (Button) v;
- if (btn.getText().toString().length() == 0) {
- btn.setText(player);
- counter++;
- findClickedPosition(btn); // update cliked row and clicked col
- if (win()) {
- // player has win the game
- Toast.makeText(this, player + " is winner!", Toast.LENGTH_LONG).show();
- gameOver = true;
- } else {
- if (counter < rows * cols) {
- if (player.equals("X")) {
- player = "O";
- } else {
- player = "X";
- }
- tvPlayerTurn.setText(player + " : playing");
- } else {
- // board is full, stop the game
- // TODO ask the player for new game
- Toast.makeText(this, "Game is over!", Toast.LENGTH_SHORT).show();
- gameOver = true;
- }
- }
- }
- }
- }
- private void findClickedPosition(Button btn) {
- for (int r = 0; r < rows; r++) {
- for (int c = 0; c < cols; c++) {
- if (btns[r][c].getId() == btn.getId()) {
- clickedRow = r;
- clickedCol = c;
- return;
- }
- }
- }
- }
- private boolean win() {
- return (winRow() || winCol() || winDiag());
- }
- private boolean winDiag() {
- // TODO for next lesson
- while (clickedCol > 0 && clickedRow > 0) {
- clickedRow--;
- clickedCol--;
- }
- return false;
- }
- private boolean winCol() {
- for (int c = 0; c < cols; c++) {
- int playCount = 0;
- for (int r = 0; r < rows; r++) {
- if (btns[r][c].getText().toString().equals(player)) {
- playCount++;
- if (playCount == winCount) {
- return true;
- }
- } else {
- playCount = 0;
- }
- }
- }
- return false;
- }
- private boolean winRow() {
- for (int r = 0; r < rows; r++) {
- int playCount = 0;
- for (int c = 0; c < cols; c++) {
- if (btns[r][c].getText().toString().equals(player)) {
- playCount++;
- if (playCount == winCount) {
- return true;
- }
- } else {
- playCount = 0;
- }
- }
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment