diesmal mussten wir ein Spiel programmieren mit Tkinter. Es scheint zu funktionieren, aber ich glaube man kann bestimmt wieder einiges "vereinfacht" bzw. "besser" scheiben.
Code: Alles auswählen
import Tkinter
from random import randint
#stores the current player
global player
player = 'x'
def who_wins(board):
"""
Determines winner of a supplied tictactoe board.
Expects a 9 element list as representation for the board
with "x" and "o" representing the players.
Returns "x" when x has won and "o" when o has won.
Returns nothing when no one has won
"""
# the 3 horizontal 3 vertical and 2 diagonal winning possibilities
rows = [(0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7), (2,5,8),
(0,4,8), (2,4,6),]
for a, b, c in rows:
if board[a] + board[b] + board[c] == 'xxx':
return 'x'
elif board[a] + board[b] + board[c] == 'ooo':
return 'o'
#return
def put(board, symbol, position):
"""
Places a symbol on a tictactoe board.
Expects a 9 element list representing a board, a symbol
and the position where to place the symbol in the list.
If the list contains a "." where the symbol should go
it is placed there and a 0 returned.
If the list doesn't contain a "." where the symbol should go
the function returns 1
"""
show(board)
if board[position] != ".":
return 1
if board[position] == ".":
board[position] = symbol
next_round()
return 0
def show(board):
"""shows the board and status messages"""
for i in range(9):
if board[i] == 'o':
ttt_button[i].config(image=ocell)
elif board[i] == 'x':
ttt_button[i].config(image=xcell)
if (board.count(".") == 0) and (who_wins(board) != ("x" or "o")):
label1.config(text="It's a Draw!")
game_over()
if who_wins(board) == ("x"):
label1.config(text="X wins!")
game_over()
if who_wins(board) == ("o"):
label1.config(text="O wins!")
game_over()
def computer_play():
"""place an 'o' on a random free space"""
while board.count(".") > 0:
RandomPosition = randint(0,8)
#try to put o on random position
if not put(board, "o", RandomPosition):
#The position was free, break the while loop
break
def next_round():
"""show board and change player"""
show(board)
global player
if player == 'o':
player = 'x'
elif player == 'x':
player = 'o'
computer_play()
def game_over():
"""disables all buttons"""
for b in ttt_button:
b.config(state = "disabled")
root=Tkinter.Tk()
#supplied .png images have been converted to gif
bcell = Tkinter.PhotoImage(file='bcell.gif')
ocell = Tkinter.PhotoImage(file='ocell.gif')
xcell = Tkinter.PhotoImage(file='xcell.gif')
board = [".",".",".",".",".",".",".",".","."]
#create list with 9 button widgets
ttt_button = []
for i in range(9):
b = Tkinter.Button(image=bcell, command=lambda i=i: put(board, player, i))
# the lambda construct is necessary or we end up passing the result of put
ttt_button.append(b)
# arrange widgets on 3x3 grid
for row in range(3):
for column in range(3):
ttt_button[row*3+column].grid(row=row, column=column)
label1 = Tkinter.Label(text="playing Tic Tac Toe")
label1.grid(row=3, column=0, columnspan = 3)
root.mainloop()