iT邦幫忙

0

Python Tic tac toe遊戲

  • 分享至 

  • xImage

大家好,我是Python新手,正嘗試做一個tic tac toe遊戲,但我在步驟4有點卡住了,不知道part4這個take turn跟get available square該如何撰寫!謝謝!

class TicTacToeSim:

# Part 1
def __init__(self):
	#Initialize the simulation
	# Set up board as a 2D list, turn to player 1, and ai to false
	self.board = [[0,0,0],[0,0,0],[0,0,0]]
	self.AI = False
	self.turn = 1,2
	while self.AI == True:
		self.turn = 1
		break
	self.AIturn = 1,2

def change_turn(self):
	# Change turn to other player
	while self.AI == False: #not play with AI
		if self.turn == 1:
			self.turn = 2
		if self.turn ==2:
			self.turn = 1
	while self.AI == True: #play with AI
		self.turn = 1
		self.AIturn = 2
	return

def play_game(self):
	# This is the driver method for the simulation
	#Display initial board
	return


# Part 2
def print_board(self):
	# Print the state of the board using X (player 1) and O (player 2)
	self.item = [' ', 'X', 'O']  # O X empty can put in the []
	for i in range(0, 3):
		for j in range(0, 3):
			if j < 2:
				print(self.item[self.board[i][j]], ' | ', end="", flush=True)
			else:
				print(self.item[self.board[i][j]], end="", flush=True)
		print(end="\n")
	print(end="\n")
	return 0

# Part 3
def get_move(self):
	# Get input from user asking for their move as a tuple
	self.row = int(input("Please enter the row(1-3) you want to move:"))
	self.col = int(input("Please enter the col(1-3) you want to move:"))
	self.move = (self.row,self.col)
	return (self.move)

# Part 4
def take_turn(self, player):
#This is the driver method for a players turn
	player = {'X':[],'O':[]}
	self.valid_move = False
	while self.valid_move == True:
		self.valid_move = self.move

	else:
		self.board[self.row][self.col] = player
	self.valid_move = True
	return
def get_available_squares(self):
	# Get a list of available squares as tuples (row,col)
	return None

def make_move(self, move, player):

	return

# Part 5
def check_winner(self):
	# Check if a player has won, there are 8 ways to win.
	# Return the player who won 0 if nobody has won, and -1 if it is a draw
	if self.board[0] == self.board[1] == self.board[2] !=" ": #row winner
		return 0
	elif self.board[3] == self.board[4] == self.board[5] !=" ":
		return 0
	elif self.board[6] == self.board[7] == self.board[8] !=" ":
		return 0
	elif self.board[0] == self.board[3] == self.board[6] !=" ": #col winner
		return 0
	elif self.board[1] == self.board[4] == self.board[7] !=" ":
		return 0
	elif self.board[2] == self.board[5] == self.board[8] !=" ":
		return 0
	elif self.board[0] == self.board[4] == self.board[8] !=" ": #diagonals winner
		return 0
	elif self.board[2] == self.board[4] == self.board[6] !=" ":
		return 0
	else:
		return-1

# Part 6
def random_move(self):
	# Choose a random move from available moves
	return None

# Part 7
def winning_move(self, player):
	# Find a winning move for a player
	return None

def threat_to_lose(self):
	# Run winning_move from other perspective
	return

def smart_move(self):
	# If there is a winning move, win
	# If there is a threat to lose, block
	# Make random move
	return

sim = TicTacToeSim()
sim.print_board()
sim.get_move()

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
海綿寶寶
iT邦大神 1 級 ‧ 2021-10-13 09:42:50

https://ithelp.ithome.com.tw/upload/images/20211013/20001787lsvSszGLsj.png

take turn
我不明白你要做什麼,恕沒有答案

get_available_squares
可以仿照 check_winner 的寫法
回傳 self.board 裡所有「可以下的位置」(註)

註:我看不懂你想用「什麼值」來表示可以下的位置
因為你在 check_winner 裡用空白來判斷
而在 init 裡又設初值為 0
這表示「一步都不用下,一呼叫 check_winner 遊戲就會結束」

我要發表回答

立即登入回答