You are viewing the MafiaScum.net Wiki. To play the game, visit the forum.

Code for Telephone Pictionary mods: Difference between revisions

From MafiaWiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 145: Line 145:
}</pre>
}</pre>


When you run this for the first time, you should find two new files the next time you check the folder. (These will both be overwritten every time you generate a new chain map, so rename them or put them in a new folder if you want to keep them.) One will be a .txt file with a quick summary of the map. You don't need this, but at least it looks nice. The other will be a .csv spreadsheet with all the same information, and also gratuitous commas.  
When you run this for the first time, you should find two new files the next time you check the folder. (These will both be overwritten every time you generate a new chain map, so rename them or put them in a new folder if you want to keep them.) One will be a .txt file with a quick summary of the map. You don't need this, but it might help you keep track of things. The other will be a .csv spreadsheet with all the same information, and also gratuitous commas.  


With your spreadsheet skeleton in hand, you can go to Google Drive and make a proper spreadsheet out of it. Just go to File - Import for that. The result will be ugly. .csv files don't care about word wrap or cell sizes or background colors or anything like that. Feel free to fix it up to your taste, and fill in the empty cells as people get their assignments in. New rows and columns ''will'' affect the wiki formatter at the end of the game, so don't add them on a whim.
With your spreadsheet skeleton in hand, you can go to Google Drive and make a proper spreadsheet out of it. Just go to File - Import for that. The result will be ugly. .csv files don't care about word wrap or cell sizes or background colors or anything like that. Feel free to fix it up to your taste, and fill in the empty cells as people get their assignments in. New rows and columns ''will'' affect the wiki formatter at the end of the game, so don't add them on a whim.

Revision as of 00:51, 16 March 2018

Starting the game

The codes written on this page are all C++. If you don't know how to use them and don't have Visual Studio on your computer already, you can download Code::Blocks and MinGW for free. This page is not a tutorial to help you set them up, but feel free to bug Ether for help if you need it.

Below is a map randomizer you can use at the start of the game. This code is not the only thing you need to run it! In the same folder, you need to also put in two .txt files: a "list.txt", with twenty lines--one for each player, and a "chainnames.txt"--also twenty lines, but noting each chain's name. (Chains don't striiiiictly have to have names, but it's much easier to keep track of. Generally you want one for each letter of the alphabet up through T. Note that the codes on this page all assume 20 players and 13 rounds, and will not work with less. Change the constants if you're working with a different number, and make sure your list and chainnames files are at least large enough to accommodate.)

#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include <stdlib.h>
using namespace std;

const int PLAYERCOUNT = 20;
const int ROUNDS = 13;

struct player{
	string name;
	bool eachchain[PLAYERCOUNT];
	int thisround;
};

int main() {

	ifstream allplayers, chainnames;
	string chain[PLAYERCOUNT][ROUNDS];
	player playerlist[PLAYERCOUNT];
	string taxonomy[PLAYERCOUNT];
	ofstream chainmap, skeleton;

	int whichperson, counter;
	bool keeprolling;

	counter = 0;
	keeprolling = true;

	// Assign players into an array.
	allplayers.open("list.txt");
	while(!allplayers.eof() && counter < PLAYERCOUNT) {
		getline(allplayers, playerlist[counter].name);
		counter++;
	}
	allplayers.close();
	counter = 0;

	chainnames.open("chainnames.txt");
	while(!chainnames.eof() && counter < PLAYERCOUNT) {
		getline(chainnames, taxonomy[counter]);
		counter++;
	}
	chainnames.close();
	counter = 0;

	for(int i = 0; i < PLAYERCOUNT; i++) {
		playerlist[i].thisround = -1;
		for(int j = 0; j < PLAYERCOUNT; j++) {
			playerlist[i].eachchain[j] = false;
		}
	}


	srand(time(NULL));

	// For Round 1, everyone just gets whatever spot they had in the playerlist. No randomization needed.
	for(int i = 0; i < PLAYERCOUNT; i++) {
		playerlist[i].eachchain[i] = true;
		chain[i][0] = playerlist[i].name;
	}

	// Generating player placements for future chains.
	for(int round = 1; round < ROUNDS; round++) {
		for(int i = 0; i < PLAYERCOUNT; i++) playerlist[i].thisround = -1;
		counter = 0;

		for(int chainnum = 0; chainnum < PLAYERCOUNT; chainnum++) {
			while(keeprolling == true) {
					// Randomize a player who hasn't been in this chain yet.
					// First, pick a random number based on how many eligible slots there are.
					whichperson = rand() % (PLAYERCOUNT - chainnum);

					// There are 20 players in total, so skip anyone who's already been assigned for this round.
					// You'll wind up with a random person out of whoever's left.
					for(int i = 0; i <= whichperson; i++) { 
						if(playerlist[i].thisround > -1) whichperson++;
					}

				// Make sure the player you picked wasn't already in the chain in an earlier round.
				if(playerlist[whichperson].eachchain[chainnum] == false) {
					keeprolling = false;
				}
				if(keeprolling == true) counter++;

				// Sometimes the map backs itself into a corner and nobody left will fit into the remaining slots.
				// Keep track of when it's looping too much, and throw the whole column out and start the round over if necessary.
				if(counter > 21) {
					for(int i = 0; i < PLAYERCOUNT; i++) {
						if(playerlist[i].thisround > -1) {
							playerlist[i].eachchain[playerlist[i].thisround] = false;
						}
						playerlist[i].thisround = -1;
					}
				counter = 0;
				chainnum = 0;
				}
			}

			chain[chainnum][round] = playerlist[whichperson].name;

			// Updates.
			playerlist[whichperson].eachchain[chainnum] = true;
			playerlist[whichperson].thisround = chainnum;
			keeprolling = true;
		}
	}

	chainmap.open("chainmap.txt");
	for(int i = 0; i < PLAYERCOUNT; i++) {
		chainmap << "Chain " << (i + 1) << ": ";
		for(int j = 0; j < ROUNDS; j++) {
			chainmap << " - " << chain[i][j];
		}
		chainmap << endl;
	}
	chainmap.close();

	skeleton.open("spreadsheet.csv");
	skeleton << "Chain";
	for (int i = 0; i < ROUNDS; i++) {
        skeleton << "," << i + 1;
    }
    skeleton << endl;
	for(int i = 0; i < PLAYERCOUNT; i++) {
		skeleton << taxonomy[i];
		for(int j = 0; j < ROUNDS; j++) {
			skeleton << "," << chain[i][j];
		}
		skeleton << endl;
		for (int j = 0; j < ROUNDS; j++) {
            skeleton << ",";
		}
		skeleton << endl;
	}

	return 0;
}

When you run this for the first time, you should find two new files the next time you check the folder. (These will both be overwritten every time you generate a new chain map, so rename them or put them in a new folder if you want to keep them.) One will be a .txt file with a quick summary of the map. You don't need this, but it might help you keep track of things. The other will be a .csv spreadsheet with all the same information, and also gratuitous commas.

With your spreadsheet skeleton in hand, you can go to Google Drive and make a proper spreadsheet out of it. Just go to File - Import for that. The result will be ugly. .csv files don't care about word wrap or cell sizes or background colors or anything like that. Feel free to fix it up to your taste, and fill in the empty cells as people get their assignments in. New rows and columns will affect the wiki formatter at the end of the game, so don't add them on a whim.

There is one exception to this: you do need to accommodate for people taking on sub duties. When someone steps up, add another column to the right of the round where a replacee missed the deadline. Fill in only the replacement's name and submission. Do not give the column a name. It's ugly, but you can hide columns in Google Spreadsheet if you don't need to look at them anymore.

Also, pilcrows (¶) will mess up the formatting for the wikification process. Professional, this code is not. Anyway, it's not like anyone would have ever had any reason to use that symbol if I hadn't made this note, so just smack them if they try.

Finishing up

When you want the wiki formatting for the index or a subpage, go to the spreadsheet and choose File - Download as - .CSV. Rename it to spreadsheet.csv. Put that into the same folder as this code and run it.

#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>

using namespace std;
const int PLAYERCOUNT = 20;
const int ROUNDS = 13;

struct cell {
    string player;
    string entry;
};

// I'm assuming people don't use pilcrows.
// If they start using them because this code is released,
// you may hit them.
string pilcrow(string line) {
    int i;
    bool done;
    done = false;
    do {
        i = line.find("\"\"");
        if(i == -1) done = true;
        else line.replace(i, 2, "¶");
    } while(!done);
    return line;
}

// This "turn all double-quotes in the line into pilcrows and then turn individual cells back" plan
// is pretty unprofessional. Good thing I'm not a professional!
string stopcrowing(string line) {
    int i;
    bool done;
    done = false;
    do {
        i = line.find("¶");
        if(i == -1) done = true;
        else line.replace(i, 1, "\"");
    } while(!done);
    return line;
}

// Split the line into two components:
// the first cell that wasn't already split off, and the rest of the line.
void separate(string& line, string& first) {
    int i;
    if(line.substr(0, 1) == "\"" || (line.substr(0, 1) == "¶" && line.substr(1, 1) == "\"")) {
        i = line.find("\",");
        if(i == -1) {
            first = line.substr(1, line.length() - 2);
            line = "";
            return;
        }
        first = line.substr(1, i - 1);
        line = line.substr(i+2, line.length());
    }
    else {
        i = line.find(",");
        if(i == -1) {
            first = line.substr(0, line.length());
            line = "";
            return;
        }
        first = line.substr(0, i);
        line = line.substr(i+1, line.length());
    }
}

string extension(string url) {
    int i;
    i = url.find_last_of(".");
    if(i == -1) return "..";
    return url.substr(i, url.length());
}

string despaced(string original) {
	int i;
	do {
		i = original.find(" ");
		if(i == -1) return original;
		original.replace(i, 1, " ");
	} while(i != -1);

	return original;
}

int main() {
    int subcheck[ROUNDS];
    for(int i = 0; i < ROUNDS; i++) subcheck[i] = 0;

    string chainname[PLAYERCOUNT];
    cell chainmap[PLAYERCOUNT][ROUNDS];
    string currentline, placeholder;
    ifstream spreadsheet;
    ofstream index_format, subpage_format, test;
    int k;
    k = 0;

    spreadsheet.open("spreadsheet.csv");
    if(!spreadsheet.is_open()) spreadsheet.open("spreadsheet.txt");
    if(!spreadsheet.is_open()) {
            cout << "That didn't open. Please add a file entitled \"spreadsheet.csv\" to this folder and try again.";
            cout << " (This should be the renamed copy of the .csv file you downloaded from Google Spreadsheets.)";
            return -1;
    }
    getline(spreadsheet, currentline);

    //Discard the "Chain" and "1" cells; those will always be in the same columns.
    separate(currentline, placeholder);
    separate(currentline, placeholder);

    // Track whether any given round has replacements to watch out for.
    while(currentline != ""){
        separate(currentline, placeholder);
        if(placeholder == "") subcheck[k]++;
        else k++;
    }

    for(int i = 0; i < PLAYERCOUNT; i++) {
        getline(spreadsheet, currentline);
        currentline = pilcrow(currentline);
        separate(currentline, chainname[i]);
        for(int j = 0; j < ROUNDS; j++) {
            for(int r = 0; r <= subcheck[j]; r++) {
                separate(currentline, placeholder);
                if(placeholder != "" || r == 0) chainmap[i][j].player = stopcrowing(placeholder);
            }
        }
        getline(spreadsheet, currentline);
        currentline = pilcrow(currentline);
        separate(currentline, placeholder);
        for(int j = 0; j < ROUNDS; j++) {
            for(int r = 0; r <= subcheck[j]; r++) {
                separate(currentline, placeholder);
                if(placeholder != "") chainmap[i][j].entry = stopcrowing(placeholder);
            }
        }
    }

    spreadsheet.close();

    //Index formatting. This accounts for any replacements that already exist.
	index_format.open("index_format.txt");
	index_format << "[insert a description here]" << endl << endl;
	index_format << "==The Player List==" << endl << endl << "{{multicol}}" << endl;
	for(int i = 1; i < (PLAYERCOUNT / 2) + 1; i++) {
		index_format << "\'\'\'" << i << "\'\'\'. {{U|" << despaced(chainmap[i-1][0].player);
		index_format << "}}<br />" << endl;
	}
	index_format << "{{multicol-break}}" << endl;
	for(int i = (PLAYERCOUNT / 2) + 1; i < PLAYERCOUNT + 1; i++) {
		index_format << "\'\'\'" << i << ".\'\'\' {{U|" << despaced(chainmap[i-1][0].player);
		index_format << "}}<br />" << endl;
	}
	index_format << "{{multicol-end}}" << endl << endl;
	index_format << "==The Chains==" << endl  << endl << endl;
	index_format << "{| style=\"border:0px !important;border-spacing:15px;\"" << endl;
	for(int i = 0; i < PLAYERCOUNT; i++) {
		index_format << "| \'\'\'<span style=\"font-size:120%;\">[[/Chain ";
		index_format << (char)(i+65) << "|" << (char)(i+65) << ". ";
		index_format << chainmap[i][0].player << "\'s Chain]]</span>\'\'\'" << endl << "----" << endl;
		for(int j = 0; j < ROUNDS; j++) {
			index_format << "*{{U|" << despaced(chainmap[i][j].player) << "}} ";
			if(j % 2 == 0) index_format << "(phrase)" << endl;
			else index_format << "(picture)" << endl;
		}
		if((i+1) % 5 == 0 && i != PLAYERCOUNT) index_format << "|-" << endl;
	}
	index_format << "|}" << endl << endl << "[[Category:Mish Mash]]" << endl;
	index_format.close();

	// Now for the subpages!
	string gamename, prefix;
	cout << "What\'s the game\'s name going to be\? (An example is \"The Ramen Years\".)" << endl;
	getline(cin, gamename);
	cout << "And the prefix for image files? (Examples from earlier games are Telcat, Telram and Telekoop.)" << endl;
	cin >> prefix;
	subpage_format.open("subpage_format.txt");
	for(int i = 0; i < PLAYERCOUNT; i++) {
		subpage_format << "<br />";
		if(i > 0) {
			subpage_format << "<div style=\"float:left;font-size:150%;font-weight:bold;\">";
			subpage_format << "[[Telephone Pictionary: " << gamename << "/Chain " << (char)(i+64);
			subpage_format << "|« Previous]]</div>";
		}
		if(i != PLAYERCOUNT - 1) {
			subpage_format << "<div style=\"float:right;font-size:150%;font-weight:bold;\">";
			subpage_format << "[[Telephone Pictionary: " << gamename << "/Chain " << (char)(i+66);
			subpage_format << "|Next »]]</div>";
		}
		subpage_format << "<br /><br />" << endl << endl;
		subpage_format << "==Chain " << (char)(i+65) << ": " << chainmap[i][0].player;
		subpage_format << "\'s Chain==" << endl << endl;
		for(int j = 0; j < ROUNDS; j++) {
			if(j % 2 == 0) {
				subpage_format << "<span style=\"font-size:130%;color:#00bf80;\">\'\'\'{{U|";
				subpage_format << despaced(chainmap[i][j].player) << "}} - Phrase \'\'\'</span>" << endl;
				subpage_format << ":<span style=\"font-size:170%;color:#434343;\">";
				subpage_format << chainmap[i][j].entry << "</span>" << endl << endl << endl;
			}
			else {
				subpage_format << "<span style=\"font-size:130%;color:#ff0040;\">\'\'\'{{U|";
				subpage_format << despaced(chainmap[i][j].player) << "}} - Picture\'\'\'</span>" << endl;
				subpage_format << ":[[Image:" << prefix << "_" << (char)(i+65) << (j+1)/2;
				subpage_format << extension(chainmap[i][j].entry) << "]]" << endl << endl << endl;;
			}
		}
		subpage_format << "<br />";
		if(i > 0) {
			subpage_format << "<div style=\"float:left;font-size:150%;font-weight:bold;\">";
			subpage_format << "[[Telephone Pictionary: " << gamename << "/Chain " << (char)(i+64);
			subpage_format << "|« Previous]]</div>";
		}
		if(i != PLAYERCOUNT - 1) {
			subpage_format << "<div style=\"float:right;font-size:150%;font-weight:bold;\">";
			subpage_format << "[[Telephone Pictionary: " << gamename << "/Chain " << (char)(i+66);
			subpage_format << "|Next »]]</div>";
		}
		subpage_format << "<br /><br />[[Category:Telephone Pictionary]]" << endl << endl << endl;
		subpage_format << "---" << endl << endl;
	}
	subpage_format.close();

	return 0;
}

That'll give you two .txt files, again in the same folder: an index_format and a subpage_format (the latter containing the code for all 20 subpages). It won't be perfectly complete unless the spreadsheet is, but you can redownload the .csv file and rerun this as many times as you want, and even if you don't, the rest is pretty easy to fill in.

Note that those text files don't accommodate for things like BBCode, HTML or line breaks in people's sentences. But that's straightforward to fix by hand.