File: readme.txt

package info (click to toggle)
pythoncard 0.8.2-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 8,452 kB
  • sloc: python: 56,787; makefile: 56; sh: 22
file content (113 lines) | stat: -rw-r--r-- 2,753 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
tictactoe shows off using a background image, the ImageButton widget and
background handlers.

Francois Granger sent the email below which you can adapt to the tictactoe
sample if you want a better computer opponent than Mr. Gumby.


-----Original Message-----
From: Francois Granger [mailto:fgranger@altern.org]
Sent: Tuesday, January 01, 2002 1:50 PM
To: Kevin Altis
Subject: TicTacToe



You published a TicTacToe game developped with PythonCard recently 
with some questions about a winning algorythm. Since I worked on the 
same subject for some time, I got a working brute force one. It needs 
cleaning. See below.

seq is the previous sequence of turns in the form [(i, j), (i, j), ...]
turn is the current turn numbered from 0

======================================
class ComputerAnal(Player):
	"""
	The computer analyse the board
	"""
	def move(self, seq, turn, warn = ""):
		"""
		Analytical seek for solution
		seq is a sequence of tuples representing previous moves.
		return a tuple i, j representing the next move.
		"""
		prompt = warn + "Player " + str(self.token) + " : "
		print prompt
		if turn == 0:
			i,j = 1,1
		elif turn == 1 and seq[0] != (1,1):
			i,j = 1,1
		elif turn == 1:
			i, j = 0,0
		else:
			b = [[0,0,0],[0,0,0],[0,0,0]]
			x = 0
			for move in seq:
				i, j = move
				if x == self.play:
					b[i][j] = 1
				else:
					b[i][j] = -1
				x = not x
			row = [0,0,0]
			col = [0,0,0]
			diag =[0,0]
			"""
			find any line where there are two similar 
                        tokens and third is empty.
			first for me so I play and win
			then for the other so I play and he can't win
			other wise at random.
			"""
			for i in range(3):
				row[i] = b[i][0] + b[i][1] + b[i][2]
			for j in range(3):
				col[j] = b[0][j] + b[1][j] + b[2][j]
			diag[0] = b[0][0] + b[1][1] + b[2][2]
			diag[1] = b[0][2] + b[1][1] + b[2][0]
			pass
			if diag[0] == 2:
				for i in range(3):
					if not b[i][i]:
						return i,i
			if diag[1] == 2:
				for i in range(3):
					if not b[i][2-i]:
						return i,2-i
			for i in range(3):
				if row[i] == 2:
					for j in range(3):
						if not b[i][j]:
							return i,j
			for j in range(3):
				if col[j] == 2:
					for i in range(3):
						if not b[i][j]:
							return i,j

			if diag[0] == -2:
				for i in range(3):
					if not b[i][i]:
						return i,i
			if diag[1] == -2:
				for i in range(3):
					if not b[i][2-i]:
						return i,2-i
			for i in range(3):
				if row[i] == -2:
					for j in range(3):
						if not b[i][j]:
							return i,j
			for j in range(3):
				if col[j] == -2:
					for i in range(3):
						if not b[i][j]:
							return i,j
			pass
			i,j = rand.randint(0, 2), rand.randint(0, 2)
		print i,j
		return i, j

======================================