Advertisement
Evyatar12

TicTacToe.java

Oct 7th, 2018
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.27 KB | None | 0 0
  1. package com.example.lab1.tictactoe;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.LayoutInflater;
  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.  
  14. import java.util.ArrayList;
  15. import java.util.Arrays;
  16. import java.util.List;
  17.  
  18. public class TicTacToe extends AppCompatActivity {
  19.  
  20.     private static final int BOARD_SIZE = 3;
  21.     private static final String DEFAULT_TEXT = "?";
  22.  
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_tic_tac_toe);
  27.  
  28.         generateLayout((ViewGroup) findViewById(R.id.ticTacToeLayout));
  29.     }
  30.  
  31.     private void log(String s) {
  32.         Log.i("BOI", s);
  33.     }
  34.  
  35.     private void generateLayout(ViewGroup parent) {
  36.         TableLayout tl = (TableLayout) new TableLayout(this);
  37.         tl.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
  38.  
  39.         parent.addView(tl);
  40.  
  41.         Counter[] xRow = new Counter[BOARD_SIZE];
  42.         Counter[] oRow = new Counter[BOARD_SIZE];
  43.         Counter[] xCol = new Counter[BOARD_SIZE];
  44.         Counter[] oCol = new Counter[BOARD_SIZE];
  45.         Counter[] xDiag = new Counter[2];
  46.         Counter[] oDiag = new Counter[2];
  47.  
  48.         Toggle turnToggle = new Toggle();
  49.         turnToggle.toggle();
  50.  
  51.         for (int i = 0; i < BOARD_SIZE; i++) {
  52.             xRow[i] = new Counter();
  53.             oRow[i] = new Counter();
  54.             xCol[i] = new Counter();
  55.             oCol[i] = new Counter();
  56.         }
  57.  
  58.         for (int i = 0; i < 2; i++) {
  59.             xDiag[i] = new Counter();
  60.             oDiag[i] = new Counter();
  61.         }
  62.  
  63.         for (int i = 0; i < BOARD_SIZE; i++) {
  64.             TableRow tr = new TableRow(this);
  65.             tr.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
  66.  
  67.             tl.addView(tr);
  68.  
  69.             for (int j = 0; j < BOARD_SIZE; j++) {
  70.                 Button tv = new Button(this);
  71.                 tr.addView(tv);
  72.                 tv.setTextAppearance(R.style.TextAppearance_AppCompat_Title);
  73.                 tv.setText(DEFAULT_TEXT);
  74.  
  75.                 List<Counter> xs = new ArrayList<>(Arrays.asList(xRow[i], xCol[j]));
  76.                 List<Counter> os = new ArrayList<>(Arrays.asList(oRow[i], oCol[j]));
  77.  
  78.                 if (i == j) {
  79.                     xs.add(xDiag[0]);
  80.                     os.add(oDiag[0]);
  81.                 } else if (i + j == BOARD_SIZE - 1) {
  82.                     log(String.format("(%s, %s)", i, j));
  83.                     xs.add(xDiag[1]);
  84.                     os.add(oDiag[1]);
  85.                 }
  86.  
  87.                 tv.setOnClickListener(new CellListener(xs, os, turnToggle));
  88.             }
  89.         }
  90.     }
  91.  
  92.  
  93.     private class Counter {
  94.  
  95.         private int val = 0;
  96.  
  97.         public int value() { return val; }
  98.  
  99.         public void increase() { val++; }
  100.     }
  101.  
  102.     private class Toggle {
  103.  
  104.         private boolean val = false;
  105.  
  106.         public boolean state() {
  107.             return val;
  108.         }
  109.  
  110.         public void toggle() {
  111.             val = !val;
  112.         }
  113.     }
  114.  
  115.     private class CellListener implements View.OnClickListener {
  116.  
  117.         private List<Counter> xs, os;
  118.         private Toggle toggle;
  119.  
  120.         public CellListener(List<Counter> xs, List<Counter> os, Toggle toggle) {
  121.             this.xs = xs;
  122.             this.os = os;
  123.             this.toggle = toggle;
  124.         }
  125.  
  126.  
  127.         @Override
  128.         public void onClick(View v) {
  129.             List<Counter> counterList = toggle.state() ? xs : os;
  130.             String name = toggle.state() ? "X" : "O";
  131.  
  132.             Button tv = (Button) v;
  133.  
  134.             if (tv.getText() != DEFAULT_TEXT) return;
  135.             else tv.setText(name);
  136.  
  137.  
  138.             for (Counter c : counterList) {
  139.                 c.increase();
  140.  
  141.                 if (c.value() == BOARD_SIZE) {
  142.                     log(name + " wins!");
  143.                     break;
  144.                 }
  145.             }
  146.  
  147.             toggle.toggle();
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement