シャノン寺 OXゲーム

http://atnd.org/events/35448
今回のIT寺小屋 シャノン寺ではOXゲームのプログラムを一緒に作りました。
参加された方の中から一番良いプログラムを載せます。

(python2.7動作確認済みです。)

import random
#import sys

#for display
L2 = [0,1,2,3,4,5,6,7,8]

#for choice
L1 = [0,1,2,3,4,5,6,7,8]

#display function
def display():

    print L2[0],
    print '|',
    print L2[1],
    print '|',
    print L2[2]

    print '__|___|___'

    print L2[3],
    print '|',
    print L2[4],
    print '|',
    print L2[5]

    print '__|___|___'

    print L2[6],
    print '|',
    print L2[7],
    print '|',
    print L2[8]

#Judge2 function
def Judge2(x,y,z,Q,R):
    if L2[x] == Q:
        if L2[y] == Q:
            if L2[z] == Q:
                print 'You' + R
                exit("Game Over")
            else:
                print '',
        else:
            print '',

#Judge function
def Judge (Q,R):
    if L2[0] == Q:
        if L2[1] == Q:
            if L2[2] == Q:
                print 'You' + R
                exit("Game Over")
            else:
                print'',
        elif L2[4] == Q:
            if L2[8] == Q:
                print 'You' + R
                exit("Game Over")
            else:
                print '',
        elif L2[3] == Q:
            if L2[6] == Q:
                print 'You' + R
                exit("Game Over")
            else:
                print '',
        else:
            print '',    

    if L2[2] == Q:
        if L2[4] == Q:
            if L2[6] == Q:
                print 'You' + R
                exit("Game Over")
            else:
                print '',
        elif L2[5] == Q:
            if L2[8] == Q:
                print 'You' + R
                exit("Game Over")
            else:
                print '',
        else:
            print '',

    Judge2(1,4,7,Q,R)
    Judge2(3,4,5,Q,R)
    Judge2(6,7,8,Q,R)    

M = [0,1,2,3,4]

for i in M:

    #turn of enemy
    r = random.choice(L1)
    while r == 9:
        r = random.choice(L1)

    L2[r] = 'o'
    L1[r] = 'o'

    L1.remove("o")
    L1.insert(r,9)

    display()

    Judge ('o','Lose')

    print ''

    #turn of player
    y = input()

    L2[y] = 'X'
    L1[y] = 'X'

    L1.remove ('X')
    L1.insert(y,9)

    display()

    print ''

    Judge ("X","Win")

    print ''

寺小屋長からのコメント:
良くできています。
while r == 9:
r = random.choice(L1)
の辺が改善のよちがありそうです。

Comments are closed.