Skip to content

Instantly share code, notes, and snippets.

@TannerRogalsky
Last active November 17, 2017 17:49
Show Gist options
  • Save TannerRogalsky/1039612 to your computer and use it in GitHub Desktop.
Save TannerRogalsky/1039612 to your computer and use it in GitHub Desktop.
An IRC bot for playing rock paper scissors.
import org.jibble.pircbot.*;
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Random;
public class Bot extends PircBot {
public final String PARENT = "C:\\Documents and Settings\\Tanner Rogalsky\\My Documents\\RPS\\users";
public String mainChannel;
public ArrayList<RPSUser> users;
public ArrayList<Player> playing;
public Player owner;
public boolean gameOn, signup, throwing;
public RPSUser player1, player2;
public String p1Throw, p2Throw;
public Bot(String channel, String nick) {
setName(nick);
setLogin(nick);
mainChannel = channel;
readStats();
newGame();
}
public void onMessage(String channel, String sender, String login, String hostname, String message) {
String[] command = message.toLowerCase().trim().split(" ");
if ((command[0].equals("!rps")) && (!gameOn)){
message ("Type \"aye\" if you wish to play. " + sender + " should type \"!start\" to begin the game.");
addPlayer(sender);
owner = new Player(sender, -1);
gameOn = true;
signup = true;
} else if ((command[0].equals("end")) && (gameOn) && (sender.equals(owner.nick))){
message("Game exited.");
User[] temp = getUsers(mainChannel);
for(User user : temp){
if(user.hasVoice()){
deVoice(mainChannel, user.getNick());
}
}
newGame();
} else if ((command[0].equals("aye")) && (signup)){
addPlayer(sender);
} else if ((command[0].equals("!start")) && (signup) && (sender.equals(owner.nick)) && (playing.size() > 1)){
signup = false;
Random r = new Random();
int x = r.nextInt(playing.size());
int y;
do {
y = r.nextInt(playing.size());
} while (x == y);
player1 = users.get(playing.get(x).index);
player2 = users.get(playing.get(y).index);
for (Player temp : playing){
System.out.println(temp);
}
message(player1.getNick() + " vs. " + player2.getNick() + ". PM me your throws.");
throwing = true;
} else if ((command[0].equals("!start")) && (signup) && (sender.equals(owner.nick)) && (playing.size() <= 1)){
message("Not enough players, douchebag. Recruit!");
}
}
public void onPrivateMessage(String sender, String login, String hostname, String message){
message = message.toLowerCase().trim();
if ((throwing) && (sender.equals(player1.getNick()))){
if (message.equals("rock")){
player1.incrementRocksThrown();
p1Throw = message;
sendMessage(sender, "Your throw has been registered.");
} else if (message.equals("paper")){
player1.incrementPapersThrown();
p1Throw = message;
sendMessage(sender, "Your throw has been registered.");
} else if (message.equals("scissors")){
player1.incrementScissorsThrown();
p1Throw = message;
sendMessage(sender, "Your throw has been registered.");
} else {
sendMessage(sender, "Invalid throw.");
}
} else if ((throwing) && (sender.equals(player2.getNick()))){
if (message.equals("rock")){
player2.incrementRocksThrown();
p2Throw = message;
sendMessage(sender, "Your throw has been registered.");
} else if (message.equals("paper")){
player2.incrementPapersThrown();
p2Throw = message;
sendMessage(sender, "Your throw has been registered.");
} else if (message.equals("scissors")){
player2.incrementScissorsThrown();
p2Throw = message;
sendMessage(sender, "Your throw has been registered.");
} else {
sendMessage(sender, "Invalid throw.");
}
} else if (message.equals("info")){
sendMessage(sender, "Nick,wins,loses,ties,rocks thrown,papers thrown,scissors thrown");
for (RPSUser user : users){
sendMessage(sender, user.toString());
}
} else if (message.equals("update")){
writeStats();
} else if (message.equals("help")){
sendMessage(sender,"Type !rps to allow players to join a game.");
sendMessage(sender,"Type aye to join a game.");
sendMessage(sender,"Type !start to begin a ladder competition.");
sendMessage(sender,"Type end to end an ongoing game if you are the person who typed !rps.");
sendMessage(sender,"IMPORTANT: Do not leave the channel in the middle of a game if you are still voiced and playing. It'll fuck it up for everyone.");
sendMessage(sender,"The spreadsheet stats are not updated in real time. I do it by hand. For real-time stats in an unfortunate format, type info.");
sendMessage(sender,"Use one nickname every time you play this game. I'mma fuck you up if I have to go through and remove your clones from the stats.");
} else if (message.equals("you should go now.")){
quitServer();
}
if ((p1Throw != null) && (p2Throw != null)){
evaluateThrows();
}
}
public void onJoin(String channel, String sender, String login, String hostname){
sendNotice(sender, "Type \"/msg " + getNick() + " help\" for help.");
}
public void onDisconnect(){
writeStats();
System.exit(0);
}
// Returns the index of the specified nick in the stats arraylist
private int indexOf(String nick){
int index = 0;
for(RPSUser user : users){
if (nick.equals(user.getNick())){
return index;
}
index++;
}
return -1;
}
private void newGame(){
playing = new ArrayList();
gameOn = false;
signup = false;
throwing = false;
player1 = null;
player2 = null;
}
private void addPlayer(String nick){
boolean already = false;
for (Player temp : playing){
if (temp.nick.equals(nick)){
already = true;
}
}
if (!already){
voice(mainChannel, nick);
int index = indexOf(nick);
if (index == -1){
users.add(new RPSUser(nick + ",0,0,0,0,0,0"));
index = users.size()-1;
}
playing.add(new Player(nick, index));
}
}
private void readStats(){
users = new ArrayList();
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader(new File(PARENT, "stats.csv"))));
s.nextLine();
while (s.hasNextLine()) {
users.add(new RPSUser(s.nextLine()));
}
} catch(Exception e){
s.close();
} finally {
if (s != null) {
s.close();
}
}
}
private void writeStats(){
PrintWriter out = null;
try {
out = new PrintWriter(new BufferedWriter(new FileWriter(new File(PARENT, "stats.csv"))));
out.println("Nick,wins,loses,ties,rocks thrown,papers thrown,scissors thrown");
for (RPSUser user : users) {
out.println(user.toString());
}
} catch(Exception e){
} finally {
if (out != null) {
out.close();
}
}
}
public void message(String message){
sendMessage(mainChannel, message);
}
private void winOrLose(RPSUser winner, RPSUser loser){
message(winner.getNick() + " wins the hand! " + loser.getNick() + " loses.");
winner.incrementWins();
loser.incrementLoses();
String nick = loser.getNick();
deVoice(mainChannel, nick);
int index = indexOfPlayer(nick);
playing.remove(index);
p1Throw = null;
p2Throw = null;
if (playing.size() > 1){
Random r = new Random();
int x = r.nextInt(playing.size());
int y;
do {
y = r.nextInt(playing.size());
} while (x == y);
player1 = users.get(playing.get(x).index);
player2 = users.get(playing.get(y).index);
message("New game: " + player1.getNick() + " vs. " + player2.getNick() + ". PM me your throws.");
} else {
deVoice(mainChannel, winner.getNick());
message(winner.getNick() + " has won the ladder! Game over.");
newGame();
}
}
private int indexOfPlayer(String s){
int index = 0;
for (Player temp : playing){
if (temp.nick.equals(s)){
return index;
}
index++;
}
return -1;
}
private void evaluateThrows(){
message(player1.getNick() + " threw " + p1Throw + ". " + player2.getNick() + " threw " + p2Throw + ". ");
if ((p1Throw.equals("rock")) && (p2Throw.equals("paper"))){
winOrLose(player2, player1);
} else if ((p1Throw.equals("paper")) && (p2Throw.equals("scissors"))){
winOrLose(player2, player1);
} else if ((p1Throw.equals("scissors")) && (p2Throw.equals("rock"))){
winOrLose(player2, player1);
} else if ((p1Throw.equals("rock")) && (p2Throw.equals("scissors"))){
winOrLose(player1, player2);
} else if ((p1Throw.equals("paper")) && (p2Throw.equals("rock"))){
winOrLose(player1, player2);
} else if ((p1Throw.equals("scissors")) && (p2Throw.equals("paper"))){
winOrLose(player1, player2);
} else if (p1Throw.equals(p2Throw)){
message("Tie! PM me your next throws.");
player1.incrementTies();
player2.incrementTies();
p1Throw = null;
p2Throw = null;
}
}
}
import org.jibble.pircbot.*;
public class Main {
public static void main(String[] args) throws Exception {
// Now start our bot up.
Bot bot = new Bot(Vars.CHANNEL, Vars.NAME);
bot.setAutoNickChange(true);
// Enable debugging output.
bot.setVerbose(false);
// Connect to the IRC server.
bot.connect(Vars.SERVER);
bot.identify(Vars.PASSWORD);
// Join the #pircbot channel.
bot.joinChannel(Vars.CHANNEL);
}
}
/**
* Write a description of class Player here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Player
{
public int index;
public String nick;
public Player(String n, int i)
{
nick = n;
index = i;
}
public String toString(){
return nick + " " + index;
}
}
public class RPSUser
{
private String nick;
private int wins, loses, ties, rocksThrown, papersThrown, scissorsThrown;
public RPSUser(String info)
{
String[] temp = info.split(",");
nick = temp[0];
wins = Integer.parseInt(temp[1]);
loses = Integer.parseInt(temp[2]);
ties = Integer.parseInt(temp[3]);
rocksThrown = Integer.parseInt(temp[4]);
papersThrown = Integer.parseInt(temp[5]);
scissorsThrown = Integer.parseInt(temp[6]);
}
public String toString(){
return nick + "," + wins + "," + loses + "," + ties + "," + rocksThrown + "," + papersThrown + "," + scissorsThrown;
}
public String getNick(){
return nick;
}
public void incrementWins(){
wins++;
}
public void incrementLoses(){
loses++;
}
public void incrementTies(){
ties++;
}
public void incrementRocksThrown(){
rocksThrown++;
}
public void incrementPapersThrown(){
papersThrown++;
}
public void incrementScissorsThrown(){
scissorsThrown++;
}
}
/**
* Write a description of class VARS here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Vars
{
public static final String CHANNEL = "#RPS";
public static final String SERVER = "irc.waypasteleven.com";
public static final String NAME = "RPSBot";
public static final String PASSWORD = "*******";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment