[SAMPLE CODE] TicTacToe

351 views
Skip to first unread message

Alex F

unread,
Jun 9, 2016, 1:00:10 AM6/9/16
to DroidScript
Here's a simple TicTacToe sample code:

/* TIC TAC TOE
by AlexF
*/

var btns = [" "," "," "," "," "," "," "," "," "];
var Player=1;
var Round=0;

function OnStart() {
lay=app.CreateLayout("Linear","FillXY");

for (var i=1; i<4; i++) {
layN=app.CreateLayout("Linear", "Horizontal");
for (var j=1; j<4; j++) AddButton(layN,i,j); lay.AddChild( layN );
}
app.AddLayout(lay);
app.ShowPopup( "It's Player 1s turn!","bottom" );
}

function AddButton( layN,i,j) {
btn = app.CreateButton( btns[3*(i-1)+j-1],0.3,0.2,"Alum" );
btn.SetTextSize(60);
btn.SetOnTouch( tap );
layN.AddChild( btn );
}

function tap() {
Btn=app.GetLastButton();
btntxt=Btn.GetText();

if ((btntxt != "O") && (btntxt != "X")) {
btns[btntxt.length -1] = Player
Btn.SetText(("XO")[Player-1]);
proof();
Player=3-Player;
app.ShowPopup( "It's Player " +Player+ "s turn!" ,"bottom");
}
}

function proof()
{
Round++
for (var i=0; i<8; i++) {
var row = "";
for (var j=0; j<3; j++) row += btns[["123","456","789","147","258","369","159","753"][i][j]-1];
if (row == "111" || row =="222") {
alert("Player "+ Player + " won!")
app.Exit();
}
}
if(Round==9) {alert("Draw!"); app.Exit()}
}

Alex F

unread,
Jun 9, 2016, 1:04:45 AM6/9/16
to DroidScript
I haven' given comments, but if you want to know what something do - ask me! ☺
Message has been deleted

Manuel Lopes

unread,
Jun 9, 2016, 7:30:21 AM6/9/16
to DroidScript
this not win when have 3 in a row

Alex F

unread,
Jun 9, 2016, 8:48:23 AM6/9/16
to DroidScript
First version works - my mistake sry

Manuel Lopes

unread,
Jun 9, 2016, 4:10:31 PM6/9/16
to DroidScript
it is ok bro ;what first;i only see one here
Message has been deleted

Alex F

unread,
Jun 9, 2016, 4:19:04 PM6/9/16
to DroidScript
Heres the working version again ;)

var felder = [""," "," "," "," "," "," "," "," "];
var Spieler=1;
var Zug=0;

function OnStart() {
lay=app.CreateLayout("Linear","FillXY");

for (var i=1; i<4; i++) {
layN=app.CreateLayout("Linear", "Horizontal");
for (var j=1; j<4; j++) AddButton(layN,i,j); lay.AddChild( layN );
}

app.AddLayout(lay);
app.ShowPopup( "Spieler 1 ist am Zug!","bottom" );
}

function AddButton( layN,i,j) {
btn = app.CreateButton( felder[3*(i-1)+j-1],0.3,0.2,"Alum" );
btn.SetTextSize(60);
btn.SetOnTouch( tap );
layN.AddChild( btn );
}

function tap()
{
Btn=app.GetLastButton();
btntxt=Btn.GetText();

if ((btntxt != "O") && (btntxt != "X"))

{
felder[this.GetText().length]= Spieler;
Btn.SetText(("XO")[Spieler-1]);
prüfe();
Spieler=3-Spieler;
app.ShowPopup( "Spieler " +Spieler +" ist am Zug!" ,"bottom");
}
}

function prüfe()
{
Zug++

for (var i=0; i<8; i++) {
var reihe = "";
for (var j=0; j<3; j++) reihe+=felder[["123","456","789","147","258","369","159","753"][i][j]-1];
if (reihe == "111" || reihe =="222") {
alert("Spieler "+ Spieler + " hat gewonnen!")
app.Exit();
}
}
if(Zug==9) {alert("Unentschieden!"); app.Exit()}
}

Alex F

unread,
Jun 9, 2016, 4:26:39 PM6/9/16
to DroidScript
Is it possible to reset the names of all buttons to their start value? I think it's impossible cauz u cn't call the buttons. Otherwise I cn also add a function allows play vs computer

Manuel Lopes

unread,
Jun 9, 2016, 5:09:40 PM6/9/16
to DroidScript
sorry same problem ;example put a row of 3 X and not win or 3 O why?i think is need anything in the code to compare the 3 X or the 3 O;thanks

Alex F

unread,
Jun 9, 2016, 5:14:28 PM6/9/16
to DroidScript
Can you tell me step by step which buttons u press to get this result?

Alex F

unread,
Jun 9, 2016, 5:19:13 PM6/9/16
to DroidScript
This check if theres a row 'OOO' or 'XXX' :

Manuel Lopes

unread,
Jun 9, 2016, 5:57:03 PM6/9/16
to DroidScript
only show the draw not win or loose

BareK

unread,
Jun 9, 2016, 6:04:59 PM6/9/16
to DroidScript
I tested it and it works correctly on my device :)
I tried lines and diagonals, both players.

Alex F

unread,
Jun 9, 2016, 6:07:49 PM6/9/16
to DroidScript
Manuel sry I cn't help u. On my devicebit is working well. Im sorry 😢

Manuel Lopes

unread,
Jun 9, 2016, 6:08:27 PM6/9/16
to DroidScript
please post the spk(share by email option and copy
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Alex F

unread,
Jun 9, 2016, 7:11:44 PM6/9/16
to DroidScript

Manuel Lopes

unread,
Jun 10, 2016, 8:05:31 AM6/10/16
to DroidScript
very thanks now works

Alex F

unread,
Aug 17, 2016, 10:56:27 AM8/17/16
to DroidScript
Updated Version!
After finished a game a new game was shown
show player points
pleasehelp me with correct text position in landscape mode ;)
Thanks
TicTacTo.spk

Manuel Lopes

unread,
Aug 17, 2016, 6:07:43 PM8/17/16
to DroidScript
cool great game;if possible add an ai to the game thanks

Alex F

unread,
Aug 18, 2016, 12:41:32 AM8/18/16
to DroidScript
What is ai?

Alex F

unread,
Aug 18, 2016, 1:13:00 AM8/18/16
to DroidScript
Fixed bug ckecks owner status
btn.Owner can be null, 0 or 1 - but 0 returns false- my mistake sry ^^
and the player2 text is on top and rotated with scale(-1) now
https://www.dropbox.com/sh/p4ub4srhl3es47a/AAC4CUtw1j3z_rhZjTB_8B-ua?

Steve Garman

unread,
Aug 18, 2016, 4:01:31 AM8/18/16
to DroidScript
Nice game Alex.

I assume when Manuel suggested AI, he wants the app to be able to play against the user.

I like the rotated text for playing across a table but it would be nice to have an option to turn it off, when players are not situated like that.

Can I also suggest for the old and forgetful like me that there be some indication on the screen of whether the current user is playing "O" or "X"

Perhaps something as simple as

txt1= PlayerText( "Player 1 (X)\nPoints : 0" );

And finally, can you please explain why you are using app.Execute()

Alex F

unread,
Aug 18, 2016, 4:30:57 AM8/18/16
to DroidScript
I will add your requests. I also think about the user has to touch his text befire he can place his symbol. And a line marks the win row.
I use app.execute to update only the changed points in the text - i will remove it brcause it isn't worth that ^^

Alex F

unread,
Aug 18, 2016, 6:32:10 AM8/18/16
to DroidScript
Ok i released the text changes now. I dont know how the screen should looks without on opposite. If I sit next to my partner we play with horizontal device.
The api needs a bit longer to add. I will add a function GetOptionalBtn() returns the best button you could press. The app can call it and i think i could add a button shows the player the op btn
Regards ^^

Manuel Lopes

unread,
Aug 18, 2016, 8:42:30 AM8/18/16
to DroidScript
yes the steve is right i mean inteligence artificial sorry my bad english

Alex F

unread,
Aug 18, 2016, 12:40:03 PM8/18/16
to DroidScript
IT'S DONE!!!
I added a settings menu where you can set who's computer or user.
For now the computer can only place random symbols but i will add intelligence soon - I'm hungry now ^^

Can you please tell me how to pause the app with app.Alert() like alert()? Because I can't set a headline with alert()
I need it because if both players are computers multiple dialogs will open - so I changed it to alert() for now.

Thanks and best regards
Alex :)

Alex F

unread,
Aug 18, 2016, 1:25:32 PM8/18/16
to DroidScript
Is there a method of strings returns how often a given string was in the string?
Like that:
str= "Hello"
alert( str.count("l") ); // -> 2

Steve Garman

unread,
Aug 18, 2016, 3:13:06 PM8/18/16
to DroidScript

function OnStart()
{
var str= "Hello";
alert(strCount(str,"l"));
}

function strCount(str, sought)
{
var arr = str.split(sought);
return arr.length -1;
}

Alex F

unread,
Aug 18, 2016, 3:16:56 PM8/18/16
to DroidScript
Haha didnt thaught about this ^^
Thanks

Alex F

unread,
Aug 18, 2016, 7:15:50 PM8/18/16
to DroidScript
I made it very difficult - I changed a lot (it dont looks so now - but it's true :( ) but it looks good now. Finally I only could add the easy mode for the computer, I will finish it soon. Enjoy
https://www.dropbox.com/sh/p4ub4srhl3es47a/AAC4CUtw1j3z_rhZjTB_8B-ua?
regards,
Alex

Alex F

unread,
Aug 18, 2016, 7:25:25 PM8/18/16
to DroidScript
A question - there must be somethink wrong with the apk file - I tried to install it on my device. Tge first thing is that it needs wifi and multiple broadcast?! And if i try to run it theres an error 'unexpected token ,'

Alex F

unread,
Aug 18, 2016, 7:51:05 PM8/18/16
to DroidScript
I tried to reinstall DroidScript (GooglePlay version) and now the error was shown in the first line of the spk/js (also if theres nothing). Please help me :(

Manuel Lopes

unread,
Aug 18, 2016, 8:43:14 PM8/18/16
to DroidScript
very cool alex you are great;a.i. good i have draw by my self :)

Alex F

unread,
Aug 19, 2016, 1:07:08 AM8/19/16
to DroidScript
I tried out different versions. My result: It works only on the latest DS version 136a4 here:
http://androidscript.org/alpha/
If I try to create, install abd run the apk it won't work like in the other DS versions (included PlayStore version :()
And the apk needs extra access the app didn't use - attached screenshot
So please help me
My previous versions of TTT never had problems like this :(
ExtraApkNeeds.png
BeforePressSmth.png
AfterPressButton.png

Steve Garman

unread,
Aug 19, 2016, 1:43:01 AM8/19/16
to DroidScript
The apk-builder does not work with features that are new in an alpha version.

They need to be at least in beta before it knows about them.

It looks like you must have used a new alpha feature.

When the spk does not work in previous versions, what line does it fail on?

If that does not help, is there anything in the release notes you recognise https://groups.google.com/d/msg/droidscriptbeta/j_NKgLkUvDU/1Vmp1VGlAAAJ ?

As far as the permissions are concerned, they also usually only change if you use an extra feature, though they can be fooled by comments.

If your apk is asking for permissions you don't need, we can fix that with a build.json file.

Alex F

unread,
Aug 19, 2016, 4:17:38 AM8/19/16
to DroidScript
I never used special built ins in this app. I dont know why but if the 'settings dialog wasn't shown when start it works. But if i open it and try to close it the error happens again. I uploaded yhis apk version named TicTacTo0 if you want to try it out. I just removed Settings.Show() at the end of OnStart()

In previous versions the error was shown every time on the first line of code

I dont know how to use build.json

Thanks for your help :)

Steve Garman

unread,
Aug 19, 2016, 4:36:52 AM8/19/16
to DroidScript
layout.Show() and layout.Hide() are pretty new.

I suggest trying

settings.SetVisibilty("Show");
and
settings.SetVisibilty("Hide");

Alex F

unread,
Aug 19, 2016, 5:06:33 AM8/19/16
to DroidScript
But 'Settings' is a Dialog!

Alex F

unread,
Aug 19, 2016, 5:24:02 AM8/19/16
to DroidScript
I uploaded now spk and apk of ttt0
I replaced settings.Show function with layset.animate and removed the dialog. It doesnt work anyways in the apk file :(

Alex F

unread,
Aug 19, 2016, 11:50:32 AM8/19/16
to DroidScript
I'll resume make the computer intelligent. But it would be nice if someone else finds the mistake causes the special wifi-needs and the error when change smth on the settings layout/dialog

Alex F

unread,
Aug 20, 2016, 7:23:20 AM8/20/16
to DroidScript
I just finished intelligence to the computer. You can set different difficulties - try to win against the computer in impossible mode and if you won, tell me how
For now if computer plays against computer there's a bug sometimes- I'll working on it.
enjoy :D
TicTacTo0.spk

Alex F

unread,
Aug 20, 2016, 7:35:37 AM8/20/16
to DroidScript
I forgot a line in GetOptionalBtn:
Add the line else pos=[1,3,5,7] so it looks like this:

if( round==3 )
if( order[2]%2 )
if( order[0]%2 ) pos=[1,3,5,7]
else pos=[0,2,6,8]
else pos=[1,3,5,7]

Manuel Lopes

unread,
Aug 20, 2016, 8:18:20 AM8/20/16
to DroidScript
what the use of this line? if( order[2]%2 )

%2?

Alex F

unread,
Aug 20, 2016, 8:28:03 AM8/20/16
to DroidScript
I solved the errors!!!
Steve was right I can't use for example btn.SetonTouch(function(){})
I replaced all I could find. Now it works as apk also.
The wifi needs are caused by app.openurl (link to my DropBox :P)

order list the buttons in the order they're pressed.
%2 =0 -> corner/middle
=1 -> edge

TicTacTo.spk

Manuel Lopes

unread,
Aug 20, 2016, 8:42:07 AM8/20/16
to DroidScript
thanks alex;you mean if i have example a array with numbers example [6,2,5] the % order to 2,5,6?

Manuel Lopes

unread,
Aug 20, 2016, 8:45:29 AM8/20/16
to DroidScript
i put cpu vs cpu and is very fast show i think need a wait more time to slow the show

Alex F

unread,
Aug 20, 2016, 9:09:56 AM8/20/16
to DroidScript
6%2 =0 - 6 mod 2
2%2 =0 - 2 mod 2
5%2 =1 - 5 mod 2
% is like mod

I found some methods to win in impossible mode. I'll fix them.

Hm I already made it slow, but I can make it slower

Reply all
Reply to author
Forward
0 new messages