Python Projects

Teaching software development? Using LiveCode to build your curriculum? This is the forum for you.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Python Projects

Post by richmond62 » Wed Nov 10, 2021 8:33 am

SShot 2021-11-10 at 9.31.40.png
-
Yes, we have to find a winner, but with LiveCode we can do that in a way that
more closely resembles real life to the way it is done in Python.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Python Projects

Post by richmond62 » Wed Nov 10, 2021 8:51 am

SShot 2021-11-10 at 9.43.55.png
-
The grey part of the LiveCode stack will be hidden to the end-user.

The script in the cells of the grid have been modifed in 2 ways:

Code: Select all

on mouseUp
   if fld "f1" is empty then
      if the shiftKey is down then
         set the textColor of fld "f1" to red
         put "O" into me
      else
         set the textColor of fld "f1" to black
         put "X" into me
      end if
      add 1 to fld "fFILLED"
      --
      if fld "fFILLED" contains "9" then
         send "mouseUp" to button "WIN"
      end if
      --
   end if
end mouseUp

Code: Select all

add 1 to fld "fFILLED"
Means that every time a cell is filled with an X or a O, the number of filled cells
is updated in the field "fFILLED" [that's the field in the grey area of the stack].

Code: Select all

if fld "fFILLED" contains "9" then
         send "mouseUp" to button "WIN"
      end if
When the field "fFILLED" contains '9' a 'mouseUp' signal is sent to the button "WIN"

(which, at the moment contains no code)

Code: Select all

on mouseUp
   --the winner will be worked out here
end mouseUp
(if one types '--' at the start of a line whatever is written after that is NOT regarded as code)

where the winner will be calculated.
Attachments
Nowts & Crosses 3.livecode.zip
Here we go.
(8.26 KiB) Downloaded 143 times

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Python Projects

Post by richmond62 » Wed Nov 10, 2021 9:03 am

eightWays.jpg
-
There are 8 ways either side can win the game.
-
eightWays2.jpg
-
4 of which can be determined by initially finding out
if the central cell (field "f5") is occupied.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Python Projects

Post by richmond62 » Wed Nov 10, 2021 10:03 am

SShot 2021-11-10 at 11.00.52.png
-
SShot 2021-11-10 at 11.01.30.png
-
We'll start by setting up 2 pictures, here called
'OWIN' and 'XWIN' respectedly.

[These were made inwith LiveCode, but they could, just as easily, be imported.]

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Python Projects

Post by richmond62 » Wed Nov 10, 2021 10:17 am

On the basis of the pictorial analysis of winning patterns we can write code in
the scriptEditor of the 'WIN' button:

Code: Select all

on mouseUp
   wait 30 ticks
   --X--
   if fld "f5" contains "X" then
      if fld "f4" contains "X" and fld "f6" contains "X" then
         set the vis of img "XWIN" to true
      end if
      if fld "f2" contains "X" and fld "f8" contains "X" then
         set the vis of img "XWIN" to true
      end if
      if fld "f1" contains "X" and fld "f9" contains "X" then
         set the vis of img "XWIN" to true
      end if
      if fld "f3" contains "X" and fld "f3" contains "X" then
         set the vis of img "XWIN" to true
      end if
   end if
   ---
   if fld "f1" contains "X" then
      if fld "f2" contains "X" and fld "f3" contains "X" then
         set the vis of img "XWIN" to true
      end if
      if fld "f4" contains "X" and fld "f7" contains "X" then
         set the vis of img "XWIN" to true
      end if
   end if
   ---
   if fld "f9" contains "X" then
      if fld "f3" contains "X" and fld "f6" contains "X" then
         set the vis of img "XWIN" to true
      end if
      if fld "f7" contains "X" and fld "f8" contains "X" then
         set the vis of img "XWIN" to true
      end if
   end if
   --O--
   if fld "f5" contains "O" then
      if fld "f4" contains "O" and fld "f6" contains "O" then
         set the vis of img "OWIN" to true
      end if
      if fld "f2" contains "O" and fld "f8" contains "O" then
         set the vis of img "OWIN" to true
      end if
      if fld "f1" contains "O" and fld "f9" contains "O" then
         set the vis of img "OWIN" to true
      end if
      if fld "f3" contains "O" and fld "f3" contains "O" then
         set the vis of img "OWIN" to true
      end if
   end if
   ---
   if fld "f1" contains "O" then
      if fld "f2" contains "O" and fld "f3" contains "O" then
         set the vis of img "OWIN" to true
      end if
      if fld "f4" contains "O" and fld "f7" contains "O" then
         set the vis of img "OWIN" to true
      end if
   end if
   ---
   if fld "f9" contains "O" then
      if fld "f3" contains "O" and fld "f6" contains "O" then
         set the vis of img "OWIN" to true
      end if
      if fld "f7" contains "O" and fld "f8" contains "O" then
         set the vis of img "OWIN" to true
      end if
   end if
end mouseUp
At which point the exercise is finished.
Attachments
Nowts & Crosses 4.livecode.zip
Here's the stack.
(22.44 KiB) Downloaded 127 times

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Python Projects

Post by richmond62 » Wed Nov 10, 2021 12:25 pm

One of the SHORTCOMINGS of this stack is that it does NOT check
for a WIN until all 9 of the cells are filled, so this should be changed:
-
SShot 2021-11-10 at 13.20.52.png
-
You will see that the condition about '9' is now redundant.
-
SShot 2021-11-10 at 13.23.16.png
-
SShot 2021-11-10 at 13.24.57.png
Last edited by richmond62 on Wed Nov 10, 2021 1:14 pm, edited 1 time in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Python Projects

Post by richmond62 » Wed Nov 10, 2021 12:32 pm

You will also realise that, now, the field "fFILLED" is also unnecessary
and can be deleted, as well as any code refering to it:
-
SShot 2021-11-10 at 13.30.37.png
-
SShot 2021-11-10 at 13.27.02.png
Attachments
Nowts & Crosses 5.livecode.zip
Here's the stack.
(22.36 KiB) Downloaded 141 times

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Python Projects

Post by richmond62 » Wed Nov 10, 2021 12:34 pm

The next step is to set up the Noughts and Crosses game so the end-user can play
against the computer
.

Obviously this will need quite a lot of code jiggling.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Python Projects

Post by richmond62 » Wed Nov 10, 2021 12:46 pm

The FIRST thing we have to do is REMOVE the chance of the end-user
inserting an 'O' into a cell:
-
SShot 2021-11-10 at 13.45.21.png
-
Then we'll have a BUTTON to allow the computer to play against the end-user:
-
SShot 2021-11-10 at 13.47.51.png
-

Code: Select all

on mouseUp
   put 1 into CELL
   put 0 into STOPPER
   repeat until CELL > 9
      put random(9) into RANDY
      if STOPPER contains 0 then
         if fld ("f" & RANDY) is empty then
            set the textColor of fld ("f" & RANDY) to red
            put "O" into fld ("f" & RANDY)
            put 256 into STOPPER            
         end if
      end if
      add 1 to CELL
   end repeat
end mouseUp

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Python Projects

Post by richmond62 » Wed Nov 10, 2021 1:00 pm

We then have to CALL the MACHINE PLAY button every time
the end-user plays:

Code: Select all

on mouseUp
   if fld "f3" is empty then
         set the textColor of fld "f3" to black
         put "X" into me
      end if
      --
      send "mouseUp" to button "MACHINE PLAY"
      --
         send "mouseUp" to button "WIN"
      --
end mouseUp
-
SShot 2021-11-10 at 14.04.33.png
Attachments
Nowts & Crosses 6.livecode.zip
Here's the stack.
(30.34 KiB) Downloaded 133 times

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Python Projects

Post by richmond62 » Wed Nov 10, 2021 2:57 pm

I am not entirely sure why, having been using Python, the next section works with SCRATCH:
-
SShot 2021-11-10 at 15.53.59.png
-
The resources referred to see inaccessible:
Add the Rudolph sprite to the project (use the resources/Rudolph.png
file)
NOT, frankly, that that should stop us.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Python Projects

Post by richmond62 » Wed Nov 10, 2021 3:01 pm

SShot 2021-11-10 at 15.59.24.png
-
So, our first 'problem' is to import a daft reindeer image, a sky-type background, and script things so that
the reindeer image FOLLOWS the MOUSE.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Python Projects

Post by richmond62 » Wed Nov 10, 2021 3:31 pm

SShot 2021-11-10 at 16.25.30.jpg
-
At the moment the image of a deer contains the following script:

Code: Select all

on mouseDown
   grab me
end mouseDown
This does NOT really do what the document requires.
Attachments
Christmas.livecode.zip
Here's the stack.
(187.86 KiB) Downloaded 116 times

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Python Projects

Post by richmond62 » Wed Nov 10, 2021 8:14 pm

Theoretical, teacher-ish pause time.
And outside of our little corner of the world, most programmers are polyglots.
Indeed, but you have to start somewhere, and preferably somewhere that has a shallower learning curve
than that from SCRATCH to Python.

I learnt a dialect of BASIC in 1977 after having learnt FORTRAN IV, and can honestly state that I became far more
competent in BASIC than I ever was in FORTRAN exactly because it was developed with beginners in mind.

LiveCode's ancestry goes back to something that was conceived of as the non-programmer's programming thing,
and to a very large extent it has preserved that, while having a considerable amount of "other stuff" on top.

Interestingly enough,
the commonalities of C-style OOP languages
I have found that children (9-12) have no obvious problems moving from BBC BASIC to LiveCode or the other
way around, yet they are not that similar.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Python Projects

Post by FourthWorld » Wed Nov 10, 2021 11:21 pm

richmond62 wrote:
Wed Nov 10, 2021 8:14 pm
I have found that children (9-12) have no obvious problems moving from BBC BASIC to LiveCode or the other
way around, yet they are not that similar.
It would seem the more useful question for addressing LC as a bridge from Scratch to anything else is:
How did those kids become proficient in BBC BASIC?

A good second question might be:
How do kids already proficient in BASIC do when learning Python vs learning LC?

BASIC is kinda funky. Can't imagine either LC or Python not being a welcome change. :)
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Teaching with LiveCode”