My first Python project Tic Tac Toe v2












0














Hey guys this is my second attempt at a Tic Tac Toe game using the advice given in my first post: My first Python project: Tic Tac Toe



Again, just looking for advice on things I can improve. There is no classes because I have not really learned a great deal about OOP yet. I wanted to make sure I could use functions proficiently before I continued. I'm sure there is a way to loop the win_check(), but I have not figured it out yet.



def num_to_coord(num):
# 0-index num
num -= 1
coord =
while True:
curr_coord = num % 3
coord.append(curr_coord)
if len(coord) >= 2:
break
num -= curr_coord
num //= 3
return coord


def change_symbol(the_symbol):
if the_symbol == 'x':
the_symbol = 'o'
return the_symbol
if the_symbol == 'o':
the_symbol = 'x'
return the_symbol


def get_position():
position = int(input("Enter a position: "))
return position


def check_position(the_position, the_taken_positions):
if the_position in the_taken_positions:
return True
if the_position not in the_taken_positions:
return False


def draw_position(location, the_symbol, the_game_board):
coord = num_to_coord(location)
the_game_board[coord[0]][coord[1]] = the_symbol
for y in range(3):
print(add_sep('|', [the_game_board[x][y] for x in range(3)]))


def add_sep(sep, lst):
"""Adding | to board"""
output = sep
for i in lst:
output += i + sep
return output


def check_win(the_game_board):
if the_game_board[0][0] == 'x' and the_game_board[1][0] == 'x' and the_game_board[2][0] == 'x' or
(the_game_board[0][0] == 'o' and the_game_board[1][0] == 'o' and the_game_board[2][0] == 'o'):
return True
if the_game_board[0][1] == 'x' and the_game_board[1][1] == 'x' and the_game_board[2][1] == 'x' or
(the_game_board[0][1] == 'o' and the_game_board[1][1] == 'o' and the_game_board[2][1] == 'o'):
return True
if the_game_board[0][2] == 'x' and the_game_board[1][2] == 'x' and the_game_board[2][2] == 'x' or
(the_game_board[0][2] == 'o' and the_game_board[1][2] == 'o' and the_game_board[2][2] == 'o'):
return True
if the_game_board[0][0] == 'x' and the_game_board[0][1] == 'x' and the_game_board[0][2] == 'x' or
(the_game_board[0][0] == 'o' and the_game_board[0][1] == 'o' and the_game_board[0][2] == 'o'):
return True
if the_game_board[1][0] == 'x' and the_game_board[1][1] == 'x' and the_game_board[1][2] == 'x' or
(the_game_board[1][0] == 'o' and the_game_board[1][1] == 'o' and the_game_board[1][2] == 'o'):
return True
if the_game_board[2][0] == 'x' and the_game_board[2][1] == 'x' and the_game_board[2][2] == 'x' or
(the_game_board[2][0] == 'o' and the_game_board[2][1] == 'o' and the_game_board[2][2] == 'o'):
return True
if the_game_board[0][0] == 'x' and the_game_board[1][1] == 'x' and the_game_board[2][2] == 'x' or
(the_game_board[0][0] == 'o' and the_game_board[1][1] == 'o' and the_game_board[2][2] == 'o'):
return True
if the_game_board[0][2] == 'x' and the_game_board[1][1] == 'x' and the_game_board[2][0] == 'x' or
(the_game_board[0][2] == 'o' and the_game_board[1][1] == 'o' and the_game_board[2][0] == 'o'):
return True
else:
return False


def intro():
intro_board = [['1', '4', '7'], ['2', '5', '8'], ['3', '6', '9']]
print("Welcome to Tic Tac Toe")
print("You can pick location by identifying the position on the board. (There are 9 positions)")
print("The player who plays first will be using 'x' and the second player will be using 'o'.")
for y in range(3):
print(add_sep('|', [intro_board[x][y] for x in range(3)]))


def main():
game_board = [[' '] * 3 for _ in range(3)]
taken_positions =
symbol = 'x'
num_moves = 0
win = False
intro()
while num_moves < 9 and not win:
position = get_position()
taken = check_position(position, taken_positions)
if taken:
print("Position taken! Try again.")
else:
draw_position(position, symbol, game_board)
taken_positions.append(position)
symbol = change_symbol(symbol)
num_moves = num_moves + 1

win = check_win(game_board)
if win:
print("We have a winner!")
break

if num_moves == 9 and not win:
print("WOW! You guys are good! DRAW!!!")


# MAIN
main()
print("Thanks for playing! Exiting")









share|improve this question







New contributor




BobPage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0














    Hey guys this is my second attempt at a Tic Tac Toe game using the advice given in my first post: My first Python project: Tic Tac Toe



    Again, just looking for advice on things I can improve. There is no classes because I have not really learned a great deal about OOP yet. I wanted to make sure I could use functions proficiently before I continued. I'm sure there is a way to loop the win_check(), but I have not figured it out yet.



    def num_to_coord(num):
    # 0-index num
    num -= 1
    coord =
    while True:
    curr_coord = num % 3
    coord.append(curr_coord)
    if len(coord) >= 2:
    break
    num -= curr_coord
    num //= 3
    return coord


    def change_symbol(the_symbol):
    if the_symbol == 'x':
    the_symbol = 'o'
    return the_symbol
    if the_symbol == 'o':
    the_symbol = 'x'
    return the_symbol


    def get_position():
    position = int(input("Enter a position: "))
    return position


    def check_position(the_position, the_taken_positions):
    if the_position in the_taken_positions:
    return True
    if the_position not in the_taken_positions:
    return False


    def draw_position(location, the_symbol, the_game_board):
    coord = num_to_coord(location)
    the_game_board[coord[0]][coord[1]] = the_symbol
    for y in range(3):
    print(add_sep('|', [the_game_board[x][y] for x in range(3)]))


    def add_sep(sep, lst):
    """Adding | to board"""
    output = sep
    for i in lst:
    output += i + sep
    return output


    def check_win(the_game_board):
    if the_game_board[0][0] == 'x' and the_game_board[1][0] == 'x' and the_game_board[2][0] == 'x' or
    (the_game_board[0][0] == 'o' and the_game_board[1][0] == 'o' and the_game_board[2][0] == 'o'):
    return True
    if the_game_board[0][1] == 'x' and the_game_board[1][1] == 'x' and the_game_board[2][1] == 'x' or
    (the_game_board[0][1] == 'o' and the_game_board[1][1] == 'o' and the_game_board[2][1] == 'o'):
    return True
    if the_game_board[0][2] == 'x' and the_game_board[1][2] == 'x' and the_game_board[2][2] == 'x' or
    (the_game_board[0][2] == 'o' and the_game_board[1][2] == 'o' and the_game_board[2][2] == 'o'):
    return True
    if the_game_board[0][0] == 'x' and the_game_board[0][1] == 'x' and the_game_board[0][2] == 'x' or
    (the_game_board[0][0] == 'o' and the_game_board[0][1] == 'o' and the_game_board[0][2] == 'o'):
    return True
    if the_game_board[1][0] == 'x' and the_game_board[1][1] == 'x' and the_game_board[1][2] == 'x' or
    (the_game_board[1][0] == 'o' and the_game_board[1][1] == 'o' and the_game_board[1][2] == 'o'):
    return True
    if the_game_board[2][0] == 'x' and the_game_board[2][1] == 'x' and the_game_board[2][2] == 'x' or
    (the_game_board[2][0] == 'o' and the_game_board[2][1] == 'o' and the_game_board[2][2] == 'o'):
    return True
    if the_game_board[0][0] == 'x' and the_game_board[1][1] == 'x' and the_game_board[2][2] == 'x' or
    (the_game_board[0][0] == 'o' and the_game_board[1][1] == 'o' and the_game_board[2][2] == 'o'):
    return True
    if the_game_board[0][2] == 'x' and the_game_board[1][1] == 'x' and the_game_board[2][0] == 'x' or
    (the_game_board[0][2] == 'o' and the_game_board[1][1] == 'o' and the_game_board[2][0] == 'o'):
    return True
    else:
    return False


    def intro():
    intro_board = [['1', '4', '7'], ['2', '5', '8'], ['3', '6', '9']]
    print("Welcome to Tic Tac Toe")
    print("You can pick location by identifying the position on the board. (There are 9 positions)")
    print("The player who plays first will be using 'x' and the second player will be using 'o'.")
    for y in range(3):
    print(add_sep('|', [intro_board[x][y] for x in range(3)]))


    def main():
    game_board = [[' '] * 3 for _ in range(3)]
    taken_positions =
    symbol = 'x'
    num_moves = 0
    win = False
    intro()
    while num_moves < 9 and not win:
    position = get_position()
    taken = check_position(position, taken_positions)
    if taken:
    print("Position taken! Try again.")
    else:
    draw_position(position, symbol, game_board)
    taken_positions.append(position)
    symbol = change_symbol(symbol)
    num_moves = num_moves + 1

    win = check_win(game_board)
    if win:
    print("We have a winner!")
    break

    if num_moves == 9 and not win:
    print("WOW! You guys are good! DRAW!!!")


    # MAIN
    main()
    print("Thanks for playing! Exiting")









    share|improve this question







    New contributor




    BobPage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0







      Hey guys this is my second attempt at a Tic Tac Toe game using the advice given in my first post: My first Python project: Tic Tac Toe



      Again, just looking for advice on things I can improve. There is no classes because I have not really learned a great deal about OOP yet. I wanted to make sure I could use functions proficiently before I continued. I'm sure there is a way to loop the win_check(), but I have not figured it out yet.



      def num_to_coord(num):
      # 0-index num
      num -= 1
      coord =
      while True:
      curr_coord = num % 3
      coord.append(curr_coord)
      if len(coord) >= 2:
      break
      num -= curr_coord
      num //= 3
      return coord


      def change_symbol(the_symbol):
      if the_symbol == 'x':
      the_symbol = 'o'
      return the_symbol
      if the_symbol == 'o':
      the_symbol = 'x'
      return the_symbol


      def get_position():
      position = int(input("Enter a position: "))
      return position


      def check_position(the_position, the_taken_positions):
      if the_position in the_taken_positions:
      return True
      if the_position not in the_taken_positions:
      return False


      def draw_position(location, the_symbol, the_game_board):
      coord = num_to_coord(location)
      the_game_board[coord[0]][coord[1]] = the_symbol
      for y in range(3):
      print(add_sep('|', [the_game_board[x][y] for x in range(3)]))


      def add_sep(sep, lst):
      """Adding | to board"""
      output = sep
      for i in lst:
      output += i + sep
      return output


      def check_win(the_game_board):
      if the_game_board[0][0] == 'x' and the_game_board[1][0] == 'x' and the_game_board[2][0] == 'x' or
      (the_game_board[0][0] == 'o' and the_game_board[1][0] == 'o' and the_game_board[2][0] == 'o'):
      return True
      if the_game_board[0][1] == 'x' and the_game_board[1][1] == 'x' and the_game_board[2][1] == 'x' or
      (the_game_board[0][1] == 'o' and the_game_board[1][1] == 'o' and the_game_board[2][1] == 'o'):
      return True
      if the_game_board[0][2] == 'x' and the_game_board[1][2] == 'x' and the_game_board[2][2] == 'x' or
      (the_game_board[0][2] == 'o' and the_game_board[1][2] == 'o' and the_game_board[2][2] == 'o'):
      return True
      if the_game_board[0][0] == 'x' and the_game_board[0][1] == 'x' and the_game_board[0][2] == 'x' or
      (the_game_board[0][0] == 'o' and the_game_board[0][1] == 'o' and the_game_board[0][2] == 'o'):
      return True
      if the_game_board[1][0] == 'x' and the_game_board[1][1] == 'x' and the_game_board[1][2] == 'x' or
      (the_game_board[1][0] == 'o' and the_game_board[1][1] == 'o' and the_game_board[1][2] == 'o'):
      return True
      if the_game_board[2][0] == 'x' and the_game_board[2][1] == 'x' and the_game_board[2][2] == 'x' or
      (the_game_board[2][0] == 'o' and the_game_board[2][1] == 'o' and the_game_board[2][2] == 'o'):
      return True
      if the_game_board[0][0] == 'x' and the_game_board[1][1] == 'x' and the_game_board[2][2] == 'x' or
      (the_game_board[0][0] == 'o' and the_game_board[1][1] == 'o' and the_game_board[2][2] == 'o'):
      return True
      if the_game_board[0][2] == 'x' and the_game_board[1][1] == 'x' and the_game_board[2][0] == 'x' or
      (the_game_board[0][2] == 'o' and the_game_board[1][1] == 'o' and the_game_board[2][0] == 'o'):
      return True
      else:
      return False


      def intro():
      intro_board = [['1', '4', '7'], ['2', '5', '8'], ['3', '6', '9']]
      print("Welcome to Tic Tac Toe")
      print("You can pick location by identifying the position on the board. (There are 9 positions)")
      print("The player who plays first will be using 'x' and the second player will be using 'o'.")
      for y in range(3):
      print(add_sep('|', [intro_board[x][y] for x in range(3)]))


      def main():
      game_board = [[' '] * 3 for _ in range(3)]
      taken_positions =
      symbol = 'x'
      num_moves = 0
      win = False
      intro()
      while num_moves < 9 and not win:
      position = get_position()
      taken = check_position(position, taken_positions)
      if taken:
      print("Position taken! Try again.")
      else:
      draw_position(position, symbol, game_board)
      taken_positions.append(position)
      symbol = change_symbol(symbol)
      num_moves = num_moves + 1

      win = check_win(game_board)
      if win:
      print("We have a winner!")
      break

      if num_moves == 9 and not win:
      print("WOW! You guys are good! DRAW!!!")


      # MAIN
      main()
      print("Thanks for playing! Exiting")









      share|improve this question







      New contributor




      BobPage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      Hey guys this is my second attempt at a Tic Tac Toe game using the advice given in my first post: My first Python project: Tic Tac Toe



      Again, just looking for advice on things I can improve. There is no classes because I have not really learned a great deal about OOP yet. I wanted to make sure I could use functions proficiently before I continued. I'm sure there is a way to loop the win_check(), but I have not figured it out yet.



      def num_to_coord(num):
      # 0-index num
      num -= 1
      coord =
      while True:
      curr_coord = num % 3
      coord.append(curr_coord)
      if len(coord) >= 2:
      break
      num -= curr_coord
      num //= 3
      return coord


      def change_symbol(the_symbol):
      if the_symbol == 'x':
      the_symbol = 'o'
      return the_symbol
      if the_symbol == 'o':
      the_symbol = 'x'
      return the_symbol


      def get_position():
      position = int(input("Enter a position: "))
      return position


      def check_position(the_position, the_taken_positions):
      if the_position in the_taken_positions:
      return True
      if the_position not in the_taken_positions:
      return False


      def draw_position(location, the_symbol, the_game_board):
      coord = num_to_coord(location)
      the_game_board[coord[0]][coord[1]] = the_symbol
      for y in range(3):
      print(add_sep('|', [the_game_board[x][y] for x in range(3)]))


      def add_sep(sep, lst):
      """Adding | to board"""
      output = sep
      for i in lst:
      output += i + sep
      return output


      def check_win(the_game_board):
      if the_game_board[0][0] == 'x' and the_game_board[1][0] == 'x' and the_game_board[2][0] == 'x' or
      (the_game_board[0][0] == 'o' and the_game_board[1][0] == 'o' and the_game_board[2][0] == 'o'):
      return True
      if the_game_board[0][1] == 'x' and the_game_board[1][1] == 'x' and the_game_board[2][1] == 'x' or
      (the_game_board[0][1] == 'o' and the_game_board[1][1] == 'o' and the_game_board[2][1] == 'o'):
      return True
      if the_game_board[0][2] == 'x' and the_game_board[1][2] == 'x' and the_game_board[2][2] == 'x' or
      (the_game_board[0][2] == 'o' and the_game_board[1][2] == 'o' and the_game_board[2][2] == 'o'):
      return True
      if the_game_board[0][0] == 'x' and the_game_board[0][1] == 'x' and the_game_board[0][2] == 'x' or
      (the_game_board[0][0] == 'o' and the_game_board[0][1] == 'o' and the_game_board[0][2] == 'o'):
      return True
      if the_game_board[1][0] == 'x' and the_game_board[1][1] == 'x' and the_game_board[1][2] == 'x' or
      (the_game_board[1][0] == 'o' and the_game_board[1][1] == 'o' and the_game_board[1][2] == 'o'):
      return True
      if the_game_board[2][0] == 'x' and the_game_board[2][1] == 'x' and the_game_board[2][2] == 'x' or
      (the_game_board[2][0] == 'o' and the_game_board[2][1] == 'o' and the_game_board[2][2] == 'o'):
      return True
      if the_game_board[0][0] == 'x' and the_game_board[1][1] == 'x' and the_game_board[2][2] == 'x' or
      (the_game_board[0][0] == 'o' and the_game_board[1][1] == 'o' and the_game_board[2][2] == 'o'):
      return True
      if the_game_board[0][2] == 'x' and the_game_board[1][1] == 'x' and the_game_board[2][0] == 'x' or
      (the_game_board[0][2] == 'o' and the_game_board[1][1] == 'o' and the_game_board[2][0] == 'o'):
      return True
      else:
      return False


      def intro():
      intro_board = [['1', '4', '7'], ['2', '5', '8'], ['3', '6', '9']]
      print("Welcome to Tic Tac Toe")
      print("You can pick location by identifying the position on the board. (There are 9 positions)")
      print("The player who plays first will be using 'x' and the second player will be using 'o'.")
      for y in range(3):
      print(add_sep('|', [intro_board[x][y] for x in range(3)]))


      def main():
      game_board = [[' '] * 3 for _ in range(3)]
      taken_positions =
      symbol = 'x'
      num_moves = 0
      win = False
      intro()
      while num_moves < 9 and not win:
      position = get_position()
      taken = check_position(position, taken_positions)
      if taken:
      print("Position taken! Try again.")
      else:
      draw_position(position, symbol, game_board)
      taken_positions.append(position)
      symbol = change_symbol(symbol)
      num_moves = num_moves + 1

      win = check_win(game_board)
      if win:
      print("We have a winner!")
      break

      if num_moves == 9 and not win:
      print("WOW! You guys are good! DRAW!!!")


      # MAIN
      main()
      print("Thanks for playing! Exiting")






      python beginner python-3.x tic-tac-toe






      share|improve this question







      New contributor




      BobPage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      BobPage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      BobPage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 22 mins ago









      BobPageBobPage

      233




      233




      New contributor




      BobPage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      BobPage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      BobPage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          BobPage is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211067%2fmy-first-python-project-tic-tac-toe-v2%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          BobPage is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          BobPage is a new contributor. Be nice, and check out our Code of Conduct.













          BobPage is a new contributor. Be nice, and check out our Code of Conduct.












          BobPage is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211067%2fmy-first-python-project-tic-tac-toe-v2%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Feedback on college project

          Futebolista

          Albești (Vaslui)