AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   New Plugin Submissions (https://forums.alliedmods.net/forumdisplay.php?f=26)
-   -   Rock Paper Scissors Game (https://forums.alliedmods.net/showthread.php?t=312582)

Smilex_Gamer 12-08-2018 18:55

Rock Paper Scissors Game
 
2 Attachment(s)
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

fysiks 12-08-2018 19:49

Re: Rock Paper Scissors Game
 
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.

Smilex_Gamer 12-08-2018 21:15

Re: Rock Paper Scissors Game
 
Quote:

Originally Posted by fysiks (Post 2627758)
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 :D
Updated code.

Natsheh 12-09-2018 08:41

Re: Rock Paper Scissors Game
 
Fysiks g_var should be only 1 dimension ---> g_var[33]:

Smilex_Gamer 12-09-2018 09:05

Re: Rock Paper Scissors Game
 
Quote:

Originally Posted by Natsheh (Post 2627852)
Fysiks g_var should be only 1 dimension ---> g_var[33]:

Why? I use it to store multiple variables.

Natsheh 12-09-2018 13:40

Re: Rock Paper Scissors Game
 
What multiple variables?

fysiks 12-09-2018 14:02

Re: Rock Paper Scissors Game
 
Quote:

Originally Posted by Natsheh (Post 2627852)
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.

Smilex_Gamer 12-09-2018 14:06

Re: Rock Paper Scissors Game
 
Quote:

Originally Posted by Natsheh (Post 2627929)
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.

JocAnis 12-09-2018 16:25

Re: Rock Paper Scissors Game
 
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

Smilex_Gamer 12-09-2018 16:36

Re: Rock Paper Scissors Game
 
Quote:

Originally Posted by JocAnis (Post 2627975)
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 :P
Btw that plugin is kinda old and this one that I created has multiplayer challenges, so... yep is different :)


All times are GMT -4. The time now is 22:13.

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