-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
86 lines (80 loc) · 2.89 KB
/
main.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
# An environment to test out stuff and execute code!
# run.bat
# @echo off
# cls
# echo The date today is %date%
# echo The time today is %time%
# echo Author: Parthiv
# echo:
# echo Program Output:
# echo:
# python environment.py
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
import random
import os
# player names
player1 = ''
player2 = ''
gameboard = [' ',' ',' ',' ',' ',' ',' ',' ',' ']
numstring=["1","2","3","4","5","6","7","8","9"]
# ways a player can win
combinations = [[0,1,2],[3,4,5],[6,7,8],[0,4,8],[2,4,6],[0,3,6],[1,4,7],[2,5,8]]
def display():
print('_____________')
print(f'| {gameboard[0]} | {gameboard[1]} | {gameboard[2]} |')
print(f'| {gameboard[3]} | {gameboard[4]} | {gameboard[5]} |')
print(f'| {gameboard[6]} | {gameboard[7]} | {gameboard[8]} |')
print('‾‾‾‾‾‾‾‾‾‾‾‾‾')
player1 = input("Player 1, enter your name.")
player2 = input("Player 2, enter your name.")
winner = ''
turn = random.randint(1,2)
move = '100'
for _ in numstring:
if winner == 'O':
print("The winner is player 1!")
elif winner == 'X':
print("The winner is player 2!")
elif winner == '':
if turn == 1:
while move not in numstring:
move = input(f"{player1}, enter an index from 1-9.")
if move not in numstring:
print("Please enter an integer from 1-9")
elif move in numstring:
if gameboard[int(move)-1] != ' ':
print("Please enter an index that does not have a character in it already.")
move = '100'
gameboard[int(move)-1] = 'O'
for c in combinations:
trueness = 0
for x in c:
if gameboard[x] == 'O':
trueness +=1
if trueness == 3:
winner = 'O'
turn = 2
move = '100'
os.system('cls')
display()
elif turn == 2:
while move not in numstring:
move = input(f"{player1}, enter an index from 1-9.")
if move not in numstring:
print("Please enter an integer from 1-9")
elif move in numstring:
if gameboard[int(move)-1] != ' ':
print("Please enter an index that does not have a character in it already.")
move = '100'
gameboard[int(move)-1] = 'X'
for c in combinations:
trueness = 0
for x in c:
if gameboard[x] == 'X':
trueness +=1
if trueness == 3:
winner = 'X'
turn = 1
move = '100'
os.system('cls')
display()