Advertisement

Help with MiniMax Algorithm for Tic Tac Toe

Started by July 02, 2015 05:09 PM
3 comments, last by Dragoncar 9 years, 4 months ago

I am currently working on creating an AI player for tic tac toe. After researching, I discovered that the minimax algorithm would be perfect for the job.

I am pretty confident in my understanding of the algorithm and how it works, but coding it has proven a little bit of a challenge. I will admit, recursion is one of my weak areastongue.png . The following code is my AI class. It currently runs, but it makes poor decisions. Could someone please point out where I went wrong? Thank You!


import tictactoe as tic   # interface to tictactoe game logic like check_victory

class AI:
	def __init__(self, mark):
		self.mark = mark

	def minimax(self, state, player):
		#end condition - final state
		if tic.check_victory(state):
			if player == self.mark:
				return 1
			else:
				return -1
		if tic.check_cat(state):
			return 0

		nextturn = tic.O if player == tic.X else tic.X
		#generate possible moves
		mvs = []
		for i, mark in enumerate(state):
			if mark == tic.EMPTY:
				mvs.append(i)

		#generate child states of parent state
		scores = []
		for mv in mvs:
			leaf = state[:]
			leaf[mv] = player
			result = self.minimax(leaf, nextturn)
			scores.append(result)
		
		if player == self.mark:
			maxelle = max(scores)
			return mvs[scores.index(maxelle)]
		else:
			minele = min(scores)
			return mvs[scores.index(minele)]

	def make_move(self, board, player):
		place = self.minimax(board, player)
		return place

When you discover a victory on the board, it is the turn of the loser, so chances are you want to return -1 if player==self.mark and +1 otherwise.
Advertisement

Unfortunatley, that was not the issue. I tried it, but it exhibited the same behavior. The AI will block winning moves, but when it has a winning move it does not capitalize on it.

Well, a perfect opportunity for you to figure out how to debug recursive calls. smile.png

I'm not familiar with Python so forgive me if i'm misunderstanding the code. It appears to me that your determining what moves will result in a victory or a loss not which is the best move to make. This is because it would seem that after you processed back to having the scores for your current set of moves (ie. the set of move the AI could make this turn) the scores will all be -1, 0 or 1 which will mean multiple moves will have the same score

This topic is closed to new replies.

Advertisement