3 minute read

About 10 minutes before I was suppose to get to my computer the power went out in my subdivision. I quickly realized that while I had setup flashlights in strategic locations in our apartment, when we moved into our house I never did the same. My wife and I both did not have our phones on us and it was DARK. Needless to say I closed the baby gate at the top of the stairs to keep the little ones safe and made my way to the kitchen where I knew a flashlight was. While I was downstairs the power came back on and my daughter yelled out, “Thanks Daddy!” I’ll take credit, yes I will. This put me behind. I then found out that I hadn’t installed Zoom and couldn’t get into the Study Group I was planning on attending. I had already decided to “take the night off” and just do the study group and work on my Tic-Tac-Toe AI some. Needless to say 30 minutes later I confirmed Zoom was working.

Tonight I realized it’s amazing what coming at a project fresh can do. I figured out why my random move in Tic-Tac-Toe wasn’t working in less than 3 minutes tonight. I had an = instead of an ==. However, after that I couldn’t get into the groove. I wrote out a couple more methods to decide on the first and second move for the computer but then just left it as random moves after that. I did some TTT strategy ready and came up with the best first move is a corner and the best second move is the center. If the center is taken the best second move is a corner. So I built that logic into the game. From move 3 on the computer plays random. The computer never beat me, it did however, tie a few times with this little bit of knowledge. I have an idea though and want to see if I can pair with someone to implement it. Here’s my final code:

module Players
  class Computer < Player
    def move(board)
      if board.cells.count{|square| square != " " } == 0
        "1"
      elsif board.cells.count{|square| square != " " } == 1
        board.cells.find_index("X") == 4 ? "1" : "5"
      else
        possible_moves = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
        valid_moves = []
        possible_moves.each do |move|
          valid_moves << move if board.cells[move.to_i-1] == " "
        end
        valid_moves.sample
      end
    end
  end
end

I started on the next project which is a scraping project. I don’t have much practice with Nokogiri but on my first attempt I was pulling the data I needed. I can see this project might go quicker than the last two. I’m going to hit it hard tomorrow night and get it done.

Time spent today: 1:33
Time spent total: 101:24
Lessons completed today: 1
Lessons completed total: 284

comments powered by Disqus