def main(): print_instruction() board = [] for i in range(9): board.append(-1) win = False move = 0 while not win: print_board(board) print "Turn number" + str(move + 1) if move % 2 == 0: turn = 'X' else: turn = 'O' user = get_input(turn) while board[user] != -1: print "Invalid move! Cell already taken. Please try again.\n" user = get_input(turn) board[user] = 1 if turn == 'X' else 0 move += 1 if move > 4: winner = check_win(board) if winner != -1: out = "The winner is" out += "X" if winner == 1 else "O" out += "=D" quit_game(board, out) elif move >= 9: quit_game(board, "No winner :(")
def main(): print_instruction() board = [] for i in range(9): board.append(-1) win = False move = 0 while not win: print_board(board) print "Turn number" + str(move+1) if move % 2 == 0: turn = 'X' else: turn = 'O' user = get_input(turn) while board[user] != -1: print "Invalid move! Cell already taken. Please try again.\n" user = get_input(turn) board[user] = 1 if turn == 'X' else 0 move += 1 if move > 4: winner = check_win(board) if winner != -1: out = "The winner is" out += "X" if winner == 1 else "O" out += "=D" quit_game(board,out) elif move >= 9: quit_game(board, "No winner :(")
def game_loop(): global active_player, current_state active_player = starting_player finished = False first_ai = True col_ai = next_ai = 3 #takes care of processing for one full game while not(finished): draw_board() flag = False #if Human v Comp and it's comp's turn if game_type == 1 and active_player == comp_player: print "Computer is thinking..." if first_ai == True: #computer takes middle column each time col_ai = 3 first_ai = False else: (value, col_ai, next_ai) = aimodule.generate_move(current_state, 0, ai_diff, float("-inf"), float("inf"), next_ai, True) temp = current_state.copy_board() while temp.make_move(col_ai, "X") == False: #check move validity col_ai += 1 print "Computer moves to column %s" % col_ai current_state.make_move(col_ai, "X") insert_piece(col_ai, active_player) else: while not(flag): try: column = int(raw_input("Pick a column (0-6) or 7 to quit\n %s > " % active_player)) except ValueError: #if users enter a non-integer print "Invalid input" continue if not(column < 8 and column > -1): print "Invalid input" else: if column == 7: #chose 9 to quit print "Quitting current game" reset_game() draw_main() process_main() else: #chose a column (0-6) if insert_piece(column, active_player): flag = True current_state.make_move(column, "O") col_ai = column else: print "Column is already filled to the top" #have a valid pick at this point winlist = checkwin.check_win(board) if winlist == []: switch_turn() else: finished = True process_result(winlist) reset_game() draw_main() process_main()
def board_win(self): winner = check_win(self.board) if winner == []: #Empty list means not finished return 0 elif winner[0] == "Draw": #Draw return 0 elif winner[1][0][2] == "X": #"X" wins (AI) return 1 else: #"O" wins (human) return -1
def main(): print_instruction() board = [] for i in range(9): board.append(-1) win = False move = 0 while not win: print_board(board) print "Turn number" + str(move+1) if move % 2 == 0: #this is the user turn = 'X' user = get_input(turn) while board[user] != -1: print "Invalid move! Cell already taken. Please try again.\n" user = get_input(turn) board[user] = 1 else: #this will be the computer turn = 'O' comp=generate_o(board)#computer behaviour define board[comp]=0 move += 1 if move > 4: winner = check_win(board) if winner != -1: out = "The winner is" out += "X" if winner == 1 else "O" out += "=D" quit_game(board,out) elif move >= 9: quit_game(board, "No winner :(")