forked from ayushv/entropy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyAI.py
174 lines (142 loc) · 3.63 KB
/
myAI.py
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# myAI.py
import sys
from random import random, choice
def printX(*message):
for msg in message:
sys.stderr.write(repr(msg) + ' ')
sys.stderr.write('\n')
N = int(raw_input())
ROLE = raw_input()
board = []
for i in range(0, N):
boardRow = []
for j in range(0, N):
boardRow.append('-')
board.append(boardRow)
def isGameOver():
for i in range(0, N):
for j in range(0, N):
if (board[i][j] == '-'):
return False
return True
## --------------------
def getPossibleOrderMoves(x, y):
possibleMoves = []
for iterator in range(x-1,-1,-1):
if board[iterator][y]=='-':
possibleMoves.append((iterator,y))
else:
break
for iterator in range(y-1,-1,-1):
if board[x][iterator]=='-':
possibleMoves.append((x,iterator))
else:
break
for iterator in range(x+1,N):
if board[iterator][y]=='-':
possibleMoves.append((iterator,y))
else:
break
for iterator in range(y+1,N):
if board[x][iterator]=='-':
possibleMoves.append((x,iterator))
else:
break
return possibleMoves
#returns x,y for next piece
def chaosAI(piece):
openSquares=[]
for x in xrange(N):
for y in xrange(N):
if board[x][y]=="-":
openSquares.append((x,y))
openSquares
return choice(openSquares)
#returns a,b,c,d -> move a,b piece to c,d : abhi random hai , isko machana h.
def orderAI():
capturedSquares=[]
for x in xrange(N):
for y in xrange(N):
if board[x][y]!="-":
capturedSquares.append((x,y,board[x][y]))
capturedSquares = sorted(capturedSquares, key=lambda t:(t[0],t[1]))
while(True):
fromPosition = choice(capturedSquares)
possibleMoves = getPossibleOrderMoves(fromPosition[0], fromPosition[1])
if len(possibleMoves)!=0:
mv = choice(possibleMoves)
ans = (fromPosition[0], fromPosition[1], mv[0], mv[1])
break
return ans
## --------------------
import os, sys
sys.path.insert(0, os.path.realpath('../utils'))
from log import *
COLORS = [bcolors.OKRED, bcolors.OKCYAN, bcolors.OKGREEN, bcolors.OKBLUE, bcolors.OKYELLOW, bcolors.OKWHITE]
TEXTCONV = {'A': 'R', 'B': 'C', 'C': 'G','D':'B', 'E':'Y', '-':'-'}
def color(tile): # character
index = ord(tile) - ord('A')
if (tile == '-'):
index = 5
return COLORS[index] + TEXTCONV[tile] + bcolors.ENDC
def printBoard():
for x in xrange(N):
print >>sys.stderr, "".join( list( map( lambda x: color(x), board[x] ) ) )
print >>sys.stderr, '\n'
# returns if the move was successful or not
def makeChaosMove(x, y, color):
global board
if (board[x][y] != '-'):
return False
board[x][y] = color
return True
# returns if the move was successful or not
def makeOrderMove(a, b, c, d):
global board
board[c][d] = board[a][b]
board[a][b] = '-'
return True
def playAsOrder():
global board
printX('ORDER')
while True:
printBoard()
line = raw_input()
# printX ('LINE:', line)
(x, y, color) = line.split(' ')
(x, y) = (int(x), int(y))
board[x][y] = color
if (isGameOver()):
return
(a, b, c, d) = orderAI()
makeOrderMove(a, b , c, d)
printBoard()
print '%d %d %d %d' % (a, b, c, d)
sys.stdout.flush()
def playAsChaos():
global board
printX('CHAOS')
color = raw_input()
(x, y) = chaosAI(color)
board[x][y] = color
print '%d %d' %(x, y)
printBoard()
while True:
if (isGameOver()):
return
his_move = raw_input()
# printX ('his move: %s'%his_move)
(a, b, c, d) = map(lambda x: int(x), his_move.split(' '))
makeOrderMove(a, b, c, d)
color = raw_input()
(x, y) = chaosAI(color)
board[x][y] = color
printBoard()
print '%d %d' %(x, y)
if (ROLE == 'ORDER'):
playAsOrder()
elif(ROLE == 'CHAOS'):
playAsChaos()
else:
print >> sys.stderr, 'I am not intelligent for this role: %s' %ROLE
printX ('--graceful exit by myAI--')