Hi,
I'm trying to create a morpion game (french name :$).
So I get the key pressed with getkey and test the value return.
And I have always the same pattern of conditional test (9 folds):
Code: getkey->k
if k = number // 71,72,73,81,82,83,91,92 or 93
then
if B // I use B as a boolean
then
cercle(X,Y,Z // differents coordonate according to the value of k
(B=0)->B // change the bool the alternate the drawing of a circle or a cross
else
ligne(X,X,X,X
ligne(Y,Y,Y,Y
(B=0)->B
end
end
And it restart 9 folds
So how can I shorten it ?
Thanks.
PS : Scuse my English :$
Code: :If max{K=(72,73,74,82,83,84,92,93,94
:Then
:If B
:Then
:Circle(X,Y,Z,{i
:Else
:<draw X here>
:End
:not(B->B
Tahnks.
but :$ I don't understand,
It's not the same coordonate for the circle and the cross for k= 72 and for k=73 and so on.
Well, for starters, you should use 'and':
Code: :If K=72 and B:Then
:Circle(...blah...
:Else
:Line(...blah...
:Line(...blah...
:End
:not(B->B
However, perhaps we can find a pattern between the key codes and the coordinates that you use? If so, we can write an equation for the Circle and Line commands and just have a single conditional. Note that not(B->B is the same as (B=0)->B.
Code: :{72,73,74,82,83,84,92,93,94->L1
:If max{K=L1
:Then
:sum((K=L1){1,2,3,4,5,6,7,8,9->D
:<list of X-coordinates>(D->E
:<list of Y-coordinates>(D->F
:If B
:Then
:Circle(X,Y,Z,{i
:Else
:<draw X here>
:End
:not(B->B
That will work, but I think we might be able to make it even better, because in Tic-Tac-Toe the Xs and Os are placed on a grid. I'm positive there's a good equation to describe that; I just want to see what coordinates that Wirekey is using for 1, 2, 3...9.
So For the coordonate :
PS : circle size : 8;
From top to bottom, from left to right, center of circle first, cross seconde,
C: 16, 53
L1:6,57,27,47
L2:6,47,27,57// There is just a permutation to make a cross
C:48,53
L1:37,57,58,47
L2:37,47,58,57
C:80,53
L1:69,57,90,47
L2:69,47,90,57
C:16,32
L1: 6,37,27,27 // just have to permute to have L2
C:48,32
L:37,37,58,27
C:80,32
L:69,37,90,27
C:16,11,8
L:6,15,27,5
C:48,11
L:37,15,58,5
C:80,11
L:69,15,90,5
thanks
Here's just the circle to start:
Circle X: 32fPart(.1K)-16
Circle Y: 200-21iPart(.1K)
Code: :If B and max{K=(72,73,74,82,83,84,92,93,94
:Then
:Circle(320fPart(.1K)-48,200-21iPart(.1K),8,{i
:Else
:...Draw Lines...
:End
:not(B->B
It works perfectly this time thank you so much
Ok I've also found for the line I post It tomorrow it's just 3 o'clock AM in france
Oh, you mean you did the same technique for the lines? If so, awesome! Congrats.
Yes I'll post the program tomorow.
:$ I've just transpose your technique for the lines :$ ^^
good night
Ok So sorry for the DP :$
The code for the the display:
Code:
:While 1
:getKey→K
:If B and max(K={72,73,74,82,83,84,92,93,94
:Then
:10*fPart(.1K)→D
:int(.1K→E
:Circle(32D-48,200-21E,8,{i
:(B=0)→B
:Else
:If K
:Then
:fPart(.1K)*10→D
:int(.1K→E
:Line(31D-56,‾20E+197,31D-35,‾20E+187
:Line(31D-35,‾20E+197,31D-56,‾20E+187
:(B=0)→B
:End
:End
It's interesting to notice that "partEnt" in French (so iPart in English) is replace by Int(...
Int() and iPart() do exactly the same thing, as far as I can tell. Very nice job on the code, just a few notes:
(1) Since you only use "D" and "E" once, for the Circle, this will slow your code down:
Code: :10*fPart(.1K)→D
:int(.1K→E
:Circle(32D-48,200-21E,8,{i
Also, never explicitly multiply (10*X) when you can implicitly multiply (10X). Instead do:Code: :Circle(320fPart(.1K)-48,200-21int(.1K),8,{i
(2) Save a few bytes: "(B=0)→B " to "not(B→B)"
(3) Save a byte by flipping adding a negative: "‾20E+197" to "197-20E"
(4) Use Ans for speed. Final code:
Code: :While 1
:getKey→K
:If B and max(K={72,73,74,82,83,84,92,93,94
:Then
:Circle(320fPart(.1K)-48,200-21int(.1K),8,{i
:not(B→B
:Else
:If K
:Then
:10fPart(.1K→D
:20int(.1K
:Line(31D-56,197-Ans,31D-35,187-Ans
:Line(31D-35,197-Ans,31D-56,187-Ans
:not(B→B
:End
:End
KermMartian wrote:
Int() and iPart() do exactly the same thing, as far as I can tell.
The former rounds down; the other truncates, which behaves differently along the negative axes for fractional values.
Weregoose, ahhh, an excellent point regarding negatives; I did not think of that at all. And souvik, thanks for the thoughts about the speed.
Code: :If B and max(K={72,73,74,82,83,84,92,93,94
=
Code: :If B and 2>abs(5-abs(5-abs(Ans-83
(Credit to DarkerLine.)
Weregoose (and DarkerLine) are my heroes. Have you checked the speed of that latter against the former, by any chance?
How large are most TIC TAC TOE programs. How small should it be. I saw a post two days ago saying that there first game was 8-12k. I started to make one yesterday, and I'm at 2500. It is done except I need to add some lines to make it smarter. You can choose what you want your x to look like. It can be a few characters and pictures.