Hey! so i have a tictactoe game that puts you against a computer to play and i have a problem. Each round wether i lose or win, the computer is given a win so i was wondering if someone could check it out and tell me the issue.(STARTERGUI)
Here is the model :
TicTacToe.rbxm (13.6 KB)
Perhaps it would be more convenient if you showed us the script instead.
1 Like
Well i linked the gui with all the scripts inside but here ;
GameLogic (Localscript) :
local module = require(script.Parent.Settings)
local status_label = script.Parent.Frame:WaitForChild'StatusLabel'
local toodle = module.why_first
local points_computer = 0
local points_player = 0
-- get 9 buttons --
local buttons = {}
for _, value in pairs(script.Parent.Frame.Game:GetChildren()) do
if value:IsA('TextButton') then
table.insert(buttons, value)
end
end
--
local win_combinations = {
{buttons[1], buttons[2], buttons[3]};
{buttons[1], buttons[5], buttons[9]};
{buttons[1], buttons[4], buttons[7]};
{buttons[2], buttons[5], buttons[8]};
{buttons[3], buttons[5], buttons[7]};
{buttons[3], buttons[6], buttons[9]};
{buttons[4], buttons[5], buttons[6]};
{buttons[7], buttons[8], buttons[9]};
}
-- if computer first
local function check_computer()
if toodle == false then
local randomCells = {2,3,5,6,8}
buttons[randomCells[math.random(1,#randomCells)]].Text = module.computer_symbol
toodle = true
end
end
check_computer()
-- when, game is over
local function message(who_win)
toodle = nil
if who_win == 'computer' then
status_label.Text = ' '..module['lost_text']..' '
status_label.BackgroundColor3 = Color3.fromRGB(255, 59, 0)
points_computer = points_computer + 1
elseif who_win == 'player' then
status_label.Text = ' '..module['win_text']..' '
status_label.BackgroundColor3 = Color3.fromRGB(0, 255, 21)
points_player = points_player + 1
else
status_label.Text = 'draw!'
status_label.BackgroundColor3 = Color3.fromRGB(255, 173, 7)
end
status_label.Visible = true
wait(1.5)
status_label.Parent.Player1Label.Text = tostring(points_player)
status_label.Parent.Player2Label.Text = tostring(points_computer)
status_label.Visible = false
for _, value in pairs(buttons) do
if value.Text ~= '' then
value.Text = ''
wait(.07)
end
end
toodle = module.why_first
check_computer()
end
-- game logic
for _, value in pairs(buttons) do
value.MouseButton1Click:Connect(function()
-- player
if toodle and value.Text == '' then
value.Text = module.player_symbol
toodle = false
end
-- computer
local win_combinations_player = {
[1] = {false,false,false};
[2] = {false,false,false};
[3] = {false,false,false};
[4] = {false,false,false};
[5] = {false,false,false};
[6] = {false,false,false};
[6] = {false,false,false};
[7] = {false,false,false};
[8] = {false,false,false};
}
local win_combinations_computer = {
[1] = {false,false,false};
[2] = {false,false,false};
[3] = {false,false,false};
[4] = {false,false,false};
[5] = {false,false,false};
[6] = {false,false,false};
[6] = {false,false,false};
[7] = {false,false,false};
[8] = {false,false,false};
}
local player_table = {}
local computer_table = {}
for _, button in pairs(buttons) do
if button.Text == module.player_symbol then
table.insert(player_table, button.Name)
elseif button.Text == module.computer_symbol then
table.insert(computer_table, button.Name)
end
end
for number, combination in pairs(win_combinations) do
for j = 1, 3 do
for i, symbol in pairs(player_table) do
if player_table[i] == combination[j].Name then
win_combinations_player[number][j] = tonumber(combination[j].Name)
end
end
for i, symbol in pairs(computer_table) do
if computer_table[i] == combination[j].Name then
win_combinations_computer[number][j] = tonumber(combination[j].Name)
end
end
end
end
-- check if player won
for i, v in pairs(win_combinations_player) do
if v[1] ~= false and v[2] ~= false and v[3] ~= false then
message('player')
end
end
-- check win combinations for computer
local numbers1 = {}
for i, v in pairs(win_combinations_computer) do
if v[1] ~= false then
numbers1[1] = v[1]
elseif v[3] ~= false then
numbers1[1] = v[3]
elseif v[2] ~= false then
numbers1[1] = v[2]
end
end
for i, v in pairs(win_combinations_computer) do
if v[1] ~= false and v[2] ~= false then
numbers1[1] = v[1]
numbers1[2] = v[2]
elseif v[1] ~= false and v[3] ~= false then
numbers1[1] = v[1]
numbers1[2] = v[3]
elseif v[2] ~= false and v[3] ~= false then
numbers1[1] = v[2]
numbers1[2] = v[3]
end
end
if numbers1[1] ~= 0 and numbers1[2] ~= 0 then
for i, v in pairs(win_combinations) do
if (tonumber(v[2].Name) == numbers1[1] and tonumber(v[1].Name) == numbers1[2]) or (tonumber(v[1].Name) == numbers1[1] and tonumber(v[2].Name) == numbers1[2]) then
numbers1[3] = tonumber(v[3].Name)
break
elseif (tonumber(v[3].Name) == numbers1[1] and tonumber(v[1].Name) == numbers1[2]) or (tonumber(v[1].Name) == numbers1[1] and tonumber(v[3].Name) == numbers1[2]) then
numbers1[3] = tonumber(v[2].Name)
break
elseif (tonumber(v[3].Name) == numbers1[1] and tonumber(v[2].Name) == numbers1[2]) or (tonumber(v[2].Name) == numbers1[1] and tonumber(v[3].Name) == numbers1[2]) then
numbers1[3] = tonumber(v[1].Name)
break
end
end
end
--
if toodle == false then
for i, v in pairs(win_combinations_computer) do
if (v[1] ~= false and v[2] ~= false) or (v[3] ~= false and v[2] ~= false) or (v[1] ~= false and v[3] ~= false) then
if (v[1] ~= false and v[2] ~= false) and (buttons[numbers1[3]].Text == '') then
buttons[numbers1[3]].Text = module.computer_symbol
message('computer')
break
elseif (v[3] ~= false and v[2] ~= false) and (buttons[numbers1[3]].Text == '') then
buttons[numbers1[3]].Text = module.computer_symbol
message('computer')
break
elseif (v[3] ~= false and v[1] ~= false) and (buttons[numbers1[3]].Text == '') then
buttons[numbers1[3]].Text = module.computer_symbol
message('computer')
break
end
end
end
end
-- check win combinations for player
local numbers = {}
for i, v in pairs(win_combinations_player) do
if v[1] ~= false and v[2] ~= false then
numbers[1] = v[1]
numbers[2] = v[2]
if win_combinations[i][3].Text == '' then
break
end
elseif v[1] ~= false and v[3] ~= false then
numbers[1] = v[1]
numbers[2] = v[3]
if win_combinations[i][2].Text == '' then
break
end
elseif v[2] ~= false and v[3] ~= false then
numbers[1] = v[2]
numbers[2] = v[3]
if win_combinations[i][1].Text == '' then
break
end
end
if v[1] ~= false and v[2] ~= false and v[3] ~= false then
message('player')
end
end
if numbers[1] ~= 0 and numbers[2] ~= 0 then
for i, v in pairs(win_combinations) do
if (tonumber(v[2].Name) == numbers[1] and tonumber(v[1].Name) == numbers[2]) or (tonumber(v[1].Name) == numbers[1] and tonumber(v[2].Name) == numbers[2]) then
numbers[3] = tonumber(v[3].Name)
break
elseif (tonumber(v[3].Name) == numbers[1] and tonumber(v[1].Name) == numbers[2]) or (tonumber(v[1].Name) == numbers[1] and tonumber(v[3].Name) == numbers[2]) then
numbers[3] = tonumber(v[2].Name)
break
elseif (tonumber(v[3].Name) == numbers[1] and tonumber(v[2].Name) == numbers[2]) or (tonumber(v[2].Name) == numbers[1] and tonumber(v[3].Name) == numbers[2]) then
numbers[3] = tonumber(v[1].Name)
break
end
end
end
if toodle == false then
for i, v in pairs(win_combinations_player) do
if (v[1] ~= false and v[2] ~= false) or (v[3] ~= false and v[2] ~= false) or (v[1] ~= false and v[3] ~= false) then
if (v[1] ~= false and v[2] ~= false) and (buttons[numbers[3]].Text == '') then
buttons[numbers[3]].Text = module.computer_symbol
toodle = true
break
elseif (v[3] ~= false and v[2] ~= false) and (buttons[numbers[3]].Text == '') then
buttons[numbers[3]].Text = module.computer_symbol
toodle = true
break
elseif (v[3] ~= false and v[1] ~= false) and (buttons[numbers[3]].Text == '') then
buttons[numbers[3]].Text = module.computer_symbol
toodle = true
break
end
end
end
if toodle == false then
for _, c in pairs(win_combinations) do
if tonumber(c[1].Name) == numbers1[1] and (c[2].Text == '' and c[3].Text == '') then
c[math.random(2,3)].Text = module.computer_symbol
toodle = true
break
elseif tonumber(c[2].Name) == numbers1[1] and (c[1].Text == '' and c[3].Text == '') then
local rd = math.random(1,2)
if rd == 1 then
c[1].Text = module.computer_symbol
else
c[3].Text = module.computer_symbol
end
toodle = true
break
elseif tonumber(c[3].Name) == numbers1[1] and (c[1].Text == '' and c[2].Text == '') then
c[math.random(1,2)].Text = module.computer_symbol
toodle = true
break
end
end
end
if toodle == false then
local empty_cells = {}
for _, v in pairs(buttons) do
if v.Text == '' then
table.insert(empty_cells, v)
end
end
if #empty_cells >= 1 then
empty_cells[math.random(1, #empty_cells)].Text = module.computer_symbol
toodle = true
end
end
end
-- if no one won(draw)
local number_buttons = 0
for _, v in pairs(buttons) do
if v.Text ~= '' then
number_buttons = number_buttons + 1
end
end
for i, v in pairs(win_combinations_computer) do
if (v[1] ~= false and v[2] ~= false) or (v[3] ~= false and v[2] ~= false) or (v[1] ~= false and v[3] ~= false) then
if (v[1] ~= false and v[2] ~= false) then
message('computer')
break
elseif (v[3] ~= false and v[2] ~= false) then
message('computer')
break
elseif (v[3] ~= false and v[1] ~= false) then
message('computer')
break
end
end
end
if toodle == false then
if number_buttons >= #buttons then
message('draw')
end
end
end)
end
Settings(ModuleScript) ;
local sett = {
computer_symbol = 'o', -- change to any symbol
player_symbol = 'x', -- change to any symbol
why_first = true, -- change to "true"-player or "false"-computer
lost_text = 'You lost :(', -- change to any text
win_text = 'You won :)' -- change to any text
}
return sett
I fixed the issue by commenting those lines:
if (v[1] ~= false and v[2] ~= false) then
--message('computer')
break
elseif (v[3] ~= false and v[2] ~= false) then
--message('computer')
break
elseif (v[3] ~= false and v[1] ~= false) then
--message('computer')
break
end
1 Like
Thanks, very much appreciated, worked first try