//
// [Link]
// EmojiPuzzle
//
// Created by iMac on 01/10/22.
//
import UIKit
import GoogleMobileAds
class GameVC: UIViewController {
//MARK: - Properties -
// Type 0
@IBOutlet weak var bottomConstant: NSLayoutConstraint!
@IBOutlet weak var lblLevelCount: UILabel!
@IBOutlet var rightLabel1: PaddingLabel!
@IBOutlet var rightLabel2: PaddingLabel!
@IBOutlet var rightLabel3: PaddingLabel!
@IBOutlet var rightLabel4: PaddingLabel!
@IBOutlet var leftLabel1: PaddingLabel!
@IBOutlet var leftLabel2: PaddingLabel!
@IBOutlet var leftLabel3: PaddingLabel!
@IBOutlet var leftLabel4: PaddingLabel!
@IBOutlet weak var backgroundImageView: UIImageView!
@IBOutlet var rightStackView: UIStackView!
@IBOutlet var leftStackView: UIStackView!
@IBOutlet var leftRightViews: UIView!
@IBOutlet var middleLevelViews: UIView!
// Type 1
@IBOutlet weak var collectionView: UICollectionView! {
didSet {
[Link] = self
[Link] = self
[Link] = self
}
}
@IBOutlet weak var collectionView2: UICollectionView!{
didSet {
[Link] = self
[Link] = self
[Link] = self
}
}
@IBOutlet var dragViews: [UIView]!
private lazy var lineShape: CAShapeLayer = {
let lineShape = CAShapeLayer()
lineShape×strokeColor = [Link]
lineShape×fillColor = [Link]
[Link] = 2.0
return lineShape
}()
private var panGestureStartPoint: CGPoint = .zero
private lazy var leftPanRecognizer1: UIPanGestureRecognizer = {
return UIPanGestureRecognizer(target: self,
action: #selector(leftPanGestureCalled(_:)))
}()
private lazy var leftPanRecognizer2: UIPanGestureRecognizer = {
return UIPanGestureRecognizer(target: self,
action: #selector(leftPanGestureCalled(_:)))
}()
private lazy var leftPanRecognizer3: UIPanGestureRecognizer = {
return UIPanGestureRecognizer(target: self,
action: #selector(leftPanGestureCalled(_:)))
}()
private lazy var leftPanRecognizer4: UIPanGestureRecognizer = {
return UIPanGestureRecognizer(target: self,
action: #selector(leftPanGestureCalled(_:)))
}()
var imageArray: [UIImage?] = [
UIImage(named: "bg1"),
UIImage(named: "bg5"),
UIImage(named: "bg7"),
UIImage(named: "bg8"),
UIImage(named: "bg9"),
UIImage(named: "bg11")
]
var adBannerView: GADBannerView?
var selectedView = [String: String]()
var correctAnswerArray = [String: String]()
var connectionLines = [CAShapeLayer]()
var selectedIndex = [Link]
var leftViews:[UILabel] = []
var rightViews:[UILabel] = []
var questions:[String] = []
var answers:[String] = []
var originalQuesArray:[String] = []
var originalAnsArray:[String] = []
var isCountFour = false
var levelPassed: [Bool] = []
//MARK: - Life Cycle Functions -
override func viewDidLoad() {
[Link]()
adBannerView = GADBannerView(adSize:
kGADAdSizeSmartBannerLandscape)
initAdMobBanner()
changeBackgroundImages()
[Link] = ([Link]([Link],
to: [Link]))
[Link](leftPanRecognizer1)
[Link](leftPanRecognizer2)
[Link](leftPanRecognizer3)
[Link](leftPanRecognizer4)
[Link]()
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
{
for (index,labelPoint) in [Link]() {
if !((selectedView["left\(index+1)"]?.contains("right") ?? false)
|| selectedView["left\(index+1)"] == "0") {
[Link] = 0
[Link] = [Link]
}
}
}
//MARK: - Selectors -
@IBAction func backButtonAction(_ sender: UIButton) {
[Link](true)
[Link](gViewController: self)
[Link]?.popViewController(animated: true)
}
@IBAction func reloadButtonAction(_ sender: UIButton) {
[Link](true)
}
@IBAction func settingsButtonAction(_ sender: UIButton) {
[Link](true)
[Link](gViewController: self)
let newVC = [Link]?.instantiateViewController(withIdentifier:
"SettingAlertVC") as! SettingAlertVC
[Link] = self
[Link] = UIColor(white: 0,
alpha: 0.5)
[Link] = true
[Link] = true
[Link] = .overFullScreen
[Link](newVC, animated: true, completion: nil)
}
@objc func leftPanGestureCalled(_ panRecognizer:
UIPanGestureRecognizer) {
let currentPanPoint = panRecognizer×location(in: [Link])
switch [Link] {
case .began:
[Link](panRecognizer,
currentPanPoint)
case .changed:
[Link](panRecognizer,
currentPanPoint)
case .ended:
[Link](panRecognizer)
default:
break
}
}
}
//MARK: - Helper Funtions -
extension GameVC {
// Change Background Images
func changeBackgroundImages() {
[Link] {
if let image = self×imageArray×randomElement() {
self×backgroundImageView×image = image
}
}
}
// Pan Gesture Began
func panGestureBegan(_ panRecognizer: UIPanGestureRecognizer,
_ currentPanPoint: CGPoint) {
var newIndex = 0
for (index,labelPoint) in [Link]() {
let point = [Link](in: labelPoint)
if [Link](point){
[Link] = 10
[Link] = 3
[Link] = [Link]
panGestureStartPoint = currentPanPoint
if selectedView["left\(index+1)"] == "" {
selectedView["left\(index+1)"] = "0"
}else{
newIndex = index+1
}
}
}
for (index,labelPoint) in [Link]() {
if !((selectedView["left\(index+1)"]?.contains("right") ?? false) ||
selectedView["left\(index+1)"] == "0") {
[Link] = 0
[Link] = [Link]
}else {
let newKeyValue = selectedView["left\(newIndex)"]
for line in connectionLines {
if [Link] == "left\(newIndex)" {
if [Link] == newKeyValue {
selectedView["left\(newIndex)"] = "0"
[Link]()
}
}
}
}
}
[Link](lineShape)
}
// Pan Gesture Changed
func panGestureChanged(_ panRecognizer: UIPanGestureRecognizer,
_ currentPanPoint: CGPoint) {
let linePath = UIBezierPath()
[Link](to: panGestureStartPoint)
[Link](to: currentPanPoint)
lineShape×path = [Link](from: panGestureStartPoint,
to: currentPanPoint,
barThickness: 1.0,
bellRadius: 3.0)
for (indx,labelPoint) in [Link]() {
let point = [Link](in: labelPoint)
if [Link](point){
lineShape×path = [Link](from: panGestureStartPoint,
to: [Link]([Link],
to: [Link]),
barThickness: 1.0,
bellRadius: 3.0)
[Link] = 3
[Link] = [Link]
} else {
if !([Link]("right\(indx+1)")) {
[Link] = 0
[Link] = [Link]
}
}
}
}
// Pan Gesture Ended
func panGestureEnded(_ panRecognizer: UIPanGestureRecognizer){
var isViewAdded = false
var newIndex = 0
for (indx,labelPoint) in [Link]() {
let point = [Link](in: labelPoint)
if [Link](point) {
[Link] = 3
[Link] = [Link]
isViewAdded = true
// green
for (index, label) in [Link]() {
if (selectedView["left\(index+1)"] == "0") {
newIndex = index+1
selectedView["left\(index+1)"] = "right\(indx+1)"
addLine(fromPoint: [Link]([Link], to:
[Link]), toPoint: [Link]([Link], to: [Link]),
color: .green, value: "right\(indx+1)", value2: "left\(index+1)")
}
}
lineShape×path = nil
[Link]()
}
}
if !isViewAdded {
lineShape×path = nil
[Link]()
for (index, _) in [Link]() {
if (selectedView["left\(index+1)"] == "0") {
selectedView["left\(index+1)"] = ""
}
}
} else {
let newKeyValue = selectedView["left\(newIndex)"]
for (_,item) in [Link]() {
if "left\(newIndex)" != item {
if (newKeyValue == selectedView[item]) {
selectedView[item] = ""
for line in connectionLines {
if [Link] != "left\(newIndex)" {
if [Link] == newKeyValue {
[Link]()
}
}
}
}
}
}
for (index,labelPoint) in [Link]() {
if !((selectedView["left\(index+1)"]?.contains("right") ?? false)
|| selectedView["left\(index+1)"] == "0") {
[Link] = 0
[Link] = [Link]
}
}
}
var isGameFinish = true
for i in selectedView {
if [Link] == "" {
isGameFinish = false
}
}
if isGameFinish {
for correct in correctAnswerArray {
[Link](selectedView[[Link]] == [Link])
if !(selectedView[[Link]] == [Link]) {
for line in connectionLines {
if [Link] == selectedView[[Link]] &&
[Link] == [Link] {
[Link] = [Link]
[Link] = [Link]
}
}
}
}
[Link](true)
let newVC = [Link]?.instantiateViewController(withIdentifier:
"SuccessAlertVC") as! SuccessAlertVC
[Link] = 
[Link] = self
[Link] = UIColor(white: 0,
alpha: 0.5)
[Link] = true
[Link] = true
[Link] = .overFullScreen
[Link] {
[Link](newVC,
animated: true,
completion: nil)
}
}
}
func levelSuccessAndFailed(count: Int) {
[Link](gViewController: self)
for item in connectionLines {
for (i,_) in [Link]() {
if [Link] == "left\(i+1)" {
[Link]()
}
}
}
isCountFour = false
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
[Link]×borderColor = [Link]
[Link]×borderColor = [Link]
[Link]×borderColor = [Link]
[Link]×borderColor = [Link]
[Link]×borderColor = [Link]
[Link]×borderColor = [Link]
[Link]×borderColor = [Link]
[Link]×borderColor = [Link]
[Link] = count
[Link]()
}
// Add Line
func addLine(fromPoint start: CGPoint,
toPoint end:CGPoint,
color: UIColor,
value: String,
value2: String) {
let line = CAShapeLayer()
let linePath = UIBezierPath()
[Link](to: start)
[Link](to: end)
line×strokeColor = [Link]
line×fillColor = [Link]
line×path = [Link](from: start, to: end, barThickness: 1.0,
bellRadius: 3.0)
[Link] = 2.0
line×accessibilityLabel = value
line×accessibilityValue = value2
[Link](line)
[Link](line)
}
// Game View Manages
func viewManageForLevel() {
[Link](gViewController: self)
[Link] = "Level \(selectedIndex+1)"
[Link] = gameData[selectedIndex].levelType != "0"
[Link] = gameData[selectedIndex].levelType == "0"
for i in dragViews {
[Link] = [Link]
}
originalAnsArray = gameData[selectedIndex].answer
originalQuesArray = gameData[selectedIndex].question
if gameData[selectedIndex].levelType == "0" {
if gameData[selectedIndex].[Link] == 2 {
[Link] = false
[Link] = false
[Link] = true
[Link] = true
[Link] = false
[Link] = false
[Link] = true
[Link] = true
leftViews = [leftLabel1, leftLabel2]
rightViews = [rightLabel1, rightLabel2]
questions = gameData[selectedIndex].question
answers = gameData[selectedIndex].[Link]().shuffled()
[Link] = questions[0]
[Link] = questions[1]
[Link] = answers[0]
[Link] = answers[1]
for (index, _) in [Link]() {
selectedView["left\(index+1)"] = ""
}
for (index, item) in [Link]() {
for (index2,ans) in [Link]() {
if item == ans {
correctAnswerArray["left\(index+1)"] = "right\(index2+1)"
}
}
}
}
else if gameData[selectedIndex].[Link] == 3 {
[Link] = false
[Link] = false
[Link] = false
[Link] = true
[Link] = false
[Link] = false
[Link] = false
[Link] = true
leftViews = [leftLabel1, leftLabel2, leftLabel3]
rightViews = [rightLabel1, rightLabel2, rightLabel3]
questions = gameData[selectedIndex].question
answers = gameData[selectedIndex].[Link]()
[Link] = questions[0]
[Link] = questions[1]
[Link] = questions[2]
[Link] = answers[0]
[Link] = answers[1]
[Link] = answers[2]
for (index, _) in [Link]() {
selectedView["left\(index+1)"] = ""
}
for (index, item) in [Link]() {
for (index2,ans) in [Link]() {
if item == ans {
correctAnswerArray["left\(index+1)"] = "right\(index2+1)"
}
}
}
}
else if gameData[selectedIndex].[Link] == 4 {
[Link] = false
[Link] = false
[Link] = false
[Link] = false
[Link] = false
[Link] = false
[Link] = false
[Link] = false
leftViews = [leftLabel1, leftLabel2, leftLabel3, leftLabel4]
rightViews = [rightLabel1, rightLabel2, rightLabel3, rightLabel4]
questions = gameData[selectedIndex].question
answers = gameData[selectedIndex].[Link]().shuffled()
[Link] = questions[0]
[Link] = questions[1]
[Link] = questions[2]
[Link] = questions[3]
[Link] = answers[0]
[Link] = answers[1]
[Link] = answers[2]
[Link] = answers[3]
for (index, _) in [Link]() {
selectedView["left\(index+1)"] = ""
}
for (index, item) in [Link]() {
for (index2,ans) in [Link]() {
if item == ans {
correctAnswerArray["left\(index+1)"] = "right\(index2+1)"
}
}
}
} else {
[Link] = false
[Link] = true
[Link] = true
[Link] = true
[Link] = false
[Link] = true
[Link] = true
[Link] = true
leftViews = [leftLabel1]
rightViews = [rightLabel1]
questions = gameData[selectedIndex].question
answers = gameData[selectedIndex].[Link]().shuffled()
[Link] = questions[0]
[Link] = answers[0]
for (index, _) in [Link]() {
selectedView["left\(index+1)"] = ""
}
for (index, item) in [Link]() {
for (index2,ans) in [Link]() {
if item == ans {
correctAnswerArray["left\(index+1)"] = "right\(index2+1)"
}
}
}
}
} else {
questions = gameData[selectedIndex].question
answers = gameData[selectedIndex].[Link]()
print(questions, answers)
[Link]()
[Link]()
}
}
}
//MARK: - Extensions -
extension GameVC: SettingAlertProtocol {
func view(_ vc: SettingAlertVC, didTapPrivacyPolicyButtonAt sender:
UIButton) {
[Link](true)
[Link](gViewController: self)
let newVC = [Link]?.instantiateViewController(withIdentifier:
"PrivacyPolicyVC") as! PrivacyPolicyVC
[Link]?.pushViewController(newVC, animated: true)
}
}
extension GameVC: SuccessAlertDelegate {
func view(_ vc: SuccessAlertVC, didTapButtonAt sender: UIButton, isPassed:
Bool) {
[Link](animated: true, completion: nil)
[Link]()
if isPassed {
let count = self×selectedIndex+1
[Link] = count
levelSuccessAndFailed(count: count)
} else {
[Link] = [Link]
levelSuccessAndFailed(count: [Link])
}
}
}
extension GameVC: UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
if collectionView == [Link] {
return [Link]
}
return [Link]
}
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView×dequeueReusableCell(withReuseIdentifier: "Cell",
for: indexPath) as! LevelCVCell
if collectionView == [Link] {
if [Link] == ([Link]-1) {
cell×lblStricker×text = nil
}else {
cell×lblStricker×text = questions[[Link]]
}
} else {
cell×lblStricker×text = answers[[Link]]
}
return cell
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
if collectionView == [Link] {
return CGSize(width: [Link]/2, height:
[Link]/2)
}else{
return CGSize(width: [Link], height:
[Link]/3)
}
}
}
// MARK: - UICollectionViewDragDelegate Methods -
extension GameVC : UICollectionViewDragDelegate {
func collectionView(_ collectionView: UICollectionView, itemsForBeginning
session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
let item = answers[[Link]]
let itemProvider = NSItemProvider(object: item as NSString)
let dragItem = UIDragItem(itemProvider: itemProvider)
[Link] = DragCoordinator(sourceIndexPath: indexPath)
dragItem×localObject = item
return [dragItem]
}
func collectionView(_ collectionView: UICollectionView, itemsForAddingTo
session: UIDragSession, at indexPath: IndexPath, point: CGPoint) ->
[UIDragItem] {
let item = answers[[Link]]
let itemProvider = NSItemProvider(object: item as NSString)
let dragItem = UIDragItem(itemProvider: itemProvider)
dragItem×localObject = item
return [dragItem]
}
func collectionView(_ collectionView: UICollectionView,
dragPreviewParametersForItemAt indexPath: IndexPath) ->
UIDragPreviewParameters? {
let previewParameters = UIDragPreviewParameters()
previewParameters×backgroundColor = .clear
return previewParameters
}
func collectionView(_ collectionView: UICollectionView, dragSessionDidEnd
session: UIDragSession) {
guard
let dragCoordinator = [Link] as? DragCoordinator,
[Link] == true,
[Link] == false
else {
return
}
let sourceIndexPath = [Link]
[Link]({
print("nameCollection Drag:", sourceIndexPath)
})
}
// MARK: - UICollectionViewDropDelegate Methods -
extension GameVC : UICollectionViewDropDelegate {
func collectionView(_ collectionView: UICollectionView, canHandle session:
UIDropSession) -> Bool {
return [Link](ofClass: [Link])
}
func collectionView(_ collectionView: UICollectionView,
dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath
destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
if let destinationIndexPath = destinationIndexPath {
if ([Link]) < 3 {
return UICollectionViewDropProposal(operation: .cancel)
}
}
if isCountFour {
return UICollectionViewDropProposal(operation: .cancel)
}
return UICollectionViewDropProposal(
operation: .move,
intent: .insertIntoDestinationIndexPath)
}
func collectionView(_ collectionView: UICollectionView, performDropWith
coordinator: UICollectionViewDropCoordinator) {
let destinationIndexPath: IndexPath
if let indexPath = coordinator×destinationIndexPath
{
destinationIndexPath = indexPath
}
else
{
// Get last index path of table view.
let section = [Link] - 1
let row = collectionView×numberOfItems(inSection: section)
destinationIndexPath = IndexPath(row: row, section: section)
}
switch [Link]
{
case .move:
guard let dragCoordinator =
[Link]?.localContext as?
DragCoordinator
else { return }
_ = [Link]
[Link](coordinator: coordinator, destinationIndexPath:
destinationIndexPath, collectionView: collectionView)
[Link] = true
break
case .copy:
[Link](coordinator: coordinator, destinationIndexPath:
destinationIndexPath, collectionView: collectionView)
default:
return
}
}
}
extension GameVC {
private func copyItems(coordinator: UICollectionViewDropCoordinator,
destinationIndexPath: IndexPath, collectionView: UICollectionView) {
[Link]({
var indexPaths = [IndexPath]()
for (index, item) in [Link]() {
let indexPath = IndexPath(row: [Link] + index,
section: [Link])
if collectionView == [Link] {
guard let dragCoordinator =
[Link]?.localContext as?
DragCoordinator
else { return }
[Link](at: [Link])
[Link](at: [Link])
[Link]([Link] as! String,
at: [Link])
if questions×count == 4 {
isCountFour = true
}else{
isCountFour = false
}
}
[Link](indexPath)
}
[Link](at: indexPaths)
print(answers, questions, originalQuesArray)
[Link](true)
let newVC = [Link]?.instantiateViewController(withIdentifier:
"SuccessAlertVC") as! SuccessAlertVC
[Link] = (questions == originalQuesArray)
[Link] = self
[Link] = UIColor(white: 0, alpha: 0.5)
[Link] = true
[Link] = true
[Link] = .overFullScreen
[Link] {
[Link](newVC, animated: true, completion: nil)
}
})
}
}
// MARK: - Banner Ads Delegate Methods
extension GameVC: GADBannerViewDelegate {
// Banner
func initAdMobBanner() {
adBannerView?.adSize = GADAdSizeFromCGSize(CGSize(width:
[Link], height: bannerHeight))
adBannerView?.frame = CGRect(x: 0, y: [Link]-
bannerHeight-bottomSpacenotBar, width: [Link], height:
bannerHeight)
adBannerView?.adUnitID = kGoogleBannerAppUnitID()
adBannerView?.rootViewController = self
adBannerView?.delegate = self
[Link](adBannerView!)
let request = GADRequest()
//request×testDevices = myTestDevices
adBannerView?.load(request)
}
// Hide the banner
func hideBanner(_ banner: UIView) {
[Link]("hideBanner", context: nil)
[Link] = CGRect(x: [Link]/2 -
[Link]/2, y: [Link]-bottomSpacenotBar -
[Link], width: [Link], height:
[Link])
[Link]()
[Link] = true
}
// Show the banner
func showBanner(_ banner: UIView) {
[Link]("showBanner", context: nil)
[Link] = CGRect(x: [Link]/2 -
[Link]/2, y: [Link]-bottomSpacenotBar -
[Link], width: [Link], height:
[Link])
[Link]()
[Link] = false
}
// AdMob banner available
func bannerViewDidReceiveAd(_ bannerView: GADBannerView) {
[Link] = bannerHeight
[Link]()
showBanner(adBannerView!)
}
func bannerView(_ bannerView: GADBannerView,
didFailToReceiveAdWithError error: Error) {
print("error :",[Link])
hideBanner(adBannerView!)
}
}