-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtic-tac-toe.py
More file actions
76 lines (65 loc) · 1.36 KB
/
Copy pathtic-tac-toe.py
File metadata and controls
76 lines (65 loc) · 1.36 KB
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
# Tic tac toe
# https://www.hackerrank.com/challenges/tic-tac-toe
import random
winning_lines = [
[
[0,0],[0,1],[0,2]
],
[
[1,0],[1,1],[1,2]
],
[
[2,0],[2,1],[2,2]
],
[
[0,0],[1,0],[2,0]
],
[
[0,1],[1,1],[2,1]
],
[
[0,2],[1,2],[2,2]
],
[
[0,0],[1,1],[2,2]
],
[
[0,2],[1,1],[2,0]
]
]
#print winning_lines
# Complete the function below to print 2 integers separated by a single space which will be your next move
def nextMove(player,board):
the_move = ['X' if player is 'X' else 'O']
# 1. Read / Analyze the board
# 2. Check for possible winning lines remaining
# 3. First Priority -
# Fill the winning line of the opponent
# Second Priority -
# Fill the winning line for yourself
#
count = 0
empty_spaces = []
for i in range(3):
for j in range(3):
if (board[i][j] == chr(95)): # If empty spaces are encountered
count += 1
empty_spaces.append([i,j])
rand_coord = random.randrange(0,count)
#print "Random number:",rand_coord
#print "My move:",empty_spaces[rand_coord]
print empty_spaces[rand_coord][0],empty_spaces[rand_coord][1]
#If player is X, I'm the first player.
#If player is O, I'm the second player.
player = raw_input()
#Read the board now. The board is a 3x3 array filled with X, O or _.
board = []
for i in xrange(0, 3):
board.append(raw_input())
nextMove(player,board);
'''
X
___
___
_XO
'''