## Adventures in Learning to Code: Sudoku Edition (Part II)

In part I, I explained how I’m attempting to write a webapp that can solve simple Sudoku puzzles. Since then, I’ve made some progress. This series will track what I’ve been working on, learning, and any difficulties I’m encountering.

With a goal in mind, it was time to just dive in and go.

## Step 1: Oh God, what is this even…

At first, it was kind of overwhelming. Where do I even start? How the heck am I going to even tackle any of this?

After learning the basics of Javascript loops, functions, arrays, and objects, thanks to Codecademy, I was mostly ready to go. What was my real first step? How the heck do I even manipulate data on a webpage, let alone collect and analyze it?

After a few tries, I had a simple project featuring a table (with cells individually labeled) up and running!

After some more tinkering, I was able to get the “Generate Numbers!” button to actually randomly generate the numbers 1 through 9 and fill in the cells without repeating any numbers. Sweet!

Hey, that’s kind of cool. Now we’re getting somewhere! I think I was ready to take it to the next level.

## Step 2: Okay… I guess we need to make a fake Sudoku grid.

There’s probably simpler ways to go about doing this, but I ended up spending a lot of time figuring out how to build a Sudoku grid in the first place. Hey, I wanted this project to look somewhat presentable when I showed it off and (hopefully) finished it.

HTML tables were driving me crazy, so I just straight up stole this helpful thing after Googling for an answer. It looks pretty good. Pretty, pretty, pretty good.

Wow, sir. That looks so good that I’d like to take two!”

Alright, one mission accomplished. Now onto another mission. How do I completely fill out the entire grid? I started with this, because I wanted to understand how to try to isolate certain sections of the Sudoku grid and see if I could create a series of non-duplicated random numbers.

Why did I start with randomly generating numbers? I initially thought I would just try to brute force solve some simple beginner’s level puzzles, but I later realized how ridiculous this would be. There’s a ridiculous number of possible permutations to try.

For now though, I decided to start filling out random numbers by row. This is what my initial code looked like.

[javascript]
function fillTable(row) {
for (var i = 1; i <= maxColumns; i++) {
var selectCell = row + i;
//console.log(selectCell);
document.getElementById(selectCell).innerHTML = fillCell(selectCell);
if (i == maxColumns && row != maxRow) {
i = 0;
row = nextChar(row);
} else if (i == maxColumns && row == maxRow) {
break;
}
}
}
[/javascript]

Did it work? Yes! I ended up with grid of random numbers filled out! Sure, there’s really no rhyme or reason for where numbers ended up, but it certainly looks like a completed Sudoku puzzle, right?

## Step 3: Let’s put some real numbers in there

Alright, entering a bunch of random numbers into a grid is pretty fun for all of 5 minutes. (To be honest, it was pretty fun) But to take this to the next level, I really needed to have the beginning of a Sudoku puzzle in there.

A few quick Google searches later, and I found a suitable candidate! One thing I decided early on was to store all this data in an object — mostly so I could easily update it with different puzzles later on. It ended up looking like this.

[javascript]
var allCells = {
a1: "", a2: 1, a3: "", a4: 6, a5: "", a6: 4, a7: 3, a8: "", a9: 7,
b1: 3, b2: 5, b3: 6, b4: "", b5: "", b6: "", b7: "", b8: "", b9: "",
c1: "", c2: "", c3: "", c4: "", c5: 5, c6: 3, c7: 6, c8: 9, c9: "",
d1: "", d2: 8, d3: 3, d4: 2, d5: 6, d6: "", d7: 4, d8: "", d9: 9,
e1: "", e2: "", e3: "", e4: "", e5: "", e6: "", e7: "", e8: "", e9: "",
f1: 4, f2: "", f3: 5, f4: "", f5: 7, f6: 8, f7: 2, f8: 6, f9: "",
g1: "", g2: "", g3: "", g4: "", g5: "", g6: "", g7: "", g8: "", g9: "",
h1: "", h2: "", h3: "", h4: "", h5: "", h6: "", h7: 7, h8: 2, h9: 4,
i1: 7, i2: "", i3: 9, i4: 4, i5: "", i6: 2, i7: "", i8: 8, i9: ""
};
[/javascript]

On top of this, I wanted to easily differentiate the initial numbers that were created on a brand new board (so I could easily see what my script was generating, versus what was already in place on the board). I wrote a function to modify the DOM and change the font weight and background color of the starting cells.

[javascript]
function setupBoard(row) {
var row = row;
var cellValue = 0;
for (i = 0; i < maxColumns; i++) {
//console.log("ROW: " + row + (i+1));
cellValue = allCells[row+(i+1)];
document.getElementById(row + (i+1)).innerHTML = allCells[row+(i+1)];

// Just highlighting what cells we initially started with.
if (Number(cellValue) > 0) {
document.getElementById(row + (i+1)).style.fontWeight = "bold";
document.getElementById(row + (i+1)).style.backgroundColor = "#F2F2F2";
} else {
// Use this to count up total number of empty cells that we need to solve for
// The idea is that we can use this to detect if we’re stuck
emptyCells++;
}
}
}
[/javascript]

You’ll notice I have a lot of console.log() calls commented out. I liberally used these all over the place so I could make sure things were working correctly. Anyway, once all that was said and done, it ended up generating a board that looked like this.

Wow, we’re starting to chug along pretty nicely! Next time, I’ll talk about the million different functions I created in trying to solve this.

## Adventures in Learning to Code: Sudoku Edition (Part I)

This series of posts will track what I’ve been working on, new things I’ve been learning, and any difficulties I’m encountering along the way. Buckle up!

One of the things I resolved to do this year was spend a bit more time learning to code. Sure, sure, I’ve dabbled in things like PHP and have used MySQL to build some pretty basic websites and tools.

Even with the little I’ve done, I’ve always enjoyed it and have found myself losing track of time while trying to solve some problem or just get this certain thing to work. It reminds me of a similar thing I’ve encountered with the addictive nature of Sid Meier’s Civilization games.

“Just one more turn…”

Late last year, I mentioned to a coworker my desire to spend a bit more time learning various techniques and theory. He suggested that if I’m serious, I should really just start trying to tackle some problems and we could go over it together.

The first thing he suggested? I should build a application to solve a Sudoku puzzle.

What??!

As tends to happen, the end of the year got a bit hectic and I put this project on the backburner. When I originally considered it, I was going to try and write it in PHP. However, I recently saw this goofy map of popular programming languages by state and thought, “I’ve never really done anything with Javascript. Maybe I should try that.”

So, that’s what I’ve been doing the last few weeks. Between taking some awesome online courses through Codecademy and Udemy, as well as reading up on various books, I’ve been trying to really get into it.

How am I doing? I’ll try to share my progress in future updates.

## GIFtv – Turn any display into a GIF picture frame

Our office has an empty TV that isn’t being properly utilized at the moment. It’s definitely a #FirstWorldProblem, but it basically boils down to the fact that we have multiple displays that act as dashboards for our various services and properties.

Anyway, we had a spare television available to show whatever we wanted. THE ENTIRE INTERNET WAS OUR OYSTER and what better way to put it to use than to display a random assortment of animated GIFs.

Our troubles arose when we realized we had to manually change out the animated GIFs, as well as zoom in in via the attached machine’s web browser so that it would properly display images.

Annoying, right?

## The Solution

Cue superhero music

Some Javascript, CSS, and PHP came the the rescue!

We’re using CSS to set a background image (based on one of our animated GIFs) that fills up the entire display. Depending on the settings of your viewport, the image has the possibility of being severly cropped, but hey, whatever!

We’re using some Javascript calls (thank you so much, Stackoverlow) to display a new animated GIF every 20 seconds or so (or really, whatever time you want, but I’m using 20 seconds), as well as build an array of animated GIFs from our images folder.

Which brings us to PHP. We’re using PHP to scan the contents of the folder and then using some Jquery calls to periodically call our PHP script to rescan and rebuild the image array — this means you can drop new animated gifs into the /images folder (or remove them, but why would you even) and it will automagically add them to the Javascript array for random consideration.

Yes? Yes!

## How to use

Upload this script to your favorite web server that supports PHP. Drop images (of the animated GIF variety) into the /images folder. Open up this page in your favorite browser on your favorite device. Cool? Cool!

## Demo time

So, what the heck does this thing look like? You can see it in action right here. (Make sure to set your browser to full screen presentation mode so you get the right effect).

## To-do’s

• Better error handling
• Figure out how to get Dropbox integration working so the whole company can get in on the fun

## Soft ‘g’ or hard ‘g’?

Hard G, dudes. It’s JIFF! I’m not wrong. You’re wrong.

## Special Thanks

Thanks to a few people who helped me try and figure this out.

• Lydia Katsamberis (@llkats)
• Jon Ursenbach (@jonursenbach)

## Quick email generator for contests and giveaways using PHP

tl;dr: Quickly generate multiple emails that you can send from your favorite mail client using mailto links and parsing data from various text files. (email addresses, email template, and optional redemption codes)

# Background

Recently, I had to contact 50 readers by email for a giveaway that we were running on our site. We’ve run similar sorts of events before and it’s always a tedious experience. It involves something like this:

1. Get email address from the list of winners.
2. Write up some sort of email template.
3. Go through and copy paste template into new email, and then copy and past the email address from list of winners.
4. Oh, don’t forget to copy and paste the subject line: “Dude, you’ve won FOO! Nice work!”
5. Oh, this particular giveaway features a unique code for each winner. Make sure you copy and paste that, too!
6. After all this copy and pasting, send the email.
7. Repeat this process until you’ve gone through the list of winners. Look at a clock and realize this has taken all afternoon.
8. “Oh, man,” you think to yourself, “there’s got to be a better way.”

# The Better Way

There is! Just download this project to your favorite PHP compatible server (or run an app like MAMP on your machine) and open up the “make_email.php” script.

There are a few required files:

• emails.txt – This is the list of email addresses you wish to send emails to. Each email address should be on a new line. No commas or funny stuff separating them, please. I didn’t build in any sort of fancy error checking.
• body.txt – This is the meat and potatoes. This is the message you’re going to want to send over and over and over and over. Sweet! One particularly important note: If there is some sort of unique redemption code or URL that winners will need to go to, you can add in ‘%code%’ (without quotes) to your email. The script will then automatically fill in unique codes when it generates the email (should you choose that options).
• codes.txt – If you have a whole list of redemption codes or URLs, this is where you put them. Like emails, each code or URL needs to be on a separate line.
That’s it! You might be wondering how you set the subject line of the email. Right now, it’s a variable at the top of the script. Just change that and you’ll be good to go!

Once you have these text files filled out, run the script, choose whether you’re generating emails with or without redemption codes and then fire away!

You can now click through the list and start firing off emails like you’re your own spambot. Except you’re giving people legitimately cool things. And you’ll still be able to enjoy your afternoon.

If you’re interested in checking it out, you can download the source code on Github.