1

これまでのところ、2 人のプレイヤーがクリックして X と O を順番に配置できるプログラムがあります。プログラムに勝者/抽選を認識させる方法がわかりません。何らかの方法で画面上で勝ち/引き分けを示す関数を作成するのを手伝ってくれたら、私はあなたを永遠に愛します. ありがとう。

from graphics import *

import sys


def player_o(win, center):
'''
Parameters:
- win: the window
'''
    outline_width = 5
    circle = Circle(center, boxsize/2)
    circle.setOutline('red')
    circle.setWidth(outline_width)
    circle.draw(win)


def player_x(win, p1x, p1y):
'''
Parameters:
- win: the window
'''
for i in range(2):
    deltaX = (-1) ** i * (boxsize / 2)
    deltaY = (boxsize / 2)
    line = Line(Point(p1x - deltaX, p1y - deltaY),
             Point(p1x + deltaX, p1y + deltaY))
    line.setFill('red')
    line.setWidth(5)
    line.draw(win)



def game():


global win
global boxsize

    try:
        winsize = int(input("How large would you like the window? (Between 100 and 3000): "))
        if winsize < 100 or winsize > 3000:
            print("Invalid window size")
            quit()

    squares = int(input("How many squares per row? (Between 3 and 10):"))
    boxsize = winsize/ squares
    if squares < 3 or squares > winsize / 10:
        print("Invalid number")
        quit()
    except ValueError:
        sys.exit("Not a valid number")

    win = GraphWin("Tic Tac Toe", winsize, winsize)

    for i in range(squares - 1):
        hline = Line(Point(0, (winsize/squares) * (i + 1)), Point(winsize,  (winsize/squares) * (i + 1)))
        hline.draw(win)
        vline = Line(Point((winsize/squares) * (i + 1), 0), Point((winsize/squares) * (i + 1), winsize))
        vline.draw(win)




for i in range((squares ** 2) // 2):

    print("X, click a square.")
    p1mouse = win.getMouse()
    p1x = p1mouse.getX()
    p1y = p1mouse.getY()
    player_x(win, p1x, p1y)

    print("O, click a square.")
    p2mouse = win.getMouse()
    p2x = p2mouse.getX()
    p2y = p2mouse.getY()
    player_o(win, Point(p2x, p2y))

if squares % 2 == 1:
    print("X, click a square.")
    p1mouse = win.getMouse()
    p1x = p1mouse.getX()
    ply = p1mouse.getY()
    player_x(win, p1x, p1y)

game()
4

3 に答える 3

6

データとデータの表現を分離してください。それが方法です。現時点では、競技場の表現 (たとえば、ボックスのリストとその状態、p1 でチェック済み、p2 でチェック済み、またはチェック解除済み) を生成するのではなく、単に描画しているだけです。必要なときにそれを使用して描画します。利点はすぐに明らかになるはずです。ゲームの状態を知っていれば、勝者がいるかどうか (そして誰が誰であるか) を判断するのは簡単です。

于 2012-12-08T17:20:24.103 に答える
0

ゲーム内で移動するたびに、2D配列または辞書(値はリスト)を使用する必要があります。次に、それぞれの勝ち方を確認できます。このようにして、移動が有効かどうかを確認することもできます---ボード上のスポットが取得されたかどうか。

また、動きを指示するために数値または座標系を使用することをお勧めします。

ボードは次のようになります。

1 2 3
4 5 6
7 8 9

番号はボード上の対応するスポットです。

例えば:

初期化では:

moves = 0
positions = {'1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0, '9':0}
# '1' - '9' are the spots on the board.
# 0 means the spot is empty, 'X' means the spot is taken by 'X', 'O' means the spot is taken by 'O'. You can use any other naming system, but this is simple.

ムーブメントコード:

while 1 == 1: # just a loop until the input is valid. See the 'break' statement below
   new_move = input("X, enter what space to move to: ")
   if positions[new_move] == 0: # if that board spot is empty
      moves += 1 #moves = moves + 1
      positions[new_move] == 'X' # board spot is now occupied by 'X'
      # code to show the piece on the board
      if moves >= 5: # least possible moves to win is 5
         win_check(positions)
      break

または、ムーブメントを関数として使用し、入力が有効になるまで再帰的に自分自身を呼び出すようにすることもできます。

def move_X():
   new_move = input("X, enter what space to move to: ")
   if positions[new_move] == 0: # if that board spot is empty
      moves += 1 #moves = moves + 1
      positions[new_move] == 'X' # board spot is now occupied by 'X'
      # code to show the piece on the board
      if moves >= 5: # least possible moves to win is 5
         win_check(positions)
      move_O() # this should be defined similarly to 'move_X' except that it would correlate to 'O'.
   else:
      move_X()

勝利チェック方法:

def win_check(positions):
   if positions['1'] == 'X' and positions['2'] == 'X' and positions['3'] == 'X':
      return "Winner: X"
   elif # similar things, checking all of the other ways to win.

各プレイヤーが勝つには8つの方法があるため、if(最初に)1つのステートメントと15のステートメントが必要です。したがって、16のチェックを行う必要があります。elif

于 2012-12-08T21:36:52.907 に答える
0

3ターン後(勝つための最小ターン)、最後にプレイされたトークンの隣にトークンがあるかどうかを2次元配列で確認します。見つかった場合は、配列インデックスに対して同じ操作を繰り返します。

2 番目の制御構造に到達した場合は、ブレークして勝者を発表します。

于 2012-12-08T17:45:00.887 に答える