repl.it
@molinitomario/

TicTacToe

Python

A little project for period 6 project

fork
loading
Files
  • main.py
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
# board should be printed out every time the player makes a move

# two users

print("=================".center(25))
print("This is based off of your keyboard numberpad".center(50))
print("7 | 8 | 9\n\t\t4 | 5 | 6\n\t\t1 | 2 | 3".center(50))
print("=================".center(25))
options = ["X", "O"]
user_one = input("(User one) Would you like to be X or O?\t").upper()
if user_one in options: 
    options.remove(user_one)
    user_two = input("(User two) Would you like to be %s\t" % options[0])
    if user_two.lower() == "no":
        user_two = options[0]
        print('To bad.')
    else:
        user_two = options[0]
        print("Okay lets begin!")

print('User one = %s' % user_one)
print("User two = %s" % user_two)


# start of game functionality

game_board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]

#======Draw board
def draw_board():
    print("{} | {} | {}".format(game_board[6], game_board[7], game_board[8])) 
    print("{} | {} | {}".format(game_board[3], game_board[4], game_board[5]))
    print("{} | {} | {}".format(game_board[0], game_board[1], game_board[2]))


def is_spot_take(index):
    if game_board[index-1] != " ":
        return True # true that it is take
    else:
        return False

def user_picking(the_game_board, user_going):
    # let user one pick
    symbol = ""
    if user_going == "one":
        symbol = user_one
    else:
        symbol = user_two

    spot = int(input("Where would you like to place your %s:\t" % symbol))
    while is_spot_take(spot) != False:
        spot = int(input("Pick again: "))
        # if the spot that the user wants is taken then he will be prompted with this message until he corrects his mistake
   
    game_board = the_game_board
    game_board[spot-1] = symbol
    return game_board # return the game board so the user can reassign the changed game board to the global gameboard

    # once the spot is chosen then the board will be drawn

# some for me to use in later game and not rewrite this code twice
def who_is_this(symbol):
    if symbol.lower() == user_one.lower():
        return "User One %s" % user_one
    else:
        return "User two %s" % user_two

def check_win(the_game_board):
    # check the win
    # since it is only a 3 by 3 it does not really matter how we check
    # return "o" or "x" for the winner 
    # return 0 for no win
    # check who the win occured
    # if win occures return the letter of the win

    def check_horizontal():
        for number in range(3, len(the_game_board)+1, 3):
            current_row = the_game_board[number - 3: number]
            # used to check for a win, we place the first letter in here, check if all other letters are the same if so then win, if not then they are different which = no win in this row
            if current_row[0] == current_row[1] and current_row[1] == current_row[2] and current_row[0] != " ":
                return current_row[0]
            else:
                continue
            # find a way to do this with a foor loop so that if the board size changes we can accomodate for those changes

    def check_vertical():
        verticals = []
        for index in range(0, 3):
            first_row, second_row, third_row = the_game_board[index], the_game_board[index+3], the_game_board[index+6]
            verticals.append([first_row, second_row, third_row])
            # not sure if this was the best way of going with this

        for group in verticals:
            current = group[0]
            equals, current_symbol = 0, ""
            for index, symbol in enumerate(group):
                if symbol == current and index != 0 and symbol != " ":
                    equals += 1
                    current_symbol = symbol
            if equals == 2:
                return symbol
            # else its going to contiue

    def slope():
        # 0 4 8
        # 3 6 9
        if the_game_board[0] != " " and  the_game_board[0] == the_game_board[4] and the_game_board[4] == the_game_board[8]:
            return the_game_board[0]
        elif the_game_board[2] != " " and  the_game_board[2] == the_game_board[4] and the_game_board[4] == the_game_board[6]:
            return the_game_board[2]
        return 0

    slope_win = slope()
    # I used the for loops because I was thinking about adding some type of addition to the code where it allows the user to determine the size of the board
    horizontal = check_horizontal()
    vertical = check_vertical()
    if horizontal != 0 and horizontal != None:
        print("we have a winner: %s" % who_is_this(horizontal))
        return horizontal
    elif vertical != 0 and vertical != None:
        print("We have a winner: %s" % who_is_this(vertical))
        return vertical
    elif slope_win != 0 and slope_win != None:
        print("We have a inner: %s" % who_is_this(slope_win))
        return slope_win
    else:
        return 0

draw_board()
# do not start checking the wins until at least 5 places have been placed ( iterate round everytime a user goes )

while True:
    print("============User One's Turn===============")
    game_board = user_picking(game_board, "one")
    draw_board()
    symbol = check_win(game_board)
    print(symbol)
    if symbol != 0: 
        break

    print("============User Two's Turn================")
    game_board = user_picking(game_board, "two")
    draw_board()
    symbol = check_win(game_board)
    print(symbol)
    if symbol != 0: 
        break

    

Fetching token
?