Skip to content

A Python Tic-Tac-Toe game with an AI opponent that learns how to play via Q-learning (Reinforcement learning).

License

Notifications You must be signed in to change notification settings

jakobkhansen/qlearning-tictactoe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Q-learning Tic-tac-toe

A Python Tic-Tac-Toe game with an AI opponent that learns how to play via Q-learning (Reinforcement learning).

Usage

There are three scripts in the root repository, human-vs-ai.py, human-vs-human.py and train-only.py. These are pretty self-explanitory. human-vs-ai.py is the main way of playing against the AI, this script first trains the AI by letting two AI's play against each other for 10000 games (modifiable), then lets you play against the AI.

You can also use human-vs-human.py for playing with two humans, but this was mostly used for testing purposes, and is not really the point of this repo.

The learning process

The AI learns via a pretty standard Q-learning algorithm. The following is the rewards given for each action done by an AI:

WIN: 1
LOSE: -1
MOVE: 0

Since the AI learns by playing against itself, the code can be a bit unclean at certain points, this is because the two AI's need to learn from each others wins and losses. This was something I realised too late.

The AI plays close to perfect with the default settings, there might be edge-cases that it doesn't encounter while training. The chance of this happening could be decreased with more epochs, which can be modified in human-vs.ai.py.

About

A Python Tic-Tac-Toe game with an AI opponent that learns how to play via Q-learning (Reinforcement learning).

Topics

Resources

License

Stars

Watchers

Forks

Languages