Monday, 14 March 2016

Trying to be clever

Since I have struggling so much with the Java 8 OCA I have trying to think laterally, how can I make myself a little brighter and improve my concentration. A lot of places list the usual get enough sleep, eat well etc, which is hard so I bought a guitar. Awaiting the cable to drive signal to the PC where I can learn to play through a game called Rocksmith. I've been impressed by the 60 day videos of gamers going from not picked up a guitar to knocking out paint it black at near perfect. The other site I have found useful is justinGuitar.

black electric guitar

The other things I am trying in my time off are pomodoro, I did not give the method a chance however with more time available I am willing to try it out for learning Java to take the OCA exam. The fitness thing is a tough one, fitbit has now been repaired and on route so at least I can get tracking my activity again.

Brushing up a Java a highly rated book was Exercises for programmers by Brian Hogan, it is similar to the daily programmer challenges with more of emphasis on learning, here is a task go make something, which is what I like. 

A5 size book with white cover and blue text

For Java learning I need something a little more in depth for what is used in industry, JPassion looks good for learnng as well as the London Java user group that is available remotely called JUG.

Friday, 4 March 2016

Bored and tic tac toe

Trying out of the work interview questions and knocked a simple text input tic tac toe game in 30 minutes ish. No problem I thought a simple front end and good to go, this took me a while but I finished, working on something for 10 minutes and getting distracted is not worth it.

intellij background with running tic tac toe program


package com.company;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;

public class TicTacToe {

    JButton[][] gameButtons;
    private char gameChar = 'X';
    private enum gameStatus {IN_PROGRESS, WON, DRAW}
    GameButton button = new GameButton();

    public TicTacToe() {
        //setup buttons
        gameButtons = new JButton[3][3];

        for (JButton[] row : gameButtons) {
            for (int i = 0; i < row.length; i++) {
                row[i] = new JButton();
            }
        }
    }

    public void startGame() {
        loadFrontEnd();
    }

    private void loadFrontEnd() {
        JFrame jframe = new JFrame("Tic Tac Toe");
        JPanel outer = new JPanel();
        outer.setLayout(new GridLayout(3, 3, 10, 10));

        for (JButton[] row : gameButtons) {
            for (JButton aButton : row) {
                aButton.addActionListener(button);
                outer.add(aButton);
            }
        }
        outer.setPreferredSize(new Dimension(300, 300));

        jframe.add(outer);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.pack();
        jframe.setVisible(true);
    }

    public char getAndFlipGameChar() {
        gameChar = gameChar == 'X' ? 'O' : 'X';
        return gameChar;
    }

    public gameStatus getGameStatus() {
        //check horizontal
        for (JButton[] row : gameButtons) {
            if (row[0].getText() != "") {
                if ((row[0].getText().equals(row[1].getText())) && row[0].getText().equals(row[2].getText()))
                    return gameStatus.WON;
            }
        }

        //check vertical
        for (int i = 0; i < gameButtons.length; i++) {
            if (gameButtons[0][i].getText() != ""
                    &&(gameButtons[0][i].getText().equals(gameButtons[1][i].getText()))
                    && (gameButtons[2][i].getText().equals(gameButtons[0][i].getText())))
                return gameStatus.WON;
        }

        //check diagonal
        if ( gameButtons[0][0].getText() != ""
                && (gameButtons[0][0].getText().equals(gameButtons[1][1].getText()))
                && (gameButtons[0][0].getText().equals(gameButtons[2][2].getText())))
            return gameStatus.WON;

        if ( gameButtons[2][0].getText() != ""
                && (gameButtons[0][2].getText().equals(gameButtons[1][1].getText()))
                && (gameButtons[0][2].getText().equals(gameButtons[2][0].getText())))
            return gameStatus.WON;
        //check no empties
        ArrayList<JButton> buttonList = new ArrayList<>(9);
        for (JButton[] row : gameButtons) {
            Collections.addAll(buttonList, row);
        }
        buttonList.removeIf( a -> a.getText() != "");

        if (!buttonList.isEmpty())
            return gameStatus.IN_PROGRESS;
        else
            return gameStatus.DRAW;
    }

    private void resetGame() {
        for (JButton[] row : gameButtons) {
            for (JButton aButton : row) {
                aButton.setText("");
            }
        }
    }

    class GameButton implements ActionListener {
        public void actionPerformed (ActionEvent e) {
            JButton theButtonPressed = (JButton) e.getSource();
            // if button not used write text to it
            if (theButtonPressed.getText().equals("") && getGameStatus() == gameStatus.IN_PROGRESS ) {
                theButtonPressed.setText( Character.toString(getAndFlipGameChar()));
            }

            gameStatus currentStatus = getGameStatus();
            if (currentStatus != gameStatus.IN_PROGRESS) {
                String conclusionMessage = currentStatus == gameStatus.WON ? gameChar + " won the game!" : "DRAW!!!";
                JOptionPane.showMessageDialog(new JFrame(), conclusionMessage);
                resetGame();
            }
        }
    }
}