U E D R , A S I H C RSS

TFP예제/Omok


1. TestCaseBoard.py

~cpp 
import unittest
from Board import * 

class BoardTestCase (unittest.TestCase):
    def setUp (self):
        self.dolboard = Board ()
        self.dolboard.Empty ()

    def tearDown (self):
        self.dolboard = None
        
    def testBoardEmpty (self):
        self.dolboard.Empty ()
        for i in range (0,19):
            for j in range (0,19):
                self.assertEquals (self.dolboard.GetDol(i,j) , DolEmpty)

    def testIsExistDolInPosition (self):
        self.dolboard.SetDol (1,1,DolWhite)
        self.assertEquals (self.dolboard.IsExistDolInPosition(1,1),1)
        self.assertEquals (self.dolboard.IsExistDolInPosition(3,6),0)
        
    def testDol (self):
        self.dolboard.SetDol (1,1,DolWhite)
        self.assertEquals (self.dolboard.GetDol(1,1), DolWhite)
        self.dolboard.SetDol (2,2,DolBlack)
        self.assertEquals (self.dolboard.GetDol(2,2), DolBlack)
        self.assertEquals (self.dolboard.GetDol(18,18), DolEmpty)
        self.assertEquals (self.dolboard.GetDol(6,6), DolEmpty)

        self.dolboard.SetDol (18,18,DolBlack)
        self.assertEquals (self.dolboard.GetDol(18,18), DolBlack)

    def testPrintEmptyBoard (self):
        self.dolboard.Empty ()
        self.assertEquals (self.dolboard.Print(), '''+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
''')
        
suite = unittest.makeSuite (BoardTestCase, "test")

runner = unittest.TextTestRunner ()
runner.run (suite)

2. TestCaseOmok.py

~cpp 
import unittest
from Omok import *

class OmokTestCase (unittest.TestCase):
    def setUp (self):
        self.omok = Omok ()
        self.omok.Start ()

    def tearDown (self):
        self.omok = None

    def testIsExistDolInPosition (self):
        self.omok.PutDol (2,2)
        self.assertEquals (self.omok.IsExistDolInPosition (2,2),1)
        self.omok.PutDol (0,0)
        self.assertEquals (self.omok.IsExistDolInPosition (0,0),1)
        self.assertEquals (self.omok.IsExistDolInPosition (16,16),0)

    def testChangeCurrentDol (self):
        currentDol = self.omok.GetCurrentDol ()
        self.omok.ChangeCurrentDol ()
        self.assertNotEqual (currentDol, self.omok.GetCurrentDol ())
            
    def testFirstDolIsBlackInStart (self):
        self.omok.Start ()
        self.assertEquals (self.omok.GetCurrentDol (), DolBlack)         

    def testPutDol (self):
        self.omok.PutDol (3,4)
        self.assertEquals (self.omok.GetDol (3,4), DolBlack)

    def testCurrentDol (self):
        self.omok.PutDol (3,4)
        self.assertEquals (self.omok.GetCurrentDol(), DolBlack)
        self.omok.PutDol (9,6)
        self.assertEquals (self.omok.GetCurrentDol(), DolBlack)

    def testCheckFiveLeftTopToRightBottom (self):
        self.omok.PutDol (0,0)      # black
        self.omok.PutDol (1,1)      # black
        self.omok.PutDol (2,2)      # black
        self.omok.PutDol (3,3)      # black
        self.omok.PutDol (4,4)      # black
        self.assertEquals (self.omok.CheckFiveLeftTopToRightBottom (4,4),1)

        self.omok.Start ()
        self.omok.PutDol (18,18)      # black
        self.omok.PutDol (17,17)      # black
        self.omok.PutDol (16,16)      # black
        self.omok.PutDol (15,15)      # black
        self.omok.PutDol (14,14)      # black
        self.assertEquals (self.omok.CheckFiveLeftTopToRightBottom (14,14),1)

        self.omok.Start ()
        self.omok.SetCurrentDol (DolWhite)
        self.omok.PutDol (5,4)      # white
        self.omok.PutDol (6,5)      # white
        self.omok.PutDol (7,6)      # white
        self.omok.PutDol (8,7)      # white
        self.omok.PutDol (9,8)      # white
        self.assertEquals (self.omok.CheckFiveLeftTopToRightBottom (9,8),1)

    def testCheckFiveLeftBottomToRightTop (self):
        self.omok.PutDol (0,5)      # black
        self.omok.PutDol (1,4)      # black
        self.omok.PutDol (2,3)      # black
        self.omok.PutDol (3,2)      # black
        self.omok.PutDol (4,1)      # black
        self.assertEquals (self.omok.CheckFiveLeftBottomToRightTop (4,1),1)

        self.omok.Start ()
        self.omok.PutDol (18,14)      # black
        self.omok.PutDol (17,15)      # black
        self.omok.PutDol (16,16)      # black
        self.omok.PutDol (15,17)      # black
        self.omok.PutDol (14,18)      # black
        self.assertEquals (self.omok.CheckFiveLeftBottomToRightTop (14,18),1)

        self.omok.Start ()
        self.omok.SetCurrentDol (DolWhite)
        self.omok.PutDol (5,8)      # white
        self.omok.PutDol (6,7)      # white
        self.omok.PutDol (7,6)      # white
        self.omok.PutDol (8,5)      # white
        self.omok.PutDol (9,4)      # white
        self.assertEquals (self.omok.CheckFiveLeftBottomToRightTop (9,4),1)

    def testCheckFiveHorizon (self):
        self.omok.PutDol (0,0)
        self.omok.PutDol (1,0)
        self.omok.PutDol (2,0)
        self.omok.PutDol (3,0)
        self.omok.PutDol (4,0)
        self.assertEquals (self.omok.CheckFiveHorizon (4,0), 1)

        self.omok.Start ()
        self.omok.PutDol (18,0)
        self.omok.PutDol (17,0)
        self.omok.PutDol (16,0)
        self.omok.PutDol (15,0)
        self.omok.PutDol (14,0)
        self.assertEquals (self.omok.CheckFiveHorizon (14,0), 1)

        self.omok.Start ()
        self.omok.PutDol (5,3) 
        self.omok.PutDol (6,3) 
        self.omok.PutDol (7,3) 
        self.omok.PutDol (8,3)
        self.omok.PutDol (9,3)
        self.assertEquals (self.omok.CheckFiveHorizon (9,3), 1)

    def testCheckFiveVertical (self):
        self.omok.PutDol (0,0)
        self.omok.PutDol (0,1)
        self.omok.PutDol (0,2)
        self.omok.PutDol (0,3)
        self.omok.PutDol (0,4)
        self.assertEquals (self.omok.CheckFiveVertical (0,4), 1)

        self.omok.Start ()
        self.omok.PutDol (0,18)
        self.omok.PutDol (0,17)
        self.omok.PutDol (0,16)
        self.omok.PutDol (0,15)
        self.omok.PutDol (0,14)
        self.assertEquals (self.omok.CheckFiveVertical (0,14), 1)

        self.omok.Start ()
        self.omok.PutDol (3,5)
        self.omok.PutDol (3,6)
        self.omok.PutDol (3,7)
        self.omok.PutDol (3,8)
        self.omok.PutDol (3,9)
        self.assertEquals (self.omok.CheckFiveVertical (3,9), 1)

suite = unittest.makeSuite (OmokTestCase, "test")

runner = unittest.TextTestRunner ()
runner.run (suite)

3. Board.py

~cpp 
import sys

DolEmpty = 9
DolBlack = 0
DolWhite = 1

class Board:
    def __init__(self):
        self.boardArray = {}

    def Empty(self):
        for i in range (0,19):
            for j in range (0,19):
                self.boardArray[(i,j)] = 9

    def GetDol (self,x,y):
        return self.boardArray[(y,x)]

    def SetDol (self,x,y,dol):
        self.boardArray[(y,x)] = dol

    def IsExistDolInPosition (self,x,y):
        if self.GetDol (x,y) != DolEmpty:
            return 1
        else:
            return 0

    def Print (self):
        PrintString = ''
        for i in range (0,19):
            for j in range (0,19):
                dol = "%s" % self.boardArray[(i,j)]
                if dol == '9':
                    dol = '+'
                PrintString += dol
            PrintString += "\n"

        return PrintString

4. Omok.py

~cpp 
from Board import *
import string

class Omok:
    def __init__(self):
        self.currentDol = DolEmpty
        self.board = Board ()
        self.currentWinner = DolEmpty
        pass

    def Start (self):
        self.currentDol = DolBlack
        self.board.Empty ()
        pass

    def GetCurrentDol (self):
        return self.currentDol

    def SetCurrentDol (self, dol):
        self.currentDol = dol    
    
    def ChangeCurrentDol (self):
        if self.currentDol == DolBlack:
            self.SetCurrentDol (DolWhite)
        elif self.currentDol == DolWhite:
            self.SetCurrentDol (DolBlack)

    def CheckFiveHorizon (self,x,y):
        for i in range (max(0,x-4),min (19,x+5)):
            CountSameDol = 0
            for j in range (i,min(19,i+5)):
                if self.board.GetDol (j,y) == self.currentDol:
                    CountSameDol += 1
                    if CountSameDol == 5:
                        return 1
        return 0

    def CheckFiveLeftTopToRightBottom (self,x,y):
        # Left-top -> Right-Bottom
        #   \
        #    \
        #     >
        for i in range (-4,5):
            CountSameDol = 0
            for j in range (i,i+5):
                if x + j < 0 or x + j > 18:
                    continue
                elif y + j < 0 or y + j > 18:
                    continue
                else:
                    if self.board.GetDol (x+j,y+j) == self.currentDol:
                        CountSameDol += 1
                        if CountSameDol == 5:
                            return 1
        return 0

    def CheckFiveLeftBottomToRightTop (self,x,y):
        # Left-Bottom -> Right-Top
        #    >
        #   /
        #  /
        for i in range (-4,5):
            CountSameDol = 0
            for j in range (i,i+5):
                if x + j < 0 or x + j > 18:
                    continue
                elif y - j < 0 or y - j > 18:
                    continue
                else:
                    if self.board.GetDol (x+j,y-j) == self.currentDol:
                        CountSameDol += 1
                        if CountSameDol == 5:
                            return 1
        return 0
           

    def CheckFiveVertical (self,x,y):
        for i in range (max(0,y-4),min (19,y+5)):
            CountSameDol = 0
            for j in range (i,min(19,i+5)):
                if self.board.GetDol (x,j) == self.currentDol:
                    CountSameDol += 1
                    if CountSameDol == 5:
                        return 1
        return 0       
        

    def PutDol (self,x,y):
        self.board.SetDol (x,y,self.currentDol)

    def GetDol (self,x,y):
        return self.board.GetDol (x,y)

    def CheckForFive (self,x,y):
        if self.CheckFiveHorizon (x,y) == 1:
            self.currentWinner = self.currentDol
        if self.CheckFiveVertical (x,y) == 1:
            self.currentWinner = self.currentDol
        if self.CheckFiveLeftTopToRightBottom (x,y) == 1:
            self.currentWinner = self.currentDol

    def CurrentWinner (self):
        return self.currentWinner
        
    def QuestionDolPosition (self):
        Position = raw_input ("Input Dol Position x,y :")
        Position = string.split (Position, ",")
        Position[0] = (int)(Position[0])
        Position[1] = (int)(Position[1])
        return Position

    def IsExistDolInPosition (self,x,y):
        return self.board.IsExistDolInPosition (x,y)
            
    def main (self):
        self.Start ()
        while self.CurrentWinner() == DolEmpty:
            print self.board.Print ()
            Position = self.QuestionDolPosition ()
            x,y = Position

            if not self.IsExistDolInPosition (x,y):
                self.PutDol (x,y)
                self.CheckForFive (x,y)
                self.ChangeCurrentDol ()

        print "Current Winner : %s" % self.CurrentWinner ()

if __name__ == '__main__':
    omok = Omok ()
    omok.main ()
    
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2009-05-27 07:09:19
Processing time 0.0957 sec