#Problem #2: # #A tic-tac-toe board is represented by a two dimensional vector. #X is represented by :x, O is represented by :o, and empty is represented by :e. #A player wins by placing three Xs or three Os in a horizontal, vertical, or diagonal row. #Write a function which analyzes a tic-tac-toe board and returns :x if X has won, :o if O has won, and nil if neither player has won. # #Example input: #[[:x :e :o] #[:x :e :e] #[:x :e :o]] #Result: #:x def check_for_game_over(boardstate): #main function that checks all possibilities #check all horizontal lines for line_num, line in enumerate(boardstate): last_check = check_horizontal_line(boardstate,line_num) if last_check != None: return last_check #check all vertical lines. Assumes a square playing board. for line_num, line in enumerate(boardstate): last_check = check_vertical_line(boardstate,line_num) if last_check != None: return last_check #check both horizontal lines. last_check = check_diagonal_lines(boardstate) return last_check def check_horizontal_line(boardstate,row_num): #checks a horizontal line for end game state return check_list_contains_all_x_or_o(boardstate[row_num]) def check_vertical_line(boardstate,col_num): #checks a vertical line for end game state line_vals=[] for vert_line in boardstate: line_vals.append(vert_line[col_num]) return check_list_contains_all_x_or_o(line_vals) def check_diagonal_lines(boardstate): #checks both diagonal lines for end game state line_vals=[] #top left to bottom right check for count1, vert_line in enumerate(boardstate): line_vals.append(vert_line[count1]) count1 += 1 check_val = check_list_contains_all_x_or_o(line_vals) if check_val != None: return check_val #bottom left to top right check. line_vals=[] for count1, vert_line in enumerate(reversed(boardstate)): line_vals.append(vert_line[count1]) count1 += 1 check_val = check_list_contains_all_x_or_o(line_vals) return check_val def check_list_contains_all_x_or_o(elementlist): #checks if list is occupied by all X or all O if is_list_of(elementlist,":x"): return ":x" if is_list_of(elementlist,":o"): return ":o" return None def is_list_of(elementlist, search_string): #returns true if list contains only the search string if elementlist.count(search_string) == len(elementlist): return True def board_to_string(boardstate): out_string = [] for horz_line in boardstate: out_string.append("| ") for val1 in horz_line: out_string.append(val1 + " |") out_string.append("\n") return "".join(out_string) #***** TESTS ****** #board list will be a list of boards (in [0]) with expected values (in [1]) board_list = [] #vertical win for x board_list.append([[[":x", ":e", ":o"],[":x", ":e", ":e"],[":x", ":e", ":o"]],":x"]) #diagonal win for x board_list.append([[[":x", ":e", ":o"],[":e", ":x", ":e"],[":e", ":o", ":x"]],":x"]) #horizontal win for x board_list.append([[[":x", ":x", ":x"],[":e", ":o", ":e"],[":e", ":e", ":o"]],":x"]) #no winner board_list.append([[[":x", ":e", ":o"],[":e", ":e", ":e"],[":x", ":e", ":o"]],None]) #vertical win for o board_list.append([[[":o", ":e", ":x"],[":o", ":e", ":e"],[":o", ":e", ":x"]],":o"]) for count1, board in enumerate(board_list): print "Checking boardstate " + str(count1+1) print board_to_string(board[0]) check_val = check_for_game_over(board[0]) if check_val == board[1]: print "The boardstate check function gives expected result: " + str(check_val) else: print "The boardstate check function DOES NOT give expected result, instead gives " + str(check_val) print "\n"
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question