Posted on

Android Amazing Tic Tac Toe pt2


andorid tic tac toe animations

 

Hi, in this part a have lot’s of stuff done. In the previous part we looked at how to animate shape drawing with Canvas. Let’s take a look at current version

 

Here’s today’s stuff.

andorid tic tac toe animations

What’s new is game board, touch event handling, ai and victory check. Blue X was AI

Game Board

Basically there’s 9 Rect objects separated by 4 lines. Using convenient Shape class from previous post I have a nice abstraction to draw any shape inside.

shape is an int: circle or x, rectangle to draw shape inside and fraction (0 to 1) for animating. Just using 1 will draw final shape which is great for using this method for animating and drawing static shape.

So square size is determined by screen size.

lineSize is separating line width. Now to actually calculate coordinats of those 9 rectangles takes bunch of horrible math, but it’s pretty simple.

And building those rects pays off when you want to handle touch input. There’s a method to check if point is inside this rect.

AI

AI I used from my very first TTT app, didn’t want to rebuild it, plus it works perfect. What I have is board represented by two-dimensional array of ints, where¬† 0 – empty, 1 – circle, 2 – x. AI checks for center first, if it’s not empty then tries to win then stop user from winning.

Goes for draft pretty well. I created a class which knows about a board and just by using makeDecision I get row/col to draw shape. Using same methods as for user then.

Victory Check

This one has its class as well, it just needs to check 8 variations of possible victories. Not that hard.  Drawing lines implemented partially yet.

 

One thing here to improve is design of course and brushes possibly. Circle looks like a hedgehog. In the next post there’s going to be FIrebase login/sign up, list of users, invite another user to play with push notifications etc. And current source code you can get here. Don’t forget to subscrbe, follow me on Twitter, Facebook and share with friends if you think they will benefit from it