-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkersViewController.swift
134 lines (107 loc) · 3.98 KB
/
WorkersViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// WorkersViewController.swift
// The Last Viking
//
// Created by Christian Vila on 7/24/17.
// Copyright © 2017 NAPPS. All rights reserved.
//
//Global Variables
var workerTextFieldGlbl:UITextField!
var workerViewGlbl:UIView!
var numOfMiners = 1000
var numOfLumberMen = 1000
var numOfBaker = 1000
var numOfStoneMen = 1000
var workersTableViewGlbl: UITableView!
var workerLabelGlbl:UILabel!
import UIKit
//arays for the tableView
var typesOfWorkers = ["Miners", "Lummber Men", "Stone Men", "Bakers"]
var arrayOfWorkers = [numOfMiners, numOfStoneMen, numOfBaker, numOfLumberMen]
var workerType: String = ""
class WorkersViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
@IBOutlet var trainWorkersTextField: UITextField!
@IBOutlet var workerView: UIView!
@IBOutlet var workerTableView: UITableView!
@IBOutlet var workerLabel: UILabel!
//prototype cell for tableView
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return(typesOfWorkers.count)
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "workersCell", for: indexPath) as! WorkerTableViewCell
cell.typeOfWorker.text = typesOfWorkers[indexPath.row]
cell.numOfWorkers.text = String(arrayOfWorkers[indexPath.row])
return(cell)
}
override func viewDidLoad() {
super.viewDidLoad()
workerTextFieldGlbl = trainWorkersTextField
workerViewGlbl = workerView
workersTableViewGlbl = workerTableView
workerLabel = workerLabelGlbl
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// You're not creating troops youre simply subtracting the populatioon so the only limit you have is the pop..
//Buttons
@IBAction func miners(_ sender: UIButton) {
workerLabel = workerLabelGlbl
workerType = "miners"
workerViewGlbl.isHidden = false
self.checkNil()
// workerLabel.text = "Train Miners"
}
@IBAction func lumberMen(_ sender: UIButton) {
workerType = "lumberMen"
}
@IBAction func stoneMen(_ sender: UIButton) {
workerType = "stoneMen"
}
@IBAction func baker(_ sender: UIButton) {
workerType = "baker"
}
@IBAction func trainWorkerButton(_ sender: UIButton) {
workerTextFieldGlbl = trainWorkersTextField
workerTableView = workersTableViewGlbl
workerLabelGlbl = workerLabel
if workerTextFieldGlbl.text != "" {
self.numOfWorkers()
print(numOfMiners)
}
self.view.endEditing(true)
workerTableView.reloadData()
}
func numOfWorkers() {
let workerInput = Int(trainWorkersTextField!.text!)!
if workerType == "miners" && workerInput <= population {
numOfMiners += workerInput
population -= workerInput
arrayOfWorkers[0] = numOfMiners
}
if workerType == "lumberMen" && workerInput <= population {
numOfLumberMen += workerInput
population -= workerInput
arrayOfWorkers[1] = numOfLumberMen
}
if workerType == "stoneMen" && workerInput <= population {
numOfStoneMen += workerInput
population -= workerInput
arrayOfWorkers[2] = numOfStoneMen
}
}
//Hide Kyboard when touches outside beging
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func checkNil() {
if workerLabelGlbl == nil {
print("label Is Nil")
}
}
}