删除 main.py
This commit is contained in:
116
main.py
116
main.py
@@ -1,116 +0,0 @@
|
|||||||
import os
|
|
||||||
|
|
||||||
def _draw_game(dd):
|
|
||||||
os.system('cls' if os.name == 'nt' else 'clear')
|
|
||||||
|
|
||||||
d = ['X' if dd[i]==0 else 'O' if dd[i]==9 else i+1 for i in range(9)]
|
|
||||||
print(
|
|
||||||
f"""
|
|
||||||
Tic Tac Toe
|
|
||||||
|
|
||||||
|
|
||||||
Player 1 (X) - Player 2 (O)
|
|
||||||
|
|
||||||
| |
|
|
||||||
{d[0]} | {d[1]} | {d[2]}
|
|
||||||
_____|_____|_____
|
|
||||||
| |
|
|
||||||
{d[3]} | {d[4]} | {d[5]}
|
|
||||||
_____|_____|_____
|
|
||||||
| |
|
|
||||||
{d[6]} | {d[7]} | {d[8]}
|
|
||||||
""")
|
|
||||||
|
|
||||||
def _gen_win_pats():
|
|
||||||
wi = [
|
|
||||||
[0,1,2],[3,4,5],[6,7,8],
|
|
||||||
[0,3,6],[1,4,7],[2,5,8],
|
|
||||||
[0,4,8],[2,4,6]
|
|
||||||
]
|
|
||||||
pats = []
|
|
||||||
for cb in wi:
|
|
||||||
m = 0
|
|
||||||
for pos in cb:
|
|
||||||
m |= (1 << pos)
|
|
||||||
pats.append(m)
|
|
||||||
return pats
|
|
||||||
|
|
||||||
_win_pats = _gen_win_pats()
|
|
||||||
|
|
||||||
def _is_winning(data, player_val):
|
|
||||||
state = 0
|
|
||||||
for i in range(9):
|
|
||||||
if data[i] == player_val:
|
|
||||||
state |= (1 << i)
|
|
||||||
|
|
||||||
for pat in _win_pats:
|
|
||||||
if (state & pat) == pat:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
def handleKBI(lmbd):
|
|
||||||
def runlambda(*args,**kwargs):
|
|
||||||
try:
|
|
||||||
return lmbd(*args,**kwargs)
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
print("\n\nThanks for playing! Goodbye.")
|
|
||||||
return runlambda
|
|
||||||
|
|
||||||
@handleKBI
|
|
||||||
def main():
|
|
||||||
while True:
|
|
||||||
data = [-1] * 9
|
|
||||||
cpl = True
|
|
||||||
|
|
||||||
while True:
|
|
||||||
_draw_game(data)
|
|
||||||
|
|
||||||
if _is_winning(data, 0):
|
|
||||||
print("RESULT\nPlayer 1 Wins!")
|
|
||||||
break
|
|
||||||
|
|
||||||
if _is_winning(data, 9):
|
|
||||||
print("RESULT\nPlayer 2 Wins!")
|
|
||||||
break
|
|
||||||
|
|
||||||
if sum(1 for x in data if x == -1) <= 1:
|
|
||||||
for i in range(9):
|
|
||||||
if data[i] == -1: data[i] = 0 if cpl else 9
|
|
||||||
if _is_winning(data, 0):
|
|
||||||
print("RESULT\nPlayer 1 Wins!")
|
|
||||||
break
|
|
||||||
if _is_winning(data, 9):
|
|
||||||
print("RESULT\nPlayer 2 Wins!")
|
|
||||||
break
|
|
||||||
|
|
||||||
print("RESULT\nGame Draw!")
|
|
||||||
break
|
|
||||||
|
|
||||||
op = input(f"Player {1 if cpl else 2}\nEnter a number: ")
|
|
||||||
|
|
||||||
try:
|
|
||||||
op = int(op)
|
|
||||||
idx = op - 1
|
|
||||||
|
|
||||||
if idx < 0 or idx > 8:
|
|
||||||
print("Invalid input. Please enter 1-9.")
|
|
||||||
continue
|
|
||||||
|
|
||||||
if data[idx] != -1:
|
|
||||||
print("Position already taken. Try again.")
|
|
||||||
continue
|
|
||||||
|
|
||||||
data[idx] = 0 if cpl else 9
|
|
||||||
cpl = not cpl
|
|
||||||
|
|
||||||
except ValueError:
|
|
||||||
print("Invalid input. Please enter a number.")
|
|
||||||
continue
|
|
||||||
|
|
||||||
play_again = input("\nPlay again? (y/n): ").lower()
|
|
||||||
if play_again != 'y':
|
|
||||||
print("\n\nThanks for playing! Goodbye.")
|
|
||||||
break
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
||||||
Reference in New Issue
Block a user