Beginner TicTacToe












3














I am trying to learn Python 3.6 with the help of a book. Until now I have learned the basics of functions, conditions and printing etc.
No loops yet - so they need to come from recursion.



To test the current stuff I wrote this basic TicTacToe - and would like to know what could be better.



def print_game(*args):
formatstring = " A B Cn" + "-" *10 + "n1 {}|{}|{}n" + "-" *10 + "n2 {}|{}|{}n" + "-" *10 + "n3 {}|{}|{}"
print(formatstring.format(*args))

def ask_player(round):
prompt_player1 = "Player one>> "
prompt_player2 = "Player two>> "

if round % 2 == 0:
current_choice = input(prompt_player2)
current_player = 2
else:
current_choice = input(prompt_player1)
current_player = 1
if current_choice != "A1" and current_choice != "B1" and current_choice != "C1" and current_choice != "A2" and current_choice != "B2" and current_choice != "C2" and current_choice != "A3" and current_choice != "B3" and current_choice != "C3":
print("Please use Field identifier like A1/B3 ...")
ask_player(round)
else:
return current_choice, current_player

def do_the_draw(game,field,player):
valid = 1
if player == 1:
char='X'
if player == 2:
char='O'
if field == 'A1' and game[0]==' ':
game[0] = char
elif field == 'B1' and game[1]==' ':
game[1] = char
elif field == 'C1' and game[2]==' ':
game[2] = char
elif field == 'A2' and game[3]==' ':
game[3] = char
elif field == 'B2' and game[4]==' ':
game[4] = char
elif field == 'C2' and game[5]==' ':
game[5] = char
elif field == 'A3' and game[6]==' ':
game[6] = char
elif field == 'B3' and game[7]==' ':
game[7] = char
elif field == 'C3' and game[8]==' ':
game[8] = char
else:
print(f"Invalid Move field {field} is already used - Choose another one")
valid = 0
print_game(*game)
return game,valid

def evaluate(s):
result = False #game continues
# Horizontal lines
if s[0]+s[1]+s[2] == 'XXX' or s[0]+s[1]+s[2] == 'OOO':
result = True
if s[3]+s[4]+s[5] == 'XXX' or s[3]+s[4]+s[5] == 'OOO':
result = True
if s[6]+s[7]+s[8] == 'XXX' or s[6]+s[7]+s[8] == 'OOO' :
result = True
# Vertical lines
if s[0]+s[3]+s[6] == 'XXX' or s[0]+s[3]+s[6] == 'OOO':
result = True
if s[1]+s[4]+s[7] == 'XXX' or s[1]+s[4]+s[7] == 'OOO':
result = True
if s[2]+s[5]+s[8] == 'XXX' or s[2]+s[5]+s[8] == 'OOO' :
result = True
# Diagonal lines
if s[0]+s[4]+s[8] == 'XXX' or s[1]+s[4]+s[7] == 'OOO':
result = True
if s[2]+s[4]+s[6] == 'XXX' or s[2]+s[5]+s[8] == 'OOO' :
result = True
return result


def main(draws,status):
user_input, player = ask_player(draws)
status,valid = do_the_draw(status,user_input,player)
if valid == 0:
return main(draws,status)
draws -=1
if evaluate(status):
draws = False
print(f"We have a winner Player: {player}")
if draws:
return main(draws,status)
elif input("try again?")=='y':
print("#" *100)
print("Here we go againnn")
draws = 9
status = " , , , , , , , , "
status = status.split(',')
print_game(*status)
return main(draws,status)



# Print the initial Game pattern
themaingame=" , , , , , , , , "
# cause i was not able to figure out how to initialise lists ... we split
themaingame = themaingame.split(',')
# Initial Print of the Board
print_game(*themaingame)

#Calling the Main Method with the maximum number of draws (9) and the prepared List
main(9,themaingame)









share|improve this question









New contributor




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




















  • Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
    – Ludisposed
    yesterday










  • I have rolled back your last edit. Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
    – Heslacher
    yesterday










  • Isn't this a duplicate to codereview.stackexchange.com/questions/211067/…
    – instaggy
    yesterday










  • thanks for pointing out - I was not sure how to do it and I only found the code of conduct.
    – Patrick
    yesterday
















3














I am trying to learn Python 3.6 with the help of a book. Until now I have learned the basics of functions, conditions and printing etc.
No loops yet - so they need to come from recursion.



To test the current stuff I wrote this basic TicTacToe - and would like to know what could be better.



def print_game(*args):
formatstring = " A B Cn" + "-" *10 + "n1 {}|{}|{}n" + "-" *10 + "n2 {}|{}|{}n" + "-" *10 + "n3 {}|{}|{}"
print(formatstring.format(*args))

def ask_player(round):
prompt_player1 = "Player one>> "
prompt_player2 = "Player two>> "

if round % 2 == 0:
current_choice = input(prompt_player2)
current_player = 2
else:
current_choice = input(prompt_player1)
current_player = 1
if current_choice != "A1" and current_choice != "B1" and current_choice != "C1" and current_choice != "A2" and current_choice != "B2" and current_choice != "C2" and current_choice != "A3" and current_choice != "B3" and current_choice != "C3":
print("Please use Field identifier like A1/B3 ...")
ask_player(round)
else:
return current_choice, current_player

def do_the_draw(game,field,player):
valid = 1
if player == 1:
char='X'
if player == 2:
char='O'
if field == 'A1' and game[0]==' ':
game[0] = char
elif field == 'B1' and game[1]==' ':
game[1] = char
elif field == 'C1' and game[2]==' ':
game[2] = char
elif field == 'A2' and game[3]==' ':
game[3] = char
elif field == 'B2' and game[4]==' ':
game[4] = char
elif field == 'C2' and game[5]==' ':
game[5] = char
elif field == 'A3' and game[6]==' ':
game[6] = char
elif field == 'B3' and game[7]==' ':
game[7] = char
elif field == 'C3' and game[8]==' ':
game[8] = char
else:
print(f"Invalid Move field {field} is already used - Choose another one")
valid = 0
print_game(*game)
return game,valid

def evaluate(s):
result = False #game continues
# Horizontal lines
if s[0]+s[1]+s[2] == 'XXX' or s[0]+s[1]+s[2] == 'OOO':
result = True
if s[3]+s[4]+s[5] == 'XXX' or s[3]+s[4]+s[5] == 'OOO':
result = True
if s[6]+s[7]+s[8] == 'XXX' or s[6]+s[7]+s[8] == 'OOO' :
result = True
# Vertical lines
if s[0]+s[3]+s[6] == 'XXX' or s[0]+s[3]+s[6] == 'OOO':
result = True
if s[1]+s[4]+s[7] == 'XXX' or s[1]+s[4]+s[7] == 'OOO':
result = True
if s[2]+s[5]+s[8] == 'XXX' or s[2]+s[5]+s[8] == 'OOO' :
result = True
# Diagonal lines
if s[0]+s[4]+s[8] == 'XXX' or s[1]+s[4]+s[7] == 'OOO':
result = True
if s[2]+s[4]+s[6] == 'XXX' or s[2]+s[5]+s[8] == 'OOO' :
result = True
return result


def main(draws,status):
user_input, player = ask_player(draws)
status,valid = do_the_draw(status,user_input,player)
if valid == 0:
return main(draws,status)
draws -=1
if evaluate(status):
draws = False
print(f"We have a winner Player: {player}")
if draws:
return main(draws,status)
elif input("try again?")=='y':
print("#" *100)
print("Here we go againnn")
draws = 9
status = " , , , , , , , , "
status = status.split(',')
print_game(*status)
return main(draws,status)



# Print the initial Game pattern
themaingame=" , , , , , , , , "
# cause i was not able to figure out how to initialise lists ... we split
themaingame = themaingame.split(',')
# Initial Print of the Board
print_game(*themaingame)

#Calling the Main Method with the maximum number of draws (9) and the prepared List
main(9,themaingame)









share|improve this question









New contributor




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




















  • Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
    – Ludisposed
    yesterday










  • I have rolled back your last edit. Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
    – Heslacher
    yesterday










  • Isn't this a duplicate to codereview.stackexchange.com/questions/211067/…
    – instaggy
    yesterday










  • thanks for pointing out - I was not sure how to do it and I only found the code of conduct.
    – Patrick
    yesterday














3












3








3


1





I am trying to learn Python 3.6 with the help of a book. Until now I have learned the basics of functions, conditions and printing etc.
No loops yet - so they need to come from recursion.



To test the current stuff I wrote this basic TicTacToe - and would like to know what could be better.



def print_game(*args):
formatstring = " A B Cn" + "-" *10 + "n1 {}|{}|{}n" + "-" *10 + "n2 {}|{}|{}n" + "-" *10 + "n3 {}|{}|{}"
print(formatstring.format(*args))

def ask_player(round):
prompt_player1 = "Player one>> "
prompt_player2 = "Player two>> "

if round % 2 == 0:
current_choice = input(prompt_player2)
current_player = 2
else:
current_choice = input(prompt_player1)
current_player = 1
if current_choice != "A1" and current_choice != "B1" and current_choice != "C1" and current_choice != "A2" and current_choice != "B2" and current_choice != "C2" and current_choice != "A3" and current_choice != "B3" and current_choice != "C3":
print("Please use Field identifier like A1/B3 ...")
ask_player(round)
else:
return current_choice, current_player

def do_the_draw(game,field,player):
valid = 1
if player == 1:
char='X'
if player == 2:
char='O'
if field == 'A1' and game[0]==' ':
game[0] = char
elif field == 'B1' and game[1]==' ':
game[1] = char
elif field == 'C1' and game[2]==' ':
game[2] = char
elif field == 'A2' and game[3]==' ':
game[3] = char
elif field == 'B2' and game[4]==' ':
game[4] = char
elif field == 'C2' and game[5]==' ':
game[5] = char
elif field == 'A3' and game[6]==' ':
game[6] = char
elif field == 'B3' and game[7]==' ':
game[7] = char
elif field == 'C3' and game[8]==' ':
game[8] = char
else:
print(f"Invalid Move field {field} is already used - Choose another one")
valid = 0
print_game(*game)
return game,valid

def evaluate(s):
result = False #game continues
# Horizontal lines
if s[0]+s[1]+s[2] == 'XXX' or s[0]+s[1]+s[2] == 'OOO':
result = True
if s[3]+s[4]+s[5] == 'XXX' or s[3]+s[4]+s[5] == 'OOO':
result = True
if s[6]+s[7]+s[8] == 'XXX' or s[6]+s[7]+s[8] == 'OOO' :
result = True
# Vertical lines
if s[0]+s[3]+s[6] == 'XXX' or s[0]+s[3]+s[6] == 'OOO':
result = True
if s[1]+s[4]+s[7] == 'XXX' or s[1]+s[4]+s[7] == 'OOO':
result = True
if s[2]+s[5]+s[8] == 'XXX' or s[2]+s[5]+s[8] == 'OOO' :
result = True
# Diagonal lines
if s[0]+s[4]+s[8] == 'XXX' or s[1]+s[4]+s[7] == 'OOO':
result = True
if s[2]+s[4]+s[6] == 'XXX' or s[2]+s[5]+s[8] == 'OOO' :
result = True
return result


def main(draws,status):
user_input, player = ask_player(draws)
status,valid = do_the_draw(status,user_input,player)
if valid == 0:
return main(draws,status)
draws -=1
if evaluate(status):
draws = False
print(f"We have a winner Player: {player}")
if draws:
return main(draws,status)
elif input("try again?")=='y':
print("#" *100)
print("Here we go againnn")
draws = 9
status = " , , , , , , , , "
status = status.split(',')
print_game(*status)
return main(draws,status)



# Print the initial Game pattern
themaingame=" , , , , , , , , "
# cause i was not able to figure out how to initialise lists ... we split
themaingame = themaingame.split(',')
# Initial Print of the Board
print_game(*themaingame)

#Calling the Main Method with the maximum number of draws (9) and the prepared List
main(9,themaingame)









share|improve this question









New contributor




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











I am trying to learn Python 3.6 with the help of a book. Until now I have learned the basics of functions, conditions and printing etc.
No loops yet - so they need to come from recursion.



To test the current stuff I wrote this basic TicTacToe - and would like to know what could be better.



def print_game(*args):
formatstring = " A B Cn" + "-" *10 + "n1 {}|{}|{}n" + "-" *10 + "n2 {}|{}|{}n" + "-" *10 + "n3 {}|{}|{}"
print(formatstring.format(*args))

def ask_player(round):
prompt_player1 = "Player one>> "
prompt_player2 = "Player two>> "

if round % 2 == 0:
current_choice = input(prompt_player2)
current_player = 2
else:
current_choice = input(prompt_player1)
current_player = 1
if current_choice != "A1" and current_choice != "B1" and current_choice != "C1" and current_choice != "A2" and current_choice != "B2" and current_choice != "C2" and current_choice != "A3" and current_choice != "B3" and current_choice != "C3":
print("Please use Field identifier like A1/B3 ...")
ask_player(round)
else:
return current_choice, current_player

def do_the_draw(game,field,player):
valid = 1
if player == 1:
char='X'
if player == 2:
char='O'
if field == 'A1' and game[0]==' ':
game[0] = char
elif field == 'B1' and game[1]==' ':
game[1] = char
elif field == 'C1' and game[2]==' ':
game[2] = char
elif field == 'A2' and game[3]==' ':
game[3] = char
elif field == 'B2' and game[4]==' ':
game[4] = char
elif field == 'C2' and game[5]==' ':
game[5] = char
elif field == 'A3' and game[6]==' ':
game[6] = char
elif field == 'B3' and game[7]==' ':
game[7] = char
elif field == 'C3' and game[8]==' ':
game[8] = char
else:
print(f"Invalid Move field {field} is already used - Choose another one")
valid = 0
print_game(*game)
return game,valid

def evaluate(s):
result = False #game continues
# Horizontal lines
if s[0]+s[1]+s[2] == 'XXX' or s[0]+s[1]+s[2] == 'OOO':
result = True
if s[3]+s[4]+s[5] == 'XXX' or s[3]+s[4]+s[5] == 'OOO':
result = True
if s[6]+s[7]+s[8] == 'XXX' or s[6]+s[7]+s[8] == 'OOO' :
result = True
# Vertical lines
if s[0]+s[3]+s[6] == 'XXX' or s[0]+s[3]+s[6] == 'OOO':
result = True
if s[1]+s[4]+s[7] == 'XXX' or s[1]+s[4]+s[7] == 'OOO':
result = True
if s[2]+s[5]+s[8] == 'XXX' or s[2]+s[5]+s[8] == 'OOO' :
result = True
# Diagonal lines
if s[0]+s[4]+s[8] == 'XXX' or s[1]+s[4]+s[7] == 'OOO':
result = True
if s[2]+s[4]+s[6] == 'XXX' or s[2]+s[5]+s[8] == 'OOO' :
result = True
return result


def main(draws,status):
user_input, player = ask_player(draws)
status,valid = do_the_draw(status,user_input,player)
if valid == 0:
return main(draws,status)
draws -=1
if evaluate(status):
draws = False
print(f"We have a winner Player: {player}")
if draws:
return main(draws,status)
elif input("try again?")=='y':
print("#" *100)
print("Here we go againnn")
draws = 9
status = " , , , , , , , , "
status = status.split(',')
print_game(*status)
return main(draws,status)



# Print the initial Game pattern
themaingame=" , , , , , , , , "
# cause i was not able to figure out how to initialise lists ... we split
themaingame = themaingame.split(',')
# Initial Print of the Board
print_game(*themaingame)

#Calling the Main Method with the maximum number of draws (9) and the prepared List
main(9,themaingame)






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






share|improve this question









New contributor




Patrick 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




Patrick 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








edited yesterday









Heslacher

45k460155




45k460155






New contributor




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









asked 2 days ago









PatrickPatrick

184




184




New contributor




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





New contributor





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






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












  • Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
    – Ludisposed
    yesterday










  • I have rolled back your last edit. Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
    – Heslacher
    yesterday










  • Isn't this a duplicate to codereview.stackexchange.com/questions/211067/…
    – instaggy
    yesterday










  • thanks for pointing out - I was not sure how to do it and I only found the code of conduct.
    – Patrick
    yesterday


















  • Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
    – Ludisposed
    yesterday










  • I have rolled back your last edit. Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
    – Heslacher
    yesterday










  • Isn't this a duplicate to codereview.stackexchange.com/questions/211067/…
    – instaggy
    yesterday










  • thanks for pointing out - I was not sure how to do it and I only found the code of conduct.
    – Patrick
    yesterday
















Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
– Ludisposed
yesterday




Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
– Ludisposed
yesterday












I have rolled back your last edit. Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
– Heslacher
yesterday




I have rolled back your last edit. Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
– Heslacher
yesterday












Isn't this a duplicate to codereview.stackexchange.com/questions/211067/…
– instaggy
yesterday




Isn't this a duplicate to codereview.stackexchange.com/questions/211067/…
– instaggy
yesterday












thanks for pointing out - I was not sure how to do it and I only found the code of conduct.
– Patrick
yesterday




thanks for pointing out - I was not sure how to do it and I only found the code of conduct.
– Patrick
yesterday










1 Answer
1






active

oldest

votes


















3














maybe I have some useful suggestions.



I think having main recursively call itself is a little weird. Try to keep the main very short, so my suggestion create a function called play_tictactoe() and call that from main instead. This also means play_tictactoe recursively calls itself instead of main.



You have a function called do_the_draw() but it also updates the game board. Functions typically do 1 thing and it should be obvious based on it's name. You could separate your draw logic and update logic. You could just rename do_the_draw() to update_board() and remove the drawing logic and call it in the play function.



evaluate can be simplified by supplying a symbol. evaluate(s, char) could reduce the function by half, so it could be evaluate(status, 'OOO') and evaluate(status, 'XXX') which will result in less typing errors like you have for if s[0]+s[4]+s[8] == 'XXX' or s[1]+s[4]+s[7] == 'OOO':



Finally if you have access to maps or arrays (ignore this if you do not) you can probably reduce some of those huge if groups down using a map. I won't go into too much detail since you probably don't have access but a map of field to numbers such as { "A1": 0, "A2": 1 } could help reduce the long if statement by just checking if the map has a key and the other if statement since it could just check game[fields[field]] == ' '. If you have access to array functions you could do something similar with just arrays and using their built in functions so you do not need to loop over them yourself.



Hopefully that makes sense and it was an inter






share|improve this answer








New contributor




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


















    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
    });


    }
    });






    Patrick 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%2f211053%2fbeginner-tictactoe%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    maybe I have some useful suggestions.



    I think having main recursively call itself is a little weird. Try to keep the main very short, so my suggestion create a function called play_tictactoe() and call that from main instead. This also means play_tictactoe recursively calls itself instead of main.



    You have a function called do_the_draw() but it also updates the game board. Functions typically do 1 thing and it should be obvious based on it's name. You could separate your draw logic and update logic. You could just rename do_the_draw() to update_board() and remove the drawing logic and call it in the play function.



    evaluate can be simplified by supplying a symbol. evaluate(s, char) could reduce the function by half, so it could be evaluate(status, 'OOO') and evaluate(status, 'XXX') which will result in less typing errors like you have for if s[0]+s[4]+s[8] == 'XXX' or s[1]+s[4]+s[7] == 'OOO':



    Finally if you have access to maps or arrays (ignore this if you do not) you can probably reduce some of those huge if groups down using a map. I won't go into too much detail since you probably don't have access but a map of field to numbers such as { "A1": 0, "A2": 1 } could help reduce the long if statement by just checking if the map has a key and the other if statement since it could just check game[fields[field]] == ' '. If you have access to array functions you could do something similar with just arrays and using their built in functions so you do not need to loop over them yourself.



    Hopefully that makes sense and it was an inter






    share|improve this answer








    New contributor




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























      3














      maybe I have some useful suggestions.



      I think having main recursively call itself is a little weird. Try to keep the main very short, so my suggestion create a function called play_tictactoe() and call that from main instead. This also means play_tictactoe recursively calls itself instead of main.



      You have a function called do_the_draw() but it also updates the game board. Functions typically do 1 thing and it should be obvious based on it's name. You could separate your draw logic and update logic. You could just rename do_the_draw() to update_board() and remove the drawing logic and call it in the play function.



      evaluate can be simplified by supplying a symbol. evaluate(s, char) could reduce the function by half, so it could be evaluate(status, 'OOO') and evaluate(status, 'XXX') which will result in less typing errors like you have for if s[0]+s[4]+s[8] == 'XXX' or s[1]+s[4]+s[7] == 'OOO':



      Finally if you have access to maps or arrays (ignore this if you do not) you can probably reduce some of those huge if groups down using a map. I won't go into too much detail since you probably don't have access but a map of field to numbers such as { "A1": 0, "A2": 1 } could help reduce the long if statement by just checking if the map has a key and the other if statement since it could just check game[fields[field]] == ' '. If you have access to array functions you could do something similar with just arrays and using their built in functions so you do not need to loop over them yourself.



      Hopefully that makes sense and it was an inter






      share|improve this answer








      New contributor




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





















        3












        3








        3






        maybe I have some useful suggestions.



        I think having main recursively call itself is a little weird. Try to keep the main very short, so my suggestion create a function called play_tictactoe() and call that from main instead. This also means play_tictactoe recursively calls itself instead of main.



        You have a function called do_the_draw() but it also updates the game board. Functions typically do 1 thing and it should be obvious based on it's name. You could separate your draw logic and update logic. You could just rename do_the_draw() to update_board() and remove the drawing logic and call it in the play function.



        evaluate can be simplified by supplying a symbol. evaluate(s, char) could reduce the function by half, so it could be evaluate(status, 'OOO') and evaluate(status, 'XXX') which will result in less typing errors like you have for if s[0]+s[4]+s[8] == 'XXX' or s[1]+s[4]+s[7] == 'OOO':



        Finally if you have access to maps or arrays (ignore this if you do not) you can probably reduce some of those huge if groups down using a map. I won't go into too much detail since you probably don't have access but a map of field to numbers such as { "A1": 0, "A2": 1 } could help reduce the long if statement by just checking if the map has a key and the other if statement since it could just check game[fields[field]] == ' '. If you have access to array functions you could do something similar with just arrays and using their built in functions so you do not need to loop over them yourself.



        Hopefully that makes sense and it was an inter






        share|improve this answer








        New contributor




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









        maybe I have some useful suggestions.



        I think having main recursively call itself is a little weird. Try to keep the main very short, so my suggestion create a function called play_tictactoe() and call that from main instead. This also means play_tictactoe recursively calls itself instead of main.



        You have a function called do_the_draw() but it also updates the game board. Functions typically do 1 thing and it should be obvious based on it's name. You could separate your draw logic and update logic. You could just rename do_the_draw() to update_board() and remove the drawing logic and call it in the play function.



        evaluate can be simplified by supplying a symbol. evaluate(s, char) could reduce the function by half, so it could be evaluate(status, 'OOO') and evaluate(status, 'XXX') which will result in less typing errors like you have for if s[0]+s[4]+s[8] == 'XXX' or s[1]+s[4]+s[7] == 'OOO':



        Finally if you have access to maps or arrays (ignore this if you do not) you can probably reduce some of those huge if groups down using a map. I won't go into too much detail since you probably don't have access but a map of field to numbers such as { "A1": 0, "A2": 1 } could help reduce the long if statement by just checking if the map has a key and the other if statement since it could just check game[fields[field]] == ' '. If you have access to array functions you could do something similar with just arrays and using their built in functions so you do not need to loop over them yourself.



        Hopefully that makes sense and it was an inter







        share|improve this answer








        New contributor




        mashumafi 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 answer



        share|improve this answer






        New contributor




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









        answered 2 days ago









        mashumafimashumafi

        461




        461




        New contributor




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





        New contributor





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






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






















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










            draft saved

            draft discarded


















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













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












            Patrick 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%2f211053%2fbeginner-tictactoe%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

            Список кардиналов, возведённых папой римским Каликстом III

            Deduzione

            Mysql.sock missing - “Can't connect to local MySQL server through socket”