Author Topic: [Python] Tic Tac Toe  (Read 432 times)

0 Members and 1 Guest are viewing this topic.

Offline AnonymousCelt

  • /dev/null
  • *
  • Posts: 11
  • Cookies: 0
    • View Profile
[Python] Tic Tac Toe
« on: March 09, 2015, 08:55:22 pm »
This is my first time programming Python so all feedback is appreciated.
Code: [Select]

#Create Empty Board
matrice = [[ '-' for i in range(3) ] for j in range(3)]


#Print Matrice
def printMatrix():
    print "\n\n"
    for i in range(3):
        print " | ",
        for j in range(3):
            print matrice[i][j],
            print " | ",
        print "\n",
    print"\n\n"
   
#Take player one's turn
def playerOneTurn():
    printMatrix()
    print "Player One:"
    x = int(input("Enter X co-ordinate: "))
    y = int(input("Enter Y co-ordinate: "))
    x -= 1
    y -= 1
    if matrice[y][x] == '-':
        matrice[y][x] = 'X'
    else:
        print "Please choose another square\n\n"
        playerOneTurn()


#Take player two's turn
def playerTwoTurn():
    printMatrix()
    print "Player Two:"
    x = int(input("   Enter X co-ordinate: "))
    y = int(input("   Enter Y co-ordinate: "))
    x -= 1
    y -= 1
    if matrice[y][x] == '-':
        matrice[y][x] = 'O'
    else:
        print "Please choose another square\n\n"
        playerTwoTurn()


#Test if Game Has Come to End
def gameStatus():
    over = '='
    for i in range(3):
        for j in range(3):
            if matrice[i][j] == '-':
                over = '-'
                break
    for i in range(3):
        if matrice[0][i] == matrice[1][i] == matrice[2][i]:
            victory(matrice[1][i])
        if matrice[i][0] == matrice[i][1] == matrice[i][2]:
            victory(matrice[i][1])
    if matrice[0][0] == matrice[1][1] == matrice[2][2]:
        victory(matrice[1][1])
    if matrice[2][0] == matrice[1][1] == matrice[0][2]:
        victory(matrice[1][1])
    if over == '=':
        victory(over)


#Victory Print
def victory(c):
    if c == 'X':
        print "\t _______"
        print "\t/       \\"
        print "\t|VICTORY|"
        print "\t|  FOR  |"
        print "\t|PLAYER |"
        print "\t|  ONE  |"
        print "\t\\_______/"
        printMatrix()
        raise SystemExit
    if c == 'Y':
        print "\t _______"
        print "\t/       \\"
        print "\t|VICTORY|"
        print "\t|  FOR  |"
        print "\t|PLAYER |"
        print "\t|  TWO  |"
        print "\t\\_______/"
        printMatrix()
        raise SystemExit
    if c == '=':
        print "\t _______"
        print "\t/       \\"
        print "\t|  DRAW |"
        print "\t\\_______/"
        printMatrix()
        raise SystemExit
   
#main logic
def main():
    while True:
        playerOneTurn()
        gameStatus()
        playerTwoTurn()
        gameStatus()


#Set to run main
if __name__ == "__main__":
    main()

Offline HTH

  • Official EZ Slut
  • Administrator
  • Knight
  • *
  • Posts: 395
  • Cookies: 158
  • EZ Titan
    • View Profile
Re: [Python] Tic Tac Toe
« Reply #1 on: March 09, 2015, 09:22:10 pm »
Just a few things to start with:

Notice how PlayerOneTurn and PlayerTwoTurn are almost identical? They should become one method

Same general idea for the victory method, some of the lines can be place outside of the conditional.

Further, entering an invalid x/y coordinate crashes the program

Otherwise, welcome to the brotherhood :)
<ande> HTH is love, HTH is life
<TurboBorland> hth is the only person on this server I can say would successfully spitefuck peoples women

Offline AnonymousCelt

  • /dev/null
  • *
  • Posts: 11
  • Cookies: 0
    • View Profile
Re: [Python] Tic Tac Toe
« Reply #2 on: March 09, 2015, 09:51:21 pm »

I have updated it to protect against the wrong number being entered and changed the two player turn methods into takeTurn. I haven't done anything about the victory method yet. 

Code: [Select]

#Create Empty Board
matrice = [[ '-' for i in range(3) ] for j in range(3)]


#Print Matrice
def printMatrix():
    print "\n\n"
    for i in range(3):
        print " | ",
        for j in range(3):
            print matrice[i][j],
            print " | ",
        print "\n",
    print"\n\n"
   
#Take player one's turn
def takeTurn(c):
    printMatrix()
    print "Player " ,c
    x = int(input("Enter X co-ordinate: "))
    y = int(input("Enter Y co-ordinate: "))
    if y > 3 or y < 1 or x > 3 or x < 1:
        print "Please choose another square\n\n"
        takeTurn(c)
    x -= 1
    y -= 1
    if matrice[y][x] == '-':
        matrice[y][x] = c
    else:
        print "Please choose another square\n\n"
        takeTurn(c)


#Test if Game Has Come to End
def gameStatus():
    over = '='
    for i in range(3):
        for j in range(3):
            if matrice[i][j] == '-':
                over = '-'
                break
    for i in range(3):
        if matrice[0][i] == matrice[1][i] == matrice[2][i]:
            victory(matrice[1][i])
        if matrice[i][0] == matrice[i][1] == matrice[i][2]:
            victory(matrice[i][1])
    if matrice[0][0] == matrice[1][1] == matrice[2][2]:
        victory(matrice[1][1])
    if matrice[2][0] == matrice[1][1] == matrice[0][2]:
        victory(matrice[1][1])
    if over == '=':
        victory(over)


#Victory Print
def victory(c):
    if c == 'X':
        print "\t _______"
        print "\t/       \\"
        print "\t|VICTORY|"
        print "\t|  FOR  |"
        print "\t|PLAYER |"
        print "\t|  ONE  |"
        print "\t\\_______/"
        printMatrix()
        raise SystemExit
    if c == 'Y':
        print "\t _______"
        print "\t/       \\"
        print "\t|VICTORY|"
        print "\t|  FOR  |"
        print "\t|PLAYER |"
        print "\t|  TWO  |"
        print "\t\\_______/"
        printMatrix()
        raise SystemExit
    if c == '=':
        print "\t _______"
        print "\t/       \\"
        print "\t|  DRAW |"
        print "\t\\_______/"
        printMatrix()
        raise SystemExit
   
#main logic
def main():
    while True:
        takeTurn('X')
        gameStatus()
        takeTurn('O')
        gameStatus()


#Set to run main
if __name__ == "__main__":
    main()


Thank you very much for the feedback.