@KK998/

tic-tac-toe

Python

No description

fork
loading
Files
  • main.py
  • README.md
main.py
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
? Tic-Tac-Toe Terminal Game:

* Make a tic tac toe game using functions and knowledge you gathered till now.

TODO: 2 players can interact with the game
TODO: Start: input -> Chose X or O

Todo *Start function* -> print(Start) -> Game Variable update (X or O)

*~*
*1. Create a game instance  | while True: -> break if Player 1 or Player 2 wins 
*2. Create a grid for tic-tac-toe   | *Function #2*
*3. number every grid cell | *Function #2*
 print {#7} | {#8} | {#9}
 print {#4} | {#5} | {#6}
 print {#1} | {#2} | {#3}
*~*
TODO: Player 1 -> Chose number for grid 
*~*
*1. Insert X or O into grid cell
*2. Update grid | *Function #1*
*3. Ask Player 2 
*~*
TODO: Player 2 -> Choose number for grid
*~*
*1. Insert X or O into grid cell
*2. Update grid | *Function #1*
*3. Ask Player 1 
*~*

Todo *Function #1* -> Check after inserting X or O if the player wins
Todo *Function #2* -> Create a grid with numbered cells that are interactive
"""


# * --[ Functions ]--
def make_a_move(grid_cell, player):
    grid[grid_cell - 1] = player
    print(f"{grid[0]} | {grid[1]} | {grid[2]} \n----------\n{grid[3]} | {grid[4]} | {grid[5]} \n----------\n{grid[6]} | {grid[7]} | {grid[8]}")


# * --[ Game Start Welcome ]--
print("Welcome to Tic-Tac-Toe!")
start = input("To START the game press any key...")
print(len(start))
if len(start) >= 0:

    #! Game variables
    player_has_won = False
    winner = ""
    grid = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
    player_turn = False  # ! if False player1 has the turn |else if True player2 has the turn

    # * --[ Player X or O ]--5
    player1 = input("Please choose X or O to start the game:\n")
    player2 = ""
    if player1 == "X":
        player2 = "O"
    if player1 == "O":
        player2 = "X"

    # * --[ Game instance ]--
    while True:
        # * --[ Game end ]--
        if player_has_won == True:
            print(f"Player {winner} has won!!!")
            break

        # * --[ Player moves ]--
        if player_turn == False:
            # * --[ Game end ]--
            if player_has_won == True:
                print(f"Player {winner} has won!!!")
                break
            player1_move = int(input(
                "Please choose a grid cell to make a move [1-9]:\n"))
            make_a_move(player1_move, player1)
            player_turn = True

        # * --[ Check if players move made him / her a winner ]--
            #! --[ Player 1 ]--
        # Horizontals Player 1
        if grid[0] == player1 and grid[1] == player1 and grid[2] == player1:
            winner = "Player One"
            player_has_won = True
        if grid[3] == player1 and grid[4] == player1 and grid[5] == player1:
            winner = "Player One"
            player_has_won = True
        if grid[6] == player1 and grid[7] == player1 and grid[8] == player1:
            winner = "Player One"
            player_has_won = True
        # Verticals Player 1
        if grid[0] == player1 and grid[3] == player1 and grid[6] == player1:
            winner = "Player One"
            player_has_won = True
        if grid[1] == player1 and grid[4] == player1 and grid[7] == player1:
            winner = "Player One"
            player_has_won = True
        if grid[2] == player1 and grid[5] == player1 and grid[8] == player1:
            winner = "Player One"
            player_has_won = True
        # Diagonals Player 1
        if grid[6] == player1 and grid[4] == player1 and grid[2] == player1:
            winner = "Player One"
            player_has_won = True
        if grid[0] == player1 and grid[4] == player1 and grid[8] == player1:
            winner = "Player One"
            player_has_won = True

        # * --[ Player moves ]--
        if player_turn == True:
            # * --[ Game end ]--
            if player_has_won == True:
                print(f"Player {winner} has won!!!")
                break
            player2_move = int(input(
                "Please choose a grid cell to make a move [1-9]:\n"))
            make_a_move(player2_move, player2)
            player_turn = False
        # * --[ Check if players move made him / her a winner ]--
            #! --[ Player 2 ]--
        # Horizontals Player 2
        if grid[0] == player2 and grid[1] == player2 and grid[2] == player2:
            winner = "Player One"
            player_has_won = True
        if grid[3] == player2 and grid[4] == player2 and grid[5] == player2:
            winner = "Player Two"
            player_has_won = True
        if grid[6] == player2 and grid[7] == player2 and grid[8] == player2:
            winner = "Player Two"
            player_has_won = True
        # Verticals Player 1
        if grid[0] == player2 and grid[3] == player2 and grid[6] == player2:
            winner = "Player Two"
            player_has_won = True
        if grid[1] == player2 and grid[4] == player2 and grid[7] == player2:
            winner = "Player Two"
            player_has_won = True
        if grid[2] == player2 and grid[5] == player2 and grid[8] == player2:
            winner = "Player Two"
            player_has_won = True
        # Diagonals Player 1
        if grid[6] == player2 and grid[4] == player2 and grid[2] == player2:
            winner = "Player Two"
            player_has_won = True
        if grid[0] == player2 and grid[4] == player2 and grid[8] == player2:
            winner = "Player Two"
            player_has_won = True
            # * --[ Game end ]--
        if player_has_won == True:
            print(f"Player {winner} has won!!!")
            break