-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtic-tac-toe-python
More file actions
110 lines (96 loc) · 3.53 KB
/
tic-tac-toe-python
File metadata and controls
110 lines (96 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""
EDITED BY: HAMMAD AHMAD
Project 2
Author: Ken
File: tictactoe.py
Model for tictactoe. Simply provides the next player's letter.
"""
import random
class TicTacToe(object):
"""Models a tictactoe game."""
def __init__(self):
"""Sets up the model."""
self.newGame()
def __str__(self):
"""Returns the string rep of the model."""
return self.letter
def newGame(self):
"""Resets the game to its initial state."""
self.letter = random.choice(("X", "O"))
self.winner = ""
self.squares = list()
for count in range(9):
self.squares.append("")
def hasWinner(self):
"""Checks to see if someone has won the game."""
if self.winner != "":
return True
else:
if self.squares[0] == self.squares[1] and self.squares[1] == \
self.squares[2] and self.squares[1] != "":
self.winner = self.squares[1]
return True
elif self.squares[3] == self.squares[4] and self.squares[4] == \
self.squares[5] and self.squares[4] != "":
self.winner = self.squares[4]
return True
elif self.squares[6] == self.squares[7] and self.squares[7] == \
self.squares[8] and self.squares[7] != "":
self.winner = self.squares[7]
return True
elif self.squares[0] == self.squares[3] and self.squares[3] == \
self.squares[6] and self.squares[3] != "":
self.winner = self.squares[3]
return True
elif self.squares[1] == self.squares[4] and self.squares[4] == \
self.squares[7] and self.squares[4] != "":
self.winner = self.squares[4]
return True
elif self.squares[2] == self.squares[5] and self.squares[5] == \
self.squares[8] and self.squares[5] != "":
self.winner = self.squares[5]
return True
elif self.squares[0] == self.squares[4] and self.squares[4] == \
self.squares[8] and self.squares[4] != "":
self.winner = self.squares[4]
return True
elif self.squares[2] == self.squares[4] and self.squares[4] == \
self.squares[6] and self.squares[4] != "":
self.winner = self.squares[4]
return True
else:
return False
def setLetter(self, index, letter):
"""Sets a letter at the index position in the squares list."""
self.squares[index] = letter
def __str__(self):
"""Returns a 3 by 3 grid that shows the current state of the game."""
grid = ""
index = 0
for row in range(3):
for column in range(3):
if self.squares[index] == "":
grid += " "
else:
grid += self.squares[index] + " "
index += 1
grid += "\n"
return grid
def nextLetter(self):
"""Returns the next letter for play."""
if self.letter == "X":
self.letter = "O"
else:
self.letter = "X"
return self.letter
def main():
"""Starting point for the application."""
model = TicTacToe()
print(model)
for count in range(9):
model.setLetter(count, model.nextLetter())
print(model)
print(model.hasWinner())
print("\n")
if __name__ == "__main__":
main()