<repo_name>skylurker/learningAndroid<file_sep>/README.md
Tictactoe: Going through this Habrahabr tutorial http://habrahabr.ru/blogs/android_development/110247/


<file_sep>/Tictactoe/app/src/main/java/com/mycompany/tictactoe/WinnerCheckerVertical.java
package com.mycompany.tictactoe;

/**
 * Created by skylurker on 10.07.15.
 */

/**
 * This one checks whether we have a winner who's filled in a vertical line.
 */

public class WinnerCheckerVertical implements WinnerCheckerInterface {

    private Game game;

    // constructor
    public WinnerCheckerVertical (Game game) {
        this.game = game;
    }

    public Player checkWinner() {
        Square[][] field = game.getField();
        Player currentPlayer; // player whose sign is in the current cell
        Player previousPlayer = null; // player whose sign was in the previous cell

        for (int i = 0, rowsTotal = field.length; i < rowsTotal; i++) {
            previousPlayer = null; //there is no "previous cell", this one is the first in the column
            int successCounter = 1;

            for (int j = 0, colsTotal = field[i].length; j < colsTotal; j++) {
                // check which player's sign is in the cell
                // it's field[j][i] because we're walking down the columns
                // it doesn't spoil things much since we have a square matrix all the same
                currentPlayer = field[j][i].getPlayer();

                // if the current cell contains the same sign as the previous (which does exist) and both are not empty...
                if (currentPlayer == previousPlayer && (currentPlayer != null && previousPlayer != null)) {
                    // ...then increase the counter value
                    successCounter++;

                    // if all the cells in the column are filled in by the same player, e.g. they all contain the same sign...
                    if (successCounter == rowsTotal) {
                        // then this player is the winner
                        return currentPlayer;
                    }
                }

                // before proceeding to check the next cell, set the current one to the "previous" state
                previousPlayer = currentPlayer;
            }
        }

        // if there is no winner
        return  null;

    }
}
<file_sep>/Tictactoe/app/src/main/java/com/mycompany/tictactoe/Square.java
package com.mycompany.tictactoe;

/**
 * Created by skylurker on 09.07.15.
 */
public class Square {
    private Player player = null;

    // fill the square with the player's 'signature'
    public void fill(Player player) {
        this.player = player;
    }

    /** TODO:
     * Check whether there is a short if-then-else, such as
     * player != null ? return true : return false
     * or maybe
     * player == null ? return false : return true
     * Also check if the method works properly; it looks
     * like there should be the ELSE keyword, shouldn't it
     * AHHHH I forgot that the RETURN statement ensures the
     * rest of the code gets omitted
     * @return
     */
    public boolean isFilled() {
        if (player != null) {
            return true;
        }
        return false;
    }

    public Player getPlayer() {
        return player;
    }
}
<file_sep>/Tictactoe/app/src/main/java/com/mycompany/tictactoe/MainActivity.java
package com.mycompany.tictactoe;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    private TableLayout layout;
    private Game game;
    private Button[][] buttons = new Button[3][3];


    //constructor
    public MainActivity() {
        game = new Game();
        game.start();
    }

    // build the game field: get the game field and populate it with buttons
    private void buildGameField(){
        // get the field made of cells aka Squares
        Square[][] field = game.getField();

        for (int i = 0, rowsTotal = field.length; i < rowsTotal; i++ ) {
            // create a table row
           TableRow row = new TableRow(this);
           for (int j = 0, colsTotal = field[i].length; j < colsTotal; j++) {
               // assign a button to the cell
               Button button = new Button(this);
               buttons[i][j] = button;
               // set a click listener
               button.setOnClickListener(new Listener(i, j));
               //add the button View to the row
               row.addView(button, new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
               /** TODO:
                * un-hardcode the button widths and heights
                */
              // button.setWidth(107);
               button.setHeight(107);


           }
            // add the row View to the table
            layout.addView(row, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
        }
        layout.setStretchAllColumns(true);
    }

    public class Listener implements View.OnClickListener {
        private int x = 0;
        private int y = 0;

        //constructor
        public Listener (int x, int y) {
            this.x = x;
            this.y = y;
        }

        public void onClick(View view) {

            Button button = (Button) view;
            Game g = game;
            Player player = g.getCurrentActivePlayer();

            // if a player made his move, set his sign into the cell
            if (g.makeTurn(x, y)){
                button.setText(player.getName());
            }

            // check whether there is a winner
            Player winner = g.checkWinner();

            // if there is a winner, congrats!
            if (winner != null) {
                gameOver(winner);
            }

            // if there is no winner, but the field is already filled in completely
            if (g.isFieldFilled()) {
                gameOver();
            }
        }
    }

    // gameOver(Player player) and gameOver() are not the same!

    private  void gameOver(Player player) {
        CharSequence text = "Player \"" + player.getName() + "\" won!";
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
        game.reset();
        refresh();
    }

    private void gameOver() {
        CharSequence text = "Draw!";
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
        game.reset();
        refresh();

    }



    private void refresh() {
        Square[][] field = game.getField();

        for (int i = 0, rowsTotal = field.length; i < rowsTotal; i++) {
            for (int j = 0, colsTotal = field[i].length; j < colsTotal; j++) {
                if (field[i][j].getPlayer() == null) {
                    buttons[i][j].setText("");
                } else { // no idea why do I need this one
                    buttons[i][j].setText(field[i][j].getPlayer().getName());
                }
            }
        }
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        layout = (TableLayout) findViewById(R.id.main_layout);
        buildGameField();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
<file_sep>/Tictactoe/app/src/main/java/com/mycompany/tictactoe/WinnerCheckerDiagonalLeft.java
package com.mycompany.tictactoe;

/**
 * Created by skylurker on 10.07.15.
 */

/**
 * This one checks whether we have a winner who's filled in a vertical line.
 */

public class WinnerCheckerDiagonalLeft implements WinnerCheckerInterface {

    private Game game;

    // constructor
    public WinnerCheckerDiagonalLeft (Game game) {
        this.game = game;
    }

    public Player checkWinner() {
        Square[][] field = game.getField();
        Player currentPlayer; // player whose sign is in the current cell
        Player previousPlayer = null; // player whose sign was in the previous cell
        int successCounter = 1;

        for (int i = 0, length = field.length; i < length; i++) {

                // check which player's sign is in the cell
                currentPlayer = field[i][i].getPlayer();


                if (currentPlayer != null) {
                    // if the current cell contains the same sign as the previous one...
                    if (currentPlayer == previousPlayer) {
                        // ...then increase the counter value
                        successCounter++;

                        // if all the cells in the row are filled in by the same player, e.g. they all contain the same sign...
                        if (successCounter == length) {
                            // then this player is the winner
                            return currentPlayer;
                        }
                    }
                }

                // before proceeding to check the next cell, set the current one to the "previous" state
                previousPlayer = currentPlayer;

        }

        // if there is no winner
        return  null;

    }
}
