リバーシ作成
By Patineboot
- 2 minutes read
- 674 words
pythonでリバーシ作ったよ。
コマが置けるかどうかの判定が難しかったよ!
Python 2.6.5で確認。
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
module details
"""
#import sys
import random
class ReversBoard:
def __init__(self):
self.board = [
[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, 2, 0, 0, 0],
[0, 0, 0, 2, 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],
]
self.directions = ((0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1))
def get_count(self, player):
count = 0
for i in self.board:
for j in i:
if j == player:
count += 1
return count
def draw(self):
index = 0
print ' 0 1 2 3 4 5 6 7'
for i in self.board:
print index,
for j in i:
if j == 1:
print 'o',
elif j == 2:
print 'x',
else:
print '.',
print index
index += 1
print ' 0 1 2 3 4 5 6 7'
print 'o = ', self.get_count(1), 'x = ', self.get_count(2)
def search_directions(self, player, x, y):
available_directions = [];
result = False
# 既においているかチェック
if self.board[y][x] != 0:
return result, available_directions
# 8方向のチェック
for xx, yy in self.directions:
# 隣のコマを調査する
challeng_x = x + xx
challeng_y = y + yy
# 盤外のチェックをしようとした場合
if (challeng_x < 0 or challeng_x > 7
or challeng_y < 0 or challeng_y > 7):
continue;
# 隣が自分のコマ、もしくはまだコマがおかれていない場合
if (self.board[challeng_y][challeng_x] == player or
self.board[challeng_y][challeng_x] == 0):
continue;
# 隣のコマの先で自分のコマが出現するかチェックをする
for i in xrange(2, 8):
challeng_x = x + xx * i
challeng_y = y + yy * i
# 盤外のチェックをしようとした場合
if (challeng_x < 0 or challeng_x > 7
or challeng_y < 0 or challeng_y > 7):
break;
# まだコマがおかれていない場合
if self.board[challeng_y][challeng_x] == 0:
break;
elif self.board[challeng_y][challeng_x] == player:
available_directions.append([xx, yy])
result = True
break;
return result, available_directions
def set_nextmove(self, player, x, y):
result, directions = self.search_directions(player, x, y)
# コマを裏返す。
for xx, yy in directions:
for i in xrange(1, 7):
challeng_x = x + xx * i
challeng_y = y + yy * i
if self.board[challeng_y][challeng_x] == player:
break
self.board[challeng_y][challeng_x] = player
# コマを置く。
self.board[y][x] = player
def is_collected(self, player, x, y):
result, directions = self.search_directions(player, x, y)
return result
class ReversPlayer:
def __init__(self, player):
self.player = player
class AutoPlayer(ReversPlayer):
def choice_nextmove(self, board):
while 1:
position = random.randint(0, 63)
x = position / 8
y = position % 8
# コマがおける場合
if board.is_collected(self.player, x, y):
break;
# コマを置く
board.set_nextmove(self.player, x, y)
class PersonPlayer(ReversPlayer):
def choice_nextmove(self, board):
while 1:
input = raw_input("push(xy): ")
if not input.isdigit():
continue
x = int(input[0])
y = int(input[1])
# コマがおける場合
if board.is_collected(self.player, x, y):
break;
# コマを置く
board.set_nextmove(self.player, x, y)
if __name__ == "__main__":
player1 = AutoPlayer(1)
# player2 = AutoPlayer(2)
player2 = PersonPlayer(2)
board = ReversBoard()
board.draw()
while 1:
player1.choice_nextmove(board)
board.draw()
player2.choice_nextmove(board)
board.draw()
count = board.get_count(1) + board.get_count(2)
if count == 64:
break;