我做了一個函式來檢查我的套接字井字游戲中是否有勝利,問題是,它很難閱讀。
有沒有辦法讓它更簡單?我考慮過 for 回圈,但我認為在我的情況下它不會是正確的
if board[0][0] == board[1][0] and board[1][0] == board[2][0]:
if board[0][0] == 'X':
client_sockets[0].send("you win")
client_sockets[1].send("you lose")
else:
client_sockets[1].send("you win")
client_sockets[0].send("you lose")
if board[0][1] == board[1][1] and board[1][1] == board[2][1]:
if board[0][1] == 'X':
client_sockets[0].send("you win")
client_sockets[1].send("you lose")
else:
client_sockets[1].send("you win")
client_sockets[0].send("you lose")
if board[0][2] == board[1][2] and board[1][2] == board[2][2]:
if board[0][0] == 'X':
client_sockets[0].send("you win")
client_sockets[1].send("you lose")
else:
client_sockets[1].send("you win")
client_sockets[0].send("you lose")
等等...
uj5u.com熱心網友回復:
這是一個使用套接字的非常酷的專案 - https://github.com/Suvoo/TicTacToe-Using-Socket-Server
試試這個片段:
import random
class TicTacToe:
def __init__(self):
self.board = []
def create_board(self):
for i in range(3):
row = []
for j in range(3):
row.append('-')
self.board.append(row)
def get_random_first_player(self):
return random.randint(0, 1)
def fix_spot(self, row, col, player):
self.board[row][col] = player
def is_player_win(self, player):
win = None
n = len(self.board)
# checking rows
for i in range(n):
win = True
for j in range(n):
if self.board[i][j] != player:
win = False
break
if win:
return win
# checking columns
for i in range(n):
win = True
for j in range(n):
if self.board[j][i] != player:
win = False
break
if win:
return win
# checking diagonals
win = True
for i in range(n):
if self.board[i][i] != player:
win = False
break
if win:
return win
win = True
for i in range(n):
if self.board[i][n - 1 - i] != player:
win = False
break
if win:
return win
return False
for row in self.board:
for item in row:
if item == '-':
return False
return True
def is_board_filled(self):
for row in self.board:
for item in row:
if item == '-':
return False
return True
def swap_player_turn(self, player):
return 'X' if player == 'O' else 'O'
def show_board(self):
for row in self.board:
for item in row:
print(item, end=" ")
print()
def start(self):
self.create_board()
player = 'X' if self.get_random_first_player() == 1 else 'O'
while True:
print(f"Player {player} turn")
self.show_board()
# taking user input
row, col = list(
map(int, input("Enter row and column numbers to fix spot: ").split()))
print()
# fixing the spot
self.fix_spot(row - 1, col - 1, player)
# checking whether current player is won or not
if self.is_player_win(player):
print(f"Player {player} wins the game!")
break
# checking whether the game is draw or not
if self.is_board_filled():
print("Match Draw!")
break
# swapping the turn
player = self.swap_player_turn(player)
# showing the final view of board
print()
self.show_board()
# starting the game
tic_tac_toe = TicTacToe()
tic_tac_toe.start()
更多資訊
uj5u.com熱心網友回復:
您可以將板的當前狀態編碼為字串而不是嵌套串列。例如,
board = [['O', 'X', ''], ['O', 'X', ''], ['X', 'X', '']]
也可以編碼為:
sboard = "OX OX XX "
(用元組替換所有串列也可以。)
接下來,您撰寫兩個函式:
def xwins():
client_sockets[0].send("you win")
client_sockets[1].send("you lose")
def owins():
client_sockets[1].send("you win")
client_sockets[0].send("you lose")
最后,您創建所有游戲結束板的字典。這是可能的,因為字串是可散列的,因此可以用作字典鍵。
victory = {
"OX OX XX ": xwins,
"OX OX OX ": ywins,
# et cetera
}
注意我們如何參考這些函式,但不要在這里呼叫它們。
每次移動后,您都會sboard根據舊字串和用戶輸入創建一個新字串。
如果棋盤是贏棋,你可以通過字典呼叫正確的函式:
if sboard in victory:
victory[sboard]()
exit_game()
# else continue playing.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/418633.html
標籤:
