Tic-Tac-Toe in Javascript

I wrote a simple Tic-Tac-Toe game in Javascript. It was an extension of some kind of Javascript tutorial that didn’t include the game logics. It uses the standard Javascript DOM functions, no external library.

Here is the main javascript code, the HTML page that setup the game board can be found at the Github link:


/* Setup tic-tac-toe board */
var naughts = 0;
var crosses = 0;
var PlayerEnum = Object.freeze({ NAUGHTS: "Naughts", CROSSES: "Crosses" });
var currentPlayer = PlayerEnum.CROSSES;

var cells = document.querySelectorAll('.tictactoe-cell');
var currentPlayerSpan = document.querySelector('#currentPlayer');
currentPlayerSpan.textContent = currentPlayer;

for (i = 0; i < cells.length; i++) {
    var cell = cells[i];
    cell.addEventListener('click', function(e) {
        if (this.textContent === '') {
            var cellValue = parseInt(this.getAttribute('data-cell'));
            if (currentPlayer == PlayerEnum.CROSSES) {
                this.textContent = 'X';
                crosses += cellValue;
                if (checkWinner(crosses)) {
                    alert("Crosses WIN!");
                    reset();
                } else {
                    currentPlayer = PlayerEnum.NAUGHTS;
                }
            } else if (currentPlayer == PlayerEnum.NAUGHTS) {
                this.textContent = 'O';
                naughts += cellValue;
                if (checkWinner(naughts)) {
                    alert("Naughts WIN!");
                    reset();
                } else {
                    currentPlayer = PlayerEnum.CROSSES;
                }
            }

            currentPlayerSpan.textContent = currentPlayer;
        };
    });
}

function reset() {
    for (i = 0; i < cells.length; i++) {
        var cell = cells[i];
        cell.textContent = '';
    }
    currentPlayer = PlayerEnum.CROSSES;
    naughts = 0;
    crosses = 0;
    currentPlayerSpan.textContent = currentPlayer;

};

var resetButton = document.querySelector('#reset');
resetButton.addEventListener('click', reset);

function checkWinner(array) {
    winningPattern = 1 + 2 + 4;
    if (checkPattern(array, winningPattern)) return true;
    winningPattern = 8 + 16 + 32;
    if (checkPattern(array, winningPattern)) return true;
    winningPattern = 64 + 128 + 256;
    if (checkPattern(array, winningPattern)) return true;
    winningPattern = 1 + 8 + 64;
    if (checkPattern(array, winningPattern)) return true;
    winningPattern = 2 + 16 + 128;
    if (checkPattern(array, winningPattern)) return true;
    winningPattern = 4 + 32 + 256;
    if (checkPattern(array, winningPattern)) return true;
    winningPattern = 1 + 16 + 256;
    if (checkPattern(array, winningPattern)) return true;
    winningPattern = 4 + 16 + 64;
    if (checkPattern(array, winningPattern)) return true;

    return false;
}

function checkPattern(array, pattern) {
    return ((array & pattern) === pattern);
}

Electronics Tutorial: Current, Voltage, Resistance

This post will attempt to introduce you to the basic concepts that make electronic circuits tick. Hopefully, the basic knowledge from this post will help you understand more complex concepts down the line. This post is not for you if you already have a grasp of the basic concepts of current, voltage, and resistance. I will attempt to introduce the basics without getting into complicated science and maths unless it is otherwise necessary.

Without further ado, let’s jump on in and explore the basic science behind electronics.

Read more

Experiment: 2 stage common emitter amplifier

I’m almost completely clueless regarding transistor circuits over and above using it as a switch. Before delving into the books and learning the theory and application of transistors, I decided to build a simple amplifier circuit, tweak it, and just test it out to see how it performs. Here follows what I ended up with.

Read more

The Arduino Mega 2560

The Arduino will be the primary microcontroller platform that I will use for my electronics projects. The Arduino is an excellent platform with different boards and Atmel microcontrollers to suit most hobby needs. My personal board of choice is the Arduino Mega2560 and my posts will focus on that board but should in most cases be compatible with other Arduino boards as well.

  • Based on ATmega2560
  • 54 digital input/output pins incl 15 PWM outputs
  • 16 analog inputs
  • 4 Hardware serial ports (UARTs)
  • 16 Mhz Crystal Oscillator
  • USB connection and power jack
  • ICSP header
  • 256KB flash memory (8KB used by bootloader)
  • 8 KB SRAM
  • 4 KB EEPROM

GET ONE NOW

Or GET FIVE NOW for a comparible price:

AliExpress.com Product – Mega2560 R3 ATmega2560-16AU (1pcs Board +1pcs USB Cable) 5 Sets Free Shipping With tracking number

Development support

Development tools for Arduino is free! Just head on down to http://arduino.cc/ for more.

Connecting an Arduino to a 7 segment display – part 2: reducing microcontroller pins

connecting an arduino to a seven segment display

In my previous post, I showed how to connect seven segment displays to your Arduino microcontroller and how to drive them through multiplexing to display numerical values from your program.
In this post, I will build on that circuit and reduce the number of pins used on your microcontroller, as promised.
The circuit so far had 12 microcontroller pins used up to drive the seven segments (and decimal point) and select each individual digit display. What if you can reduce the number of pins to just 3? Easy! We can do this using shift registers.

Read more