オセロを作る(4)
- 今日の作業
- 相手に色を挟む位置にしかコマを置けないようにした(今まではクリックすればどこでも置けた)。
- マジックナンバーをなくした。変数BLACK, WHITE, BLANKを定義。
- クリック毎に順番を変えるのでなく、コマを交互に置けるようにした(あたり前)。
- 今後の作業
- 終了時の勝敗判定をする。
- 置く場所がないときパスできるようにする。
- 人間対コンピュータを実現(Pygameとは別の話)。
ソースは以下の通り。人間対人間用。黒が先手です。
#!/bin/env python # coding: utf-8 # ------------------------------------------------------------------ # Mini Othello ver.0.4 # 2008/11/06 import pygame import sys from pygame.locals import * WDTH = 50 # 盤上に並べるタイルの幅 TNUM = 8 # 一辺のタイルの数:総数はTNUM*TNUMになる BLACK = 1 WHITE = 2 BLANK = 0 SCR_W = WDTH * TNUM # ウィンドウの大きさ(幅) SCR_H = WDTH * TNUM # ウィンドウの大きさ(縦) WPOS = WDTH*(TNUM-1) """ 初期設定 """ pygame.init() screen = pygame.display.set_mode( (SCR_W, SCR_H) ) image1 = pygame.image.load('tile.bmp') image2 = pygame.image.load('black.bmp') image3 = pygame.image.load('white.bmp') image1 = image1.convert() image2 = image2.convert() image3 = image3.convert() ckey2 = image2.get_at((0,0)) ckey3 = image3.get_at((0,0)) image2.set_colorkey(ckey2) image3.set_colorkey(ckey3) imagerect1 = image1.get_rect() imagerect2 = image2.get_rect() imagerect3 = image3.get_rect() """ コマの色を記録する変数を定義 komacolor=BLANK : コマは置かれていない =BLACK : 黒コマ =WHITE : 白コマ """ komacolor = [[0 for x in range(TNUM)] for x in range(TNUM)] global rnum def flipkomaN1(x, y, rnum): """ 白コマを反転させる(north方向)""" if komacolor[x][y-1] == WHITE and y >= 2: if komacolor[x][y-2] == BLANK: return False if komacolor[x][y-2] == BLACK: for i in range(rnum+1): screen.blit(image2, imagerect2.move(WDTH*x,WDTH*(y-1+i))) komacolor[x][y-1+i] = BLACK return True else: rnum +=1 flipkomaN1(x,y-1, rnum) return False def flipkomaN2(x, y, rnum): """ 黒コマを反転させる(north方向)""" if komacolor[x][y-1] == BLACK and y >= 2: if komacolor[x][y-2] == BLANK: return False if komacolor[x][y-2] == WHITE: for i in range(rnum+1): screen.blit(image3, imagerect3.move(WDTH*x,WDTH*(y-1+i))) komacolor[x][y-1+i] = WHITE return True else: rnum +=1 flipkomaN2(x,y-1, rnum) return False def flipkomaS1(x, y, rnum): """ 白コマを反転させる(south方向)""" if y <= TNUM-3 and komacolor[x][y+1] == WHITE: if komacolor[x][y+2] == BLANK: return False if komacolor[x][y+2] == BLACK: for i in range(rnum+1): screen.blit(image2, imagerect2.move(WDTH*x,WDTH*(y+1-i))) komacolor[x][y+1-i] = BLACK return True else: rnum +=1 flipkomaS1(x,y+1, rnum) return False def flipkomaS2(x, y, rnum): """ 黒コマを反転させる(south方向)""" if y <= TNUM-3 and komacolor[x][y+1] == BLACK: if komacolor[x][y+2] == BLANK: return False if komacolor[x][y+2] == WHITE: for i in range(rnum+1): screen.blit(image3, imagerect3.move(WDTH*x,WDTH*(y+1-i))) komacolor[x][y+1-i] = WHITE return True else: rnum +=1 flipkomaS2(x,y+1, rnum) return False def flipkomaW1(x, y, rnum): """ 白コマを反転させる(west方向)""" if komacolor[x-1][y] == WHITE and x >= 2: if komacolor[x-2][y] == BLANK: return False if komacolor[x-2][y] == BLACK: for i in range(rnum+1): screen.blit(image2, imagerect2.move(WDTH*(x-1+i),WDTH*y)) komacolor[x-1+i][y] = BLACK return True else: rnum +=1 flipkomaW1(x-1,y, rnum) return False def flipkomaW2(x, y, rnum): """ 黒コマを反転させる(west方向)""" if komacolor[x-1][y] == BLACK and x >= 2: if komacolor[x-2][y] == BLANK: return False if komacolor[x-2][y] == WHITE: for i in range(rnum+1): screen.blit(image3, imagerect3.move(WDTH*(x-1+i),WDTH*y)) komacolor[x-1+i][y] = WHITE return True else: rnum +=1 flipkomaW2(x-1,y, rnum) return False def flipkomaE1(x, y, rnum): """ 白コマを反転させる(east方向)""" if x <= TNUM-3 and komacolor[x+1][y] == WHITE: if komacolor[x+2][y] == BLANK: return False if komacolor[x+2][y] == BLACK: for i in range(rnum+1): screen.blit(image2, imagerect2.move(WDTH*(x+1-i),WDTH*y)) komacolor[x+1-i][y] = BLACK return True else: rnum +=1 flipkomaE1(x+1,y, rnum) return False def flipkomaE2(x, y, rnum): """ 黒コマを反転させる(east方向)""" if x <= TNUM-3 and komacolor[x+1][y] == BLACK: if komacolor[x+2][y] == BLANK: return False if komacolor[x+2][y] == WHITE: for i in range(rnum+1): screen.blit(image3, imagerect3.move(WDTH*(x+1-i),WDTH*y)) komacolor[x+1-i][y] = WHITE return True else: rnum +=1 flipkomaE2(x+1,y, rnum) return False def flipkomaNW1(x, y, rnum): """ 白コマを反転させる(north-west方向)""" if komacolor[x-1][y-1] == WHITE and y >= 2 and x >= 2: if komacolor[x-2][y-2] == BLANK: return False if komacolor[x-2][y-2] == BLACK: for i in range(rnum+1): screen.blit(image2, imagerect2.move(WDTH*(x-1+i),WDTH*(y-1+i))) komacolor[x-1+i][y-1+i] = BLACK return True else: rnum +=1 flipkomaNW1(x-1,y-1, rnum) return False def flipkomaNW2(x, y, rnum): """ 黒コマを反転させる(north-west方向)""" if komacolor[x-1][y-1] == BLACK and y >= 2 and x >= 2: if komacolor[x-2][y-2] == BLANK: return False if komacolor[x-2][y-2] == WHITE: for i in range(rnum+1): screen.blit(image3, imagerect3.move(WDTH*(x-1+i),WDTH*(y-1+i))) komacolor[x-1+i][y-1+i] = WHITE return True else: rnum +=1 flipkomaNW2(x-1,y-1, rnum) return False def flipkomaSE1(x, y, rnum): """ 白コマを反転させる(south-east方向)""" if x <= TNUM-3 and y <= TNUM-3 and komacolor[x+1][y+1] == WHITE: if komacolor[x+2][y+2] == BLANK: return False if komacolor[x+2][y+2] == BLACK: for i in range(rnum+1): screen.blit(image2, imagerect2.move(WDTH*(x+1-i),WDTH*(y+1-i))) komacolor[x+1-i][y+1-i] = BLACK return True else: rnum +=1 flipkomaSE1(x+1,y+1, rnum) return False def flipkomaSE2(x, y, rnum): """ 黒コマを反転させる(south-east方向)""" if x <= TNUM-3 and y <= TNUM-3 and komacolor[x+1][y+1] == BLACK: if komacolor[x+2][y+2] == BLANK: return False if komacolor[x+2][y+2] == WHITE: for i in range(rnum+1): screen.blit(image3, imagerect3.move(WDTH*(x+1-i),WDTH*(y+1-i))) komacolor[x+1-i][y+1-i] = WHITE return True else: rnum +=1 flipkomaSE2(x+1,y+1, rnum) return False def flipkomaSW1(x, y, rnum): """ 白コマを反転させる(south-west方向)""" if y <= TNUM-3 and x >= 2 and komacolor[x-1][y+1] == WHITE: if komacolor[x-2][y+2] == BLANK: return False if komacolor[x-2][y+2] == BLACK: for i in range(rnum+1): screen.blit(image2, imagerect2.move(WDTH*(x-1+i),WDTH*(y+1-i))) komacolor[x-1+i][y+1-i] = BLACK return True else: rnum +=1 flipkomaSW1(x-1,y+1, rnum) return False def flipkomaSW2(x, y, rnum): """ 黒コマを反転させる(south-west方向)""" if y <= TNUM-3 and x >= 2 and komacolor[x-1][y+1] == BLACK: if komacolor[x-2][y+2] == BLANK: return False if komacolor[x-2][y+2] == WHITE: for i in range(rnum+1): screen.blit(image3, imagerect3.move(WDTH*(x-1+i),WDTH*(y+1-i))) komacolor[x-1+i][y+1-i] = WHITE return True else: rnum +=1 flipkomaSW2(x-1,y+1, rnum) return False def flipkomaNE1(x, y, rnum): """ 白コマを反転させる(north-east方向)""" if x <= TNUM-3 and y >= 2 and komacolor[x+1][y-1] == WHITE: if komacolor[x+2][y-2] == BLANK: return False if komacolor[x+2][y-2] == BLACK: for i in range(rnum+1): screen.blit(image2, imagerect2.move(WDTH*(x+1-i),WDTH*(y-1+i))) komacolor[x+1-i][y-1+i] = BLACK return True else: rnum +=1 flipkomaNE1(x+1,y-1, rnum) return False def flipkomaNE2(x, y, rnum): """ 黒コマを反転させる(north-east方向)""" if x <= TNUM-3 and y >= 2 and komacolor[x+1][y-1] == BLACK: if komacolor[x+2][y-2] == BLANK: return False if komacolor[x+2][y-2] == WHITE: for i in range(rnum+1): screen.blit(image3, imagerect3.move(WDTH*(x+1-i),WDTH*(y-1+i))) komacolor[x+1-i][y-1+i] = WHITE return True else: rnum +=1 flipkomaNE2(x+1,y-1, rnum) return False def main(): screen.fill((0,0,0)) pygame.display.set_caption('Mini Othello') pygame.mouse.set_visible(True) whoturn = 0 # どちらの順番かを記録しておく変数 """ 初期画面を描画 """ for i in xrange(0, WPOS+1, WDTH): for j in xrange(0, WPOS+1, WDTH): screen.blit(image1, imagerect1.move(i,j)) """ 始めから置かれている4つのコマ """ screen.blit(image2, imagerect2.move(WDTH*TNUM/2,WDTH*TNUM/2)) screen.blit(image2, imagerect2.move(WDTH*(TNUM/2-1),WDTH*(TNUM/2-1))) screen.blit(image3, imagerect3.move(WDTH*(TNUM/2-1),WDTH*TNUM/2)) screen.blit(image3, imagerect3.move(WDTH*TNUM/2,WDTH*(TNUM/2-1))) komacolor[TNUM/2][TNUM/2] = BLACK komacolor[TNUM/2-1][TNUM/2-1] = BLACK komacolor[TNUM/2-1][TNUM/2] = WHITE komacolor[TNUM/2][TNUM/2-1] = WHITE pygame.display.flip() while 1: for event in pygame.event.get(): if (event.type == KEYDOWN and event.key == K_ESCAPE): pygame.quit() sys.exit() if event.type == MOUSEBUTTONDOWN: xpos = int(pygame.mouse.get_pos()[0]/WDTH) ypos = int(pygame.mouse.get_pos()[1]/WDTH) if komacolor[xpos][ypos] == BLANK: if whoturn%2 == 0: """ 挟んだコマを裏返す """ if flipkomaN1(xpos, ypos, rnum=0) or\ flipkomaS1(xpos, ypos, rnum=0) or\ flipkomaW1(xpos, ypos, rnum=0) or\ flipkomaE1(xpos, ypos, rnum=0) or\ flipkomaNW1(xpos, ypos, rnum=0) or\ flipkomaSE1(xpos, ypos, rnum=0) or\ flipkomaSW1(xpos, ypos, rnum=0) or\ flipkomaNE1(xpos, ypos, rnum=0): """ 黒コマを置く """ screen.blit(image2, imagerect2.move(WDTH*xpos,WDTH*ypos)) komacolor[xpos][ypos] = BLACK whoturn += 1 else: """ 挟んだコマを裏返す """ if flipkomaN2(xpos, ypos, rnum=0) or\ flipkomaS2(xpos, ypos, rnum=0) or\ flipkomaW2(xpos, ypos, rnum=0) or\ flipkomaE2(xpos, ypos, rnum=0) or\ flipkomaNW2(xpos, ypos, rnum=0) or\ flipkomaSE2(xpos, ypos, rnum=0) or\ flipkomaSW2(xpos, ypos, rnum=0) or\ flipkomaNE2(xpos, ypos, rnum=0): """ 白コマを置く """ screen.blit(image3, imagerect3.move(WDTH*xpos,WDTH*ypos)) komacolor[xpos][ypos] = WHITE whoturn += 1 pygame.display.flip() if __name__ == '__main__': main()