これまでのコードを作成しましたが、このエラーが出てきます。私はそれを修正する方法がわかりません。誰も助けることができますか?私は初心者のプログラマーなので、コーディングやエラーの修正方法はあまり経験していません。ボードのループを作成する方法は?
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'を取るが、あなたは私がそれをした –