-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtick_tack_toe_ardop.py
166 lines (108 loc) · 3.73 KB
/
tick_tack_toe_ardop.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
#!/usr/bin/env python
# license removed for brevity
import rospy
from std_msgs.msg import Int16
board = [' ' for x in range(10)]
def insert_letter (letter,pos):
board[pos] = letter
def free_space(pos):
return board[pos] == ' '
def print_board(board):
print('===========================')
print(' | |')
print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
print(' | |')
print('-----------')
print(' | |')
print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
print(' | |')
print('-----------')
print(' | |')
print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
print(' | |')
print('===========================')
def is_winner_detected(board,letter):
return (board[7]== letter and board[8]== letter and board[9]==letter) or (board[4]== letter and board[5]== letter and board[6]==letter) or (board[1]== letter and board[2]== letter and board[3]==letter) or (board[1]== letter and board[4]== letter and board[7]==letter) or (board[2]== letter and board[5]== letter and board[8]==letter) or (board[3]== letter and board[6]== letter and board[9]==letter) or (board[1]== letter and board[5]== letter and board[9]==letter) or (board[3]== letter and board[5]== letter and board[7]==letter)
def main(n):
print_board(board)
while not is_board_full(board):
'''
if not is_winner_detected(board,'X'): ## computer o, human x
move = ARDOP_move()
insert_letter('O', move)
print_board(board)
else:
print('Human wins')
break
'''
if not is_winner_detected(board,'O'): ## computer o, human x
player_move(n)
print_board(board)
else:
print('ARDOP wins')
break
if not is_winner_detected(board,'X'): ## computer o, human x
move = ARDOP_move()
insert_letter('X', move)
print_board(board)
else:
print('Human wins')
break
if is_board_full(board):
print('TIE')
def ARDOP_move():
possibleMoves = [x for x, letter in enumerate(board) if letter == ' ' and x != 0] # Create a list of possible moves
move = 0
for let in ['O','X']:
for i in possibleMoves:
boardCopy = board[:]
boardCopy[i] = let
if is_winner_detected(boardCopy, let):
move = i
return move
corner_avalaible = []
for i in possibleMoves:
if i in [1,3,7,9]:
corner_avalaible.append(i)
if len(corner_avalaible) > 0:
move = selectRandom(corner_avalaible)
return move
if 5 in possibleMoves:
move = 5
return move
edges_avalaible = []
for i in possibleMoves:
if i in [2,4,6,8]:
edges_avalaible.append(i)
if len(edges_avalaible) > 0:
move = selectRandom(edges_avalaible)
return move
import random
def selectRandom(L):
ln =len(L)
r = random.randrange(0,ln)
return L[r]
def is_board_full(board):
if board.count(' ') > 1:
return False
else:
return True
def player_move(n):
move = n
if free_space(int(move)):
insert_letter('O', int(move))
#main()
def callback(data):
main(data.data)
def listener():
# In ROS, nodes are uniquely named. If two nodes with the same
# name are launched, the previous one is kicked off. The
# anonymous=True flag means that rospy will choose a unique
# name for our 'listener' node so that multiple listeners can
# run simultaneously.
rospy.init_node('Board', anonymous=True)
rospy.Subscriber("Board_Index", Int16, callback)
# spin() simply keeps python from exiting until this node is stopped
rospy.spin()
if __name__ == '__main__':
listener()