Last active
May 5, 2018 09:05
-
-
Save ivopetiz/c9f6625be573102d13be0a4cbcde84c6 to your computer and use it in GitHub Desktop.
Tictactoe game written in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import time | |
win_combos = [[1,2,3], | |
[4,5,6], | |
[7,8,9], | |
[1,5,9], | |
[3,5,7], | |
[1,4,7], | |
[2,5,8], | |
[3,6,9]] | |
## | |
## | |
## ao passar para classe variaveis ficam no __init__ | |
## | |
def initBoard(): | |
clearWindow() | |
global new_a_board, new_a_unused, \ | |
new_array_X, new_array_O | |
new_a_board = [' '] * 10 | |
new_a_unused = [1,2,3,4,5,6,7,8,9] | |
new_array_X = [] | |
new_array_O = [] | |
#print '_|_|_\n_|_|_\n | |' | |
def humanPlay(a): | |
print 'Human\'s time to play' | |
while True: | |
print 'Select from available positions\n', \ | |
a | |
position = int(raw_input('> ')) | |
if position in a: | |
return position | |
else: | |
print "Wrong move!" | |
def printBoard(a): | |
clearWindow() | |
print ' ',a[1],a[2],a[3] | |
print ' ',a[4],a[5],a[6] | |
print ' ',a[7],a[8],a[9] | |
def randomPlay(a): | |
pos = random.choice(a) | |
return pos | |
def checkMove(pos): | |
if new_a_board[pos] in new_a_unused: | |
return True | |
else: | |
False | |
def checkWin(player): | |
if player == 'X': | |
for i in win_combos: | |
if i[0] in new_array_X and \ | |
i[1] in new_array_X and \ | |
i[2] in new_array_X: | |
print "Player1 wins!" | |
time.sleep(2) | |
return True | |
else: | |
for i in win_combos: | |
if i[0] in new_array_O and \ | |
i[1] in new_array_O and \ | |
i[2] in new_array_O: | |
print "Player2 wins!" | |
time.sleep(2) | |
return True | |
return False | |
def logMove(pos, player): | |
print player, 'in position', pos | |
def move(pos, player): | |
new_a_board[pos] = player | |
if player == 'X': | |
new_array_X.append(pos) | |
else: | |
new_array_O.append(pos) | |
new_a_unused.remove(pos) | |
def gameType(): | |
print "Human vs Human" | |
print "Human vs Robot" | |
print "Robot vs Robot" | |
def firstToPlay(): | |
print "First to Play:\n", \ | |
"1 - Human\n", \ | |
"2 - Robot" | |
n = int(raw_input('> ')) | |
if n is 1: | |
return 1 | |
elif n is 2: | |
return 0 | |
def clearWindow(): | |
print "\033c" | |
def main(): | |
var = firstToPlay() | |
initBoard() | |
finished = False | |
while not finished: | |
time.sleep(1) | |
var += 1 | |
if var%2: | |
pl='X' | |
ps = randomPlay(new_a_unused) | |
else: | |
pl='O' | |
ps = humanPlay(new_a_unused) | |
#ps = randomPlay(new_a_unused, pl) | |
move(ps,pl) | |
printBoard(new_a_board) | |
logMove(ps, pl) | |
if len(new_a_unused) <=4 : | |
finished = checkWin(pl) | |
if len(new_a_unused) is 0 and not finished: | |
print "Nobody wins!" | |
time.sleep(1) | |
var = 0 | |
initBoard() | |
if __name__ == "__main__": | |
while True: | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment