## Solving tic tac toe problem with python

| |
[ 所属分类 开发（python） | 发布者 店小二04 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏

Hello again, today we are going to continue exploring py.checkio.org site by solving this tic tac toe question with python, this question requires us to find out which player has won the tic tac toe game by returning ‘X’ if X wins, returning ‘Y’ if Y wins or ‘D’ if it is a draw. After solving this question I think I will create a tic tac toe game with python later on since I already have the solution for the game mechanism so better don’t waste this wonderful resource! Below is the entire solution to the above tic tac toe question. I have thought of making the solution shorter by introducing a few functions to replace the long solution but for now, we will stick to this one.

The solution below might not be the best so do comment under this article and tell me what do you think about the below solution.

def checkio(game_result): first = game_result[0] second = game_result[1] third = game_result[2] if(first[0] == first[1] == first[2] and first[0] != '.'): if(first[0] == 'X'): return 'X' elif(first[0] == 'O'): return 'O' elif(second[0] == second[1] == second[2] and second[1] != '.'): if(second[0] == 'X'): return 'X' elif(second[0] == 'O'): return 'O' elif(third[0] == third[1] == third[2] and third[0] != '.'): if(third[0] == 'X'): return 'X' elif(third[0] == 'O'): return 'O' elif(first[0] == second[0] == third[0] and third[0] != '.'): if(third[0] == 'X'): return 'X' elif(third[0] == 'O'): return 'O' elif(first[1] == second[1] == third[1] and second[1] != '.'): if(third[1] == 'X'): return 'X' elif(third[1] == 'O'): return 'O' elif(first[2] == second[2] == third[2] and second[2] != '.'): if(third[2] == 'X'): return 'X' elif(third[2] == 'O'): return 'O' elif(first[0] == second[1] == third[2] and second[1] != '.'): if(third[2] == 'X'): return 'X' elif(third[2] == 'O'): return 'O' elif(first[2] == second[1] == third[0] and third[0] != '.'): if(third[0] == 'X'): return 'X' elif(third[0] == 'O'): return 'O' else: return 'D'

The solution above will serve as a framework for those who want to develop your own tic tac toe game. I will continue to explore this site and see what it actually does by solving more python related problems in the coming articles.

• ### 富人思维：亿万富翁不是有亿万存款，而是有这个能力

1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责；
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性，不作出任何保证或承若；
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。