Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from classes import player
- from classes import bcolors
- print('''
- 1 | 2 | 3
- ---------
- 4 | 5 | 6
- ---------
- 7 | 8 | 9
- Give these values, as input where
- you want to mark your territory.
- ''')
- grid = [
- [0, 0, 0],
- [0, 0, 0],
- [0, 0, 0]
- ]
- def print_grid():
- print('\n-------------')
- for x in grid:
- print("| ", end='')
- for y in x:
- if y == 0:
- print('- | ', end='')
- else:
- print(str(y) + ' | ', end='')
- print('\n-------------')
- print_grid()
- name1 = input('Player 1: ')
- name2 = input('Player 2: ')
- player1 = player(name1, 'X')
- player2 = player(name2, 'O')
- players = [player1, player2]
- # control the flow of the game
- running = True
- def take_input():
- chance = int(input(player.get_name() +
- ', please specify where you want to mark: '))
- print(player.get_name()+' has chosen input as ' + str(chance))
- if chance == 1:
- if grid[0][0] != 0:
- print(bcolors.FAIL + bcolors.BOLD +
- '\nSorry, already taken!\n'+bcolors.ENDC)
- take_input()
- else:
- grid[0][0] = player.get_symbol()
- elif chance == 2:
- if grid[0][1] != 0:
- print(bcolors.FAIL + bcolors.BOLD +
- '\nSorry, already taken!\n'+bcolors.ENDC)
- take_input()
- else:
- grid[0][1] = player.get_symbol()
- elif chance == 3:
- if grid[0][2] != 0:
- print(bcolors.FAIL + bcolors.BOLD +
- '\nSorry, already taken!\n'+bcolors.ENDC)
- take_input()
- else:
- grid[0][2] = player.get_symbol()
- elif chance == 4:
- if grid[1][0] != 0:
- print(bcolors.FAIL + bcolors.BOLD +
- '\nSorry, already taken!\n'+bcolors.ENDC)
- take_input()
- else:
- grid[1][0] = player.get_symbol()
- elif chance == 5:
- if grid[1][1] != 0:
- print(bcolors.FAIL + bcolors.BOLD +
- '\nSorry, already taken!\n'+bcolors.ENDC)
- take_input()
- else:
- grid[1][1] = player.get_symbol()
- elif chance == 6:
- if grid[1][2] != 0:
- print(bcolors.FAIL + bcolors.BOLD +
- '\nSorry, already taken!\n'+bcolors.ENDC)
- take_input()
- else:
- grid[1][2] = player.get_symbol()
- elif chance == 7:
- if grid[2][0] != 0:
- print(bcolors.FAIL + bcolors.BOLD +
- '\nSorry, already taken!\n'+bcolors.ENDC)
- take_input()
- else:
- grid[2][0] = player.get_symbol()
- elif chance == 8:
- if grid[2][1] != 0:
- print(bcolors.FAIL + bcolors.BOLD +
- '\nSorry, already taken!\n'+bcolors.ENDC)
- take_input()
- else:
- grid[2][1] = player.get_symbol()
- elif chance == 9:
- if grid[2][2] != 0:
- print(bcolors.FAIL + bcolors.BOLD +
- '\nSorry, already taken!\n'+bcolors.ENDC)
- take_input()
- else:
- grid[2][2] = player.get_symbol()
- while running:
- for player in players:
- # if any player wins, the flag turns true
- flag = False
- # checks
- flag2 = False
- for i in grid:
- for j in i:
- if j == 0:
- flag2 = True
- if flag2 == False:
- print(bcolors.WARNING + 'This game ends in a draw.'+bcolors.ENDC)
- running = False
- break
- # horizontal check
- for i in range(2):
- if grid[i][0] == grid[i][1] == grid[i][2]:
- flag = True
- if grid[i][0] == player1.get_symbol():
- print(bcolors.OKGREEN + bcolors.BOLD +
- player1.get_name() + ' won the game!'+bcolors.ENDC)
- elif grid[i][0] == player2.get_symbol():
- print(bcolors.OKGREEN + bcolors.BOLD +
- player2.get_name() + ' won the game!'+bcolors.ENDC)
- # Vertical check
- for i in range(2):
- if grid[0][i] == grid[1][i] == grid[2][i]:
- flag = True
- if grid[0][i] == player1.get_symbol():
- print(bcolors.OKGREEN + bcolors.BOLD +
- player1.get_name() + ' won the game!'+bcolors.ENDC)
- elif grid[0][i] == player2.get_symbol():
- print(bcolors.OKGREEN + bcolors.BOLD +
- player2.get_name() + ' won the game!'+bcolors.ENDC)
- # Diagonal checks
- if grid[0][0] == grid[1][1] == grid[2][2] or grid[0][2] == grid[1][1] == grid[2][0]:
- flag = True
- if grid[1][1] == player1.get_symbol():
- print(bcolors.OKGREEN + bcolors.BOLD +
- player1.get_name() + ' won the game!'+bcolors.ENDC)
- elif grid[1][1] == player2.get_symbol():
- print(bcolors.OKGREEN + bcolors.BOLD +
- player2.get_name() + ' won the game!'+bcolors.ENDC)
- if flag == True:
- running = False
- break
- take_input()
- print_grid()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement