mmayoub

XO, 16-23.10.2021, GameActivity.java

Oct 16th, 2021 (edited)
1,130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.15 KB | None | 0 0
  1. package com.example.myxogame;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.Button;
  10. import android.widget.TableLayout;
  11. import android.widget.TableRow;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14.  
  15. public class GameActivity extends AppCompatActivity implements View.OnClickListener {
  16.     int rows, cols, winCount;
  17.     String player;
  18.     TableLayout tblBoard;
  19.     TextView tvPlayerTurn;
  20.     int counter = 0;
  21.  
  22.     Button[][] btns;
  23.     boolean gameOver = false;
  24.     int clickedRow, clickedCol;
  25.  
  26.     @Override
  27.     protected void onCreate(Bundle savedInstanceState) {
  28.         super.onCreate(savedInstanceState);
  29.  
  30.  
  31.         Intent intent = getIntent();
  32.         rows = intent.getIntExtra("ROWS_COUNT", -1);
  33.         cols = intent.getIntExtra("COLS_COUNT", -1);
  34.         winCount = intent.getIntExtra("WIN_COUNT", -1);
  35.  
  36.         if (rows == -1 || cols == -1 || winCount == -1) {
  37.             // handle exception
  38.             Toast.makeText(this, "Error! missing or error data.", Toast.LENGTH_LONG).show();
  39.             finish();
  40.         }
  41.         setContentView(R.layout.activity_game);
  42.         //Toast.makeText(this, rows + "," + cols + "," + winCount, Toast.LENGTH_LONG).show();
  43.         btns = new Button[rows][cols];
  44.         tblBoard = findViewById(R.id.tblBoard);
  45.         tvPlayerTurn = findViewById(R.id.tvPlayerTurn);
  46.  
  47.         for (int r = 0; r < rows; r++) {
  48.             TableRow newRow = new TableRow(this);
  49.             TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 0, 1);
  50.             newRow.setLayoutParams(rowParams);
  51.             for (int c = 0; c < cols; c++) {
  52.                 Button newButton = new Button(this);
  53.  
  54.                 TableRow.LayoutParams buttonParams = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT, 1);
  55.                 newButton.setLayoutParams(buttonParams);
  56.  
  57.                 newButton.setOnClickListener(this);
  58.  
  59.                 newRow.addView(newButton);
  60.                 btns[r][c] = newButton;
  61.             }
  62.  
  63.             tblBoard.addView(newRow);
  64.  
  65.         }
  66.  
  67.         player = "X";
  68.         tvPlayerTurn.setText(player + " : playing");
  69.     }
  70.  
  71.     @Override
  72.     public void onClick(View v) {
  73.         if (!gameOver) {
  74.             Button btn = (Button) v;
  75.             if (btn.getText().toString().length() == 0) {
  76.                 btn.setText(player);
  77.                 counter++;
  78.  
  79.                 findClickedPosition(btn); // update cliked row and clicked col
  80.  
  81.                 if (win()) {
  82.                     // player has win the game
  83.                     Toast.makeText(this, player + " is winner!", Toast.LENGTH_LONG).show();
  84.                     gameOver = true;
  85.                 } else {
  86.                     if (counter < rows * cols) {
  87.                         if (player.equals("X")) {
  88.                             player = "O";
  89.                         } else {
  90.                             player = "X";
  91.                         }
  92.  
  93.                         tvPlayerTurn.setText(player + " : playing");
  94.                     } else {
  95.                         // board is full, stop the game
  96.                         // TODO ask the player for new game
  97.                         Toast.makeText(this, "Game is over!", Toast.LENGTH_SHORT).show();
  98.                         gameOver = true;
  99.                     }
  100.                 }
  101.             }
  102.         }
  103.     }
  104.  
  105.     private void findClickedPosition(Button btn) {
  106.         for (int r = 0; r < rows; r++) {
  107.             for (int c = 0; c < cols; c++) {
  108.                 if (btns[r][c].getId() == btn.getId()) {
  109.                     clickedRow = r;
  110.                     clickedCol = c;
  111.                     return;
  112.                 }
  113.             }
  114.         }
  115.     }
  116.  
  117.     private boolean win() {
  118.         return (winRow() || winCol() || winDiag());
  119.     }
  120.  
  121.     private boolean winDiag() {
  122.         // TODO for next lesson
  123.         while (clickedCol > 0 && clickedRow > 0) {
  124.             clickedRow--;
  125.             clickedCol--;
  126.         }
  127.         return false;
  128.     }
  129.  
  130.     private boolean winCol() {
  131.         for (int c = 0; c < cols; c++) {
  132.             int playCount = 0;
  133.             for (int r = 0; r < rows; r++) {
  134.                 if (btns[r][c].getText().toString().equals(player)) {
  135.                     playCount++;
  136.                     if (playCount == winCount) {
  137.                         return true;
  138.                     }
  139.                 } else {
  140.                     playCount = 0;
  141.                 }
  142.             }
  143.         }
  144.         return false;
  145.     }
  146.  
  147.     private boolean winRow() {
  148.         for (int r = 0; r < rows; r++) {
  149.             int playCount = 0;
  150.             for (int c = 0; c < cols; c++) {
  151.                 if (btns[r][c].getText().toString().equals(player)) {
  152.                     playCount++;
  153.                     if (playCount == winCount) {
  154.                         return true;
  155.                     }
  156.                 } else {
  157.                     playCount = 0;
  158.                 }
  159.             }
  160.         }
  161.         return false;
  162.     }
  163. }
Add Comment
Please, Sign In to add comment