Page 3 of 4
Re: Python Projects
Posted: Wed Nov 10, 2021 8:33 am
by richmond62
-
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.
Re: Python Projects
Posted: Wed Nov 10, 2021 8:51 am
by richmond62
-
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
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.
Re: Python Projects
Posted: Wed Nov 10, 2021 9:03 am
by richmond62
-
There are 8 ways either side can win the game.
-
-
4 of which can be determined by initially finding out
if the central cell (field "f5") is occupied.
Re: Python Projects
Posted: Wed Nov 10, 2021 10:03 am
by richmond62
-
-
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.]
Re: Python Projects
Posted: Wed Nov 10, 2021 10:17 am
by richmond62
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.
Re: Python Projects
Posted: Wed Nov 10, 2021 12:25 pm
by richmond62
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:
-
-
You will see that the condition about '9' is now redundant.
-
-
Re: Python Projects
Posted: Wed Nov 10, 2021 12:32 pm
by richmond62
You will also realise that, now, the field "fFILLED" is also unnecessary
and can be deleted, as well as any code refering to it:
-
-
Re: Python Projects
Posted: Wed Nov 10, 2021 12:34 pm
by richmond62
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.
Re: Python Projects
Posted: Wed Nov 10, 2021 12:46 pm
by richmond62
The
FIRST thing we have to do is
REMOVE the chance of the end-user
inserting an 'O' into a cell:
-
-
Then we'll have a
BUTTON to allow the computer to play against the end-user:
-
-
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
Re: Python Projects
Posted: Wed Nov 10, 2021 1:00 pm
by richmond62
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
-
Re: Python Projects
Posted: Wed Nov 10, 2021 2:57 pm
by richmond62
I am not entirely sure why, having been using Python, the next section works with SCRATCH:
-
-
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.
Re: Python Projects
Posted: Wed Nov 10, 2021 3:01 pm
by richmond62
-
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.
Re: Python Projects
Posted: Wed Nov 10, 2021 3:31 pm
by richmond62
-
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.
Re: Python Projects
Posted: Wed Nov 10, 2021 8:14 pm
by richmond62
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.
Re: Python Projects
Posted: Wed Nov 10, 2021 11:21 pm
by FourthWorld
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.
