Internet Computer Mobile Games

Help with MiniMax Algorithm for Tic Tac Toe

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 areas . 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

Top similar posts to Help with MiniMax Algorithm for Tic Tac Toe

Tic Tac Toe

I got everything until to the point that check if the space is emprty or not. I dont know how to do it. can someone give me an example? ...

Cat Dog Toe Halloween – Tic Tac Toe [Halloween Free Game]

Hello I would like to show you my new game for Android. “Cat Dog Toe Halloween”, Tic Tac Toe Halloween Free Game. Below the links to the game......

Tic tac toe game

If I wanted to make a tic tac toe game using purely HTML, CSS, and javascript where would I start? Just for the sake of learning I want to make a simple 1 player game that plays against the program....

Winning Lines for Tic-Tac-Toe (C programming)

The Question Winning Lines Tic-tac-toe is a game played by two players o and x. A player wins when he/she succeeds in placing three respective marks in a horizontal, vertical or diagonal row. Given a 3-by-3 tic-tac-toe board, a winning line is a line... Winning Lines for Tic-Tac-Toe (C programming)...

Implementing Tic Tac Toe on iOS

An implementation of Tic Tac Toe on iOS...

Implementing Tic Tac Toe on iOS

An implementation of Tic Tac Toe on iOS...

Help with Tic Tac Toe

Im trying to code a tic tac toe game in C language. Human vs Human not Human vs Computer and im having some trouble with the loop. It goes only 1 time into the loop i have a do while statement. Can anyone help me out here ? Code: #include<stdio.h> int X,i,sum,sym; char board[10] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; int drawboard() {                 printf("\n");printf("\n");           &...

iPhone Instant Games - the Widget: Tic Tac Toe

Hi there! Check out the Tic Tac Toe game widget for today view. You will play against unpredictable robot for defense of the honor of humanity. appinfo_getByUrl("https://itunes.apple.com/nz/app/instant-games-the-widget/id935793735?ls=1&mt=8","all"); ...

Development Tic Tac Toe on PascalABC.NET

Hey! Sorry for my English, I am a student from Russia. I was given the assignment to write a tic tac toe with the algorithm "minimax" in PaskalABS.NET. But I am a newbie and just recently started to learn, so I know the basics of the language:(. must give the program at the end of November.:eek: Can anyone help online? ...

In need of some assistance with a Tic Tac Toe game I am working on

Hey there guys. I recently have been working on a tic tac toe game and at the moment I am stumped. You see the Tic Tac Toe board shows up fine, and you can place down X's and O's on all 9 cells. The only thing is that if you click the cell again it will switch to either and X or O and you can just keep doing it. Could anybody shed some light on this or shoot some help my way? http://jsfiddle.net/78pu4y7t/6/ Basically I am trying to think of a way to save the letter to the cell, but I can't figur...

In need of help for my Tic Tac Toe game I am working on

Hello everyone. I have a tic tac toe game I am working on and I managed to make it so you can place X's and O's respectively. My next problem is making a win condition so it checks to see if the X's won the game or the O's did. Here is what I have so far. Edit fiddle - JSFiddle What I am basically thinking is that when you are clicking I want whatever letter it is that is placed to be saved and then I can check to see if all 3 are equal to that letter. It is a bit hard to explain. Essentially I...

Roll-It Tic-Tac-Toe (First edition)

A new board game version has been added to the database: Roll-It Tic-Tac-Toe (First edition) ...

Introduction - Learning to Think Ahead - Tic Tac Toe, Checkers, and Connect Four

by Aaron Honsowetz & Austin Smokowicz The classic lecture series studies old board games to find lessons useful for the development of new board game.In this lecture I discuss how Tic Tac Toe, Checkers, and Connect Four teach young board game players to think ahead.http://drwictzboardgames.blogspot.com/2013/10/classics-lectu...Originally Published: October 18, 2013 ...

C++ move validity for tic tac toe

Hi, I am a newby and am surprised I made it this far. My program is a TicTacToe program. I am having troubles with checking the validity of each move. I have tried multiple things, but could not figure it out. If someone could help point me in the right direction, that would be much appreciated. #include <iostream> using namespace std; class ticTacToe { private: char board[3][3]; char player; int move; public: void initiate(); void postboard(); void input(); ...

Design Apple Development Security Automobile Network Photography Health Money Travel Shopping Issues Operating systems Drivers Software Programming Tech Home Science Sport Solution