Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.lab1.tictactoe;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- 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 java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class TicTacToe extends AppCompatActivity {
- private static final int BOARD_SIZE = 3;
- private static final String DEFAULT_TEXT = "?";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_tic_tac_toe);
- generateLayout((ViewGroup) findViewById(R.id.ticTacToeLayout));
- }
- private void log(String s) {
- Log.i("BOI", s);
- }
- private void generateLayout(ViewGroup parent) {
- TableLayout tl = (TableLayout) new TableLayout(this);
- tl.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
- parent.addView(tl);
- Counter[] xRow = new Counter[BOARD_SIZE];
- Counter[] oRow = new Counter[BOARD_SIZE];
- Counter[] xCol = new Counter[BOARD_SIZE];
- Counter[] oCol = new Counter[BOARD_SIZE];
- Counter[] xDiag = new Counter[2];
- Counter[] oDiag = new Counter[2];
- Toggle turnToggle = new Toggle();
- turnToggle.toggle();
- for (int i = 0; i < BOARD_SIZE; i++) {
- xRow[i] = new Counter();
- oRow[i] = new Counter();
- xCol[i] = new Counter();
- oCol[i] = new Counter();
- }
- for (int i = 0; i < 2; i++) {
- xDiag[i] = new Counter();
- oDiag[i] = new Counter();
- }
- for (int i = 0; i < BOARD_SIZE; i++) {
- TableRow tr = new TableRow(this);
- tr.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
- tl.addView(tr);
- for (int j = 0; j < BOARD_SIZE; j++) {
- Button tv = new Button(this);
- tr.addView(tv);
- tv.setTextAppearance(R.style.TextAppearance_AppCompat_Title);
- tv.setText(DEFAULT_TEXT);
- List<Counter> xs = new ArrayList<>(Arrays.asList(xRow[i], xCol[j]));
- List<Counter> os = new ArrayList<>(Arrays.asList(oRow[i], oCol[j]));
- if (i == j) {
- xs.add(xDiag[0]);
- os.add(oDiag[0]);
- } else if (i + j == BOARD_SIZE - 1) {
- log(String.format("(%s, %s)", i, j));
- xs.add(xDiag[1]);
- os.add(oDiag[1]);
- }
- tv.setOnClickListener(new CellListener(xs, os, turnToggle));
- }
- }
- }
- private class Counter {
- private int val = 0;
- public int value() { return val; }
- public void increase() { val++; }
- }
- private class Toggle {
- private boolean val = false;
- public boolean state() {
- return val;
- }
- public void toggle() {
- val = !val;
- }
- }
- private class CellListener implements View.OnClickListener {
- private List<Counter> xs, os;
- private Toggle toggle;
- public CellListener(List<Counter> xs, List<Counter> os, Toggle toggle) {
- this.xs = xs;
- this.os = os;
- this.toggle = toggle;
- }
- @Override
- public void onClick(View v) {
- List<Counter> counterList = toggle.state() ? xs : os;
- String name = toggle.state() ? "X" : "O";
- Button tv = (Button) v;
- if (tv.getText() != DEFAULT_TEXT) return;
- else tv.setText(name);
- for (Counter c : counterList) {
- c.increase();
- if (c.value() == BOARD_SIZE) {
- log(name + " wins!");
- break;
- }
- }
- toggle.toggle();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement