Skip to content

Instantly share code, notes, and snippets.

@ENDERZOMBI102
Last active May 20, 2022 21:14
Show Gist options
  • Save ENDERZOMBI102/f6a6b76995969577ec7d64397fecb489 to your computer and use it in GitHub Desktop.
Save ENDERZOMBI102/f6a6b76995969577ec7d64397fecb489 to your computer and use it in GitHub Desktop.
Tic Tac Toe in a single line, requires wxPython
# this code is under the MIT license ( https://spdx.org/licenses/MIT.html )
( itertools := __import__('itertools'), unittest := __import__('unittest'), random := __import__('random'), wx := __import__('wx'), Try := lambda block, catch, finalli=None: ( res := unittest.FunctionTestCase( block )( type( 'TR', ( unittest.TestResult, ), { '__init__': lambda self, *args, **kwargs: ( super(self.__class__, self).__init__( *args, **kwargs ), setattr( self, 'errs', list() ) )[0], 'addError': lambda self, test, err: self.errs.append( err[1] ) } )() ), list( map( lambda err: catch.get( err.__class__, lambda err: print( err ) )( err ), res.errs ) ), finalli() if finalli is not None else None, None )[-1], While := lambda cond, block: Try( lambda: [ ( block(), Throw( UnicodeTranslateError( '', 0, 0, '' ) ) if not cond() else None ) for _ in itertools.repeat(None) ], catch={ UnicodeTranslateError: lambda err: None } ), Throw := lambda exc: ( _ for _ in [] ).throw( exc ), Tris := type( 'Tris', ( wx.Frame, object ), { '__init__': lambda self: ( super(Tris, self).__init__( parent=None, title='TrisLiner', style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER ), setattr( self, 'done', list() ), setattr( self, 'buttons', list() ), [ [ ( btn := wx.Button( parent=self, name=f'{x},{y}' ), btn.SetPosition( wx.Point( btn.GetSize().GetWidth() * x, btn.GetSize().GetHeight() * y ) ), btn.Bind( wx.EVT_BUTTON, self.onPress, btn ), self.buttons.append( btn ) ) for x in range( 0, 3 ) ] for y in range( 0, 3 ) ], setattr( self, 'win_states', [ [ ( 0, 0 ), ( 1, 0 ), ( 2, 0 ) ], [ ( 0, 1 ), ( 1, 1 ), ( 2, 1 ) ], [ ( 0, 2 ), ( 1, 2 ), ( 2, 2 ) ], [ ( 0, 0 ), ( 1, 1 ), ( 2, 2 ) ], [ ( 2, 0 ), ( 1, 1 ), ( 0, 2 ) ], [ ( 1, 0 ), ( 1, 1 ), ( 1, 2 ) ], [ ( 2, 0 ), ( 2, 1 ), ( 2, 2 ) ], [ ( 0, 0 ), ( 0, 1 ), ( 0, 2 ) ], ]), self.SetSize( 240, 108 ), self.Show(), None )[-1], 'onPress': lambda self, evt: ( btn := evt.GetEventObject(), btn.Disable(), btn.SetLabel('X'), self.done.append( ( int( btn.GetName()[0] ), int( btn.GetName()[2] ), 'X' ) ), shallReturn := False, ( wx.MessageBox( 'Nobody wins!', 'Stalemate!' ), self.Destroy(), shallReturn := True ) if len( self.done ) == 9 else None, [ ( ( wx.MessageBox( 'The player wins!', 'You Win!' ), self.Destroy(), shallReturn := True ) if ( state[0] + ( 'X', ) in self.done and state[1] + ( 'X', ) in self.done and state[2] + ( 'X', ) in self.done ) else ( wx.MessageBox( 'The AI wins!', 'You Loose!' ), self.Destroy(), shallReturn := True ) if ( state[0] + ( 'O', ) in self.done and state[1] + ( 'O', ) in self.done and state[2] + ( 'O', ) in self.done ) else None ) for state in self.win_states if not shallReturn ] if len( self.done ) > 4 and not shallReturn else None, loccals := { 'state': [], 'shallReturn': shallReturn }, While( cond=lambda: ( ( not loccals['shallReturn'] ) and len( set( ( state := random.choice( self.win_states ), loccals.__setitem__( 'state', state ) )[0] ).intersection( self.done ) ) < 3 ), block=lambda: ( [ ( ( self.buttons[ pos[0] * 3 + pos[1] ].Disable(), self.buttons[ pos[0] * 3 + pos[1] ].SetLabel('O'), self.done.append( pos + ( 'O', ) ), loccals.__setitem__( 'shallReturn', True ) ) if pos + ( 'X', ) not in self.done and pos + ( 'O', ) not in self.done else None ) for pos in loccals['state'] if not loccals[ 'shallReturn' ] ] ) ) ) }), app := wx.App(), root := Tris(), app.SetTopWindow( root ), app.MainLoop() )
# this code is under the MIT license ( https://spdx.org/licenses/MIT.html )
# noinspection PyArgumentList
(
itertools := __import__('itertools'),
unittest := __import__('unittest'),
random := __import__('random'),
wx := __import__('wx'),
Try := lambda block, catch, finalli=None: (
res := unittest.FunctionTestCase( block )(
type( 'TR', ( unittest.TestResult, ), {
'__init__': lambda self, *args, **kwargs: (
super(self.__class__, self).__init__( *args, **kwargs ),
setattr( self, 'errs', list() )
)[0],
'addError': lambda self, test, err: self.errs.append( err[1] )
} )()
),
list( map(
lambda err: catch.get( err.__class__, lambda err: print( err ) )( err ),
res.errs
) ),
finalli() if finalli is not None else None,
None
)[-1],
While := lambda cond, block: Try(
lambda: [
(
block(),
# end the loop if cond() returns False
Throw( UnicodeTranslateError( '', 0, 0, '' ) ) if not cond() else None
) for _ in itertools.repeat(None)
],
catch={ UnicodeTranslateError: lambda err: None }
),
Throw := lambda exc: ( _ for _ in [] ).throw( exc ),
Tris := type( 'Tris', ( wx.Frame, object ), {
'__init__': lambda self: (
super(Tris, self).__init__( parent=None, title='TrisLiner', style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER ),
setattr( self, 'done', list() ),
setattr( self, 'buttons', list() ),
[
[
(
btn := wx.Button( parent=self, name=f'{x},{y}' ),
btn.SetPosition( wx.Point( btn.GetSize().GetWidth() * x, btn.GetSize().GetHeight() * y ) ),
btn.Bind( wx.EVT_BUTTON, self.onPress, btn ),
self.buttons.append( btn )
) for x in range( 0, 3 )
] for y in range( 0, 3 )
],
setattr( self, 'win_states', [
# horizontal
[ ( 0, 0 ), ( 1, 0 ), ( 2, 0 ) ], # xxx ... ...
[ ( 0, 1 ), ( 1, 1 ), ( 2, 1 ) ], # ... xxx ...
[ ( 0, 2 ), ( 1, 2 ), ( 2, 2 ) ], # ... ... xxx
# diagonal
[ ( 0, 0 ), ( 1, 1 ), ( 2, 2 ) ], # x.. .X. ..X
[ ( 2, 0 ), ( 1, 1 ), ( 0, 2 ) ], # ..X .X. X..
# vertical
[ ( 1, 0 ), ( 1, 1 ), ( 1, 2 ) ], # .X. .X. .X.
[ ( 2, 0 ), ( 2, 1 ), ( 2, 2 ) ], # ..X ..X ..X
[ ( 0, 0 ), ( 0, 1 ), ( 0, 2 ) ], # X.. X.. X..
]),
self.SetSize( 240, 108 ),
self.Show(),
None
)[-1],
'onPress': lambda self, evt: (
btn := evt.GetEventObject(),
btn.Disable(),
btn.SetLabel('X'),
self.done.append( ( int( btn.GetName()[0] ), int( btn.GetName()[2] ), 'X' ) ),
shallReturn := False,
(
wx.MessageBox( 'Nobody wins!', 'Stalemate!' ),
self.Destroy(),
shallReturn := True
) if len( self.done ) == 9 else None,
# win logic
[
(
(
wx.MessageBox( 'The player wins!', 'You Win!' ),
self.Destroy(),
shallReturn := True
) if (
state[0] + ( 'X', ) in self.done and
state[1] + ( 'X', ) in self.done and
state[2] + ( 'X', ) in self.done
) else (
wx.MessageBox( 'The AI wins!', 'You Loose!' ),
self.Destroy(),
shallReturn := True
) if (
state[0] + ( 'O', ) in self.done and
state[1] + ( 'O', ) in self.done and
state[2] + ( 'O', ) in self.done
) else None
) for state in self.win_states if not shallReturn
] if len( self.done ) > 4 and not shallReturn else None,
# ai
loccals := { 'state': [], 'shallReturn': shallReturn },
While(
cond=lambda: (
( not loccals['shallReturn'] ) and
len(
set(
(
state := random.choice( self.win_states ),
loccals.__setitem__( 'state', state )
)[0]
).intersection( self.done )
) < 3
),
block=lambda: (
[
(
(
self.buttons[ pos[0] * 3 + pos[1] ].Disable(),
self.buttons[ pos[0] * 3 + pos[1] ].SetLabel('O'),
self.done.append( pos + ( 'O', ) ),
loccals.__setitem__( 'shallReturn', True )
) if pos + ( 'X', ) not in self.done and pos + ( 'O', ) not in self.done else None
) for pos in loccals['state'] if not loccals[ 'shallReturn' ]
]
)
)
)
}),
app := wx.App(),
root := Tris(),
app.SetTopWindow( root ),
app.MainLoop()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment