지금까지 코드를 만들었으므로이 오류가 발생합니다. 어떻게 수정해야할지 모르겠습니다. 누구든지 도와 줄 수 있습니까? 나는 초급 프로그래머이기 때문에 코딩과 오류 수정 방법에 익숙하지 않습니다. 주에 대한보드의 루프를 만드는 방법은 무엇입니까?
winner = gboard.CheckWin()
TypeError: CheckWin() missing 1 required positional argument: 'color'
>>>
내 코드 :
def main():
gboard = ConnectFourBoard()
hp = HumanPlayer("R", gboard)
cp = ComputerPlayer("B", gboard)
players_1st = (hp, cp)
winner = False
gboard.show_board_dynamic()
while (winner == False):
for p in players_1st:
p.play()
gboard.show_board_dynamic()
winner = gboard.CheckWin()
if winner == True:
print()
print('Player %s has Won!' % p.get_player_symbol())
break
main()
게임 보드 코드 :
RED = 'R'
BLUE = 'B'
NONE = '.'
class ConnectFourBoard:
def __init__(self, cols = 7, rows = 6, requiredToWin = 4):
self.__space = ''
self.__board = []
self.cols = cols
self.rows = rows
self.Win = requiredToWin
self.__board = [[''] * cols for i in range(rows)]
def MakeMove(self, col, color):
if self.AllowMove(col):
for row in range(self.rows):
if self.__board[row][col] != '':
self.__board[row-1][col] = color
return
self.__board[self.rows-1][col] = color
else: return
def AllowMove(self, col):
if 0 <= col < self.cols:
return self.__board[0][col] == ''
if self.__board[0][col] == '':
return
def CheckWin(self, color):
#Checking for horizontal win
for row in range(0, self.rows):
for col in range(0, self.cols-3):
if self.__board[row][col] == color and\
self.__board[row][col+1] == color and\
self.__board[row][col+2] == color and\
self.__board[row][col+3] == color:
return True
#Checking for vertical win
for row in range(0, self.rows-3):
for col in range(0, self.cols):
if self.__board[row][col] == color and\
self.__board[row+1][col] == color and\
self.__board[row+2][col] == color and\
self.__board[row+3][col] == color:
return True
#Checking for negative diagonal win
for row in range(0, self.rows-3):
for col in range(0, self.cols-3):
if self.__board[row][col] == color and\
self.__board[row+1][col+1] == color and\
self.__board[row+2][col+2] == color and\
self.__board[row+3][col+3] == color:
return True
#Checking for positive diagonal win
for row in range(0, self.rows-3):
for col in range(0, self.cols):
if self.__board[row][col] == color and\
self.__board[row+1][col-1] == color and\
self.__board[row+2][col-2] == color and\
self.__board[row+3][col-3] -- color:
return True
def FullBoard(self):
for col in range(self.rows):
if self.__board[0][col] == '':
return False
return True
def FreeSpace(self, row, col):
if self.__board[row][col] == "":
return True
return False
def show_board_dynamic(self):
print()
print("-------")
for i in range(len(self.__board)):
for j in range(len(self.__board[0])):
print("|", end = "")
print(self.__board[i][j], end = ""),
print("|")
print("-------")
print()
- 게임 보드 코드에서
class Player:
def __init__(self, color, board):
self.symbol = color
class HumanPlayer(Player):
def __init__(self, color, board):
self.symbol = color
self.gboard = board
def get_player_symbol(self):
return self.symbol
def play(self):
print('Player %s turn' %self.get_player_symbol())
c = int(input('Please enter column no: '))
self.gboard.MakeMove(c, self.get_player_symbol())
import random
class ComputerPlayer(Player):
def __init__(self, color, board):
self.symbol = color
self.gboard = board
def get_player_symbol(self):
return self.symbol
def play(self):
print("Player %s turn" %self.get_player_symbol())
c = random.randint(0, 6)
self.__gboard.MakeMove(c, self.get_player_symbol())
는'CheckWin'는 인수'color' 필요하지만 당신은 인수없이 호출 –