澳门至尊网站-首页

您的位置:澳门至尊网站 > 免费资源 > 2048小游戏4X4C语言

2048小游戏4X4C语言

2019-10-29 01:10

图片 1图片 2

1.angular.bind()

  1 /*2048*/
  2 
  3 #include<stdio.h>
  4 #include<stdlib.h>
  5 #include<conio.h>
  6 #include<time.h>
  7 
  8 void print(void);/*显示游戏界面*/
  9 int add(void);/*对中间变量数组进行处理*/
 10 
 11 int code[4][4] =
 12    {
 13     {0,0,0,0},
 14     {0,0,0,0},
 15     {0,0,0,0},
 16     {0,0,0,0},
 17    };/*游戏中的16个格子*/
 18 int temp[5];/*中间变量*/
 19 int move = 0;/*移动次数*/
 20 int score = 0;/*分数*/
 21 
 22 
 23 
 24 int main(void)
 25 {
 26      int gameover = 0;/*判断游戏是否结束,1结束,0继续*/
 27      int i,j;
 28      int change = 1;/*判断格子中的数是否改变,0不变*/
 29      char input;
 30      srand((unsigned)time(NULL));/*设置随机数的起点*/
 31      while(gameover == 0)
 32          {
 33               if(change >= 1)/*仅当数发生改变时添加新数*/
 34                {
 35                     do
 36                      {
 37                           i=((unsigned)rand()) % 4;
 38                           j=((unsigned)rand()) % 4;
 39                      }while(code[i][j] != 0);
 40                     if(((unsigned)rand()) % 4 == 0)
 41                      {
 42                           code[i][j]=4;
 43                      }
 44                     else
 45                      {
 46                           code[i][j]=2;/*随机选一个空格填上2或4*/
 47                      }
 48                     move++;/*增加次数*/
 49                }
 50               print();/*显示*/
 51               input = getch();/*输入方向*/
 52               change = 0;
 53               switch(input)
 54                    {
 55                         case '0':/*退出*/
 56                                  printf("Are you sure to exit?(y/n)");
 57                                  input=getchar();
 58                                  if(input=='y'||input=='Y')
 59                                     exit(0);
 60                                  break;
 61 
 62                         case 'W':
 63 
 64                         case 'w':/*上*/
 65                                  for(j=0;j<=3;j++)
 66                                    {
 67                                         for(i=0;i<=3;i++)
 68                                           {
 69                                                temp[i]=code[i][j];/*把一列数移到中间变量*/
 70                                           }
 71                                         temp[4]=0;
 72                                         change += add();
 73                                         for(i=0;i<=3;i++)
 74                                           {
 75                                                code[i][j]=temp[i];/*把处理好的中间变量移回来*/
 76                                           }
 77                                     }
 78                                   break;
 79 
 80                         case 'A':
 81 
 82                         case 'a':/*左*/
 83                                  for(i=0;i<=3;i++)
 84                                    {
 85                                         for(j=0;j<=3;j++)
 86                                           {
 87                                                temp[j]=code[i][j];/*把一行数移到中间变量*/
 88                                           }
 89                                         temp[4]=0;
 90                                         change += add();
 91                                         for(j=0;j<=3;j++)
 92                                           {
 93                                                code[i][j]=temp[j];/*把处理好的中间变量移回来*/
 94                                           }
 95                                    }
 96                                  break;
 97 
 98                         case 'S':
 99 
100                         case 's':/*下*/
101                                  for(j=0;j<=3;j++)
102                                    {
103                                         for(i=0;i<=3;i++)
104                                           {
105                                                temp[i]=code[3-i][j];/*把一列数移到中间变量*/
106                                           }
107                                         temp[4]=0;
108                                         change += add();
109                                         for(i=0;i<=3;i++)
110                                           {
111                                                code[3-i][j]=temp[i];/*把处理好的中间变量移回来*/
112                                           }
113                                    }
114                                  break;
115 
116                         case 'D':
117 
118                         case 'd':/*右*/
119                                  for(i=0;i<=3;i++)
120                                    {
121                                         for(j=0;j<=3;j++)
122                                           {
123                                                temp[j]=code[i][3-j];/*把一行数移到中间变量*/
124                                           }
125                                         temp[4]=0;
126                                         change += add();
127                                         for(j=0;j<=3;j++)
128                                           {
129                                                code[i][3-j]=temp[j];/*把处理好的中间变量移回来*/
130                                           }
131                                    }
132                                  break;
133                    }
134               gameover=1;
135               for(i=0;i<=3;i++)
136               for(j=0;j<=3;j++)
137               if(code[i][j]==0)
138               gameover=0;/*所有格子都填满则游戏结束*/
139         }
140     printf("Game over!n");
141     getch();
142 
143     return 0;
144 }
145 
146 void print(void)/*显示游戏界面*/
147 {
148     int i,j;
149     system("CLS");/*清屏*/
150     printf("2048n");
151     printf("W--UP A--LEFT S--DOWN D--RIGHT 0--EXITn");
152     printf("Score:%d Move:%dn",score,move);
153     printf("Made by lanshanxiaon");
154     printf("|-----------------------|n");/*显示横向分隔线*/
155     for(i=0;i<=3;i++)
156        {
157            for(j=0;j<=3;j++)
158               {
159                    if(code[i][j]==0)
160                      {
161                           printf("|     ");/*0显示空格*/
162                      }
163                    else
164                      {
165                           printf("|%5d",code[i][j]);/*显示数字和分隔线*/
166                      }
167               }
168            printf("|n|-----------------------|n");/*显示横向分隔线*/
169        }
170 }
171 
172 int add(void)/*对中间变量数组进行处理*/
173 {
174     int i;
175     int t=0;
176     int change=0;/*判断数组是否有改变,0不变,1变化*/
177     do
178      {
179           for(i=0;i<=3;i++)
180             {
181                  if(temp[i]==0)
182                   {
183                        if(temp[i]!=temp[i+1])
184                          change=1;/*当一个0后面不是0时数组改变*/
185                        temp[i]=temp[i+1];
186                        temp[i+1]=0;
187                   }
188             }/*去掉中间的0*/
189           t++;
190      }while(t<=3);/*重复多次*/
191     for(i=1;i<=3;i++)
192         {
193              if(temp[i]==temp[i-1])
194               {
195                    if(temp[i]!=0)
196                      {
197                           change=1;/*当两个非零相同的数相加时数组改变*/
198                           score=score+temp[i];/*加分*/
199                      }
200                    temp[i-1]=temp[i-1]*2;
201                    temp[i]=0;
202               }
203         }/*把两个相邻的相同的数加起来*/
204 
205     return change;
206 }

图片 3图片 4

View Code

<!DOCTYPE HTML>
<html ng-app>
<head>
</head>
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</body>
<script>
  /*  angluar.bind(obj,fn,[args]) 把对象绑定到函数里,生成新的函数  */
    var obj={name:'xiaoming'};
    var fn= function (arg) {
        console.log(this.name+' is '+arg);  // this 指 obj
    }
    var f1=angular.bind(obj,fn,'so handsome'); // 参数可选
    f1();
    var f2=angular.bind(obj,fn);          // 可在最后才传参数
    f2('weak!');
</script>
</body>
</html>

2048小游戏代码在上面的View Code中,将加号展开就可以看到。

View Code

点击源码上的复制按键还可以直接将源代码复制下来。

 2.angular.bootstrap()

 

图片 5图片 6

 

<!DOCTYPE HTML>
<meta charset="utf-8">
<head>
</head>
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</body>
<script>
    //   angular.bootstrap(); 手动加载模块,   模仿 ng-app自动加载模块    注意不能出现  ng-app
    //创建模块1
  var module1=angular.module('module1',[]);
    module1.controller('controller1', function ($scope) {
        $scope.name='haha';
    })
    //创建模块2
  var module2=angular.module('module2',[]);
  module2.controller('controller2', function ($scope) {
      $scope.name='hehe';
  })
    // 页面加载完成后再加载模块
    angular.element(document).ready(function () {
        angular.bootstrap(document.getElementById("div1"),["module1"]);    //  div1加载 模块1
        angular.bootstrap(document.getElementById("div2"),["module2"]);
    })
</script>
<div id="div1" ng-controller="controller1">div1: {{name}}</div>   <!--  这里要有控制器 -->
<div id="div2" ng-controller="controller2">div2: {{name}}</div>
</body>
</meta>

 

View Code

 

 3.angular.element()  应该用 directive

 

图片 7图片 8

 

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
    <style>
        .a{color:red;}
    </style>
</head>
<body>
    <p>12345</p>
    <script>
        var p1=document.getElementsByTagName("p")[0];
        angular.element(p1).addClass('a');
    </script>
</body>
</html>

 

View Code

 

 4. JSON.stringify()  // 对象转成string

 

图片 9图片 10

 

  <script>
        var a={a:1,b:2};
        console.log("xx: "+typeof a+" :"+a);
        var b=JSON.stringify(a);    // 转成string 类型
        console.log("x2x: "+typeof b+" :"+b);
    </script>

View Code

 5.angular.extend(obj2,obj1)  //  把obj1的属性复制给obj2并把结果赋值给obj2  

图片 11图片 12

<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
    <script>
        var obj1={name:'tom'};
        var obj2={sex:'female'};
        var obj_new=angular.extend(obj2,obj1);    // 返回值赋给obj2
        console.log("obj1: "+JSON.stringify(obj1));
        console.log("obj2: "+JSON.stringify(obj2));
        console.log("obj_new: "+JSON.stringify(obj_new));
    </script>

View Code

 6. webStorm 智能提示jq angularjs语法

图片 13

 7.声明 controllers 的推荐的方法是使用数组表示法

图片 14

8.angularjs.forEach()  angularjs.fromJson()

图片 15图片 16

<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
    <script>
        /* 1. angularjs.forEach();  */
        var objs =[{a:1},{a:2}];
        angular.forEach(objs, function(data,index,array){
            //data等价于array[index]
            console.log(data.a+'='+array[index].a);
            console.log("xx: "+data.a);
        });
        /*  2.  angular.fromJson()  */
        var json='{"name":"haha","password":"321"}';
        var jsonStr=angular.fromJson(json);     //  把json转成对象
        console.log("jsonStr: "+typeof jsonStr);
        console.log("jsonStr name: "+jsonStr.name);
    </script>

View Code

 9. ng-class

图片 17图片 18

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        .testCol{color:blue;}
        .testSize{font-size: 30px;}
        .testBorder{border: 1px solid red;}
        .red{color:red;}
    </style>
</head>

<body>
    <div ng-app="app" ng-controller="myCtrl">

    <p ng-class="{testCol:color,testSize:size,testBorder:border}">{{name}}</p>
                    <!--  属性是样式,值是true or false -->
        <p ng-class="{strike: deleted, bold: important, red: error}">Map Syntax Example</p>
        <input type="checkbox" ng-model="deleted"> deleted (apply "strike" class)<br>
        <input type="checkbox" ng-model="important"> important (apply "bold" class)<br>
        <input type="checkbox" ng-model="error"> error (apply "red" class)
        <hr>


        </div>
        <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
        <script>
            angular.module("app",[])
                .controller("myCtrl",["$scope", function ($scope) {
                     $scope.name='haha';
                        $scope.color=true;     /*  true 则应用该样式*/
                        $scope.size=false;
                        $scope.border=true;
                }])
        </script>
</body>
</html>

View Code

 10.ng-copy 复制  ng-paste粘贴  ng-cut 剪切

图片 19图片 20

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>

</head>
<body>
<div ng-app="app" ng-controller="myCtrl">
        <div ng-bind="name"></div>
    <input type="text" ng-model="name" ng-copy="copy()"/>
    </div>
    <script>
        /*触发条件:复制。鼠标右键复制和快捷键Ctrl+C都会触发。*/  
        angular.module("app",[])
            .controller("myCtrl",["$scope", function ($scope) {
                 $scope.name='haha';
                    $scope.copy= function () {
                        alert(1);
                    }
            }])

    </script>
</body>
</html>

View Code

 11.ng-list

图片 21图片 22

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="app" ng-controller="myCtrl">
    <p>ng-list自定义输入数组   a.b.c.d </p>
    <input type="text" ng-model="lists" ng-list="."/><br/>
    {{lists}}
    <li ng-repeat="list in lists">{{list}}</li>
    </div>
    <script>
        angular.module("app",[])
            .controller("myCtrl",["$scope", function ($scope) {
                $scope.lists=['a','b','c'];
            }])
    </script>
</body>
</html>

View Code

 12. angular 获得input的值

图片 23图片 24

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
  <!--<script src="http://apps.bdimg.com/libs/angular.js/1.5.3/angular.min.js"></script>-->
  <script src="//cdn.bootcss.com/angular.js/1.5.3/angular.min.js"></script>
</head>
<body>
<div ng-app="app" ng-controller="myCtrl">
<input type="text" ng-model="orderNum"/>
  <button ng-click="check()">btn</button>
</div>
<script>
  angular.module("app",[])
    .controller("myCtrl", function ($scope) {
      $scope.check= function () {
         alert($scope.orderNum)
      }
    })
</script>
</body>
</html>

View Code

 13.angular factory service

图片 25图片 26

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
  <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="appTest" ng-controller="MyController">
<ul>
  <li ng-repeat="test in tests">
    id: {{test.id}} 名字: {{test.name}}
    信息: {{test.lastText}}
    <button ng-click="remove(test)">delete</button>
  </li>
</ul>
</div>
<script>
  var appTest = angular.module("appTest",[]);
  appTest.factory("myFactory", function () {
      var f={};
      f.remove= function (arr,item) {
        return arr.splice(arr.indexOf(item),1);
      }
      return f;
    });
  appTest.service("myService", function (myFactory) {
      this.remove=function(arr,item){
        return myFactory.remove(arr,item);
      }
    });
  appTest.controller("MyController", function ($scope,$http,myService) {
      $http.get("test.json").success(function (data) {
        $scope.tests=data;
      });
      //    把factory service 删除,直接在控制器中可以实现删除效果
      /* $scope.remove= function (test) {
       $scope.tests.splice($scope.tests.indexOf(test),1);
       }*/
      $scope.remove= function (test) {
         myService.remove($scope.tests,test);
      }
    });
</script>

</body>
</html>

View Code

14.angular 指令(1)

图片 27图片 28

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body ng-app="app">
  <hello-word>12345</hello-word>
  <hello-word></hello-word>
  <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
  <script>
    angular.module("app",[])
      .directive('helloWord', function () {
        return{
          restrict:'E',    //  E 元素  A 属性
          template:'<div>hi, how are yousss<br/></div>',
          replace:true,    /* 隐藏 <hello-word>标签 */
          transclude:true     /* 保留 标签里面的内容: 12345 */
        }
      })
  </script>
</body>
</html>

View Code

15.anguar 指令 link

图片 29图片 30

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
  <style>
    .expander {
      border: 1px solid black;
      width: 250px;
    }

    .expander>.title {
      background-color: black;
      color: white;
      padding: .1em .3em;
      cursor: pointer;
    }

    .expander>.body {
      padding: .1em .3em;
    }
  </style>
</head>
<body ng-app="expanderModule">
<div ng-controller='SomeController'>
  <expander class='expander' expander-title='title'>
    {{text}}
  </expander>

</div>
  <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
  <script>
    var expanderModule=angular.module('expanderModule', [])
    expanderModule.directive('expander', function() {
      return {
        restrict : 'EA',
        replace : true,
        transclude : true,
        scope : {
          title : '=expanderTitle'
        },
        template : '<div>'
        + '<div class="title" ng-click="toggle()">{{title}}</div>'
        + '<div class="body" ng-show="showMe" ng-transclude></div>'
        + '</div>',
        link : function(scope, element, attrs) {
          scope.showMe = false;
          scope.toggle = function toggle() {
            scope.showMe = !scope.showMe;
          }
        }
      }
    });
    expanderModule.controller('SomeController',function($scope) {
      $scope.title = '点击展开';
      $scope.text = '这里是内部的内容。';
    });
  </script>
</body>
</html>

View Code

 

本文由澳门至尊网站发布于免费资源,转载请注明出处:2048小游戏4X4C语言

关键词: