Search code examples
game-maker

Incorrect result to input checking in GameMaker


Message for MHS readers: Do not copy this code. Just use it for help with yours.

I have 10 words loading from a file and then 9 are displayed in a grid. After 30 seconds 1 is removed and the 10th word from the file is put in. The user is then asked to guess these 2 words. They have 3 chances for each. When I enter the correct answer it still says I got it wrong. I even have it draw the correct answers to assist in testing. The result always comes back false.

These are the scripts: ReadFile

global.DelWord = "Unknown";
randomize();                                                               
Fopen = file_text_open_read(working_directory + "\Words.txt");    
global.WordList = ds_list_create();
global.FileLen = file_text_eof(Fopen);
while(!file_text_eof(Fopen)){
    Fread = string_upper(file_text_readln(Fopen));
    ds_list_add(global.WordList,Fread);
    }
file_text_close(Fopen);
ds_list_shuffle(global.WordList);
global.WildCard = ds_list_find_value(global.WordList,global.FileLen);
ds_list_delete(global.WordList,global.FileLen);

DrawWords

z=0;
draw_set_colour(c_black);
draw_set_font(font0);
draw_set_valign(fa_left);
draw_set_halign(fa_bottom);
draw_text(100,100,"Code by kammak743);
draw_text(1600,900,string_lower(global.WildCard));
draw_text(1200,900,string_lower(global.DelWord));



for (x=0; x<3; x+=1){
    for (y=0; y<3; y+=1){
        draw_text((x*400)+700,(y*200)+290,ds_list_find_value(global.WordList,z));
        z=z+1
        }
    }

NewGrid

ds_list_shuffle(global.WordList);
global.DelWord = ds_list_find_value(global.WordList,global.FileLen);
ds_list_delete(global.WordList,global.FileLen);
ds_list_insert(global.WordList,global.FileLen,global.WildCard);
ds_list_shuffle(global.WordList);

CheckWords

Correct1 = 0;                                                        
OldWordA = false;                                                   
while (OldWordA == false) && (Correct1 <3){                      
    global.OldWordQ = get_string("What word was replaced?","");         
    if string_lower(global.OldWordQ) == string_lower(global.DelWord){  
        OldWordA= true;                                
        }
    Correct1 += 1;                                           
    }


Correct2 = 0;                                            
NewWordA = false;
globalvar NewWordA;
while (NewWordA == false) && (Correct2 <3){
    global.NewWordQ = get_string("What word is new?","");
    if string_lower(global.NewWordQ) == string_lower(global.WildCard){
        NewWordA= true;
        }
    Correct2 += 1;
    }


global.Win=false;
if (NewWordA == true) && (OldWordA == true){  
    global.Win=true;                            
    }
else{
    global.Win=false;                        
    }

DrawCheck

if global.Win == true{
    draw_text(900,900,"You won!!!");
    }
else{
    draw_text(900,900,"You failed");
    }
draw_text(400,50,global.OldWordQ);
draw_text(900,50,global.NewWordQ);

Solution

  • You used file_text_readln() for read words from file, but this function also reads CR/LF codes ($0d, $0a), so when you compare two strings, like

    ABOUT (ASCII codes: 97, 98, 111, 117, 116)

    and

    ABOUT (ASCII codes: 97, 98, 111, 117, 116, 13, 10)

    it will return false.

    Therefore you need change

    Fread = string_upper(file_text_readln(Fopen));
    

    to

    Fread  = string_upper(file_text_read_string(Fopen));
    file_text_readln(Fopen);