-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.py
More file actions
105 lines (95 loc) · 3.11 KB
/
tictactoe.py
File metadata and controls
105 lines (95 loc) · 3.11 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
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
#!/usr/bin/env python
#from IPython.display import clear_output
import os
def DisplayBoard(board):
#clear_output()
os.system('clear')
print('check 1')
print(board[7]+'|'+board[8]+'|'+board[9])
print("-----")
print(board[4]+'|'+board[5]+'|'+board[6])
print("-----")
print(board[1]+'|'+board[2]+'|'+board[3])
def PlayerAssignment():
Player1 = ''
while not(Player1 == 'X' or Player1 =='O'):
marker = input('Player 1 please select either X or O: ')
Player1 = marker.upper()
if Player1 == 'X':
return ['X','O']
else:
return ['O','X']
def PlayerMove(board,player):
"""
Function that purely gets the input from the player, checks it is a viable input and then returns the location
the input can be between
"""
count = 0
while count < 5:
marker = input('Player '+player+' please make a move. (keys 1-9) : ')
if marker.isnumeric() and int(marker) < 10 and int(marker) > 0:
if board[int(marker)] == ' ':
return int(marker)
else:
print('That position is taken')
else:
print('That is not an appropriate move.')
count += 1
return 0
def CheckWin(board):
"""
This function checks if there are any wins currently on the Tic-Tac-Toe board. To win a player must have at least
1 of square 1, 5, and 9. Thus these squares are focused on. If none of them are taken then no win has occurred.
"""
if board[1] == 'X' or board[1] == 'O':
if board[1]==board[2]==board[3]:
return board[1]
elif board[1]==board[4]==board[7]:
return board[1]
elif board[1]==board[5]==board[9]:
return board[1]
if board[5] == 'X' or board[5] == 'O':
if board[5]==board[3]==board[7]:
return board[5]
elif board[5]==board[4]==board[6]:
return board[5]
elif board[5]==board[2]==board[8]:
return board[5]
if board[9] == 'X' or board[9] == 'O':
if board[9]==board[3]==board[6]:
return board[9]
elif board[9]==board[8]==board[7]:
return board[9]
return 0
def TicTacToe():
"""
Starts a game of Tic-Tac-Toe between two players on one computer. This game involves the placeing of markers
in a 3 by 3 grid. The first player to get three of their markers (an X or an O) in a row wins the game.
In the event that every square is taken without any wins, the game is a tie.
"""
os.system('clear')
playerTurn = 0
players = PlayerAssignment()
board = ['-',' ',' ',' ',' ',' ',' ',' ',' ',' ']
winFlag = 0
DisplayBoard(board)
while winFlag == 0:
moveRet = PlayerMove(board, players[playerTurn])
if moveRet:
board[moveRet] = players[playerTurn]
else:
input('5 unsuccessful attempts have been made to make a move, you are probably just screwing around right now...')
break
DisplayBoard(board)
winFlag = CheckWin(board)
if not winFlag == 0:
print('Player '+ winFlag +' has won the game!')
print('CONGRATULATIONS!')
if playerTurn == 0:
playerTurn = 1
else:
playerTurn = 0
if board.count(' ') == 0:
print('The board is full without any winners, thus it is a tie!\nBetter luck next time!')
break
TicTacToe()