Example #1
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 #2
0
 def testName(self):
     for x in range(1,10000):
         b=Board()
         randomAI=AI('x',AI.moveRandom)
         smartAI=AI('o',AI.moveAI)
         while not b.isFilled():
             randomAI.turn(b)
             if b.isWinner('o'):
                print 'o is winner!\n' + str(b)
                break
             smartAI.turn(b)
             if b.isWinner('x'):
                print 'x is winner!\n' + str(b)
                self.fail('X won with moves ' + str(b.moves))
         print 'Finished game ' + str(x) + '\n'
     pass
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)