将两个功能合并为一个

huangapple go评论96阅读模式
英文:

Combining two functions in one

问题

  1. import numpy as np
  2. class Board:
  3. def __init__(self):
  4. self.cells = np.zeros((3, 3, 3, 3))
  5. self.block = np.zeros((3, 3))
  6. def mark_combined(self, main_row, main_col, row=None, col=None, player=None):
  7. if row is None and col is None and player is None:
  8. self.block[main_row][main_col] = player
  9. else:
  10. self.cells[main_row][main_col][row][col] = player
英文:

How can I combine functions mark_block and mark_cell into single function?

  1. import numpy as np
  2. class Board:
  3. def __init__(self):
  4. self.cells = np.zeros((3, 3, 3, 3))
  5. self.block = np.zeros((3, 3))
  6. def mark_block(self, main_row, main_col, player):
  7. self.block[main_row][main_col] = player
  8. def mark_cell(self, main_row, main_col, row, col, player):
  9. self.cells[main_row][main_col][row][col] = player

EDIT: Would it also be possible to do it without if statement?

答案1

得分: 1

  1. import numpy as np
  2. class Board:
  3. def __init__(self):
  4. self.cells = np.zeros((3, 3, 3, 3))
  5. self.block = np.zeros((3, 3))
  6. def mark(self, main_row, main_col, player, row=None, col=None):
  7. if row is None and col is None:
  8. self.block[main_row][main_col] = player
  9. else:
  10. self.cells[main_row][main_col][row][col] = player
英文:
  1. import numpy as np
  2. class Board:
  3. def __init__(self):
  4. self.cells = np.zeros((3, 3, 3, 3))
  5. self.block = np.zeros((3, 3))
  6. def mark(self, main_row, main_col, player, row=None, col=None):
  7. if row is None and col is None:
  8. self.block[main_row][main_col] = player
  9. else:
  10. self.cells[main_row][main_col][row][col] = player

huangapple
  • 本文由 发表于 2023年2月17日 23:36:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定