Last active
April 11, 2017 13:09
-
-
Save JHerseth/afc6793388c7991e72d2d279292db479 to your computer and use it in GitHub Desktop.
simple tictactoe game
This file contains hidden or 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
class tictactoe(object): | |
def __init__(self): | |
self.x = [] | |
self.o = [] | |
self.validmoves = [1,2,3,4,5,6,7,8,9] | |
self.turncounter = 0 | |
self.winningpos = [(1, 2, 3), (4, 5, 6), (7, 8, 9), | |
(1, 4, 7), (2, 5, 8), (3, 6, 9), | |
(1, 5, 9), (3, 5, 7)] | |
def drawpos(self,x,o,pos): | |
""" | |
:param x: a list of moves X has done | |
:param o: a list of moves O has done | |
:param pos: the position to draw an X or O | |
Values from 1 to 9 as follows: | |
----------- | |
| 7 | 8 | 9 | | |
----------- | |
| 4 | 5 | 6 | | |
----------- | |
| 1 | 2 | 3 | | |
----------- | |
:return: | |
""" | |
if pos in self.x: | |
print("x", end='') | |
elif pos in self.o: | |
print("o", end='') | |
else: | |
print(" ", end='') | |
def drawboard(self): | |
""" | |
Draw the Tick Tack Toe board | |
""" | |
print("-------------") | |
# print first xo line | |
print("| ", end='') | |
self.drawpos(self.x, self.o, 7) | |
print(" | ", end='') | |
self.drawpos(self.x, self.o, 8) | |
print(" | ", end='') | |
self.drawpos(self.x, self.o, 9) | |
print(" |", end='\n') | |
print("-------------") | |
# print second xo line | |
print("| ", end='') | |
self.drawpos(self.x, self.o, 4) | |
print(" | ", end='') | |
self.drawpos(self.x, self.o, 5) | |
print(" | ", end='') | |
self.drawpos(self.x, self.o, 6) | |
print(" |", end='\n') | |
print("-------------") | |
# print third xo line | |
print("| ", end='') | |
self.drawpos(self.x, self.o, 1) | |
print(" | ", end='') | |
self.drawpos(self.x, self.o, 2) | |
print(" | ", end='') | |
self.drawpos(self.x, self.o, 3) | |
print(" |", end='\n') | |
print("-------------") | |
def isvalid(self, allmoves, userinput): | |
""" | |
Checks whether a move is valid or not | |
:param allmoves: a list of all moves done so far | |
:param userinput: move to check | |
:return: True if valid move, False if invalid move | |
""" | |
if userinput not in self.validmoves: | |
return False | |
elif userinput in allmoves: | |
print("That move has already been made") | |
return False | |
else: | |
return True | |
def checkwinner(self): | |
""" | |
:return: "x" if x is the winner, "o" if o is the winner, else False if no winner | |
""" | |
for n in self.winningpos: | |
if n[0] in self.x and n[1] in self.x and n[2] in self.x: | |
return "x" | |
if n[0] in self.o and n[1] in self.o and n[2] in self.o: | |
return "o" | |
return False | |
def getinput(self): | |
userinput = "" | |
try: | |
userinput = int(input("Enter a position: ")) | |
except ValueError: | |
print("Invalid input, try again.") | |
if self.isvalid(self.x + self.o, userinput): | |
if self.turncounter % 2: | |
self.o.append(userinput) | |
self.turncounter += 1 | |
else: | |
self.x.append(userinput) | |
self.turncounter += 1 | |
def main(): | |
finished = False | |
t = tictactoe() | |
t.drawboard() | |
while not finished: | |
t.getinput() | |
t.drawboard() | |
if t.checkwinner(): | |
print("{} is the winner".format(t.checkwinner())) | |
finished = True | |
if len(t.o) + len(t.x) == 9: | |
print("The game is a draw!") | |
finished = True | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment