Hi! I need help with some of these functions. This program is in LUA. Here is my
ID: 3746302 • Letter: H
Question
Hi! I need help with some of these functions. This program is in LUA. Here is my code below, please use this in your coding:
-- game board display layout
xx = display.contentCenterX;
yy = display.contentCenterY;
local zone = display.newRect(xx, yy, 300, 300)
zone.strokeWidth = 2
zone:setFillColor(0,0.2,0)
zone.isSensor = true
local ver = display.newRect(xx, yy, 100, 300)
ver:setFillColor(0,0,0,0)
ver.strokeWidth = 2
local hor = display.newRect(xx, yy, 300, 100)
hor:setFillColor(0,0,1,0)
hor.strokeWidth = 2
--piece
function piece(type, x, y)
if (type==0) then
shape = display.newCircle (x, y, 50)
shape:setFillColor(1,0,0)
else
shape = display.neRect(x, y, 100, 100)
shape.rotation = 45
shape:setFillColor(1,0,0)
end
return shape
end
--these need to be functions
local board = {}
local player = 0
local function zoneHandler(event)
local x, y = event.target:contentToLocal(event.x, event.y)
x = x + 150
y = y + 150
x = math.ceil(x/100)
y = math.ceil(y/100)
if (gameMark(x,y) == false) then
return
end
checkWin()
end
zone:addEventListener("tap", zoneHandler)
function gameMark (x,y)
if (board[x][y] ~= -1) then
return false
end
board [x][y] = player
local _x, _y = zone:localToContent(event.x, event.y)
piece[x][y] = piece(player, _x,_y)
player = (player + 1) % 2
return true
end
local function checkWin()
for i = 1, 3 do
if (board[i][1] == board[i][2] and
board[i][1] == board[i][3] and
board[i][1] ~= -1) then
local winner = tostring(board[i][1]).."wins!";
display.newText(winner, 100, 200, native.systemFont, 40)
end
end
end
For this assignment, you will implement a simple but fully working 2-player tic-tac-toe game supporting a mobile phone. I am providing an incomplete code (main_assignment1.lua in the File page of our class canvas) which includes required functions and you can start this homework by completing the incomplete codes provided by me, or you can write each function from scratch to create this game. Your main task is to complete the tic-tac-two codes according to the following requirements. Again, you can create each function from scratch, but you must implement them as described. FUNCTIONS to be implemented for the app (check CS371Lecture04 Intro to CoronaSDK P2 eventstransitionaroups.pdf in detail) gameBoard() You must draw a 3x3 layout for the tic-tac-toe game board. Adjust the sizes of the board, diamonds and circles (currently the sizes of these game objects do not fit into the general screen).Explanation / Answer
Here is the code for the given problem.
Please comment below for further queries or any modifications regarding the question.
1_PLAYER_HUMAN = true
2_PLAYER_HUMAN = false
BOARD_SIZE = 3
1_PLAYER = "x"
2_PLAYER = "o"
EMPTY_SPACES = " "
HORIZONTAL_SEPARATOR = "-"
VERTICAL_SEPARATOR = " | "
BOARD_SIZE_MAX = 100
if BOARD_SIZE > BOARD_SIZE_MAX then os.exit(0) end
space = {}
for i = 0, (BOARD_SIZE - 1) do
spaces[i] = {}
for j = 0, (BOARD_SIZE - 1) do
spaces[i][j] = nil
end
end
function get_Piece(x, y)
return spaces[x][y]
end
function get_Piece_No_Nil(x, y)
if get_Piece(x, y) ~= nil then
return get_Piece(x, y)
else
return EMPTY_SPACES
end
end
function is_Empty(x, y)
if get_Piece(x, y) == nil then
return true
else
return false
end
end
function place_Piece(x, y, piece)
if is_Empty(x, y) == true then
spaces[x][y] = piece
return true
else
return false
end
end
function is_Game_Over()
if check_Win() == false then
for i = 0, (BOARD_SIZE - 1) do
for j = 0, (BOARD_SIZE - 1) do
if is_Empty(i, j) == true then return false end
end
end
return true
else
return true
end
end
function repeatString(toRepeat, Amountt)
if Amountt <= 0 then return "" end
local to_return = ""
for i = 1, Amountt do
to_return = to_return .. toRepeat
end
return to_return
end
function board_Display()
local WidestPiece = math.max(string.len(1_PLAYER), string.len(2_PLAYER), string.len(EMPTY_SPACES))
io.write(" ")
for i = (BOARD_SIZE - 1), 0, -1 do
local row = "" -- start with an empty row
for j = 0, (BOARD_SIZE - 1) do -- generate that row
local piece = get_Piece_No_Nil(j, i)
row = row .. piece
row = row .. repeatString(" ", WidestPiece - string.len(piece))
if j ~= (BOARD_SIZE - 1) then
row = row .. VERTICAL_SEPARATOR
end
end
io.write(row)
if i ~= 0 then
io.write(" ")
local repeats = math.ceil(string.len(row) / string.len(HORIZONTAL_SEPARATOR))
io.write(repeatString(HORIZONTAL_SEPARATOR, repeats))
io.write(" ")
end
end
io.write(" ")
end
region = {}
region_number = 0
for i = 0, (BOARD_SIZE - 1) do
region[region_number] = {}
for j = 0, (BOARD_SIZE - 1) do
region[region_number][j] = {}
region[region_number][j]["x"] = i
region[region_number][j]["y"] = j
end
region_number = region_number + 1
end
for i = 0, (BOARD_SIZE - 1) do
region[region_num] = {}
for j = 0, (BOARD_SIZE - 1) do
region[region_num][j] = {}
region[region_num][j]["x"] = j
region[region_num][j]["y"] = i
end
region_num = region_num + 1
end
region[region_num] = {}
for i = 0, (BOARD_SIZE - 1) do
region[region_num][i] = {}
region[region_num][i]["x"] = i
region[region_num][i]["y"] = i
end
region_num = region_num + 1
region[region_num] = {}
for i = (BOARD_SIZE - 1), 0, -1 do
region[region_num][i] = {}
region[region_num][i]["x"] = BOARD_SIZE - i - 1
region[region_num][i]["y"] = i
end
region_num = region_num + 1
function get_Region(number)
return region[number]
end
function check_WinInRegion(number)
local to_return = 0
for i, v in pairs(get_Region(number)) do
local piece = get_Piece(v["x"], v["y"])
if piece == 1_PLAYER then to_return = to_return + 1 end
if piece == 2_PLAYER then to_return = to_return - 1 end
end
return to_return
end
function check_Win()
for i in pairs(region) do
local win = check_WinInRegion(i)
if math.abs(win) == BOARD_SIZE then
if win == math.abs(win) then
return 1_PLAYER
else
return 2_PLAYER
end
end
end
return false
end
function human_Play(piece)
io.write(piece .. ", here's the board: ")
board_Display()
local placed = false
while placed == false do
io.write(" Where would you like to play your " .. piece .. "? ")
io.write("Give the X-coordinate (starting with 0). ")
local x = tonumber(io.read())
io.write("Now give the Y-coordinate (starting with 0). ")
local y = tonumber(io.read())
placed = place_Piece(x, y, piece)
if placed == false then
io.write("I'm afraid you can't play there!")
end
end
board_Display()
io.write(" ")
end
function AIPlay(piece)
local me = 0
if piece == 1_PLAYER then me = 1 end
if piece == 2_PLAYER then me = -1 end
for i in pairs(region) do
local win = check_Win_In_Region(i)
if win == ((BOARD_SIZE - 1) * me) then
for j, v in pairs(get_Region(i)) do
if is_Empty(v["x"], v["y"]) == true then
place_Piece(v["x"], v["y"], piece)
return
end
end
end
end
for i in pairs(region) do
local win = check_Win_In_Region(i)
if win == ((BOARD_SIZE - 1) * (me * -1)) then
for j, v in pairs(get_Region(i)) do
if is_Empty(v["x"], v["y"]) == true then
place_Piece(v["x"], v["y"], piece)
return
end
end
end
end
for i = 0, (BOARD_SIZE - 1) do
for j = 0, (BOARD_SIZE - 1) do
if place_Piece(i, j, piece) ~= false then return end
end
end
end
io.write("Welcome to Tic-Tac-Toe! ")
while true do
if is_Game_Over() == true then break end
if 1_PLAYER_HUMAN == true then human_Play(1_PLAYER)
else AIPlay(1_PLAYER) end
if is_Game_Over() == true then break end
if 2_PLAYER_HUMAN == true then human_Play(2_PLAYER)
else AIPlay(2_PLAYER) end
end
io.write("The final board: ")
board_Display()
io.write(" ")
win = check_Win()
if win == false then
io.write("Tie game! ")
else
io.write(win)
io.write(" wins! ")
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.