My code actually works. I was just wondering if there was alternative ways, maybe even more efficient ways of coding my program. This is suppose to be a game of drawing stick, and it's recording the score after each game. If I'm missing anything can someone let me know? Sort of new at this. Here is my code:
import UIKit
import QuartzCore
class PlayerVsPlayerViewController: UIViewController {
var stats = GameStats.sharedStats
var isPlayingAi = false
var ai: SticksAi?
@IBOutlet weak var Player2Score: UILabel!
@IBOutlet weak var Player1Score: UILabel!
@IBOutlet weak var SticksLeft: UILabel!
@IBOutlet weak var SticksSlider: UISlider!
@IBOutlet weak var PlayerWinsLabel: UILabel!
@IBOutlet weak var playerChoiceSticksLabel: UILabel!
@IBOutlet weak var btnStartGame: UIButton!
@IBOutlet weak var NumberOfSticksSegmentCtrl: UISegmentedControl!
@IBOutlet weak var btnStartNewGame: UIButton!
@IBOutlet weak var lblAiChoice: UILabel!
var currentSticks = 0
var sticksTaken = 0
var p1Score = 0
var p2Score = 0
var whoWins = ""
var isPlayerNumber = 1
var originalSticksCount = 0
override func viewDidLoad() {
super.viewDidLoad()
lblAiChoice.isHidden = true
PlayerWinsLabel.text = whoWins
btnStartNewGame.isHidden = true
SticksLeft.text = String(55)
Player1Score.text = String(p1Score)
Player2Score.text = String(p2Score)
SticksSlider.value = 55
NumberOfSticksSegmentCtrl.isEnabled = false
print("Am i playing against an AI: \(isPlayingAi)")
if ai == nil {
ai = SticksAi(withStickCount: currentSticks)
}
}
@IBAction func sliderMoved(_ slider: UISlider) {
let value = lroundf(slider.value)
currentSticks = value
originalSticksCount = value
if isPlayingAi {
ai = SticksAi(withStickCount: currentSticks)
}
SticksLeft.text = String(currentSticks)
}
@IBAction func segmentSlected(_ segment: UISegmentedControl) {
currentSticks -= segment.selectedSegmentIndex + 1
segment.setEnabled(currentSticks >= 3, forSegmentAt: 2)
segment.setEnabled(currentSticks >= 2, forSegmentAt: 1)
SticksLeft.text = String(currentSticks)
segment.selectedSegmentIndex = -1
isPlayerNumber = isPlayerNumber == 1 ? 2 : 1
if isPlayerNumber == 1 {
playerChoiceSticksLabel.text = "Player 1: choose your sticks"
} else {
if isPlayingAi {
aiTurn()
if currentSticks > 0 {
isPlayerNumber = 1
}
} else {
playerChoiceSticksLabel.text = "Player 2: choose your sticks"
}
}
if currentSticks == 0 {
NumberOfSticksSegmentCtrl.isEnabled = false
if isPlayingAi && isPlayerNumber == 2 {
PlayerWinsLabel.text = "AI Wins!"
} else {
PlayerWinsLabel.text = "Player \(isPlayerNumber) Wins!"
}
if isPlayerNumber == 1 {
p1Score += 1
stats.addGame(type: isPlayingAi ? .PlayerVsAi : .PlayerVsPlayer, won: true, sticks: originalSticksCount, playerMoves: 0, opponentMoves: 0)
} else {
p2Score += 1
stats.addGame(type: isPlayingAi ? .PlayerVsAi : .PlayerVsPlayer, won: false, sticks: originalSticksCount, playerMoves: 0, opponentMoves: 0)
}
Player1Score.text = String(p1Score)
Player2Score.text = String(p2Score)
playerChoiceSticksLabel.isHidden = true
}
}
@IBAction func startGame() {
lblAiChoice.isHidden = true
NumberOfSticksSegmentCtrl.isEnabled = true
SticksSlider.isEnabled = false
btnStartGame.isHidden = true
btnStartNewGame.isHidden = false
NumberOfSticksSegmentCtrl.setEnabled(true, forSegmentAt: 2)
NumberOfSticksSegmentCtrl.setEnabled(true, forSegmentAt: 1)
isPlayerNumber = 1
playerChoiceSticksLabel.isHidden = false
}
@IBAction func startNewGame() {
lblAiChoice.isHidden = true
btnStartNewGame.isHidden = true
btnStartGame.isHidden = false
SticksSlider.isEnabled = true
NumberOfSticksSegmentCtrl.isEnabled = false
SticksLeft.text = String(55)
Player1Score.text = String(p1Score)
Player2Score.text = String(p2Score)
SticksSlider.value = 55
currentSticks = Int(SticksLeft.text!)!
}
func aiTurn() {
if isPlayerNumber == 2 && currentSticks > 0 {
let aiMove = ai?.takeTurn(sticksLeft: currentSticks)
currentSticks -= aiMove!
SticksLeft.text = String(currentSticks)
lblAiChoice.isHidden = false
lblAiChoice.text = "AI has chosen \(aiMove!) sticks."
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
@IBAction func closeGame(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
}