iOS开发数独小游戏实例
本文实例为大家分享了ios实现数独小游戏的具体代码,供大家参考,具体内容如下
一、界面设计
如下图。图1是用户打开程序后的主页面,点击“开始游戏”后进入游戏页面(图2,图3)。游戏界面上方是显示条,显示玩家当前选中的按钮。当玩家想要在九宫格中填入数字时,只需点击下方1-9的数字控件,显示条上会自动显示玩家当前选中的数字编号,再点击玩家想要填入的位置,就可以填入控件了。
最下方三个按钮分别是“提交”“橡皮擦”“标记”控件。当玩家点击“提交”按钮,系统会自动为玩家结算游戏分数;当玩家点击“橡皮擦”时,可以擦除九宫格上指定位置的数字;当玩家点击“标记”时,可以为九宫格上的格子添加一个小红旗作为标记。游戏界面上方有一个导航条,点击house图标会回到主页面。
二、实现
1. 先搭入口界面,包括一个image view和button,添加一个viewcontroller作为游戏界面,在入口界面和游戏界面之间添加segue,当点击“开始游戏”时跳转到游戏界面。
2.为游戏界面添加必要的控件,用9x9个button实现九宫格,添加9个数字按钮和3个功能控件,为了便于用户使用,需要添加一个显示条。通过用户按下button得知玩家想要在九宫上填入数字的位置坐标,button通过tag识别。
3.关联代码。将九宫格,9个数字按钮,以及3个功能控件与代码关联,同时新建一个board类,board类包含九宫格游戏进度和答案计算等。
// // viewcontroller.swift // import uikit class viewcontroller: uiviewcontroller { var inputnumber: string = " " var board = board() var oldx: int = 0 var oldy: int = 0 var oldvalue: int = 0 @iboutlet weak var display: uilabel! @ibaction func innumber(_ sender: uibutton) { if let num = sender.currenttitle{ switch num { case "" :inputnumber = "" ;display.text = "" case "1" :inputnumber = "1" ;display.text = "1" case "2" :inputnumber = "2" ;display.text = "2" case "3" :inputnumber = "3" ;display.text = "3" case "4" :inputnumber = "4" ;display.text = "4" case "5" :inputnumber = "5" ;display.text = "5" case "6" :inputnumber = "6" ;display.text = "6" case "7" :inputnumber = "7" ;display.text = "7" case "8" :inputnumber = "8" ;display.text = "8" case "9" :inputnumber = "9" ;display.text = "9" default: break } } } @ibaction func setnumber(_ sender: uibutton) { let pushtag: int = sender.tag let highttag: int = pushtag / 10 let lowtag: int = pushtag % 10 if board.question[highttag - 1][lowtag - 1] == 0 { sender.settitle(inputnumber, for: uicontrol.state.normal) board.change(x: highttag - 1, y: lowtag - 1, value: int(inputnumber) ?? 0) oldx = highttag - 1 oldy = lowtag - 1 oldvalue = int(inputnumber) ?? 0 } } @ibaction func eraser(_ sender: uibutton) { inputnumber = " " display.text = "橡皮擦" } @ibaction func finish(_ sender: uibutton) { if board.check() { display.text = "you win!" } else { display.text = "score: \(board.score)" } } @ibaction func backhome(_ sender: uibarbuttonitem) { dismiss(animated: true, completion: nil) } override func viewdidload() { super.viewdidload() // do any additional setup after loading the view. display.text = "begin" } }
// // board.swift // import uikit class board{ var score: int = 0 var question: [[int]] = [[0,0,0,1,8,0,9,3,0], [1,8,6,0,0,0,0,0,7], [0,5,0,7,4,0,0,0,0], [5,0,4,0,0,0,3,0,6], [0,0,0,3,9,4,0,0,2], [2,3,0,0,0,0,0,1,0], [0,0,9,0,0,3,0,0,8], [0,0,2,9,0,5,7,0,0], [0,7,0,0,0,8,0,9,0],] var nowboard: [[int]] = [[0,0,0,1,8,0,9,3,0], [1,8,6,0,0,0,0,0,7], [0,5,0,7,4,0,0,0,0], [5,0,4,0,0,0,3,0,6], [0,0,0,3,9,4,0,0,2], [2,3,0,0,0,0,0,1,0], [0,0,9,0,0,3,0,0,8], [0,0,2,9,0,5,7,0,0], [0,7,0,0,0,8,0,9,0],] var answer: [[int]] = [[4,2,7,1,8,6,9,3,5], [1,8,6,5,3,9,2,4,7], [9,5,3,7,4,2,6,8,1], [5,9,4,8,2,1,3,7,6], [7,6,1,3,9,4,8,5,2], [2,3,8,6,5,7,4,1,9], [6,1,9,4,7,3,5,2,8], [8,4,2,9,1,5,7,6,3], [3,7,5,2,6,8,1,9,4],] func getnum(x: int, y: int) -> int { return nowboard[x][y] } func change(x: int, y: int, value: int) { nowboard[x][y] = value } func check() -> bool { var countcorrect: float = 0 var countempty: float = 0 for i in 0...8{ for j in 0...8 { if nowboard[i][j] == answer[i][j] { countcorrect += 1 } if question[i][j] == 0 { countempty += 1 } } } //score = int(((countques - countcorrect) / countques) * 100) score = int( ((countcorrect + countempty - 81) / countempty) * 100 ) if nowboard == answer { score = 100 return true } else { return false } } }
三、试运行(附gif)
四、评价
优点:程序简单,代码量小,无复杂算法。
缺点:游戏盘面相对固定,未实现数独盘面的随机生成、自动生成数独的解、以及不同难度生成。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: 佛跳墙是什么菜组成的?原来都有这些食材