Search This Blog

Sunday, May 14, 2017

Tic Tac Toe (Python)




I've been working on Python and wanted to challenge myself. I had wanted to make a Tic Tac Toe game for a while, so I decided to give it a try.


The first thing I had to figure out were the rules of Tic Tac Toe as a computer would need to understand them. Here's what I came up with, in no particular order:


1) X's go first, then O's, then X's again, and so on until someone wins or the game ties.

2) If you go first, take the middle spot.

3) Try to get 3 of your symbol in a row.

4) Prevent your opponent from getting 3 of their symbols in a row.

5) You cannot place a symbol on an already occupied space.

6) The maximum amount of moves in a game is 9: 5 X's, 4 O's.

7) If you get 3 of your symbol in a row, the game is over and you've won.

8) If your opponent gets 3 symbols in a row, the game is over and your opponent has won.

9) If neither you nor your opponent get 3 symbols in a row, the game over and tied.


Then next thing I had to figure out was a way for the computer to keep track of what symbols were where, and which spaces were free. To do this, I assigned a letter to each spot on the grid:


A | B | C

----------

D | E | F

----------

G | H | I


To keep track of the game-play, I used arrays: one for the overall game-play, one for the computers moves, and one for the humans moves. So one game might look like this: [E,A,B,C,D,F,H] with X taking [E,B,D,H] and O taking [A,C,F] for a win for X.


To check for a win, I determined which combinations of taken places resulted in a win (there's 8 combinations). Then I had the computer check each players arrays against those arrays after every turn. If a player had any combination of spaces used by a winning combination, the game would end.


Now, it was easy enough to have the computer just play random spots (if they weren't taken), but a human player doesn't do that, they strategize. Unfortunately, I had no idea how to make the computer observe the field and determine (using algorithms) the best move to make. What I did know, was that it could carry out preprogrammed moves. So my first thought was to program every possible game of Tic Tac Toe and have the computer determine which moves have already been made, compare that to the database, then make a move. It turns out there are 255168 (not including symmetries) games of Tic Tac Toe that can be played. So that idea was out. Then I thought, "why not make it learn from it's victories?" So I did. When the computer won or tied, it would save the gameplay, and refer to it at each stage of subsequent games. In summary, the computer's thought process would be:


1) Is there a winning move I can make right now? if so, make it. If not:

2) Is there a move I can make right now that will prevent the human from winning afterward? If so, make it. If not:

3) Do the current pieces in this game match a game I've won before? If so, what was the next move I made at that time? Make it. If not:

4) Do the current pieces in this game match a game I've tied before? If so, what was the next move i made at that time? Make it. If not:

5) Make a random move.


Then I programmed in the niceties: placing X's and O's, updating the playing field, saving the game-plays, and so on. The last thing I did was give the user the option of playing X's or O's. Now I'm a bit embarrassed, but my solution to this broke a fundamental rule of programming: Don't Repeat Yourself. Although I repeated myself all over the place, the quickest and easiest solution was to duplicate the entire game with the symbols swapped, then give the player the choice of which game to start. It's not a perfect program, but I accomplished what I set out to do which is the first step of any bit of code.

Tuesday, April 25, 2017

WeightLifts: Editing is fixed!

Fixed the issue with editing: update 2.1.0.4 is ready for download and contains the fix. Sorry for the trouble! 

Monday, April 24, 2017

Weightlifts: Editing is broken!!

Warning! I just found a glitch: editing an entry in a workout will cause your historical data to get overwritten! Avoid using the edit function until I fix it!

Sunday, March 26, 2017

WeightLifts Coming Update 2.1.0.2

I just finished working on the next updates for WeightLifts and will release it after about a week of testing (so around April 7th to 9th). See how I went from 2.0.0.0 to 2.1.0.2? In my testing so far I've not seen any issues going from 2.0.0.0 to 2.1.0.2 (I finally sorted out the issue of it erasing all the data when making changes to the memory structure, so future updates shouldn't do that either)

Update 2.1.0.2 will have the following changes:

- The "goals" section has been replaced with a nice looking line chart instead. 
- Bug fix: Exercise Info now updates immediately after changes have been made. 
- The photo on the Exercise Info screen can be swiped to view a small line chart of the exercise history. 
- Added the ability to reorder exercises within a workout
- Added the ability to rename and reorder workouts in the workouts screen. 
- Added a bunch of new exercises to the default roster which will be installed automatically with this update. EXISTING DATA WILL NOT BE OVERWRITTEN. If you already have an exercise with the exact same name as one of the ones being added, you won't notice any change. 



Saturday, February 11, 2017

WeightLifts Bug Fix: Statistics Search

I recently discovered a bug in the search feature of the statistics menu. When a user would search for an exercise, then tap the one they want, they would end up being taken to the statistics for a different exercise (the exercise they'd get taken to was actually the one at the same row number on the full list). I've got a fix ready, and it should be available in a few days. 

Saturday, December 10, 2016

WeightLifts bugs - Pickers and secondary muscle exercises

I discovered 2 bugs in WeightLifts:

1) the Muscle group pickers weren't being filled out when a user edits an exercise by going: "Workout" -> "Exercise" -> "Edit"

and 

2) Secondary exercises weren't being populated. 

I've got these both fixed, and will the updated should be out shortly. 


Sunday, November 13, 2016

WeightLifts Date Bug

I recently discovered a bug in WeightLifts that prevented entries made on single digit dates (so, November 7, November 8, etc.) from showing up properly in history and when exported. 

The cause of the bug was a change in the way dates were displayed after the update to Swift 3. 

I had set all dates to be formatted as "MMMM dd, yyyy" which meant that dates would show up as "November 07, 2016" for example. Note the "0" before the 7. After the update, however, Apple began removing the leading "0". Since I save all my date info as strings of text instead of date objects (I find it's easier to search for strings), when retrieving data (to populate previous workout info for example) the app was searching for "November 8, 2016" when the data was saved as "November 08, 2016" and thus not finding anything. 

I have corrected the bug, and will be releasing an update towards the end of the week (after some testing). This update will contain a little bit of code that will also go through and format all the date entries to the original double digit date style. For those of you with lots of entries, you may find slight delays when loading the app. I will remove this bit of code after 3 weeks, once everyone has updated.