1

Word Shuffle App – Corona SDK Word Game Template

Corona SDK Word Game Template Word Shuffle

Today , lets learn Mobile App Development with Corona SDK by making a Word Shuffle Game.

Note : The tutorial teaches you how to built using a stand alone Corona module without using Composer or Storyboard scene management.

If you are new to Corona , you can learn about basic installation of Corona here and start building this Word Shuffle App in next 5 minutes. Corona is easy to learn.

This template works well on Corona SDK Free version also. You donot need a paid subscription of Corona for this template to work.

A Word Shuffle Mobile Application is an app where you have to shuffle the letters shown and make a meaningful world.

You can play some other games at below links

Google Play Store

https://play.google.com/store/apps/developer?id=Smart%20Games%20Fun%20Lab

Amazon Play Store

http://www.amazon.com/s/ref=bl_sr_mobile-apps?_encoding=UTF8&field-brandtextbin=Smart%20Games%20Fun%20Lab&node=2350149011

 

 

Lets see the folder structure

 

Word Shuffle Mobile App

Word Shuffle Mobile App

 

Lets get into core functions and modules.

loadsave.lua  -> This module saves and retrieves the json score file from Documents directory.

Below is the what does simply.

 

local _ = {}
local json = require("json")
local DefaultLocation = system.DocumentsDirectory
local RealDefaultLocation = DefaultLocation
local ValidLocations = {
   [system.DocumentsDirectory] = true,
   [system.CachesDirectory] = true,
   [system.TemporaryDirectory] = true
}

function _.saveTable(t, filename, location)
    if location and (not ValidLocations[location]) then
     error("Attempted to save a table to an invalid location", 2)
    elseif not location then
      location = DefaultLocation
    end
    
    local path = system.pathForFile( filename, location)
    local file = io.open(path, "w")
    if file then
        local contents = json.encode(t)
        file:write( contents )
        io.close( file )
        return true
    else
        return false
    end
end
 
function _.loadTable(filename, location)
    if location and (not ValidLocations[location]) then
     error("Attempted to load a table from an invalid location", 2)
    elseif not location then
      location = DefaultLocation
    end
    local path = system.pathForFile( filename, location)
    local contents = ""
    local myTable = {}
    local file = io.open( path, "r" )
    if file then
        -- read all contents of file into a string
        local contents = file:read( "*a" )
        myTable = json.decode(contents);
        io.close( file )
        return myTable
    end
    return nil
end

function _.changeDefault(location)
	if location and (not location) then
		error("Attempted to change the default location to an invalid location", 2)
	elseif not location then
		location = RealDefaultLocation
	end
	DefaultLocation = location
	return true
end

return _

There are two main functions “loadTable” and “saveTable”

loadTable – Loads the data from the app

saveTable – Saves the data to the app

main.lua – Then lets see about the main.lua

Shuffle The String in the App using shuffleString(str) function.

 

local function shuffleString(inputStr)
	local outputStr = "";
	local strLength = string.len(inputStr);
	
	while (strLength ~= 0) do
		
		local pos = math.random(strLength);		
		outputStr = outputStr..string.sub(inputStr,pos,pos);
		inputStr = inputStr:sub(1, pos-1) .. inputStr:sub(pos+1);
		strLength = string.len(inputStr);
	end
	
	return outputStr;
end

Below function is used to shuffle the array

function shuffle(t1)
  local n1 = #t1
  while n1 >= 2 do
    local k1 = math.random(n1)
    t1[n1], t1[k1] = t1[k1], t1[n1]
    n1 = n1 - 1
 end
 return t1
end

All the words that are used for shuffling are read from a text file in App Resource Directory as below.

function readTextFile()
	local path = system.pathForFile( wordListFile, system.ResourceDirectory)
	local file = io.open( path, "r" )
	local findSemiColon

	for i=1,#wordList do
		table.remove( wordList[i] )
	end

	for line in file:lines() do
		 line = string.sub(line, 
       		1, #line);
		 findSemiColon = string.find( line, ";" )
		 line =  line:sub(1,findSemiColon-1)
         table.insert(wordList,line)
	end
	io.close( file )
	file = nil
end

For downloading the app and playing , we award some coins as below to start the game.

function checkScoresAndCoins()

	date = os.date( "*t" )    
	
	if(loadsave.loadTable(gameTable) == nil) then
		currentScore = coinsForNewGamers
		userData.coinCount = currentScore
		userData.dateSave = date.year..date.month..date.day
		userData.saveQuestion = 0
		loadsave.saveTable(userData, gameTable)
		lastSavedQuestion = getLastSavedQuestion()
		local puzzleFinishedSound = audio.loadSound( audioPuzzleSolvedSound )
		puzzleAudioChannel = audio.play( puzzleFinishedSound,{ loops=0, duration=3000, fadein=2000 } )
		local alert = native.showAlert( "Welcome", "We just awarded you "..coinsForNewGamers.." coins for playing the game. Come back daily for more coins.", {"OK"})
	else
		--addScoreAndCoins(eachPuzzleCoinValue)
		lastSavedQuestion = getLastSavedQuestion()

	end    
	return currentScore
end

Every time the user comes back to play , the app checks if the user is coming in and playing for the first time of the day. If the user is coming back the next day , we are going to award him 30 coins for playing the game. This function checks the date and awards the coins.

function checkDateAndAddNewCoins()
	addCoinsFlag = 0
	userData = loadsave.loadTable(gameTable)
	lastSavedDate = userData.dateSave

	local date = os.date( "*t" )

	local currentDate 
	currentDate = date.year..date.month..date.day

	if(string.find(lastSavedDate, currentDate) == 1) then
		addCoinsFlag = 0
	else
		addCoinsFlag = 1	
	end

	if(addCoinsFlag == 1) then
		userData.coinCount = userData.coinCount + dailyAddCoins
		wordCounterForTheDay = 1	
    	userData.dateSave = currentDate
    	loadsave.saveTable(userData, gameTable)
    	local puzzleFinishedSound = audio.loadSound( audioPuzzleSolvedSound )
		puzzleAudioChannel = audio.play( puzzleFinishedSound,{ loops=0, duration=3000, fadein=2000 } )
	  	local alert = native.showAlert( "Congrats - Daily Coins", "We just awarded you "..dailyAddCoins.." coins for coming back today", {"OK"})
    end
end

Below two functions reads the data about score/coins and also updates the data.

function addScoreAndCoins(coinx)
	   userData = loadsave.loadTable(gameTable)
	   userData.coinCount = userData.coinCount + coinx
	   loadsave.saveTable(userData, gameTable)
end

function getScoreAndCoins()
	userData = loadsave.loadTable(gameTable)
	return userData.coinCount
end

Then the function getHint() gives the hint to the user by deducting some coins.

Below are the configuration variables

local coinsForNewGamers = 50 – Number of coins to be awarded for downloading the game
local hintCost = 10 – Cost of the hint
local eachPuzzleCoinValue = 20 – Each puzzle value
local dailyAddCoins = 30 – Daily coins ate the coins that gets added for coming back and playing everyday

Download The Template Here

I have spend a considerable time in developing this template and writing this post. If you can spend 1 second , please share this link using the share buttons on this page.

  • disqusUser911

    I think the game does not work properly after I click “hint”. If I click any letters, they just change their position (shuffling).