|
1 | | -def start(): |
| 1 | +class Board: |
| 2 | + def __init__(self): |
| 3 | + self.boardState = [0, 0, 0, 0, 0, 0, 0, 0, 0] |
2 | 4 |
|
3 | | - def sum(a, b, c): |
4 | | - return a + b + c |
5 | | - |
6 | | - |
7 | | - def printBoard(xState, zState): |
8 | | - zero = 'X' if xState[0] else ('O' if zState[0] else 0) |
9 | | - one = 'X' if xState[1] else ('O' if zState[1] else 1) |
10 | | - two = 'X' if xState[2] else ('O' if zState[2] else 2) |
11 | | - three = 'X' if xState[3] else ('O' if zState[3] else 3) |
12 | | - four = 'X' if xState[4] else ('O' if zState[4] else 4) |
13 | | - five = 'X' if xState[5] else ('O' if zState[5] else 5) |
14 | | - six = 'X' if xState[6] else ('O' if zState[6] else 6) |
15 | | - seven = 'X' if xState[7] else ('O' if zState[7] else 7) |
16 | | - eight = 'X' if xState[8] else ('O' if zState[8] else 8) |
17 | | - print(f"{zero} | {one} | {two} ") |
18 | | - # code by Dev Varun |
19 | | - print(f"--|---|---") |
20 | | - print(f"{three} | {four} | {five} ") |
21 | | - print(f"--|---|---") |
22 | | - print(f"{six} | {seven} | {eight} ") |
23 | | - |
24 | | - |
25 | | - def checkWin(xState, zState): |
26 | | - wins = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]] |
27 | | - for win in wins: |
28 | | - if (sum(xState[win[0]], xState[win[1]], xState[win[2]]) == 3): |
29 | | - print("X Won the match") |
30 | | - return 1 |
31 | | - if (sum(zState[win[0]], zState[win[1]], zState[win[2]]) == 3): |
32 | | - print("O Won the match") |
33 | | - return 0 |
34 | | - return -1 |
35 | | - |
36 | | - |
37 | | - if __name__ == "__main__": |
38 | | - xState = [0, 0, 0, 0, 0, 0, 0, 0, 0] |
39 | | - zState = [0, 0, 0, 0, 0, 0, 0, 0, 0] |
| 5 | + def play(self): |
40 | 6 | turn = 1 # 1 for X and 0 for O |
41 | 7 | print("Welcome to Tic Tac Toe") |
42 | | - while (True): |
43 | | - printBoard(xState, zState) |
44 | | - if (turn == 1): |
| 8 | + while True: |
| 9 | + self.printBoard() |
| 10 | + if turn == 1: |
45 | 11 | print("X's Chance") |
46 | | - value = int(input("Please enter a value: ")) |
47 | | - xState[value] = 1 |
| 12 | + value = self.checkInput() |
| 13 | + self.boardState[value] = "X" |
48 | 14 | else: |
49 | 15 | print("O's Chance") |
50 | | - value = int(input("Please enter a value: ")) |
51 | | - zState[value] = 1 |
52 | | - cwin = checkWin(xState, zState) |
53 | | - if (cwin != -1): |
| 16 | + value = self.checkInput() |
| 17 | + self.boardState[value] = "O" |
| 18 | + |
| 19 | + cwin = self.checkWin() |
| 20 | + if cwin: |
54 | 21 | print("Match over") |
55 | 22 | break |
56 | 23 |
|
57 | | - turn = 1 - turn |
| 24 | + turn = 0 if turn else 1 |
| 25 | + |
| 26 | + def checkInput(self): |
| 27 | + while True: |
| 28 | + try: |
| 29 | + value = int(input("Please enter a value: ")) |
| 30 | + except ValueError: |
| 31 | + print("Please enter a valid value") |
| 32 | + continue |
| 33 | + if value > 9 or value < 0: |
| 34 | + print("Please enter a valid value") |
| 35 | + continue |
| 36 | + break |
| 37 | + return value |
| 38 | + |
| 39 | + def printBoard(self): |
| 40 | + print("---------") |
| 41 | + for i in range(0, 9): |
| 42 | + if i % 3 != 0: |
| 43 | + print("|", end=" ") |
| 44 | + if self.boardState[i] == "X": |
| 45 | + print("X", end=" ") |
| 46 | + elif self.boardState[i] == "O": |
| 47 | + print("O", end=" ") |
| 48 | + else: |
| 49 | + print(i, end=" ") |
| 50 | + if (i + 1) % 3 == 0: |
| 51 | + print("\n---------") |
| 52 | + |
| 53 | + def checkWin(self): |
| 54 | + wins = [ |
| 55 | + [0, 1, 2], |
| 56 | + [3, 4, 5], |
| 57 | + [6, 7, 8], |
| 58 | + [0, 3, 6], |
| 59 | + [1, 4, 7], |
| 60 | + [2, 5, 8], |
| 61 | + [0, 4, 8], |
| 62 | + [2, 4, 6], |
| 63 | + ] |
| 64 | + |
| 65 | + for win in wins: |
| 66 | + if ( |
| 67 | + self.boardState[win[0]] == "X" |
| 68 | + and self.boardState[win[1]] == "X" |
| 69 | + and self.boardState[win[2]] == "X" |
| 70 | + ): |
| 71 | + print("X wins") |
| 72 | + return True |
| 73 | + elif ( |
| 74 | + self.boardState[win[0]] == "O" |
| 75 | + and self.boardState[win[1]] == "O" |
| 76 | + and self.boardState[win[2]] == "O" |
| 77 | + ): |
| 78 | + print("O wins") |
| 79 | + return True |
| 80 | + |
| 81 | + if 0 not in self.boardState: |
| 82 | + print("Draw") |
| 83 | + return True |
| 84 | + |
| 85 | + return False |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + Board().play() |
| 90 | + while True: |
| 91 | + print("Do you want to restart ? \n") |
| 92 | + restart = input("y/n\n").lower() |
| 93 | + if restart == "y" or restart == "n": |
| 94 | + break |
58 | 95 |
|
59 | | -start() |
60 | | -def restart_and_exit(): |
61 | | - print("do you want to restart ? \n") |
62 | | - restart = input("y/n \n") |
63 | 96 | if restart == "y": |
64 | | - start() |
| 97 | + Board().play() |
| 98 | + |
65 | 99 | if restart == "n": |
66 | 100 | print("okay, exiting.....") |
67 | 101 | print("end of program") |
68 | 102 | exit() |
69 | | - |
70 | | -restart_and_exit() |
71 | | - |
72 | | -xyz_pro_plus = 1 |
73 | | - |
74 | | -while (xyz_pro_plus < 2): |
75 | | - restart_and_exit() |
76 | | - |
77 | | - |
78 | | - |
79 | | - |
80 | | - |
81 | | - |
82 | | - |
83 | | - |
84 | | - |
85 | | - |
86 | | - |
87 | | - |
88 | | - |
89 | | - |
90 | | - |
|
0 commit comments