I am looking for a way to check if more than 2 values are equal.
The Premise
I have been attempting to remake The Coding Train’s Tic Tac Toe game (seen here) in Roblox, and to make the game on a 5x5 grid. The board is a table that holds 5 more
tables, each with 5 string values to determine blank, X or O.
The Problem
To check if every value in a row is equal (meaning someone has won the game), he uses a for loop and does something like:
This does not appear to work in Lua, and I did some reading and it turns out that == returns true if the given values are equal. When x == y == z is used in Lua, (from my knowledge) x == y will return true or false, and then it will check if true or false is equal to z.
This is, obviously, not the desired outcome. I have poked around the Devforum for a while to see if anyone else had this problem, but I might have missed a post so let me know.
Current Code
Here’s my current function for checking for a winner:
local function checkWinner()
local winner = nil
-- horizontal checks
for i = 1, #board do
if board[i][1] == board[i][2] == board[i][3] == board[i][4] == board[i][5] then
winner = board[i][1]
end
end
-- vertical checks
for i = 1, #board do
if board[1][i] == board[2][i] == board[3][i] == board[4][i] == board[5][i] then
winner = board[1][i]
end
end
-- diagonal checks
if board[1][1] == board[2][2] == board[3][3] == board[4][4] == board[5][5] then
winner = board[1][1]
end
if board[1][5] == board[2][4] == board[3][3] == board[4][2] == board[5][1] then
winner = board[1][5]
end
if (winner == nil) and (#available == 0) then
print("It's a tie!")
elseif winner ~= nil then
print(winner .. " wins!")
else
print("No outcome yet!")
end
end
I should’ve mentioned that I’m avoiding this because the amount you have to type scales up very quickly with this method. I’ll probably resort to this in the tic-tac-toe game, but I’d also like to find something that works for large amounts of values.
local t = {1, 1, 1, 1, 3, 3, 3, 3, 4, 5, 6, 7, 8, 11, 11, 11, 11} -- 1, 3, 11
local function DuplicatesExist(arr: {any}): {any}
local curr = {}
local hash = {}
for _,v in ipairs(arr) do
if not table.find(curr, v) then
table.insert(curr, v)
else
if not table.find(hash, v) then
table.insert(hash, v)
end
end
end
return hash
end
for i,v in ipairs(DuplicatesExist(t)) do
warn(v) -- 1, 3, 11
end
if #DuplicatesExist(t) > 2 then
warn("NOO THERE ARE MORE THAN 2 EQUAL VALUES!!") -- this prints out
end
You can use a for loop with #array (or #array - 1 if you don’t wanna check arr[1] again) iterations. You would just have to check if the element at arr[i] is equal to the first element, arr[1]. If it isn’t then continue to the next row. If the loop reached the #arrayth iteration, then the entire row matched. This should work for an array of any size.
Ex:
local board = {1, 1, 1, 1, 1, 1, 1}
local function checkWinner()
local first = board[1]
for row = 1, #board do
if board[row] ~= first then
print'Entire row didn\'t match :('
break
elseif row == #board then
print'Entire row matched!'
end
end
end
checkWinner()
You can also check out my reply to someone who also needed help implementing tic tac toe here: Help with Tic-Tac-Toe game - #3 by heII_ish
I pretty much gave them the entire implementation for checking only the rows of the board (adding checks for columns and diagonals should be pretty straightforward from there)
Thanks! This seems to be the most straightforward method, although I’ve already implemented an optimised version of what @Katrist posted. I may rewrite the system once it is completed.