Esempio n. 1
0
def test_xo_True():
    assert xo('xo') is True
    assert xo('XO') is True
    assert xo('xo0') is True
    assert xo('') is True
    assert xo('xxxxxoooxooo') is True
    assert xo('xxxxxoooXooo') is True
    assert xo('abcdefghijklmnopqrstuvwxyz') is True
Esempio n. 2
0
    def test(self):
        self.assertTrue(xo('xo'), 'True expected')
        self.assertTrue(xo('XO'), 'True expected')
        self.assertTrue(xo('xo0'), 'True expected')
        self.assertTrue(not xo('xxxoo'), 'False expected')

        self.assertTrue(xo(''),
                        'Empty string contains equal amount of x and o')
        self.assertTrue(xo('xxxxxoooxooo'), 'True expected')
        self.assertTrue(xo('xxxxxoooXooo'), 'Comparision is case-insensitive')
        self.assertTrue(xo('abcdefghijklmnopqrstuvwxyz'),
                        'Alphabet contains equal amount of x and o')
Esempio n. 3
0
 def test_rand(self):
     for _ in range(0, 20):
         x, exp = randint(10, 30), False
         s = list(x * "o" + x * "x" + "AbCdEfGhJkLmNpQrStUvWyZ"[0:x % 24])
         shuffle(s)
         s = "".join(s)
         if x % 2:
             exp = True
             r = randint(0, 5)
             if r == 1: s = s.replace('x', 'X')
             if r == 2: s = s.replace('o', 'O')
         else:
             s = s.replace('o', '', 1 + randint(0, 10))
         self.assertEqual(xo(s), exp)
Esempio n. 4
0
def main():
    PLAY_ORDER = [1, 2]
    g = xo(3, 3, 2, ['X', 'O'], PLAY_ORDER)
    player = tdPlayer(1, 'tdTest', g)
Esempio n. 5
0
def main():
    global LINE
    PLAY_ORDER = [1, 2]
    g = xo(3, 3, 2, ['X', 'O'], PLAY_ORDER)
    name = input("Player 1 - Enter your name :")
    player1 = humanPlayer(1, name, g)
    player2 = randomPlayer(2, "Random Player", g)
    print('\nTic Tac Toe Platform\n' + player1.title + ' vs ' + player2.title)
    console_log(0, LINE)

    rand1_win = 0
    rand1_win_turns = 0
    rand2_win = 0
    rand2_win_turns = 0
    draw = 0

    f = open('game_log_' + player1.name + '-vs-' + player2.name + '.csv', 'w')
    # Play N Games
    for i in range(0, N_GAMES):
        csv_line = ''
        g.reset()
        current_winner = 0
        # create a game instance
        if ROLL:
            PLAY_ORDER = roll(PLAY_ORDER, 1)
        # Keep playing till the game in not over.
        while (g.game_over == False):
            while True:
                move = player1.play()
                result = g.mark(move, 1)
                console_log(3, 'Result: ' + str(result) + "\n")
                if (not ((result == err.INVALID_MOVE) |
                         (result == err.OUT_OF_TURN))):
                    break
            if (result == err.DRAW):
                console_log(2, g.get_board())
                current_winner = 0
                draw += 1
            if (result == err.WIN):
                console_log(2, '______________________')
                console_log(2, g.get_board())
                console_log(1, player1.name + ' has won')
                current_winner = 1
                console_log(2, '______________________')
                rand1_win += 1
                rand1_win_turns += g.game_turn
            if (g.game_over == False):
                while True:
                    move = player2.play()
                    result = g.mark(move, 2)
                    console_log(3, 'Result: ' + str(result) + "\n")
                    if (not ((result == err.INVALID_MOVE) |
                             (result == err.OUT_OF_TURN))):
                        break
                if (result == err.DRAW):
                    console_log(2, g.get_board())
                    current_winner = 0
                    draw += 1
                if (result == err.WIN):
                    console_log(2, "______________________")
                    console_log(2, g.get_board())
                    console_log(1, player2.name + " has won")
                    console_log(2, "______________________")
                    current_winner = 2
                    rand2_win += 1
                    rand2_win_turns += g.game_turn
        # Log game to CSV file
        for i, e in enumerate(g.play_order):
            csv_line += str(e) + ','
        csv_line += str(current_winner)
        for e in g.gameLog[:, 1]:
            csv_line += ', ' + str(e)
        csv_line += '\n'
        f.write(csv_line)
    f.close()
    console_log(1, LINE)
    print(player1.name + " Winner :" + str(rand1_win))
    if (rand1_win > 0):
        print("Average Moves to win :" + str(rand1_win_turns / rand1_win))
    print(player2.name + " Winner :" + str(rand2_win))
    if (rand2_win > 0):
        print("Average Moves to win :" + str(rand2_win_turns / rand2_win))
    print("Draw :" + str(draw))
Esempio n. 6
0
from xo import xo

print (xo('xo'))
print (xo('xo0'))
print (xo('xxxoo'))

Esempio n. 7
0
def main():
    global LINE
    PLAY_ORDER = [2, 1]
    g = xo(3, 3, 2, ["X", "O"], PLAY_ORDER)
    player1 = random1SSadistPlayer(1, "Rand1SSaddist", g)
    player2 = random1SPlayer(2, "Rnd1SPlayer", g)
    print("\nThe XO - Tic Tac Toe Platform\n" + player1.title + " vs " + player2.title)
    console_log(0, LINE)

    player1_win = 0
    player1_win_turns = 0
    player2_win = 0
    player2_win_turns = 0
    draw = 0

    # Play N Games
    csv_line = ""
    for i in range(0, N_GAMES):
        g.reset()
        player1.reset()
        player2.reset()
        current_winner = 0
        # create a game instance
        if ROLL:
            PLAY_ORDER = roll(PLAY_ORDER, 1)
            # Keep playing till the game in not over.
        while not g.game_over:
            move, result = player1.play()
            # result = g.mark(move, 1)
            console_log(3, "Result: " + str(result) + "\n")
            if result == err.DRAW:
                console_log(2, "______________________")
                console_log(2, g.get_board())
                console_log(1, "Game Drawn")
                console_log(2, "______________________")
                current_winner = 0
                draw += 1
            if result == err.WIN:
                console_log(2, "______________________")
                console_log(2, g.get_board())
                console_log(1, player1.name + " has won")
                console_log(2, "______________________")
                current_winner = 1
                player1_win += 1
                player1_win_turns += g.game_turn
            if not g.game_over:
                move, result = player2.play()
                # result = g.mark(move, 2)
                console_log(3, "Result: " + str(result) + "\n")
                if result == err.DRAW:
                    console_log(2, "______________________")
                    console_log(2, g.get_board())
                    console_log(1, "Game Drawn")
                    console_log(2, "______________________")
                    draw += 1
                    current_winner = 0
                    draw += 1
                if result == err.WIN:
                    console_log(2, "______________________")
                    console_log(2, g.get_board())
                    console_log(1, player2.name + " has won")
                    console_log(2, "______________________")
                    current_winner = 2
                    player2_win += 1
                    player2_win_turns += g.game_turn
                    # Log game to CSV file
        for i, e in enumerate(g.play_order):
            csv_line += str(e) + ","
        csv_line += str(current_winner)
        for e in g.gameLog[:, 1]:
            csv_line += ", " + str(e)
        csv_line += "\n"
    f = open("game_log_" + player1.name + "-vs-" + player2.name + ".csv", "w")
    f.write(csv_line)
    f.close()
    console_log(1, "\n" + LINE)
    print(player1.name + " Winner :" + str(player1_win))
    if player1_win > 0:
        print("Average Moves to win :" + str(player1_win_turns / player1_win))
    print(player2.name + " Winner :" + str(player2_win))
    if player2_win > 0:
        print("Average Moves to win :" + str(player2_win_turns / player2_win))
    print("Draw :" + str(draw))
Esempio n. 8
0
 def test_3(self):
     result = xo("xxxoo")
     self.assertEqual(result, False)
Esempio n. 9
0
 def test_2(self):
     self.expect(xo('xo0'))
Esempio n. 10
0
 def test_1(self):
     self.expect(xo('xo'))
Esempio n. 11
0
 def test_3(self):
     self.expect(not xo('xxxoo'))
Esempio n. 12
0
def main():
    global LINE
    PLAY_ORDER = [1, 2]
    g = xo(3, 3, 2, ['X', 'O'], PLAY_ORDER)
    player1 = tdPlayer(2, "tdPlayer", g)
    player2 = randomPlayer(1, "Rand", g)
    print('\nThe XO - Tic Tac Toe Platform\n' + player1.title + ' vs ' +
          player2.title)
    console_log(0, LINE)

    player1_win = 0
    player1_win_turns = 0
    player2_win = 0
    player2_win_turns = 0
    draw = 0

    # Play N Games
    csv_line = ''
    for i in range(0, N_GAMES):
        g.reset()
        player1.reset()
        player2.reset()
        current_winner = 0
        # create a game instance
        if ROLL:
            PLAY_ORDER = roll(PLAY_ORDER, 1)
        # Keep playing till the game in not over.
        while (not g.game_over):
            move = player1.play()
            result = g.mark(move, 1)
            console_log(3, 'Result: ' + str(result) + '\n')
            if (result == err.DRAW):
                console_log(2, '______________________')
                console_log(2, g.get_board())
                console_log(1, 'Game Drawn')
                console_log(2, '______________________')
                current_winner = 0
                draw += 1
            if (result == err.WIN):
                console_log(2, '______________________')
                console_log(2, g.get_board())
                console_log(1, player1.name + ' has won')
                console_log(2, '______________________')
                current_winner = 1
                player1_win += 1
                player1_win_turns += g.game_turn
            if (not g.game_over):
                move = player2.play()
                result = g.mark(move, 2)
                console_log(3, 'Result: ' + str(result) + '\n')
                if (result == err.DRAW):
                    console_log(2, '______________________')
                    console_log(2, g.get_board())
                    console_log(1, 'Game Drawn')
                    console_log(2, '______________________')
                    draw += 1
                    current_winner = 0
                    draw += 1
                if (result == err.WIN):
                    console_log(2, "______________________")
                    console_log(2, g.get_board())
                    console_log(1, player2.name + " has won")
                    console_log(2, "______________________")
                    current_winner = 2
                    player2_win += 1
                    player2_win_turns += g.game_turn
        # Log game to CSV file
        for i, e in enumerate(g.play_order):
            csv_line += str(e) + ','
        csv_line += str(current_winner)
        for e in g.gameLog[:, 1]:
            csv_line += ', ' + str(e)
        csv_line += '\n'
    f = open('game_log_' + player1.name + '-vs-' + player2.name + '.csv', 'w')
    f.write(csv_line)
    f.close()
    console_log(1, '\n' + LINE)
    print(player1.name + ' Winner :' + str(player1_win))
    if (player1_win > 0):
        print('Average Moves to win :' + str(player1_win_turns / player1_win))
    print(player2.name + ' Winner :' + str(player2_win))
    if (player2_win > 0):
        print("Average Moves to win :" + str(player2_win_turns / player2_win))
    print('Draw :' + str(draw))
Esempio n. 13
0
 def test_xo(self):
     self.assertEqual(xo.xo("ooxx"), True)
     self.assertEqual(xo.xo("xooxx"), False)
     self.assertEqual(xo.xo("ooxXm"), True)
     self.assertEqual(xo.xo("zpzpzpp"), True)
     self.assertEqual(xo.xo("zzoo"), False)
Esempio n. 14
0
 def test_2(self):
     result = xo("xo0")
     self.assertEqual(result, True)
Esempio n. 15
0
def test_xo_False():
    assert xo('xxxoo') is False
    assert xo('xXxXOoOoOo') is False
    assert xo('x') is False
    assert xo('O') is False
Esempio n. 16
0
def main():
	global LINE
	PLAY_ORDER = [1,2]
	g=xo(3,3,2,['X','O'],PLAY_ORDER)
	name = input("Player 1 - Enter your name :")
	player1 = humanPlayer(1,name,g)
	player2 = randomPlayer(2,"Random Player",g)	
	print('\nTic Tac Toe Platform\n'+player1.title+' vs '+player2.title)
	console_log(0,LINE)
	
	rand1_win = 0
	rand1_win_turns = 0
	rand2_win = 0
	rand2_win_turns = 0
	draw = 0
	
	f = open('game_log_'+player1.name+'-vs-'+player2.name+'.csv', 'w')
	# Play N Games
	for i in range (0, N_GAMES):
		csv_line = ''
		g.reset()
		current_winner = 0
		# create a game instance
		if ROLL :
			PLAY_ORDER = roll (PLAY_ORDER,1)
		# Keep playing till the game in not over.
		while(g.game_over == False):
			while True:
				[move,result] = player1.play()
				console_log (3,'Result: '+str(result)+"\n")
				if (not((result == err.INVALID_MOVE) | (result == err.OUT_OF_TURN))):
					break
			if (result == err.DRAW):
				console_log (2, g.get_board())
				current_winner = 0;
				draw += 1
			if (result == err.WIN):
				console_log (2, '______________________')
				console_log (2, g.get_board())
				console_log (1, player1.name+' has won')
				current_winner = 1
				console_log (2, '______________________')
				rand1_win += 1
				rand1_win_turns += g.game_turn
			if (g.game_over == False):
				while True:
					[move, result] = player2.play()
					console_log (3,'Result: '+str(result)+"\n")
					if (not((result == err.INVALID_MOVE) | (result == err.OUT_OF_TURN))):
						break
				if (result == err.DRAW):
					console_log (2,g.get_board())
					current_winner = 0
					draw += 1
				if (result == err.WIN):
					console_log (2, "______________________")
					console_log (2, g.get_board())
					console_log (1, player2.name+" has won")
					console_log (2, "______________________")
					current_winner = 2
					rand2_win += 1
					rand2_win_turns += g.game_turn;
		# Log game to CSV file
		for i, e in enumerate(g.play_order):
			csv_line += str(e)+','
		csv_line += str(current_winner)
		for e in g.gameLog[:,1]:
			csv_line += ', '+str(e)
		csv_line += '\n'
		f.write(csv_line)
	f.close()
	console_log(1,LINE)
	print (player1.name+" Winner :"+str(rand1_win))
	if (rand1_win>0):
		print ("Average Moves to win :"+str(rand1_win_turns/rand1_win))
	print (player2.name+" Winner :"+str(rand2_win))
	if (rand2_win>0):
		print ("Average Moves to win :"+str(rand2_win_turns/rand2_win))
	print ("Draw :"+str(draw))
Esempio n. 17
0
def main():
	PLAY_ORDER = [1, 2]
	g = xo(3, 3, 2, ['X', 'O'], PLAY_ORDER)
	player = tdPlayer(1, 'tdTest', g)