472,325 Members | 1,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,325 software developers and data experts.

tictactoe (reprise)


"""
AUTHOR: Sean McIlroy
LANGUAGE: Python 2.5
OVERVIEW: instances of "tictactoeplayer" play optimal tictactoe
SPECIALIZED TYPES: player={ex,oh}; empty={blank}; cell=player+empty;
board=[cell]
TYPE RELATIONS: bool(c)==True for any c in cell
"""

ex, oh, blank = 'X', '0', ' '
linear = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],
[2,4,6]]
lines = lambda board: [[board[i] for i in x] for x in linear]
mover = lambda board: board.count(ex)==board.count(oh) and ex or oh
opponent = lambda player: player==ex and oh or ex
makesthreat = lambda player, cells: cells.count(blank)==1 and
opponent(player) not in cells
numthreats = lambda player, board: len([x for x in lines(board) if
makesthreat(player,x)])
haswon = lambda player, board: [player]*3 in lines(board)
marked = lambda board, index: [(board[i],mover(board))[i==index] for i
in range(9)]
display = lambda board: '\n\n' + '\n-+-+-\n'.join(['|'.join(board[3*i:
3*(i+1)]) for i in range(3)]) + '\n\n'
blankindices = lambda board: [i for i in range(9) if board[i]==blank]
isfinished = lambda board: blankindices(board)==[] or [ex]*3 in
lines(board) or [oh]*3 in lines(board)
numownthreats = lambda board: numthreats(mover(board),board)
numopponentsthreats = lambda board:
numthreats(opponent(mover(board)),board)
outcomevalue = lambda board: haswon(opponent(mover(board)),board) and
(-1) or haswon(mover(board),board) and (+1) or 0
assessment = lambda board: [outcomevalue(board),
(-1)*numopponentsthreats(board),(+1)*numownthreats(b oard)]
value = lambda board, index: blankindices(board) in [[],[index]] and
assessment(marked(board,index)) or \

min([assessment(marked(marked(board,index),i)) for i in
blankindices(board)if not i==index])
blankindexvalues = lambda board: [value(board,i) for i in
blankindices(board)]
analogisoptimal = lambda list1, list2, optimum: [x for (i,x) in
enumerate(list1) if list2[i]==optimum(list2)]
optimalblankindices = lambda board: blankindices(board) and
analogisoptimal(blankindices(board),blankindexvalu es(board),max)
optimalmoves = lambda board: [marked(board,i) for i in
optimalblankindices(board)]
centergrabs = lambda board: [marked(board,i) for i in [4] if i in
blankindices(board)]
cornergrabs = lambda board: [marked(board,i) for i in [0,2,6,8] if i
in blankindices(board)]
tictactoemove = lambda board: len(blankindices(board)) in [8,9] and
(centergrabs(board) or cornergrabs(board))[0] or \
optimalmoves(board) and
optimalmoves(board)[0] or isfinished(board) and board

class tictactoeplayer:
def __init__(self):
globals()['mark'] = self.mark
globals()['newgame'] = self.newgame
globals()['turn'] = self.turn
print 'ENTER mark(i) TO PLACE A MARK ON THE i-TH CELL'
print '123' + '\n' + '456' + '\n' + '789'
print 'ENTER newgame() TO START A NEW GAME'
print 'ENTER turn() TO GIVE UP YOUR TURN'
self.newgame()
def mark(self,offbyoneindex):
self.board = marked(self.board,offbyoneindex-1)
print 'your move:' + display(self.board)
self.turn()
def newgame(self):
self.board = [blank]*9
print 'new game:' + display(self.board)
def turn(self):
self.board = tictactoemove(self.board)
print 'my move:' + display(self.board)
Jun 28 '08 #1
0 866

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave | last post by:
Hello, Below, I have included a short program that does not compile. The problematic line is indicated with a comment. Comeau produces this...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in...
25
by: Xah Lee | last post by:
Python Doc Problem Example: gzip Xah Lee, 20050831 Today i need to use Python to compress/decompress gzip files. Since i've read the official...
1
by: John Baker | last post by:
HI again: I have made 2 posts on this issue, and for some reason still haven't got the right result. I have a form which contains a number of...
0
by: chandniashar | last post by:
This is my perl program for tictactoe. Can anyone help me run the program as a cgi script? #!/usr/bin/perl # Description: This program implements...
9
by: wizardRahl | last post by:
Hello, I'm attempting to write a TicTacToe program for class and need some help with arrays. We have to write a program that will allow two users...
7
by: Warren Hoskins | last post by:
Old title: Homework Due 2-20-07 can"t understand why this will not compile. I've been working on tis all week end. Need Help desperately
1
by: racshah | last post by:
I have a tictactoe script with 2 users playing. I need a perl script in which one user plays with the computer. Can anyone help me with it???
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.