Home > other >  ASCII Strength Game will not calculate "Bot" word value
ASCII Strength Game will not calculate "Bot" word value

Time:01-27

I'm making a game that tests the ASCII strength of a user versus a bot. (There is also a 2 player mode but that's working fine.) The full description is given at the top of my .cpp file. As a basic breakdown, the bot opens a txt file with 500 common four letter words and inserts them into a size 500 array. It then randomly generates a number to pick a random one, and then goes through the process of tovalue() to recieve its ASCII value, where in tovalue() runs through chartoint() four times, one for each character of the word. My issue is that the program calculates the ASCII value perfectly fine of the user generated word, but always returns 0 (0000) for the botword, no matter what the word.

I've tried a few iterations of the generateword() function including using a vector but always get the same resutls. I've done a lot of digging about this and haven't quite found any solutions, although I suspect that the chartoint() function could be better optimized, just not sure how to impliment any better solutions for this specific case. Also, don't think the problem is with chartoint() since it works fine for user input, but I'm pretty sure the problem is with generateword(). Suggestions for making chartoint() would be helpful, but its not my main priority right now since I just need the program to 100% work first. Also, I've confirmed that all of the words in my .txt file are all caps and only four characters per line.

// Write the code for a game called “ASCII Strength” of a four-letter word selected by Player 1 
// followed by a four-letter word selected by Player 2. The result would be the sum 
//of the ASCII value of each of the letters of the selected words and whoever has higher sum (called ASCII strength) wins. 

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;;

int chartoint(char a) {

    switch (a) {
    case 'A':
        return 1;
        break;
    case 'B':
        return 2;
        break;
    case 'C':
        return 3;
        break;
    case 'D':
        return 4;
        break;
        case 'E':
            return 5;
            break;
        case 'F':
            return 6;
            break;
        case 'G':
            return 7;
            break;
        case 'H':
            return 8;
            break;
        case 'I':
            return 9;
            break;
        case 'J':
            return 10;
            break;
        case 'K':
            return 11;
            break;
        case 'L':
            return 12;
            break;
        case 'M':
            return 13;
            break;
        case 'N':
            return 14;
            break;
        case 'O':
            return 15;
            break;
        case 'P':
            return 16;
            break;
        case 'Q':
            return 17;
            break;
        case 'R':
            return 18;
            break;
        case 'S':
            return 19;
            break;
        case 'T':
            return 20;
            break;
        case 'U':
            return 21;
            break;
        case 'V':
            return 22;
            break;
        case 'W':
            return 23;
            break;
        case 'X':
            return 24;
            break;
        case 'Y':
            return 25;
            break;
        case 'Z':
            return 26;
            break;
    }

    return 0;
}

int tovalue(string input) {


    int first = chartoint(input[0]);
    int second = chartoint(input[1]);
    int third = chartoint(input[2]);
    int fourth = chartoint(input[3]);

    cout << first << second << third << fourth; // EXISTS TO TEST CALCULATION

    int value = first   second   third   fourth;

    return value;
}

string generateword() {

    string arr[500];
    
    ifstream file("words.txt");
    if (file.is_open())
    {
        for (int i = 0; i < 500; i  ) {
            string temp;
            getline(file, temp);
            arr[i] = temp;
        }
        file.close();
    }
    else
    {
        cout << "Error: Unable to open file.";
        exit(0);
    }

    srand(time(0));
    int random_index = rand() % 500;
    string random_word = arr[random_index];
    return random_word;

}

int main()
{

    cout << "Welcome to ASCII strength, a game where the strongest word wins!";
    cout << "\nTo play, you must enter a four letter word. The program will calculate the 'ASCII strength' of your word and compare it to your opponent.";
    cout << "\nWhoever has the higher sum will win!";

    char another;
    another = 'y';
    while (another == 'y' || another == 'Y') {

        cout << "\nWould you like to play against a friend, or against a bot? (F/B)";
        char mode;
        cin >> mode;

        if (mode == 'F' || mode == 'f') {

            cout << "\nPlayer 1, please input your four letter word in all caps: ";
            string answer1;
            cin >> answer1;

            int value1;
            value1 = tovalue(answer1);

            cout << "\nPlayer 2, please input your four letter word in all caps: ";
            string answer2;
            cin >> answer2;

            int value2;
            value2 = tovalue(answer2);

            if (value1 > value2) {
                cout << "\nPlayer 1 wins!";
            }
            else if (value2 > value1) {
                cout << "\nPlayer 2 wins!";
            }
            else if (value1 == value2) {
                cout << "\nTie!";
            }
        }
        else if (mode == 'B' || mode == 'b') {

            cout << "\nPlease input your four letter word in all caps: ";
            string answer;
            cin >> answer;

            int valueanswer;
            valueanswer = tovalue(answer);

            string botword;
            botword = generateword();

            cout << "\nThe bot generates a random word based on a list of popular four letter words.";
            cout << "\nThe bot has generated this word: " << botword;


            int valuebot;
            valuebot = tovalue("botword");

            cout << valueanswer << "          " << valuebot; // THIS EXISTS PURELY TO TEST WHETHER THE VALUES ARE PROPERLY CALCULATING

            if (valueanswer > valuebot) {
                cout << "\nYou win!";
            }
            else if (valuebot > valueanswer) {
                cout << "\nThe bot wins!";
            }
            else if (valueanswer == valuebot) {
                cout << "\nTie!";

            }
        }

        cout << "\nWould you like to start a new game? (y/n)";
        cin >> another;
    }
}

CodePudding user response:

Your problem is this line:

valuebot = tovalue("botword");

Since all characters in "botword" are lowercase, you get all 0 score. You probably meant to write

valuebot = tovalue(botword);
  • Related