#!/usr/bin/perl -w # # Implements the scramble game # written by thux 16/04/200 # thux@hotmail.com # # I wrote this because i saw a game like this one in a mIRC script, and there # wasn't none for my linux box, so..., why not, xchat rulez, perl rulez (this is # my first piece of code in perl (i suck!) i'm sure this is code is full of # mistakes...BUT what the hell... it works =) it just a fucking game... # any patch/update/rewrite of anything, send me updated game =) # IRC::print ":: Scramble game is loaded ::\n"; IRC::add_command_handler("scramble", "scramble_game"); IRC::add_message_handler("PRIVMSG", "catch_word"); sub print_out { IRC::print "@_\n"; } sub command { IRC::command "@_"; } sub update_score { my $updated = 0; if (@score == ()) { push (@score, $_[0], 1); } else { for (my $i=0; $i <= $#score; $i=$i+2) { if ($score[$i] =~ $_[0]) { $score[$i+1] = $score[$i+1]+1; $updated = 1; } } if ( $updated == 0 ) { push (@score, $_[0], 1); } } } sub get_score { for (my $i=0; $i < @score; $i=$i+2) { if ($score[$i] =~ $_[0]) { return $score[$i+1]; } } return 0; } sub show_score { my $points = get_score($_[0]); my $irc_nick = $_[0]; if ($points != 1) { command("$irc_nick has $points points."); } else { command("$irc_nick has $points point."); } } sub scramble_word { my $temp_word = $_[0]; my $i = 0; while ($i < length($_[0]) ) { my $is_member = 0; my $j = int(rand(length($_[0]))+1); for (my $k = 0 ; $k <= $i ; $k++ ) { if ( $temp_array[$k] == $j ) { $is_member = 1; } } if ($is_member == 0) { push (@temp_array, $j); substr ($temp_word, $i, $i+1, substr($_[0], $j-1, $j)); $i++; } } @temp_array = (); return (substr ($temp_word, 0, length($_[0]))); } sub scramble_line { my $i = 2; my $j = 0; $line_scrambled = " "; while ($args[$i] ne "") { substr($line_scrambled, $j, $j+length($args[$i]), scramble_word($args[$i])); $j = $j+length($args[$i])+1; $i++; } substr ($line_scrambled, $j-1, 50, ""); return ($line_scrambled); } sub catch_word { $irc_message = shift(@_); $irc_message =~ /:(.*)!(\S+) PRIVMSG (.*) :(.*)/i; $irc_nick = $1; $message = lc($4); if ($message eq $line) { command("Bingo. $irc_nick did it!!!"); command("The correct word was $line"); $line = ""; command("!score to see your points"); update_score($irc_nick); show_score($irc_nick); } elsif ($message eq "!score") { $irc_nick = $1; show_score($irc_nick); } } sub scramble_game { @args = split(/ /, shift); if ($args[0] eq "send") { $hint=$args[1]; command("-----"); command("Hint: $hint"); my $i = 2; my $j = 0; $line = " "; while ($args[$i] ne "") { $temp = $j+length($args[$i]); substr($line, $j, $j+length($args[$i]), $args[$i]); $j = $j+length($args[$i])+1; $i++; } substr($line, $j-1, 50, ""); print_out("Word: $line"); $scrambled_word = scramble_line($line); command("Scrambled word: $scrambled_word"); command("---------------"); return 1; } elsif ($args[0] eq "resend" && $args[1] eq "") { command("-----"); command("Hint: $hint"); print_out("Word: $line"); command("Scrambled word: $scrambled_word"); command("\00---------------\00"); return 1; } elsif ($args[0] eq "reset" && $args[1] eq "") { @score = (); command("-----"); command("Score reset"); command("-----------"); return 1; } elsif (($args[0] eq "help" && $args[1] eq "") || $args[0] eq "") { print_out("syntax help for /scramble [hint] [word]"); print_out("code by thux"); print_out("help ---> this help screen"); print_out("send ---> send new hint and scrambled word"); print_out("reset --> resets all score"); print_out("resend -> resend current hint and scrambled word"); return 1; } return 0; }