Last active
November 16, 2018 15:37
-
-
Save 3211133/c271b1ad2371d767293f8196490f458e to your computer and use it in GitHub Desktop.
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 - osero | |
import sys | |
import random | |
def main(): | |
mode = int(input('プレイヤー人数を入力してください-0,1,2')) | |
while True: | |
盤面 = [[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]] | |
ターン = 1 | |
マス = ['○','×','●','☆'] | |
勝利 = 0 | |
符号 = [[1,1], [1,0], [1,-1], [0,1], [0,-1], [-1,1], [-1,0], [-1,-1]] | |
print('メイン関数') | |
while True: | |
#置ける駒を調べる | |
置けるか = False | |
for x in range(8):#全てのxyの組み合わせで調べてる | |
for y in range(8): | |
if 盤面[x][y] == 2:#前回☆にしたところを×に戻してる | |
盤面[x][y] = 0 | |
if 盤面[x][y] == 0:#×の中で置けるマスを探す | |
for X, Y in 符号: | |
cx, cy = x + X, y + Y#隣のマスが盤内で敵コマだったら調査続行 | |
if (-1 < cx < 8 and -1 < cy < 8) and 盤面[cx][cy] == -1 * ターン: | |
while True: | |
cx += X | |
cy += Y | |
if not (-1 < cx < 8 and -1 < cy < 8):#更に隣のマスが盤外だったら調査終了次へ | |
break | |
elif 盤面[cx][cy] == -1 * ターン:#更に隣のマスが敵コマだったらさらに隣のマスへ | |
continue | |
elif not 盤面[cx][cy] == ターン:#×とか☆とかだったら終了 | |
break | |
else:#自コマだったら置けるマスなので☆にする | |
置けるか = True | |
盤面[x][y] = 2 | |
break | |
else: | |
continue | |
#現在の盤面を表示する | |
print('*y|12345678')#上枠 | |
print('x |________') | |
for x in range(8): | |
print(x + 1, '|', end ='')#左枠 | |
for y in range(8): | |
print(マス[盤面[x][y] + 1], end = '')#中身 | |
print()#改行 | |
print(マス[ターン + 1] + 'のターンです') | |
#パス処理と試合終了処理 | |
if 置けるか:#置けるマスがあったら勝利カウントリセット | |
勝利 = 0 | |
else:#無かったらカウント上げる | |
勝利 += 1 | |
if 勝利 == 2:#両方置けなかったら試合終了 | |
print('試合終了') | |
試合結果 = 0 | |
for x in range(8): | |
for y in range(8): | |
試合結果 += 盤面[x][y]#このとき盤面に2はないので全部足し上げると●と〇の差が求められる | |
if 試合結果 < 0: | |
print('○の勝ち') | |
elif 試合結果 > 0: | |
print('●の勝ち') | |
else: | |
print('引き分け') | |
break | |
else:#まだ片方だけだったらターン切り替えて終える | |
ターン *= -1 | |
continue | |
#入力処理 | |
while True: | |
if (ターン == 1 and mode == 1) or mode == 2:#モードによって手動か否かを切り替える(1Pの〇は未対応) | |
try:#2桁の数字で座標指定させて分割してる | |
n = input('xy =') | |
n = int(n) | |
x = n // 10 - 1 | |
y = n % 10 - 1 | |
except:#エラー回避ついでに抜けたいときに抜けれる機能搭載 | |
print('正しく入力してください') | |
if n == 'exit': | |
sys.exit() | |
else:#適当にマスを選んで☆になるまで繰り返す。無駄すぎるから直す。 | |
x = random.randrange(8) | |
y = random.randrange(8) | |
if -1 < x < 8 and -1 < y < 8 and 盤面[x][y] == 2:#盤内かつ☆なら通して他はやり直し | |
break | |
#ひっくり返す処理 | |
for X, Y in 符号:#33行目とやってることはほぼ同じ | |
cx, cy = x + X, y + Y | |
if not(-1 < cx < 8 and -1 < cy < 8) or not 盤面[cx][cy] == -1 * ターン: | |
continue | |
else:#隣が敵駒か | |
while True: | |
cx += X | |
cy += Y | |
if not(-1 < cx < 8 and -1 < cy < 8) or 盤面[cx][cy] == 0 or 盤面[cx][cy] == 2: | |
break | |
elif 盤面[cx][cy] == -1 * ターン: | |
continue | |
elif 盤面[cx][cy] == ターン:#ここの処理だけ違う | |
while True:#一個ずつ戻りながら自駒にしてる | |
cx -= X | |
cy -= Y | |
盤面[cx][cy] = ターン | |
if cx == x and cy == y:#入力したところまでいったら終了 | |
break | |
break | |
ターン *= -1#最後にターン切り替えて終了 | |
select = input('continue?-no, yes:')#試合終わったら終わるかどうか聞いてnoじゃなかったら繰り返す | |
if select == 'no': | |
sys.exit() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment