OSDN Git Service

3巻の内容をpython3に対応
authoryamat0jp <yamat0jp@yahoo.co.jp>
Sat, 28 Apr 2018 08:39:31 +0000 (17:39 +0900)
committeryamat0jp <yamat0jp@yahoo.co.jp>
Sat, 28 Apr 2018 08:39:31 +0000 (17:39 +0900)
puzzle/picture.jpg [new file with mode: 0644]
puzzle/puzzle.py [new file with mode: 0644]
temp.py [new file with mode: 0644]

diff --git a/puzzle/picture.jpg b/puzzle/picture.jpg
new file mode 100644 (file)
index 0000000..99a3758
Binary files /dev/null and b/puzzle/picture.jpg differ
diff --git a/puzzle/puzzle.py b/puzzle/puzzle.py
new file mode 100644 (file)
index 0000000..dc1219e
--- /dev/null
@@ -0,0 +1,94 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Sat Apr 28 16:54:27 2018
+
+@author: fuke masasi
+"""
+
+import pygame,sys,os
+from pygame.locals import *
+
+Wid = 4
+Hei = 3
+
+class Tile():
+    def __init__(self,x,y):
+        s = (x*picture[0],y*picture[1])
+        self.area = pygame.Rect(s,picture)
+        self.source = (s)
+    def start(self):
+        self.area = pygame.Rect(self.source,picture)
+
+def path():
+    for name in sys.argv:
+        if os.path.exists(name) == True:
+            return os.path.dirname(name)
+    return ''
+
+image = pygame.image.load(os.path.join(path(),'picture.jpg'))
+picture = (image.get_width()//Wid,image.get_height()//Hei)
+screen = pygame.display.set_mode((picture[0]*Wid,picture[1]*Hei))
+image.convert()
+ground = [[Tile(x,y) for y in range(Hei)] for x in range(Wid)]
+
+def main():
+    while True:
+        keydown()
+        draw()
+        
+def keydown():
+    for event in pygame.event.get():
+        if event.type == QUIT:
+            sys.exit()
+        if event.type == KEYDOWN:
+            if event.key == K_ESCAPE:
+                sys.exit()
+            elif event.key == K_RETURN:
+                start()
+        if event.type == MOUSEBUTTONDOWN:
+            x,y = pygame.mouse.get_pos()
+            click(x//picture[0],y//picture[1])
+
+class Empty():
+    def __init__(self):
+        self.x = Wid-1
+        self.y = Hei-1
+        
+empty = Empty()
+
+def click(x,y):
+    if x == empty.x:
+        if y > empty.y:
+            for w in range(empty.y,y):
+                ground[x][w].area = ground[x][w+1].area
+        elif y < empty.y:
+            for w in range(empty.y,y,-1):
+                ground[x][w].area = ground[x][w-1].area
+        empty.x,empty.y = x,y
+    elif y == empty.y:
+        if x > empty.x:
+            for w in range(empty.x,x):
+                ground[w][y].area = ground[w+1][y].area
+        elif x < empty.x:
+            for w in range(empty.x,x,-1):
+                ground[w][y].area = ground[w-1][y].area
+        empty.x,empty.y = x,y
+        
+def draw():
+    for x in range(Wid):
+        for y in range(Hei):
+            s = ground[x][y]
+            if (x,y) == (empty.x,empty.y):
+                pygame.draw.rect(screen,(255,255,255),pygame.Rect((empty.x*picture[0],empty.y*picture[1]),picture))
+            else:
+                screen.blit(image,(x*picture[0],y*picture[1]),s.area)
+                pygame.draw.rect(screen,(0,0,0),pygame.Rect((x*picture[0],y*picture[1]),picture),1)
+    pygame.display.update()
+
+def start():
+    empty.x,empty.y = Wid-1,Hei-1
+    for x in range(Wid):
+        for y in range(Hei):
+            ground[x][y].start()
+
+main()
\ No newline at end of file
diff --git a/temp.py b/temp.py
new file mode 100644 (file)
index 0000000..422a8d5
--- /dev/null
+++ b/temp.py
@@ -0,0 +1,42 @@
+#coding: UTF-8
+
+import random
+import sys
+
+p = 4
+numbers = [0 for x in range(p)]
+for x in range(p):
+    while True:     
+        n = random.randint(0,9) #新たに0から9までを用意        
+        for y in range(x): #決定しているx-1番目までを見る          
+            if numbers[y] == n:#重複していればやりなおし
+                break
+        else:
+            numbers[x] = n
+            break
+temp = ""
+for x in numbers:
+    temp += str(x)
+while True:   
+    print(u"数値を入力してください")
+    code = input()#入力をcodeに記憶
+    if code == "":
+        print(temp,u" :終了します")#空文字なら答えを表示して終了
+        input()#入力があるまで終了させない
+        sys.exit()
+    while len(code) < p:
+        code += "?"#適当な文字列で埋める
+    m = 0#hitを格納
+    n = 0#blowを格納
+    for x in range(p):
+        for y in range(p):
+            if temp[x] == code[y]:#等しい数字(文字)があるか確認         
+                if x == y:
+                    m += 1#位置が同じならhit
+                else:
+                    n += 1#違う場合はblow
+    print(m," hit, ",n," blow.")
+    if m == p:#すべてhitなら終了
+        break
+print(u"おめでとうございます")
+input()#終了を一時待つ