Skip to content

Commit 1afc1c6

Browse files
committed
Adding Tic Tac Toe
1 parent ecccc43 commit 1afc1c6

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

TicTacToe.py

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
board = [' ' for x in range(10)]
2+
3+
def insertLetter(letter,pos):
4+
board[pos] = letter
5+
6+
def spaceIsFree(pos):
7+
return board[pos] == ' '
8+
9+
def printBoard(board):
10+
print(' | | ')
11+
print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
12+
print(' | | ')
13+
print('------------')
14+
print(' | | ')
15+
print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
16+
print(' | | ')
17+
print('------------')
18+
print(' | | ')
19+
print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
20+
print(' | | ')
21+
22+
def isBoardFull(board):
23+
if board.count(' ') > 1:
24+
return False
25+
else:
26+
return True
27+
28+
def IsWinner(b,l):
29+
return ((b[1] == l and b[2] == l and b[3] == l) or
30+
(b[4] == l and b[5] == l and b[6] == l) or
31+
(b[7] == l and b[8] == l and b[9] == l) or
32+
(b[1] == l and b[4] == l and b[7] == l) or
33+
(b[2] == l and b[5] == l and b[8] == l) or
34+
(b[3] == l and b[6] == l and b[9] == l) or
35+
(b[1] == l and b[5] == l and b[9] == l) or
36+
(b[3] == l and b[5] == l and b[7] == l))
37+
38+
def playerMove():
39+
run = True
40+
while run:
41+
move = input("please select a position to enter the X between 1 to 9: ")
42+
try:
43+
move = int(move)
44+
if move > 0 and move < 10:
45+
if spaceIsFree(move):
46+
run = False
47+
insertLetter('X' , move)
48+
else:
49+
print('Sorry, this space is occupied')
50+
else:
51+
print('please type a number between 1 and 9: ')
52+
53+
except:
54+
print('Please type a number')
55+
56+
def computerMove():
57+
possibleMoves = [x for x , letter in enumerate(board) if letter == ' ' and x != 0 ]
58+
move = 0
59+
60+
for let in ['O' , 'X']:
61+
for i in possibleMoves:
62+
boardcopy = board[:]
63+
boardcopy[i] = let
64+
if IsWinner(boardcopy, let):
65+
move = i
66+
return move
67+
68+
cornersOpen = []
69+
for i in possibleMoves:
70+
if i in [1 , 3 , 7 , 9]:
71+
cornersOpen.append(i)
72+
73+
if len(cornersOpen) > 0:
74+
move = selectRandom(cornersOpen)
75+
return move
76+
77+
if 5 in possibleMoves:
78+
move = 5
79+
return move
80+
81+
edgesOpen = []
82+
for i in possibleMoves:
83+
if i in [2,4,6,8]:
84+
edgesOpen.append(i)
85+
86+
if len(edgesOpen) > 0:
87+
move = selectRandom(edgesOpen)
88+
return move
89+
90+
def selectRandom(li):
91+
import random
92+
ln = len(li)
93+
r = random.randrange(0,ln)
94+
return li[r]
95+
96+
def main():
97+
print("Welcome to the game!")
98+
printBoard(board)
99+
100+
while not(isBoardFull(board)):
101+
if not(IsWinner(board , 'O')):
102+
playerMove()
103+
printBoard(board)
104+
else:
105+
print("sorry you loose!")
106+
break
107+
108+
if not(IsWinner(board , 'X')):
109+
move = computerMove()
110+
if move == 0:
111+
print(" ")
112+
else:
113+
insertLetter('O' , move)
114+
print('computer placed an o on position' , move , ':')
115+
printBoard(board)
116+
else:
117+
print("you win!")
118+
break
119+
120+
if isBoardFull(board):
121+
print("Tie game")
122+
123+
while True:
124+
x = input("Do you want to play? (y/n): ")
125+
if x.lower() == 'y':
126+
board = [' ' for x in range(10)]
127+
print('--------------------')
128+
main()
129+
else:
130+
break

0 commit comments

Comments
 (0)