Question

I am trying to compare two strings where the first string is a single element of an array and the second array is just a regular string. For some reason, the comparison is not coming through as true. Hopefully, someone can point me in the right direction!

empty =  "        " 
pawn =   "  Pawn  " 
rook =   "  Rook  " 
knight = " Knight " 
bishop = " Bishop " 
queen =  "  Queen " 
king =   "  King  "

emptyspot = {} 
for i = 1, 8 do
    emptyspot[i] = {}
    for j = 1, 8 do
        emptyspot[i][j] = "    /    "   
    end 
end

function chessBoard() 
    io.write("\n\nWelcome to LuaChess!\n\n") 
    io.write("          1        2        3        4        5        6        7        8    \n") 
    io.write("      *************************************************************************\n") 
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("    A *".. emptyspot[1][1] ..
    "*"..emptyspot[1][2].."*"..emptyspot[1][3].."*"..emptyspot[1][4]..      
    "*"..emptyspot[1][5].."*"..emptyspot[1][6].."*"..emptyspot[1][7].."*"..emptyspot[1][8].."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("     *************************************************************************\n") 
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("    B *".. emptyspot[2][1] ..
    "*"..emptyspot[2][2].."*"..emptyspot[2][3].."*"..emptyspot[2][4]..      
    "*"..emptyspot[2][5].."*"..emptyspot[2][6].."*"..emptyspot[2][7].."*"..emptyspot[2][8].."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("     *************************************************************************\n") 
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("    C *".. emptyspot[3][1] ..
    "*"..emptyspot[3][2].."*"..emptyspot[3][3].."*"..emptyspot[3][4]..      
    "*"..emptyspot[3][5].."*"..emptyspot[3][6].."*"..emptyspot[3][7].."*"..emptyspot[3][8].."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("     *************************************************************************\n") io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("    D *".. emptyspot[4][1] ..
    "*"..emptyspot[4][2].."*"..emptyspot[4][3].."*"..emptyspot[4][4]..      
    "*"..emptyspot[4][5].."*"..emptyspot[4][6].."*"..emptyspot[4][7].."*"..emptyspot[4][8].."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("     *************************************************************************\n") io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("    E *".. emptyspot[5][1] ..
    "*"..emptyspot[5][2].."*"..emptyspot[5][3].."*"..emptyspot[5][4]..      
    "*"..emptyspot[5][5].."*"..emptyspot[5][6].."*"..emptyspot[5][7].."*"..emptyspot[5][8].."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("     *************************************************************************\n") io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("    F *".. emptyspot[6][1] ..
    "*"..emptyspot[6][2].."*"..emptyspot[6][3].."*"..emptyspot[6][4]..      
    "*"..emptyspot[6][5].."*"..emptyspot[6][6].."*"..emptyspot[6][7].."*"..emptyspot[6][8].."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("     *************************************************************************\n") io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("    G *".. emptyspot[7][1] ..
    "*"..emptyspot[7][2].."*"..emptyspot[7][3].."*"..emptyspot[7][4]..      
    "*"..emptyspot[7][5].."*"..emptyspot[7][6].."*"..emptyspot[7][7].."*"..emptyspot[7][8].."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("     *************************************************************************\n") io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("    H *".. emptyspot[8][1] ..
    "*"..emptyspot[8][2].."*"..emptyspot[8][3].."*"..emptyspot[8][4]..      
    "*"..emptyspot[8][5].."*"..emptyspot[8][6].."*"..emptyspot[8][7].."*"..emptyspot[8][8].."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("      *".. empty ..
    "*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*"..empty.."*\n")
    io.write("     *************************************************************************\n\n")

    io.write("      Choose a piece to move and the location\n      for it to be moved to separated by a space:\n") 
end

function initBoard()    
    emptyspot[1][1] = rook  
    emptyspot[1][2] = knight
    emptyspot[1][3] = bishop    
    emptyspot[1][4] = queen     
    emptyspot[1][5] = king  
    emptyspot[1][6] = bishop    
    emptyspot[1][7] = knight
    emptyspot[1][8] = rook  
    emptyspot[8][1] = rook  
    emptyspot[8][2] = knight    
    emptyspot[8][3] = bishop    
    emptyspot[8][4] = queen
    emptyspot[8][5] = king  
    emptyspot[8][6] = bishop    
    emptyspot[8][7] = knight    
    emptyspot[8][8] = rook  
    for i = 1, 8 do         
        emptyspot[2][i] = pawn  
    end     
    for i = 1, 8 do         
        emptyspot[7][i] = pawn  
    end
end

function swapPieces(piece1, piece2)     
    temp = piece1   
    piece1 = piece2
    piece2 = temp 
end

function movePawnPlayerOne(spot1, spot2)    
    spot1 = spot1 + 1
    emptyspot[spot1][spot2] = pawn  
    print(emptyspot[spot1][spot2]) 
end


initBoard() 
while true do   
    chessBoard()
    --Take first input for movement     
    input = io.read("*line")    
    loc1 = string.sub(input,1,1)
    loc2 = string.sub(input,2,2)

    if((loc1 == "A") or (loc1 == "a")) then         
        loc1 = 1        
    end     
    if ((loc1 == "B") or (loc1 == "b")) then        
        loc1 = 2        
    end     
    if ((loc1 == "C") or (loc1 == "c")) then        
        ioc1 = 3
    end     
    if ((loc1 == "D") or (loc1 == "d")) then        
        loc1 = 4        
    end     
    if ((loc1 == "E") or (loc1 == "e")) then
        loc1 = 5
    end     
    if ((loc1 == "F") or (loc1 == "f")) then        
        loc1 = 6
    end     
    if ((loc1 == "G") or (loc1 == "g")) then    
        loc1 = 7
    end     
    if ((loc1 == "H") or (loc1 == "h")) then        
        loc1 = 8
    end

    if (emptyspot[loc1][loc2] == pawn) then         
        movePawnPlayerOne(loc1, loc2)   
    end

    print(emptyspot[3][1])

    if string.find(input, "restart") then 
        initBoard()     
    end     
    if string.find (input, "exit") then 
        os.exit()   
    end 
end

The piece of code in question is towards the bottom. Both emptyspot[][] and pawn are both strings.

Was it helpful?

Solution

input = io.read("*line") 
loc1 = string.sub(input, 1, 1) 
loc2 = string.sub(input, 2, 2)

You're reading the input in as strings but your emptyspot table uses numeric indices as keys. Try fixing it with:

loc2 = tonumber(string.sub(input, 2, 2))

OTHER TIPS

The answer from greatwolf is certainly the source of your immediate problem.

It is not that Lua can't compare the strings, but that you had actually looked up a non-existent element of the 2D array you indexed, and were always comparing nil to pawn as a result.

That said, I have a few comments about the rest of the code, and the problems you are going to encounter soon.

First, if you find yourself copying and pasting code to build up a large regular structure, look for opportunities to use loops and functions to abstract out the common elements. The monster function chessBoard is a good example. Fundamentally, what it does is print out the board, which is an array of 64 squares, where each square is printed with a certain amount of ASCII-art. As you have it written, the code that indexes each square's current piece is deeply entangled with the code that is drawing the rest of the square. When you discover that you would rather use a different notation for the pieces (for instance, to distinguish White from Black which you don't do now), you will have a mess on your hands to edit that monolithic collection of io.write built up with carefully constructed strings.

Second, consider separating the model even further from the view. The model in this case is of a chessboard, which is an array of squares that may be empty or contain one of several kinds of piece of two colors. The view is how you draw the board to present it to the user. Your code here blends model and view because the pieces themselves are represented in the model by the actual string used to present them to the user in the view. Recognizing that this abstraction exists is important as you move forward to implementing algorithms more complex than merely displaying the board and moving pieces.

Third, long chains of if statements that repeat the same test with different constants are almost always easy to turn in to much more compact (and more efficient) expressions.

You write if ((loc1 == "C") or (loc1 == "c")) then loc1=3 end for each of A to H. This can be simplified two ways. First, fold the input case so you are testing for a single case of character, then use string.find to convert from letter to integer at once. So the whole block of 8 if-then statements could be written as

loc1 = ("abcdefgh"):find(loc1:lower())

which will either make loc1 be an integer from 1 to 8, or nil if that character is not in the range A to H.

Fourth, io.write can take multiple arguments, which will be output consecutively without any delimiters (unlike print which inserts tabs between arguments and a newline at the end). So those long expressions in chessBoard() can have all of the string concatenation replaced with simple commas. This is not only faster, but it prevents the creation of a great deal of garbage for the garbage collector to eventually handle which comes from all of the temporary strings needed to complete the expression.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top