Created
August 5, 2018 13:27
-
-
Save 3211133/2e10674614d82d3931b7dfa95a6f599e to your computer and use it in GitHub Desktop.
osero.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# osero.py -- オセロ | |
list = [[0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 1, -1, 0, 0, 0], | |
[0, 0, 0, -1, 1, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0], ] | |
mark = ['○', '×', '●', '☆'] | |
yourturn = 1 | |
fugo =[[1,1], [1,0], [1,-1], [0,1], [0,-1], [-1,1], [-1,0], [-1,-1]] #8方向チェック用符号 | |
while True: | |
goodturn = False #置ける場所があるか | |
for x in range(0,7): | |
for y in range(0,7): | |
if list[x][y] == 2: | |
list[x][y] = 0 | |
if list[x][y] == 0: | |
for X, Y in fugo: | |
cx = x + X#横が敵の石かどうかチェック | |
cy = y + Y | |
if list[cx][cy] == -1 * yourturn: | |
while -1 < cx < 8 and -1 < cy < 8:#盤外ではないことを確認 | |
cx += X | |
cy += Y | |
if list[cx][cy] == 0:#隣の隣以降空白ならその方向は裏返さない | |
break | |
elif list[cx][cy] == yourturn:#隣の隣以降自石なら裏返す処理 | |
list[x][y] = 2 | |
goodturn = True | |
x, y = 0, 0 | |
print(''' y01234567 | |
x/――――――――''') | |
while x < 8: | |
print(x, '|', end ='') | |
while y < 8: | |
print(mark[list[x][y] + 1], end ='') | |
y += 1 | |
print('') | |
y = 0 | |
x += 1 | |
if goodturn: | |
countdown = 0 | |
x = int(input('Turn:{}. Select ☆(x).'.format(mark[yourturn + 1]))) | |
y = int(input('Turn:{}. Select ☆(y).'.format(mark[yourturn + 1]))) | |
if list[x][y] == 2: | |
for X, Y in fugo: | |
cx = x + X#横が敵の石かどうかチェック | |
cy = y + Y | |
if list[cx][cy] == -1 * yourturn: | |
while -1 < cx < 8 and -1 < cy < 8:#盤外ではないことを確認 | |
cx += X | |
cy += Y | |
if list[cx][cy] == 0:#隣の隣以降空白ならその方向は裏返さない | |
break | |
elif list[cx][cy] == yourturn:#隣の隣以降自石なら裏返す処理 | |
while not (cx == x and cy == y): | |
list[cx - X][cy - Y] = yourturn | |
cx -= X | |
cy -= Y | |
break | |
else: | |
print('{} cant put anywhere.'.format(yourturn)) | |
countdown += 1#2になったら試合終了 | |
if countdown == 2: | |
break | |
yourturn *= -1 | |
winner = 0 | |
for n in list: | |
for m in n: | |
winner += m | |
if winner == 0: | |
print('引き分け') | |
elif winner < 0: | |
print('○の勝ち') | |
else: | |
print('●の勝ち') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment