Example #1
0
 def testWin(self):
     b=Board()
     [b.mark(l,m) for l,m in ((2,'x'),(4,'x'))]
     self.assertFalse(b.isWinner('x'))
     b.mark(6, 'x')
     print b
     self.assertTrue(b.isWinner('x'))
Example #2
0
def play():
    b=Board()
    ai=AI('o',AI.moveAI)
    while not b.isFilled():
        print str(b) + '\nPlease input a number representing the space '\
          'you want to mark in the range 0-8, then press enter: '
        msg = sys.stdin.readline()
        try:
            location=int(msg)
            if b.isMarked(location):
                print('Please choose a space that has not already been filled in')
                continue
            b.mark(location, 'x')
            if b.isWinner('x'):
                print('X wins!')
                return
            if not b.isFilled():
                ai.turn(b)
                if b.isWinner('o'):
                    print('O wins!')
                    return
        except:
           print('Please input a valid number between 0-8')
    print('Stalemate, please play again!')
Example #3
0
 def testDetectLine(self):
     b=Board()
     [b.mark(l,m) for l,m in ((0,'x'),(4,'o'),(8,'x'))]
     smartAI=AI('o',AI.moveAI)
     self.assertEqual(smartAI.moveAI(b), 1)
Example #4
0
 def testDetectWin(self):
     b=Board()
     [b.mark(l,m) for l,m in ((2,'x'),(4,'x'))]
     smartAI=AI('x',AI.moveAI)
     self.assertEqual(smartAI.detectWin(b, 'x'), 6)
Example #5
0
 def testEmptyExcept(self):
     b=Board()
     [b.mark(l,m) for l,m in ((2,'x'),(4,'x'))]
     self.assertTrue(b.isEmptyExcept((2,4)))