Skip to main content
Question Protected by Jamal
deleted 20 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Hi I am looking on how I can improve my writing style and/or how to simplify how I write my code. Thanks in advance!

Hi I am looking on how I can improve my writing style and/or how to simplify how I write my code. Thanks in advance!

I am looking on how I can improve my writing style and/or how to simplify how I write my code.

added 97 characters in body
Source Link
TCG
  • 117
  • 2
  • 9

Update: new version

Update: new version

added 16 characters in body
Source Link
TCG
  • 117
  • 2
  • 9
# USER: TCG
# Tic Tac Toe (User vs Computer)

import random

def place(num,x):
    # Returns all the moves made
    for i in range(len(x)):
        if x[i] == num:
            pos = x.index(num)
            return x[(pos + 1)]
    return str(num)

def print_grid(move,player,x):

    pos_list.extend([move,player])

    # Grid on which the player and computer play on            
    grid = ["","-------------\n",
        "|",place(1,pos_list),"|",place(2,pos_list),"|",place(3,pos_list),"|\n",
        "|---+---+---|\n",
        "|",place(4,pos_list),"|",place(5,pos_list),"|",place(6,pos_list),"|\n",
        "|---+---+---|\n",
        "|",place(7,pos_list),"|",place(8,pos_list),"|",place(9,pos_list),"|\n",
        "-------------\n"]
    if x == 2:
        # Only prints one the player has made a move
        print '\n', ' '.join(grid)
    
def winner(x,player,xx):

    # Checks if there is a winner (Really messy, could do with code simplifying)
    if ((1 in x and 4 in x and 7 in x)or(1 in x and 2 in x and 3 in x)or(2 in x and 5 in x and 8 in x)or(3 in x and 6 in x and 9 in x)or
    (4 in x and 5 in x and 6 in x)or(7 in x and 8 in x and 9 in x)or(1 in x and 5 in x and 9 in x)or(3 in x and 5 in x and 7 in x)):
    # If prevents the A.I part from printing the statemnt
        if xx <> 1:
            print '\n'*5,"\'%s\'" %player, "HAS WON!"
        return 1 == 1

def computer_AI_part(listx):
    global computer_move

    # Chceks all possible values which the player can and enter to win and blocks it
    for x in range(1,10):
        if x not in pos_list:
            listx.append(x)
            if (winner(listx,'Computer',1)) == True:
                del listx[-1]
                computer_move = x
                return 1
            del listx[-1]

def computer_and_player():
    global computer_move,pos_list,player_list,computer_list
    replay,draw = 0,0

    while True:

        # Replay's the game
        if replay == 1:
            restart = raw_input("Would you like to replay?: ")
            if restart == "yes":
                pass
            else:
                return
        else:
            print "\nTic Tac Toe - Computer vs You", '\n'*2,"Computer goes first\n"
        
        replay,computer_move,players_move,loop_count,pos_list,player_list,computer_list = 0,0,0,0,[],[],[]
    
        for each in "XXXXX":
            loop_count += 1
        
            # Computer's Move
            if computer_AI_part(computer_list) or computer_AI_part(player_list) == 1:
                pass     
            else:
                while True:
                    computer_move = random.randint(1,9)
                    if computer_move in pos_list:
                        continue
                    break
            computer_list.append(computer_move)
            # Prints Grid
            print_grid(computer_move,'O',2)

            if loop_count == 5:
                if winner(player_list,'player',2) == True or winner(computer_list,'Computer',2) == True:
                    pass
                else:
                    print "Match Was a draw!"
                replay = 1
                break

            # Checks winner
            if winner(computer_list,'Computer',2) == True:
                replay = 1
                break

            # Player's Move
            while True:
                try:
                    players_move = int(raw_input("\n\'%s\' Enter a value from the grid to plot your move: " %each))
                    if players_move in pos_list or players_move < 1 or players_move > 9:
                        print "Enter an available number that's between 1-9"
                        continue
                    break
                except:
                    print "Enter a number"
            
            player_list.append(players_move)
            # Sets player's move for printing
            print_grid(players_move,each,1)

            # Checks winner again
            if winner(player_list,'player',1) == True:
                print_grid(players_move,each,2)
                winner(player_list,'player',2)
                replay = 1
                break
    
if __name__ == "__main__":
    computer_and_player()
# USER: TCG
# Tic Tac Toe (User vs Computer)

import random

def place(num,x):
    # Returns all the moves made
    for i in range(len(x)):
        if x[i] == num:
            pos = x.index(num)
            return x[(pos + 1)]
    return str(num)

def print_grid(move,player,x):

    pos_list.extend([move,player])

    # Grid on which the player and computer play on            
    grid = ["","-------------\n",
        "|",place(1,pos_list),"|",place(2,pos_list),"|",place(3,pos_list),"|\n",
        "|---+---+---|\n",
        "|",place(4,pos_list),"|",place(5,pos_list),"|",place(6,pos_list),"|\n",
        "|---+---+---|\n",
        "|",place(7,pos_list),"|",place(8,pos_list),"|",place(9,pos_list),"|\n",
        "-------------\n"]
if x == 2:
    # Only prints one the player has made a move
    print '\n', ' '.join(grid)
    
def winner(x,player,xx):

    # Checks if there is a winner (Really messy, could do with code simplifying)
    if ((1 in x and 4 in x and 7 in x)or(1 in x and 2 in x and 3 in x)or(2 in x and 5 in x and 8 in x)or(3 in x and 6 in x and 9 in x)or
    (4 in x and 5 in x and 6 in x)or(7 in x and 8 in x and 9 in x)or(1 in x and 5 in x and 9 in x)or(3 in x and 5 in x and 7 in x)):
    # If prevents the A.I part from printing the statemnt
    if xx <> 1:
        print '\n'*5,"\'%s\'" %player, "HAS WON!"
    return 1 == 1

def computer_AI_part(listx):
    global computer_move

    # Chceks all possible values which the player can and enter to win and blocks it
    for x in range(1,10):
        if x not in pos_list:
            listx.append(x)
            if (winner(listx,'Computer',1)) == True:
                del listx[-1]
                computer_move = x
                return 1
            del listx[-1]

def computer_and_player():
    global computer_move,pos_list,player_list,computer_list
    replay,draw = 0,0

    while True:

        # Replay's the game
        if replay == 1:
            restart = raw_input("Would you like to replay?: ")
            if restart == "yes":
                pass
            else:
                return
        else:
            print "\nTic Tac Toe - Computer vs You", '\n'*2,"Computer goes first\n"
        
        replay,computer_move,players_move,loop_count,pos_list,player_list,computer_list = 0,0,0,0,[],[],[]
    
        for each in "XXXXX":
            loop_count += 1
        
            # Computer's Move
            if computer_AI_part(computer_list) or computer_AI_part(player_list) == 1:
                pass     
            else:
                while True:
                    computer_move = random.randint(1,9)
                    if computer_move in pos_list:
                        continue
                    break
            computer_list.append(computer_move)
            # Prints Grid
            print_grid(computer_move,'O',2)

            if loop_count == 5:
                if winner(player_list,'player',2) == True or winner(computer_list,'Computer',2) == True:
                    pass
                else:
                    print "Match Was a draw!"
                replay = 1
                break

            # Checks winner
            if winner(computer_list,'Computer',2) == True:
                replay = 1
                break

            # Player's Move
            while True:
                try:
                players_move = int(raw_input("\n\'%s\' Enter a value from the grid to plot your move: " %each))
                    if players_move in pos_list or players_move < 1 or players_move > 9:
                        print "Enter an available number that's between 1-9"
                        continue
                    break
                except:
                    print "Enter a number"
            
            player_list.append(players_move)
            # Sets player's move for printing
            print_grid(players_move,each,1)

            # Checks winner again
            if winner(player_list,'player',1) == True:
                print_grid(players_move,each,2)
                winner(player_list,'player',2)
                replay = 1
                break
    
if __name__ == "__main__":
    computer_and_player()
# USER: TCG
# Tic Tac Toe (User vs Computer)

import random

def place(num,x):
    # Returns all the moves made
    for i in range(len(x)):
        if x[i] == num:
            pos = x.index(num)
            return x[(pos + 1)]
    return str(num)

def print_grid(move,player,x):

    pos_list.extend([move,player])

    # Grid on which the player and computer play on            
    grid = ["","-------------\n",
        "|",place(1,pos_list),"|",place(2,pos_list),"|",place(3,pos_list),"|\n",
        "|---+---+---|\n",
        "|",place(4,pos_list),"|",place(5,pos_list),"|",place(6,pos_list),"|\n",
        "|---+---+---|\n",
        "|",place(7,pos_list),"|",place(8,pos_list),"|",place(9,pos_list),"|\n",
        "-------------\n"]
    if x == 2:
        # Only prints one the player has made a move
        print '\n', ' '.join(grid)
    
def winner(x,player,xx):

    # Checks if there is a winner (Really messy, could do with code simplifying)
    if ((1 in x and 4 in x and 7 in x)or(1 in x and 2 in x and 3 in x)or(2 in x and 5 in x and 8 in x)or(3 in x and 6 in x and 9 in x)or
    (4 in x and 5 in x and 6 in x)or(7 in x and 8 in x and 9 in x)or(1 in x and 5 in x and 9 in x)or(3 in x and 5 in x and 7 in x)):
    # If prevents the A.I part from printing the statemnt
        if xx <> 1:
            print '\n'*5,"\'%s\'" %player, "HAS WON!"
        return 1 == 1

def computer_AI_part(listx):
    global computer_move

    # Chceks all possible values which the player can and enter to win and blocks it
    for x in range(1,10):
        if x not in pos_list:
            listx.append(x)
            if (winner(listx,'Computer',1)) == True:
                del listx[-1]
                computer_move = x
                return 1
            del listx[-1]

def computer_and_player():
    global computer_move,pos_list,player_list,computer_list
    replay,draw = 0,0

    while True:

        # Replay's the game
        if replay == 1:
            restart = raw_input("Would you like to replay?: ")
            if restart == "yes":
                pass
            else:
                return
        else:
            print "\nTic Tac Toe - Computer vs You", '\n'*2,"Computer goes first\n"
        
        replay,computer_move,players_move,loop_count,pos_list,player_list,computer_list = 0,0,0,0,[],[],[]
    
        for each in "XXXXX":
            loop_count += 1
        
            # Computer's Move
            if computer_AI_part(computer_list) or computer_AI_part(player_list) == 1:
                pass     
            else:
                while True:
                    computer_move = random.randint(1,9)
                    if computer_move in pos_list:
                        continue
                    break
            computer_list.append(computer_move)
            # Prints Grid
            print_grid(computer_move,'O',2)

            if loop_count == 5:
                if winner(player_list,'player',2) == True or winner(computer_list,'Computer',2) == True:
                    pass
                else:
                    print "Match Was a draw!"
                replay = 1
                break

            # Checks winner
            if winner(computer_list,'Computer',2) == True:
                replay = 1
                break

            # Player's Move
            while True:
                try:
                    players_move = int(raw_input("\n\'%s\' Enter a value from the grid to plot your move: " %each))
                    if players_move in pos_list or players_move < 1 or players_move > 9:
                        print "Enter an available number that's between 1-9"
                        continue
                    break
                except:
                    print "Enter a number"
            
            player_list.append(players_move)
            # Sets player's move for printing
            print_grid(players_move,each,1)

            # Checks winner again
            if winner(player_list,'player',1) == True:
                print_grid(players_move,each,2)
                winner(player_list,'player',2)
                replay = 1
                break
    
if __name__ == "__main__":
    computer_and_player()
Post Undeleted by TCG
Post Deleted by TCG
deleted 36 characters in body
Source Link
TCG
  • 117
  • 2
  • 9
Loading
edited body
Source Link
TCG
  • 117
  • 2
  • 9
Loading
added 2 characters in body
Source Link
TCG
  • 117
  • 2
  • 9
Loading
added 6 characters in body
Source Link
TCG
  • 117
  • 2
  • 9
Loading
Source Link
TCG
  • 117
  • 2
  • 9
Loading