Raised This Month: $51 Target: $400
 12% 

Rock Paper Scissors Game


Post New Thread Reply   
 
Thread Tools Display Modes
Plugin Info:     Modification:   Counter-Strike        Category:   Fun Stuff       
Smilex_Gamer
Senior Member
Join Date: Apr 2017
Location: Portugal
Old 12-08-2018 , 18:55   Rock Paper Scissors Game
Reply With Quote #1

Hi all! For some of you that access AlliedModders "en espaņol" might notice that I've already posted this plugin there, but now I'm gonna post it here

Features:
  • You can play against the PC or a Player
  • You can block game challenges in the main menu
  • /rps - opens plugin's main menu
  • If your opponent disconnects, the game is canceled

Updates:
  • 1.0: First Version
  • 1.1: Deleted all variables and created a global variable to optimize the code. Thanks Exertency
  • 1.2: Optimized code. Thanks fysiks
Attached Files
File Type: sma Get Plugin or Get Source (rps1.1.sma - 366 views - 17.1 KB)
File Type: sma Get Plugin or Get Source (rps1.2.sma - 376 views - 14.1 KB)

Last edited by Smilex_Gamer; 12-08-2018 at 21:13.
Smilex_Gamer is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-08-2018 , 19:49   Re: Rock Paper Scissors Game
Reply With Quote #2

It looks like several of your variables are mutually exclusive and therefore create unnecessary code to have them in separate variables. For example, "rock", "paper", and "scissors" are mutually exclusive and would reduce the code required to handle them if they were in a single variable.

Examples:

Define the following:
PHP Code:
enum Options {
    
NONE,
    
ROCK,
    
PAPER,
    
SCISSORS
}

new 
OptionStrings[Options][] = {
    
"None",
    
"Rock",
    
"Paper",
    
"Scissors"

Then replace this:
PHP Code:
    switch(item)
    {
        case 
0:
        {
            
g_vars[id][rock] = 1
        
}
        case 
1:
        {
            
g_vars[id][paper] = 1
        
}
        case 
2:
        {
            
g_vars[id][scissors] = 1
        
}
    } 
with this:
PHP Code:
g_vars[id][option] = Options:(item+1
Replace this:
PHP Code:
    new played1[32]
    if(
g_vars[id][rock])
        
formatex(played1charsmax(played1), "Rock")
    else if(
g_vars[id][paper])
        
formatex(played1charsmax(played1), "Paper")
    else if(
g_vars[id][scissors])
        
formatex(played1charsmax(played1), "Scissors"
with this (no new variable needed):
PHP Code:
OptionStrings[g_vars[id][option]] 
You an also calculate a winner with a simple, reusable function instead of the 40+ lines you do currently in two places:
PHP Code:
stock check_winner(player1_optionplayer2_option)
{
    if( 
player1_option == player2_option )
    {
        return 
// no winner akak 'draw'
    
}
    else if( 
player1_option player2_option || (player1_option == ROCK && player2_option == SCISSORS))
    {
        return 
// player1 is winner
    
}
    else
    {
        return 
// player2 is winner
    
}

where you would do something like this to evaluate a round:
PHP Code:
switch( check_winner(g_vars[id][option], g_vars[g_vars[id][player]][option]) )
{
    case 
0:
    {
        
// Draw
    
}
    case 
1:
    {
        
// id won
    
}
    case 
2:
    {
        
// g_vars[id][player] won
    
}

It looks like you can do something similar with the win, loss, draw variables.
__________________

Last edited by fysiks; 12-08-2018 at 19:55.
fysiks is offline
Smilex_Gamer
Senior Member
Join Date: Apr 2017
Location: Portugal
Old 12-08-2018 , 21:15   Re: Rock Paper Scissors Game
Reply With Quote #3

Quote:
Originally Posted by fysiks View Post
It looks like several of your variables are mutually exclusive and therefore create unnecessary code to have them in separate variables. For example, "rock", "paper", and "scissors" are mutually exclusive and would reduce the code required to handle them if they were in a single variable.

Examples:

Define the following:
PHP Code:
enum Options {
    
NONE,
    
ROCK,
    
PAPER,
    
SCISSORS
}

new 
OptionStrings[Options][] = {
    
"None",
    
"Rock",
    
"Paper",
    
"Scissors"

Then replace this:
PHP Code:
    switch(item)
    {
        case 
0:
        {
            
g_vars[id][rock] = 1
        
}
        case 
1:
        {
            
g_vars[id][paper] = 1
        
}
        case 
2:
        {
            
g_vars[id][scissors] = 1
        
}
    } 
with this:
PHP Code:
g_vars[id][option] = Options:(item+1
Replace this:
PHP Code:
    new played1[32]
    if(
g_vars[id][rock])
        
formatex(played1charsmax(played1), "Rock")
    else if(
g_vars[id][paper])
        
formatex(played1charsmax(played1), "Paper")
    else if(
g_vars[id][scissors])
        
formatex(played1charsmax(played1), "Scissors"
with this (no new variable needed):
PHP Code:
OptionStrings[g_vars[id][option]] 
You an also calculate a winner with a simple, reusable function instead of the 40+ lines you do currently in two places:
PHP Code:
stock check_winner(player1_optionplayer2_option)
{
    if( 
player1_option == player2_option )
    {
        return 
// no winner akak 'draw'
    
}
    else if( 
player1_option player2_option || (player1_option == ROCK && player2_option == SCISSORS))
    {
        return 
// player1 is winner
    
}
    else
    {
        return 
// player2 is winner
    
}

where you would do something like this to evaluate a round:
PHP Code:
switch( check_winner(g_vars[id][option], g_vars[g_vars[id][player]][option]) )
{
    case 
0:
    {
        
// Draw
    
}
    case 
1:
    {
        
// id won
    
}
    case 
2:
    {
        
// g_vars[id][player] won
    
}

It looks like you can do something similar with the win, loss, draw variables.
You're right, thanks so much
Updated code.
Smilex_Gamer is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 12-09-2018 , 08:41   Re: Rock Paper Scissors Game
Reply With Quote #4

Fysiks g_var should be only 1 dimension ---> g_var[33]:
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
Smilex_Gamer
Senior Member
Join Date: Apr 2017
Location: Portugal
Old 12-09-2018 , 09:05   Re: Rock Paper Scissors Game
Reply With Quote #5

Quote:
Originally Posted by Natsheh View Post
Fysiks g_var should be only 1 dimension ---> g_var[33]:
Why? I use it to store multiple variables.
Smilex_Gamer is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 12-09-2018 , 13:40   Re: Rock Paper Scissors Game
Reply With Quote #6

What multiple variables?
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-09-2018 , 14:02   Re: Rock Paper Scissors Game
Reply With Quote #7

Quote:
Originally Posted by Natsheh View Post
Fysiks g_var should be only 1 dimension ---> g_var[33]:
Why are you telling me? I didn't author this plugin. Also, it technically is only 1 dimension since the second is just an enum.
__________________

Last edited by fysiks; 12-09-2018 at 14:04.
fysiks is offline
Smilex_Gamer
Senior Member
Join Date: Apr 2017
Location: Portugal
Old 12-09-2018 , 14:06   Re: Rock Paper Scissors Game
Reply With Quote #8

Quote:
Originally Posted by Natsheh View Post
What multiple variables?
In my first version (1.0), all variables were separated like, the variable that tells if you're playing or not, or the variable of block challenges, all them were separated.
And that was unecessary, so by Exertency's opinion, I stored all variables in one global variable to optimize the code.

Last edited by Smilex_Gamer; 12-09-2018 at 14:08.
Smilex_Gamer is offline
JocAnis
Veteran Member
Join Date: Jun 2010
Old 12-09-2018 , 16:25   Re: Rock Paper Scissors Game
Reply With Quote #9

cna you give more informations..like does it works only with say command -> menu (choose 1 of 3 options) -> result...or it has some animations, sprites ect?
cuz i think its the same as: https://forums.alliedmods.net/showthread.php?t=220300
__________________
KZ Public Autocup - PrimeKZ

My blog: http://primekz.xyz (in progress...) - not active (dec 2022)
JocAnis is offline
Smilex_Gamer
Senior Member
Join Date: Apr 2017
Location: Portugal
Old 12-09-2018 , 16:36   Re: Rock Paper Scissors Game
Reply With Quote #10

Quote:
Originally Posted by JocAnis View Post
cna you give more informations..like does it works only with say command -> menu (choose 1 of 3 options) -> result...or it has some animations, sprites ect?
cuz i think its the same as: https://forums.alliedmods.net/showthread.php?t=220300
I was thinking of adding some animations of rock paper scissors game, but I don't have any modeler
Btw that plugin is kinda old and this one that I created has multiplayer challenges, so... yep is different
Smilex_Gamer is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:11.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode