from random import randint from random import shuffle from math import sqrt import os import re import string size_board = 4 def create_board(n): # board = ['A','F','A','F']mix it # seen = [False, False, False, False] make all false board = list(range(n/2))*2 seen = [False]*n shuffle(board) return board, seen def print_board(board, seen): liste =[] for i in range(len(seen)): if seen[i] == True: liste.append(board[i]) else : liste.append("-") width = int(sqrt(len(board))) for i in range(width): for j in range(width): print liste[i*width+j], print "" def check_input(l): width = int(sqrt(l)) my_check = False while True: try: (x, y) = map(str,(raw_input("Pick a spot has to be two integers like ab").split())) while not x.isdigit() or int(x) > width-1 or int(x) < 0\ or not y.isdigit() or int(y) > width-1 or int(y) < 0 or seen[int(x)*width+int(y)] == True: (x, y) = map(str,(raw_input("Pick a spot has to be two integers like ab").split())) else: return int(x)*width+int(y) # my_check = True except ValueError: print "Value would not work try an other one" # my_check = False board, seen = create_board(size_board) clear = lambda: os.system('cls') cntr = 0 print_board(board,seen) turn = 0 print "Turn %d" % turn while False in seen: a = check_input(size_board) b = check_input(size_board) while a == b: b = check_input(size_board) cntr += 1 seen[(a)] = True seen[(b)] = True if cntr >= 1: clear() cntr = 0 turn += 1 my_check = False print "Turn %d" % turn print_board(board,seen) if board[int(a)] != board[int(b)]: seen[int(a)] = False seen[int(b)] = False print "GAME-OVER" print "GAME-OVER" print "GAME-OVER" print "It took you %d turns to finish it." % turn print "GAME-OVER" print "GAME-OVER" print "GAME-OVER" high_scores = open("hghscr.txt", "r") name = raw_input("Enter your name for the score board ") high_scores.close() the_list = [re.split(' ',line) for line in open('hghscr.txt')] the_list.append([name,int(turn)]); # print the_list the_list.sort(key = lambda tup: int(tup[1])) f = open('hghscr.txt', 'w') for item in the_list: # item[1] = string.replace(item[1],'\n','') f.write("%s %d\n" % (item[0], int(item[1]))) f.close() with open("hghscr.txt") as f: content = f.read() print content
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