lychnisalkali
Profilo di
Nome | lychnisalkali |
---|---|
Indirizzo email | n/a |
Messaggi | 1 |
-
- 2025-01-13 08:43:05
- Re: Tris
- Forum >> Programmazione Python >> Videogames
- I fixed a mistake for you
```python
random import
def draw_table(table):
print(" | |")
print(" " + table7 + " | " + table8 + " | " + table[ 9])
print(" | |")
print("-----------")
print(" | |")
print(" " + table4 + " | " + table[5 ] + " | " + table6)
print(" | |")
print("-----------")
print(" | |")
print(" " + table1 + " | " + table2 + " | " + table3)
print(" | |")
def letterPlayer():
letter = " "
while letter not in ["X", "O"]:
print("Do you want X or O?")
letter = input().upper()
if letter == "X":
return "X", " O"
else:
return "O", "X"
def who_starts():
if random.randint(0, 1) == 0:
return "computer"
else:
return "player"
def play_again():
print("Do you want to play again? (Y=Yes, N=No )")
return input().lower().startswith("y")
def make_move(table, letter, move):
tablemove = letter
def winner(table, letter):
return (
(table7 = = table8 == table9 == letter) or
(table4 == table5 == table6 == letter) or
(table1 == table2 == table3 == letter) or
(table7 == table4 == table1 == letter) or
(table8 == table5 == table2 == letter) or
(table[ 9] == table6 == table3 == letter) or
(table7 == table5 == table3 == letter) or
(table9 == table5 == table1 == letter)
)
def freespace(table, move):
return tablemove == " "
def create_copy_table(table):
return table[:]
def player_move(table):
move = " "
while move not in "1 2 3 4 5 6 7 8 9".split() or not freespace(table, int(move)):
print("What will be your next move? (1-9)")
move = input()
return int(move)
def random_move_from_list(table, move_list):
possible_moves = [i for i in move_list if freespace(table, i)]
return random.choice(possible_moves) if possible_moves else None
def computer_move(table, computer_letter):
player_letter = "O" if computer_letter == "X" else "X"
for i in range(1, 10):
copy = create_copy_table(table)
if freespace(copy, i):
make_move(copy, computer_letter, i)
if winner(copy,computer_letter):
return i
for i in range(1, 10):
copy = create_copy_table(table)
if freespace(copy, i):
make_move(copy, player_letter, i)
if winner(copy, player_letter):
return i
move = random_move_from_list(table, [1, 3, 7, 9])
if move:
return move
if freespace(table, 5):
return 5
return random_move_from_list(table, [2, 4, 6, 8])
def is_table_full(table ):
return all(space != " " for space in table[1:])
print("Play tic tac toe!")
while True:
the_table = [" "] * 10
player_letter, computer_letter = letterPlayer()
turn = who_starts()
print("The " + turn + " goes first.")
game_in_progress = True
while game_in_progress:
if turn == "player":
draw_table(the_table)
move = player_move(the_table)
make_move(the_table, player_letter, move)
if winner(the_table, player_letter):
draw_table(the_table)
print("Hurray! You won the game!")
game_in_progress = False
else:
if is_table_full(the_table):
draw_table(the_table)
print("The game is tied!")
break
else:
turn = "computer"
else:
move = computer_move(the_table, computer_letter)
make_move(the_table , computer_letter, move)
if winner(the_table, computer_letter):
draw_table(the_table)
print("The computer beat you! You lost.")
game_in_progress = False
else:
if is_table_full(the_table):
draw_table(the_table)
print("The game is tied!")
break
else:
turn = "player"
if not play_again():
break
```
--- Ultima modifica di lychnisalkali in data 2025-01-13 08:43:52 ---
--- Ultima modifica di lychnisalkali in data 2025-01-13 08:52:43 ---