zoukankan      html  css  js  c++  java
  • Swift写的超级简单的五子棋

    复制下面的代码粘贴到新建的Swift工程的ViewController.swift 中就ok

    //

    //  ViewController.swift

    //  Gobing_Swift

    //

    //  Created by dongqiangfei on 16/3/17.

    //  Copyright © 2016年 dongqiangfei. All rights reserved.

    //

    import UIKit

    class ViewController: UIViewController {

        var blackOrWhite : Bool!//用于交替下子

        override func viewDidLoad() {

            super.viewDidLoad()

            self.view.backgroundColor = UIColor.lightGrayColor()

            blackOrWhite = true

            makeQiPan()//做五子棋的棋盘

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

        }

        //做棋盘

        private func makeQiPan() {

            

            var lineNumHor : Int?//水平线的个数

            lineNumHor =  (Int(UIScreen.mainScreen().bounds.size.height) - 64 )/30 + 1

            

            //树脂的

            var lineNumShow : Int //竖直线的个数

            lineNumShow = (Int(UIScreen.mainScreen().bounds.size.width - 15 )/30 ) + 1

            

            for var i = 0 ; i < lineNumHor ; i++ {//水平线

                let lineHoral : UIView!

                lineHoral = UIView.init()

                lineHoral.frame = CGRectMake(15, 64 + CGFloat(Float(i*30)), CGFloat(Float(lineNumShow*30)) - 30 , 1)

                lineHoral.backgroundColor = UIColor.blackColor()

                self.view .addSubview(lineHoral)

            }

            

            for var i = 0; i < lineNumShow; i++ {//竖直线

                let lineShow : UIView!

                lineShow = UIView.init()

                lineShow.frame = CGRectMake(15 + CGFloat(Float(i*30)), 64, 1, CGFloat(Float(lineNumHor!*30)) - 30)

                lineShow.backgroundColor = UIColor.blackColor()

                self.view .addSubview(lineShow)

            }

            

            

        }

        

        override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {// 触摸事件的触发

            //获得触摸的位置

            var touch : UITouch!

            touch = touches.first

            var touchPoint : CGPoint!

            touchPoint = touch.locationInView(self.view)

            print("(touchPoint.x)")

            

            if touchPoint.y < 60 {

                return;

            }

            

            var myView : UIView!

            myView = UIView.init()

            

            //规整触摸的位置在棋盘的交叉点

            var xPoint : Int!

            var yPoint : Int!

            xPoint = (Int(touchPoint.x) - 15 )%30

            if xPoint > 15 {

                xPoint = ((Int(touchPoint.x) - 15)/30)*30 + 15 + 30 - 10

            }else{

                xPoint = ((Int(touchPoint.x) - 15)/30)*30 + 15 - 10

            }

            yPoint = (Int(touchPoint.y) - 64 )%30

            if yPoint > 15 {

                yPoint = ((Int(touchPoint.y) - 64)/30)*30 + 64 + 30 - 10

            }else{

                yPoint = ((Int(touchPoint.y) - 64)/30)*30 + 64 - 10

            }

            

            myView.frame = CGRectMake(CGFloat(Float(xPoint)), CGFloat(Float(yPoint)), 20, 20)

            myView.layer.masksToBounds = true

            myView.layer.cornerRadius = 10

            if (blackOrWhite != nil) {//判断是白色子还是红色的字儿

                myView.backgroundColor = UIColor.blackColor()

                blackOrWhite = nil

            }else {

                myView.backgroundColor = UIColor.whiteColor()

                blackOrWhite = true

            }

            self.view.addSubview(myView)//添加这个子儿

            

        }

        

        

        

        override func didReceiveMemoryWarning() {

            super.didReceiveMemoryWarning()

            // Dispose of any resources that can be recreated.

        }

    }

  • 相关阅读:
    DOM几个场景的优化场景?
    git查看commit提交的内容
    Win10 右键卡顿解决办法
    Unity4中的lightmap怎么在Unity5及其以上版本中使用
    vscode 安装了vetur插件vue html没有智能提示
    vant安装后没有样式
    docker 使用命令
    element ui 第一次点击排序为倒序
    vue强制渲染页面
    vue element el-tooltip自定义样式
  • 原文地址:https://www.cnblogs.com/godlovexq/p/5395118.html
Copyright © 2011-2022 走看看