473,472 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 925

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 error: "ComeauTest.c", line 19: error: type name...
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 Python, use the “sort” method. For example: ...
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 Python tutorial 8 months ago, have spent 30...
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 totals. These totals use unbound control boxes and...
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 the game of Tic Tac Toe # The grid...
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 to play tic-tac-toe. The program needs to have...
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: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.