So I've got 72 different sets for each combo of rock paper of scissors etc, and it's taking up like 2k bytes. I'm sure there is a way to make it better, but I can't figure it out. For example, one set is
If K=92
Then
If J=93
Output(1,1,"covers"
Kinda like that. K is player 1 getkey choice, J is player 2. 1 is rock, 2 is paper, 3 is scissors, etc. I would love it if someone could help me with this, especially if the solution involves lists, which I am only just now starting to understand. Thanks so much!
What I would do is to ise conditional statementw:
0->S
0->Q
0->R
0->P
While(not(K=45))
Repeat not(P=Q)
If K=92:1->P //the players selection is stored into P
If K=93:2->P
If K=94:3->P
If K=82:4->P
If K=83:5->P
If K=84:6->P
If K=72:7->P
If K=73:8->P
If K=74:9->P
End
P->Q
Repeat not(C=R)
RandInt(1,9)->C //Computers choice is randomly generated
End
C->R
If C<P and not((P-C)=8) // is C less than P AND is P not 9 and C not 1
Then
ClearHome
S+1->S
Output(1,1,"Covers
Output(2,1,"Score:
Output(2,7,S
For(Z,1,75)
End
Output(4,1,"Make your move
Else
ClearHome
Output(1,1,"Doesnt Cover
Output(2,1,"Game Over!
For(Z,1,75)
End
45->K
End
End
What that code does is it saves the users selection into P making sure the player did not pick the same number twice in a row. It then randomly generates the computers choice, making sure it is not the same twice in a row. It then checks to see if the players choice is greater than the computers, and also checks to see that the result is not 9-1. If the players number is greater, it adds one to the players score and displays it. Otherwise it ends the game.
To set up other combinations Change the If-Then-Else statement in the code abovd to:
If P=9 and C=1: Goto L //player looses
If P=9 and C=2: Goto W //player wins
//rest of the combinations here
Lbl L
ClearHome
Output(1,1,"Doesnt Cover
Output(2,1,"Game Over!
For(Z,1,75)
End
45->K
Lbl W
ClearHome
S+1->S
Output(1,1,"Covers
Output(2,1,"Score:
Output(2,7,S
For(Z,1,75)
End
Output(4,1,"Make your move
That would allow you to set which numbers beat which and lost to which. Goto L is what happens if its a loosing choice and ends the game. Goto W is if its a win, and should loop back to the beginning automatically
Any questions on my code, feel free to ask and I will try and explain it
Edit: Also, for those more advanced programmers who realize that the first code creates a memory leak by breaking the while loop from inside an If-Then-Else ; It doesnt really exist since that only happens when the program is exiting, in which case it will automatically clear the stack upon exiting. That means it really isnt a leak
Hey, I appreciate the help, but thats not really what I was going for. I already have it looking kinda like that, I wanted to know how to make it smaller. Your way may be a bit smaller than mine, but not by much. I think what you didn't realize (and I didn't communicate well evidently) is that not all combinations end with a "covers" there are like 36 or something different possible endings. I have a working program that is wonderful, I just need a way to condense it, since it is a bunch of the same stuff. Also, I don't really mind if they do the same choice as last time, and I have 2 player option (which isn't prevalent to my question but whatever). Thanks again for the help, but I'm not sure if you fully understood my predicament. (How did you do that pretty program paste? I'm kinda new to this site)
It also seems that your code will have to be re executed each time you want to play one round, I have mine set up so it loops. So there is a memory loop for me, if I did it your way. Also, saying not(k=45) is the same as saying k=\=45, right? Just checking, cuz I rarely use not(
While 1
Output(1,1,"Player 1:
Repeat K=/=0
Getkey-> K
End// player 1 choice
Output(1,1," Player 2:
Repeat J=/=0
Getkey->J
End// player 2 choice
Output(1,1," "
If K=J
Output(1,1," Tie
If K=92
Output(2,1,"Rck
If K=93
Output(2,1,"Ppr
If K=94
Output(2,1,"Scs
If K=82
Output(2,1,"Lzd
If K=83
Output(2,1,"Spk
If K=84
Output(2,1,"Spn
If K=72
Output(2,1,"Btn
If K=73
Output2,1,"Wzd
If K=74
Output(2,1,"Glk
If J=92
Output(2,14,"Rck
If J=93
Output(2,14,"Ppr
If J=94
Output(2,14,"Scs
If J=82
Output(2,14,"Lzd
If J=83
Output(2,14,"Spk
If J=84
Output(2,14,"Spn
If J=72
Output(2,14,"Btn
If J=73
Output2,14,"Wzd
If J=74
Output(2,14,"Glk
If K=92 //all possiblilities if player 1 goes rock
Then
If J=93
Output(1,1," COVERS ->
If J=94
Output(1,1,"<- CRUSHES
If J=82
Output(1,1,"<- SQUISHES
If J=83
Output(1,1," VAPORIZES ->
If J=84
Output(1,1,"<- KNOCKS OUT
If J=72
Output(1,1," EXPLODES ->
If J=73
Output(1,1,"<- INTERRUPTS
If J=74
Output(1,1," BREAKS ->
End
If K=94 // I accidently wrote stuff for scissors and I don't want to rewrite everything
Then
If J=92
Output(1,1," SMASHES ->
If J=93
Output(1,1,"<- CUTS
If J=82
Output(1,1,"<- DECAPITATES
If J=83
Output(1,1," SMASHES ->
If J=84
Output(1,1,"<- SNIPS
If J=72
Output(1,1," DISMANTLES ->
If J=73
Output(1,1,"<- STABS
If J=74
Output(1,1," DENTS ->
End
If K=93
Then
If J=92
Output(1,1,"<- COVERS
If J=94
Output(1,1," CUTS ->
If J=82
Output(1,1," EATS ->
If J=83
Output(1,1,"<- DISPROVES
If J=84
Output(1,1," RIPS ->
If J=72
Output(1,1,"<- DELAYS
If J=73
Output(1,1," BURNS ->
If J=74
Output(1,1,"<- JAMS
End
If K=82
Then
If J=92
Output(1,1," SQUISHES ->
If J=93
Output(1,1,"<- EATS
If J=94
Output(1,1," DECAPITATES ->
If J=83
Output(1,1,"<- POISONS
If J=84
Output(1,1," DEFEATS ->
If J=72
Output(1,1,"<- CONFUSES
If J=73
Output(1,1," TRANSFORMS ->
If J=74
Output(1,1,"<-TOO SMALL FOR
End // is there an easier way to type all this? I'm on my phone. GAH!
If K=83
Then
If J=92
Output(1,1,"<- VAPORIZES
If J=93
Output(1,1," DISPROVES ->
If J=94
Output(1,1,"<- SMASHES * only got to here not done yet
If J=82
Output(1,1," POISONS ->
If J=84
Output(1,1,"<- BEFUDDLES
If J=72
Output(1,1," HANGS ->
If J=73
Output(1,1,"<- ZAPS
If J=74
Output(1,1," SHOOTS ->
End
If K=84
Then
If J=92
Output(1,1," KNOCKS OUT ->
If J=93
Output(1,1,"<- RIPS
If J=94
Output(1,1," SNIPS ->
If J=82
Output(1,1,"<- DEFEATS
If J=83
Output(1,1," BEFUDDLES ->
If J=72
Output(1,1," SCARES ->
If J=73
Output(1,1,"<- ANNOYS
If J=74
Output(1,1,"<- DISARMS
End
If K=72
Then
If J=92
Output(1,1,"<- EXPLODES
If J=93
Output(1,1," DELAYS ->
If J=94
Output(1,1,"<- DISMANTLES
If J=82
Output(1,1," CONFUSES ->
If J=83
Output(1,1,"<- HANGS
If J=84
Output(1,1,"<- SCARES
If J=73
Output(1,1," STUNS ->
If J=74
Output(1,1," KILLS MOM ->
End
If K=73
Then
If J=92
Output(1,1," INTERRUPTS ->
If J=93
Output(1,1,"<- BURNS
If J=94
Output(1,1," STABS ->
If J=82
Output(1,1,"<- TRANSFORMS
If J=83
Output(1,1," ZAPS ->
If J=84
Output(1,1," ANNOYS ->
If J=72
Output(1,1,"<- STUNS
If J=74
Output(1,1,"<- MELTS
End
If K=74
Then
If J=92
Output(1,1,"<- BREAKS
If J=93
Output(1,1," JAMS ->
If J=94
Output(1,1,"<- DENTS
If J=82
Output(1,1," TO SMALL FOR->
If J=83
Output(1,1,"<- SHOOTS
If J=84
Output(1,1," DISARMS ->
If J=72
Output(1,1,"<- KILLS MOM
If J=73
Output(1,1," MELTS ->
End
Repeat I=22 or I=105 or I=24 or I=26
Getkey->I
End
If I=22
Returun
If I=24
X+1->X
If I=26
Y+1->Y
Output(8,2,X
Output(8,15,Y
End
So, pretty much everywhere there is a … I'm pretty sure there is a way to make it like 100x smaller, but I can't really figure out how. I also left out everything before the while, which is just defining variables and displaying what number corresponds to which choice. Also left out the part I added to make it computer v computer, human v computer, or human v human. If you want to see it, I will gladly type it out later.
The more of the sourcecode you provide, the easier it is for me to simplify it
But, What I am thinking is this:
If K=92 and sum(J={#,#,#}): Output(1,1," COVERS ->
Where you replace the #'s with the values of J which loose to K=92; I can easily do that quite quickly If I had the full code to work on.
Then you can do K=93, K=64 etc. But that still probably Isn't the most efficient way. But it might be better than what you have. By compressing each condition, we could also add the scores automatically, rather than have the user do that at the end.
Also, I would save all your text into strings. Based on your code above, I can reduce it by a few bytes by doing:
"Rck"->Str1
"Ppr->Str2
"Scs->Str3
"Lzd->Str4
"Spk->Str5
"Spm->Str6
"Btm->Str7
"Wiz->Str8
"Glk->Str9
"<- CRUSHES , COVERS ->"->Str0 //We will use sub( to split this string later
14->A //This just saves a few bytes Later
While (1) //While loop
Output(1,1,"Player 1:
Repeat sum(K={92,93,94,82,83,84,72,73,74,45}):Getkey-> K:End // player 1 choice
If K=45:Stop
Output(1,7,"Player 2:
Repeat sum(J={92,93,94,82,83,84,72,73,74,45}):Getkey->J:End // player 2 choice
If J=45:Stop
Output(1,1," "
If K=J:Output(1,1," Tie "
If K=92:Output(2,1,"Str1
If K=93:Output(2,1,"Str2
If K=94:Output(2,1,"Str3
If K=82:Output(2,1,"Str4
If K=83:Output(2,1,"Str5
If K=84:Output(2,1,"Str6
If K=72:Output(2,1,"Str7
If K=73:Output(2,1,"Str8
If K=74:Output(2,1,"Str9
If J=92:Output(2,A,"Str1
If J=93:Output(2,A,"Str2
If J=94:Output(2,A,"Str3
If J=82:Output(2,A,"Str4
If J=83:Output(2,A,"Str5
If J=84:Output(2,A,"Str6
If J=72:Output(2,A,"Str7
If J=73:Output(2,A,"Str8
If J=74:Output(2,A,"Str9
If K=92:Then //all possiblilities if player 1 goes rock
If J=92:
If J=93:Output(1,1,sub(Str0,18,16) //notice how sub( can display a specific part of a string.
If J=94:Output(1,1,sub(Str0,1,16)
If J= 72:
... //it runs through the rest of rck
End
If K=93 // all possibilities for paper
Then
...
End
... //rest of K possibilities
Repeat sum(I={45,105,24,26 //clear,enter,left,right:Getkey->I:End
If I=22 :Return // end program if done playing
If I=24:X+1->X // add a point to player 1
If I=26:Y+1->Y // add a point to player 2
Output(8,2,X
Output(8,15,Y // shows score
End
Delvar Str0 //Delete Str0 since it takes up a fair bit of space
Thanks for the string idea, that's a good help. Could you explain to me how the sum( works in this scenario? I read up on it and i dont see how it would work. And when I get a chance I will put in the rest of My code for ya, don't have enough time right now. Thanks a bunch!
So, when you take a list and check if a value is equal to it, it returns a list of true (1) or false (0). Consider the following code:
Repeat K
Getkey->K
End
K={45,105}
The returned value if Clear was pressed would be {1,0} if Enter was pressed it would be {0,1} if 2nd was pressed it would be {0,0}
The sum( command adds together all the values in a list. If K equals any of the values in our list, the sum will be 1; else it will be 0. In this case, we can use it to repeat a block of code until one of the numbers or clear is pressed. I will break down the code piece by piece:
Repeat //create a repeat loop
sum( //Take the sum of the list
K= //This is the conditional that will create the final list
{72,73,74,82,83,84,92,93,94,45} //This is the list of keycodes that we want to allow
//Code
End //End the repeat loop
So putting it all together looks like:
Repeat sum(K={72,73,74,82,83,84,92,93,94,45}
//Code
End
Conversely, you can test to make sure a key ISN'T pressed by adding not( and 0 to the list
Repeat not(sum(K={0,105,45 //This waits until any key is pressed EXCEPT for Enter or Clear
//code
End
Oh. That's helpful, thanks a bunch. I usually just say if K=\= 0, but this is a bit more specific. Thanks for the explanation, gonna finish up the source code now :)
EDIT: finished up the source code, hopefully that will help you out. Also realized that I could cut it in half by displaying the arrows based on who wins or loses, then just have the action displayed for both of them. For example,
If K=92 and sum(J={93,83,72,74} //did I use sum( properly?
Output(1,15,"->
If K=92 and sum(J={94,82,84,73}
Output1,1,"<-
If (K=92 and J=93) or (J=92 and K=93
Output(1,6,"COVERS
this would make it smaller,(no pesky spaces, 36 outputs rather than 72, plus I could make it auto add points to whoever wins…) but I'm thinking since it would have to check 36 things rather than 17, it'll take a lot longer too. Anyway around that?
There is a way to make the program about 900 bytes, and it only checks 9 cases.
"Rck"→Str1 //Set up the strings. Since we use them twice later, this saves space and time
"Ppr→Str2
"Scs→Str3
"Lzd→Str4
"Spk→Str5
"Spm→Str6
"Btm→Str7
"Wiz→Str8
"Glk→Str9
While (1) //initiate the main loop
Output(1,1,"Player 1:
Repeat sum(K={92,93,94,82,83,84,72,73,74,45}):Getkey→K:End //Get the player 1's choice
If K=45:Return
Output(1,7,"Player 2:
Repeat sum(J={92,93,94,82,83,84,72,73,74,45}):Getkey→J:End //Get the player 2's choice
If J=45:Return
ClrHome //Clear the screen
If K=92:Output(2,1,"Str1 //Display Player 1's choice
If K=93:Output(2,1,"Str2
If K=94:Output(2,1,"Str3
If K=82:Output(2,1,"Str4
If K=83:Output(2,1,"Str5
If K=84:Output(2,1,"Str6
If K=72:Output(2,1,"Str7
If K=73:Output(2,1,"Str8
If K=74:Output(2,1,"Str9
If J=92:Output(2,14,"Str1 //Display Player 2's Choice
If J=93:Output(2,14,"Str2
If J=94:Output(2,14,"Str3
If J=82:Output(2,14,"Str4
If J=83:Output(2,14,"Str5
If J=84:Output(2,14,"Str6
If J=72:Output(2,14,"Str7
If J=73:Output(2,14,"Str8
If J=74:Output(2,14,"Str9
If K=J:Goto C //Goto the "Continue" Label
If K=92 and sum(J={93,83,72,74 //Test this possible choice for P1. If P1 wins, Goto Label 1. Otherwise, continue
Goto 1
If K=94 and sum(J={92,83,72,74 //Test this possible choice for P1. If P1 wins, Goto Label 1. Otherwise, continue
Goto 1
If K=93 and sum(J={94,82,84,73 //Test this possible choice for P1. If P1 wins, Goto Label 1. Otherwise, continue
Goto 1
If K=82 and sum(J={92,94,84,73 //Test this possible choice for P1. If P1 wins, Goto Label 1. Otherwise, continue
Goto 1
If K=83 and sum(J={93,82,72,74 //Test this possible choice for P1. If P1 wins, Goto Label 1. Otherwise, continue
Goto 1
If K=84 and sum(J={92,94,83,72 //Test this possible choice for P1. If P1 wins, Goto Label 1. Otherwise, continue
Goto 1
If K=72 and sum(J={93,82,73,74 //Test this possible choice for P1. If P1 wins, Goto Label 1. Otherwise, continue
Goto 1
If K=73 and sum(J={92,94,83,84 //Test this possible choice for P1. If P1 wins, Goto Label 1. Otherwise, continue
Goto 1
If K=74 and sum(J={93,82,84,73 //Test this possible choice for P1. If P1 wins, Goto Label 1. Otherwise, continue
Goto 1
Lbl 2 //If and only if P1 doesn't win will this code be executed.
Y+1→Y //add one to P2 score
Output(2,8,"Win -> //Show P2 win
Goto C //Skip the next label
Lbl 1 //This code is executed If and only if P1 wins
X+1→X //Add one to P1 Score
Output(2,4,"<- Win //Show P1 Win
//Notice there is no "Goto" here. The code will automatically continue on the next line, which is Lbl C
Lbl C //This is the continue label.
Output(8,2,X //Show the scores
Output(8,15,Y
Repeat sum(I={45,105 //Wait for Clear or Enter
Getkey→I
If I=45:Return //If Clear, End the program
End //Close our main loop
What you need to know about this:
1) As soon as player 1 wins, it skips checking the rest of the possibilities. This saves time
2) Lbl 1 and Lbl 2 and Lbl C are in the order they are in for a reason. Don't change it
3) The goto at the end of Lbl 2 is what makes it skip Lbl 1. We don't need it on Lbl 1 since Lbl C will execute automatically
4) This doesn't allow for custom phrases per win condition
5) What if the players get to scores that don't fit on screen?
6) Its only about 900 Bytes compared to your original 3239
7) Automatic scoring!
8) Using Lbl and Goto makes it easy to have multiple scenarios point to the same code
Also, in your post you said you usually use:
If K=/=0
That can be simpified if you are going to use it in other programs:
If K
Oh fancy. Didn't realize that if K means if K=\=0. Makes sense now that I think about it though.but still no way to make it quite a bit smaller and keep the action that occurs. Darn. Because that's a really important part to me. Like glock killing batman's mom really makes the game more enjoyable rather than Glock beats batman. Anyways, thanks a bunch for the tips and tricks, and if you are up for it, my pong program needs some optimizing too.