最初にお手伝いをしてくれてありがとう。TicTacToeで動的境界チェックを使用する方法
私はあなたがここにチェックアウトすることができますpythonで完成三目並べゲームを持っている:https://github.com/Weffe/TicTacToe
現在、すべてが3x3の競技場のための完全に正常に実行されます。私は、問題なく9x9に容易に拡大できるという意味で、ゲームを動的にしたいと思っていました。しかし、私は少し脳死しており、これを可能にする最もクリーンな方法を理解することはできません。主な問題は、Player.pyの関数get_player_loc_input()にあります。
今のところ、それは3x3の競技場にのみ設定されています。しかし、私はそれを動的にしたい。
オリジナルコード:
def get_player_loc_input(self):
player_input = input('Enter in location for your move: ') # player input is with respect to field index location
translated_input = int(player_input)
if 7 <= translated_input <= 9:
translated_input = [0, (translated_input - 7)] # row, col
elif 4 <= translated_input <= 6:
translated_input = [1, (translated_input - 4)] # row, col
elif 1 <= translated_input <= 3:
translated_input = [2, (translated_input - 1)] # row, col
else:
raise ValueError('Input is not an index on the playing field. Try again\n')
return translated_input
私の現在の動的な試み:
def get_player_loc_input(self, num_rows, num_cols):
value = num_rows*num_cols
num_rows = num_rows - 1 #adjust from n+1 to n
for x in range(value, 0, 3):
lower_bound = x-2
upper_bound = x
if lower_bound <= translated_input <= upper_bound:
translated_input = [num_rows, (translated_input - lower_bound)]
num_rows = num_rows - 1 #move down to the next row we're on
return translated_input
それが動作する方法は、数を入力することである
-------------
| 7 | 8 | 9 | #row 0
-------------
| 4 | 5 | 6 | #row 1
-------------
| 1 | 2 | 3 | #row 2
-------------
競技場の写真置き換えるインデックス。関数はその数値を[row、col] "value"に変換します。
ですから、例えば、4 => [1,0] OR 3 => [2,2]
任意のアイデア?私の頭をきれいにするために何か他のことをするつもりです。
整数除算を使用し、それは剰余です。 row = input/rowlength、col = input%rowlengthです。行を上から下に数えると、行が元に戻りますが、あなたはその考えを得ることを願っています。 –