프로그래밍/IOS

[IOS] 게임: Noughts and Corsses

Gilvert 2015. 12. 22. 19:31
728x90

#1. 목표

- Noughts and Crosses 라는 게임을 만들어 보자.

* Noughts and Corsses란?

O,X 숫자를 넣으면서 연속된 3자리를 먼저 차지하는 사람이 이기는 게임임


#2. 정리


1. 버튼

- 총 9개 블록을 버튼으로 구성함

- gameState[] 배열로 작성 : gameState[0,0,0,0,0,0,0,0,0]

- gameState는 블럭의 상태를 반영함.


2. 사용자 선택

- 사용자가 선택을 하면 activePlayer에 의해서 값이 1 or 2로 선택

- 해당 값이 gameState 값이 전달됨 0에서 변경


3. 승패

- 이길 수 있는 조합 생성:  winning= [[0,1,2], [3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]

- 해당 조합을 알 수 있는 것은 중첩 배열(nested array)를 통해 확인 가능

- for var in winning를 통해 값을 판별

- gameState[combination[0]],gameState[combination[1]],gameState[combination[2]]

- combination[] 값 0,1,2는  8행 3열을 말하는 것임(3행 8열로 출력)

* combination[0] 이라고 하면 0,3,6…..2를 말함, 행의 첫번째를 말함

- for문을 돌려서 확인하고

- gameSate[combination[0]]이 1 이면 Naught의 승리로 봄

* 3행8열로 출력 되기 때문에 activePalyer로 인해 gameState가 1인 상태

즉 처음 시작한 사람이 Nought이기 때문에 승리로 봄


#.3 소스


import UIKit


class ViewController: UIViewController {

    

    var activePlayer = 1 // 1 = noughts, 2 = crosses

    

    var gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0]

    

    let winningCombinations = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]

    

    var gameActive = true


    @IBOutlet var playAgainButton: UIButton!

  

    @IBAction func playAgain(sender: AnyObject) {

        

        gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0]

        

        activePlayer = 1

        

        gameActive = true

        

        gameOverLabel.hidden = true

        

        gameOverLabel.center = CGPointMake(gameOverLabel.center.x - 500, gameOverLabel.center.y)

        

        playAgainButton.hidden = true

        

        playAgainButton.center = CGPointMake(playAgainButton.center.x - 500, playAgainButton.center.y)

        

        var buttonToClear : UIButton

        

        for var i = 0; i < 9; i++ {

            

            buttonToClear = view.viewWithTag(i) as! UIButton

            

            buttonToClear.setImage(nil, forState: .Normal)

            

        }

        

    }

    

    @IBOutlet var button: UIButton!

    

    @IBOutlet var gameOverLabel: UILabel!

    

    @IBAction func buttonPressed(sender: AnyObject) {

        

        if (gameState[sender.tag] == 0 && gameActive == true) {

            

            gameState[sender.tag] = activePlayer

        

            if activePlayer == 1 {

        

                sender.setImage(UIImage(named: "nought.png"), forState: .Normal)

            

                activePlayer = 2

            

            } else {

            

                sender.setImage(UIImage(named: "cross.png"), forState: .Normal)

            

                activePlayer = 1

            

            }

            

            for combination in winningCombinations {

                

                

                

                if (gameState[combination[0]] != 0 && gameState[combination[0]] == gameState[combination[1]] && gameState[combination[1]] == gameState[combination[2]]) {

                    

                    gameActive = false

                    

                    if gameState[combination[0]] == 1 {

                        

                        gameOverLabel.text = "Noughts have won!"

                        

                    } else {

                        

                        gameOverLabel.text = "Crosses have won!"

                        

                    }

                    

                    endGame()

                }

                

            }

            

            if gameActive == true {

            

                gameActive = false

            

                for buttonState in gameState {

                

                    if buttonState == 0 {

                    

                        gameActive = true

                    

                    }

                

                }

            

                if gameActive == false {

                

                    gameOverLabel.text = "It's a draw!"

                

                    endGame()

                

                }

         

            }

        }

        

    }

    

    func endGame() {

    

        gameOverLabel.hidden = false

        playAgainButton.hidden = false

    

        UIView.animateWithDuration(0.5, animations: { () -> Void in

    

            self.gameOverLabel.center = CGPointMake(self.gameOverLabel.center.x + 500, self.gameOverLabel.center.y)

    

            self.playAgainButton.center = CGPointMake(self.playAgainButton.center.x + 500, self.playAgainButton.center.y)

    

        })


    }

    

    

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        

        gameOverLabel.hidden = true

        

        gameOverLabel.center = CGPointMake(gameOverLabel.center.x - 500, gameOverLabel.center.y)

        

        playAgainButton.hidden = true

        

        playAgainButton.center = CGPointMake(playAgainButton.center.x - 500, playAgainButton.center.y)

        

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }



}



#5. 이미지





가우넷 구름 USB 충전식 양면 대용량 보조배터리 손난로, 단일 상품, 화이트



"파트너스 활동을 통해 일정액의 수수료를 제공받을 수 있음"