<commit_before>#!/usr/bin/env python

from blinkenlights import setup, cleanup
from fourleds import light, clear
from time import sleep
import random

pins = [37, 33, 31, 29, 36, 32, 22, 18]
#       yp  ym  gp  gm  rp  rm  bp  bm

setup(pins)
clear(pins)
for i in pins:
    light(i)
    sleep(0.1)
    clear(pins)

class Ball:
    def __init__(self, LL, UL, LR, UR):
        self.field = [[LL, UL], [LR, UR]]
        self.field_pins = [LL, UL, LR, UR]
        self.x = random.randint(0,1)
        self.y = random.randint(0,1)
        clear(self.field_pins)
        light(self.field[self.x][self.y])
    def hit(self):
        self.x = self.x ^ 1 # go to opposite side
        self.y = random.randint(0,1)
        clear(self.field_pins)
        light(self.field[self.x][self.y])
        sleep(1)
    def miss(self):
        clear(self.field_pins)
        for i in range(4):
            light(self.field_pins)
            sleep(0.2)
            clear(self.field_pins)
            sleep(0.2)
    def swing_by(self, player):
        if player.y == self.y:
            self.hit()
            return True
        else:
            self.miss()
            return False

class Player:
    def __init__(self, low, high):
        self.range = [low, high]
        self.y = random.randint(0,1)
        clear(self.range)
        for i in range(6):
            light(self.range[self.y])
            sleep(0.1)
            clear(self.range)
            sleep(0.1)
        light(self.range[self.y])
    def move(self, direction):
        assert (direction==0 or direction==1)
        self.y = direction
        clear(self.range)
        light(self.range[self.y])

myball = Ball(33, 29, 32, 18)
p1 = Player(37, 31)
p2 = Player(36, 22)

order = []
if myball.x == 0:
    order = [p1, p2]
else:
    order = [p2, p1]

for i in range(5):
    order[0].move(myball.y) # auto move
    sleep(0.5)
    myball.swing_by(order[0])
    order[1].move(myball.y) # auto move
    sleep(0.5)
    myball.swing_by(order[1])

order[0].move(myball.y ^ 1) # auto fail
sleep(0.5)
myball.swing_by(order[0])

cleanup()<commit_msg>Simulate player skill. Test for a miss and break from the game loop.<commit_after>#!/usr/bin/env python

from blinkenlights import setup, cleanup
from fourleds import light, clear
from time import sleep
import random

pins = [37, 33, 31, 29, 36, 32, 22, 18]
#       yp  ym  gp  gm  rp  rm  bp  bm

setup(pins)
clear(pins)
for i in pins:
    light(i)
    sleep(0.1)
    clear(pins)

class Ball:
    def __init__(self, LL, UL, LR, UR):
        self.field = [[LL, UL], [LR, UR]]
        self.field_pins = [LL, UL, LR, UR]
        self.x = random.randint(0,1)
        self.y = random.randint(0,1)
        clear(self.field_pins)
        light(self.field[self.x][self.y])
    def hit(self):
        self.x = self.x ^ 1 # go to opposite side
        self.y = random.randint(0,1)
        clear(self.field_pins)
        light(self.field[self.x][self.y])
        sleep(1)
    def miss(self):
        clear(self.field_pins)
        for i in range(4):
            light(self.field_pins)
            sleep(0.2)
            clear(self.field_pins)
            sleep(0.2)
    def swing_by(self, player):
        if player.y == self.y:
            self.hit()
            return True
        else:
            self.miss()
            return False

class Player:
    def __init__(self, low, high):
        self.range = [low, high]
        self.y = random.randint(0,1)
        clear(self.range)
        for i in range(6):
            light(self.range[self.y])
            sleep(0.1)
            clear(self.range)
            sleep(0.1)
        light(self.range[self.y])
    def move(self, direction):
        assert (direction==0 or direction==1)
        self.y = direction
        clear(self.range)
        light(self.range[self.y])

myball = Ball(33, 29, 32, 18)
p1 = Player(37, 31)
p2 = Player(36, 22)

order = []
if myball.x == 0:
    order = [p1, p2]
else:
    order = [p2, p1]

def imperfect(player, ball, success_rate=0.9):
    assert 0 <= success_rate <= 1
    if random.uniform(0,1) < success_rate:
        player.move(ball.y)
    else:
        player.move(ball.y ^ 1)

while True:
    imperfect(order[0], myball)
    sleep(0.5)
    inplay = myball.swing_by(order[0])
    if not inplay:
        break
    imperfect(order[1], myball)
    sleep(0.5)
    inplay = myball.swing_by(order[1])
    if not inplay:
        break

cleanup()
{'fraction_non_alphanumeric': 0.13598183881952328, 'fraction_numerical': 0.03518728717366629, 'mean_word_length': 2.0054570259208733, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1801450', 'n_tokens_mistral': 1704, 'n_tokens_neox': 1582, 'n_words': 388}