Tuesday, January 17, 2017

I win my first noughts and crosses game!

Much excitement amongst regular readers here as to when I will play my first game against my AI noughts and crosses system.

Well, it just happened and I won playing X.

Here's the game-transcript.

---
CL-USER 22 >  (play-game 'X *initial-board* 'human 'rand-player)

Next to play:  X

     0 | 1 | 2
     3 | 4 | 5
     6 | 7 | 8

"Please enter an available number: " 8

     0 | 1 | 2
     3 | 4 | 5
     6 | 7 | X

--- Computer playing O now moves ---

     O | 1 | 2
     3 | 4 | 5
     6 | 7 | X


"Please enter an available number: " 6

     O | 1 | 2
     3 | 4 | 5
     X | 7 |


--- Computer playing O now moves ---

     O | 1 | 2
     3 | 4 | 5
     X | O | X

"Please enter an available number: " 2

     O | 1 | X
     3 | 4 | 5
     X | O | X

--- Computer playing O now moves ---

     O | 1 | X
     O | 4 | 5
     X | O | X


"Please enter an available number: " 1

     O | X | X
     O | 4 | 5
     X | O | X

--- Computer playing O now moves ---

     O | X | X
     O | 4 | O
     X | O | X


"Please enter an available number: " 4

Win for X

     O | X | X
     O | X | O
     X | O | X

Perhaps I shouldn't get too excited: here's my opponent - noting that ...
  • the parameter m is the 'mark' -  X or O
  • b is the evolving board, eg:  '(X 1 O O 4 5 X 7 X).

(defun rand-player (m b)
   (let* ((remaining-numbers (remove 'O (remove 'X b)))
          (n                 (length remaining-numbers))
          (random-index      (random n))  )
      (nth random-index remaining-numbers))  )

The considerably more difficult task of getting the tree-search system debugged I'll leave for tomorrow!

---

You may have noticed my poor fourth move. OK, I was toying with it.

---

Update Wednesday 18th January.

To Roy's comment below: yes, it's random. I used it to test the game-playing framework and user interface. It is rather spooky and apparently cunning to play against - hard to second-guess 😏.

Today I've been working on the tree-generation algorithm and minimax. Probably a session or two away from the first capable AI player.

1 comment:

  1. So the machine was using Random play?

    I wonder whether the ideal here is to (generally) lose rather than win. In that case you have made a system better than you...

    ReplyDelete

Comments are moderated. Keep it polite and no gratuitous links to your business website - we're not a billboard here.