Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Omer Fadil Usta < omerusta@gmail.com > , 2017
- # Tic Tac Toe 1.3
- import sys,os,time
- clearScreen = lambda : os.system("clear") or None
- board = [["---","---","---"],
- ["---","---","---"],
- ["---","---","---"]]
- towin =[[[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]]]
- def showBoard():
- clearScreen()
- print ("{:>21} {:>4} {:>4}".format(1,2,3))
- for i in range(3):
- print("{:>18} {:^4} {:^4} {:^4}".format(i+1,board[i][0],board[i][1],board[i][2]))
- def takeInput(title):
- input_check = ["1", "2", "3", "q", "Q"]
- while True:
- inp = input("{} - enter [1,2,3 or q to quit] :".format(title).ljust(40))
- if inp in input_check:
- if inp.lower() == "q":
- print("ByeBye")
- sys.exit(0)
- return int(inp)
- break
- else:
- print("Wrong Entry. RETRY Again !")
- time.sleep(3)
- showBoard()
- store={"X":[],"O":[]}
- situation={"X":[],"O":[]}
- turn = 1
- while True:
- showBoard()
- sign = "X" if turn % 2 == 0 else "O"
- print("Player {}\n".format(sign))
- x = takeInput("From top to down") -1
- y = takeInput("From left to right") -1
- if board[x][y] == "---":
- board[x][y] = sign
- store[sign].append([x,y])
- turn += 1
- else:
- print("\nOops! Not an empty space. Try again.\n")
- time.sleep(3)
- showBoard()
- for i in towin:
- situation["X"]=[z for z in i if z in store["X"]]
- situation["O"]=[z for z in i if z in store["O"]]
- if len(situation["X"]) == len(i):
- print(" X WON!")
- sys.exit(0)
- elif len(situation["O"]) == len(i):
- print(" O WON!")
- sys.exit(0)
- if turn == 10:
- print(" DRAW!")
- sys.exit(0)
Add Comment
Please, Sign In to add comment