A crazy guy
  • C写的一个连线游戏

    初学C语言,经过写完这个游戏发现与众多语言有很大差别的,虽说语法都差不多,但不能直接return数组这种让我觉得写C是一项体力活啊

    这个游戏规则为呈现一个九宫格,电脑和人两个玩家对玩,只要有任意一方优先连成一条直线(3个)就算赢

    算下来赢法一共就有8种,横线3条竖线3条对角线2条

    以下是代码

    #include <stdio.h>
    #include <stdbool.h>
    #include <stdlib.h>
    #include <time.h>
    
    bool isDone (int arr[9], char a) {
      int len = 9;
      int len1 = 3;
      int correct = 0;
      bool isWin = false;
    
      for (unsigned int i = 0; i < len; i++) {
        if (arr[i] == a) {
          correct++;
        }
        
        if ((i + 1) % len1 == 0) {
          if (correct == len1) {
            isWin = true;
            break;
          } else {
            correct = 0;
          }
        }
      }
    
      for (unsigned int i = 0; i < len1; i++) {
        correct = 0;
    
        for (unsigned int j = i; j < len; j+=len1) {
          if (arr[j] == a) {
            correct++;
          }
        }
    
        if (correct == 3) {
          isWin = true;
          printf("\n Here 2");
          break;
        }    
      }
    
      if (arr[0] == a && arr[4] == a && arr[8] == a) {
        isWin = true;
      } else if (arr[2] == a && arr[4] == a && arr[6] == a) {
        isWin = true;
      }
    
      return isWin;
    }
    
    int showGrid (int arr[9]) {
      // int len = sizeof(arr) / sizeof(arr[0]);
      int len = 9;
    
      printf("\n");
      for (unsigned int i = 0; i < len; i++) {
        if (arr[i] != 0) {
          printf(" %c ", arr[i]);
        } else {
          printf(" %d ", i + 1);      
        }
    
        if ((i + 1) % 3 == 0) {
          printf("\n");
        }
      }
    
      printf("\n");
      return 0;
    }
    
    int avalible (int arr[9]) {
      // int arrLen = sizeof(arr) / sizeof(arr[0]);
      int arrLen = 9;
      int av = 0;
      for (unsigned int i = 0; i < arrLen; i++) {
        if (arr[i] == 0) {
          av++;
        }
      }
      return av;
    }
    
    int clear (int arr[9]) {
      int len = 9;
      for (unsigned int i = 0; i < len; i++) {
        arr[i] = 0;
      }
    
      return 0;
    }
    
    int random1 (int min, int max) {
      srand(time(NULL));
      return rand()%((max+1)-min) + min;
    }
    
    int main (void) {
      int grid[9] = {0};
      int gridCount = sizeof(grid) / sizeof(grid[0]);
      int times = 0;
      int enter = 0;
      int enterTimes = 0;
      int av = 0;
      bool done = false;
      char a = 65;
      char b = 68;
      char restart = 0;
      showGrid(grid);
    
      printf(" %d", 1 % 3);
      do {
        times++;
        enterTimes = 0;
        enter = 0;
    
        if (times % 2 == 0) {
          enter = random1(1, 9);
          while (grid[enter - 1] != 0) {
            if (enter >= gridCount) {
              enter = 0;
            }
            
            enter++;
          }
    
          printf("\n Computer enter the number : %d", enter);
        } else {
          printf("\n User enter the number : ");
          do {
            scanf(" %d", &enter);        
            
            ++enterTimes;
            if (grid[enter - 1] > 0) {
              printf("\n Error: cannot enter this number. You can enter below number. \n ");
              avalible(grid);
              for (unsigned int i = 0; i < gridCount; i++) {
                if (grid[i] == 0) {
                  printf(" %d ", i + 1);
                }
              }
              printf("\n Re-enter number please \n ");
            }
          } while (grid[enter - 1] > 0);
        }
    
        if (times % 2 == 0) {
          grid[enter - 1] = a;
        } else {
          grid[enter - 1] = b;
        }
    
        showGrid(grid);
        av = avalible(grid);
    
        if (av >= 1) {
          if (times % 2 == 0) {
            done = isDone(grid, a);
          } else {
            done = isDone(grid, b);
          }
    
          if (done) {
            if (times % 2 == 0) { 
              printf("\n Computer win! ");
            } else {
              printf("\n User win! ");
            }
    
            printf("\n Do you wanna restart? (Y/n) ");
            scanf(" %c", &restart);
    
            while (restart != 'y' && restart != 'n') {
              printf("\n Please input y or n ");
              scanf(" %c", &restart);
            }
    
            if (restart == 'y') {
              clear(grid);
              showGrid(grid);
              times = 0;
              done = false;
            } else if (restart == 'n') {
              done = true;
            }
          }
        } else {
          printf("\n Game end! ");
          printf("\n Do you wanna restart? (Y/n) ");
          scanf(" %c", &restart);
    
          while (restart != 'y' && restart != 'n') {
            printf("\n Please input y or n ");
            scanf(" %c", &restart);
          }
    
          if (restart == 'y') {
            clear(grid);
            showGrid(grid);
            times = 0;
            done = false;
          } else if (restart == 'n') {
            done = true;
          }
        }
      } while (!done);
      return 0;
    }
    

    ...

    READ ALL

  • MySQL导入sql文件

    备份数据库的时候多是备份为sql后缀的文件,因此导入sql文件也是恢复数据的必会方法。

    有两种方法导入sql文件

    第一种、直接在命令下敲

    mysql -u root -p database_name < /mysql/data.sql
    
    • database_name 数据库名
    • data.sql 写绝对路径或相对当前路径下

    第二种、登录MySQL后操作

    mysql> source /mysql/data.sql;
    
    • data.sql 文件最好写绝对路径

    其它方法

    可以通过phpmyadmin、SQLyog等数据库管理软件工具导入

    ...

    READ ALL

  • 本站迁回国内机房!

    记得今年春我因忍受不了美国VPS的乌龟速度转投口碑比较好的Linode东京机房。当然,在我决定购买Linode之前在网上进行了大量的搜索比对。当时心里想的是找一个可以稍微贵点,但速度与稳定性要好,一旦选好以后就不再折腾了,因为迁站一次实在很费精力。

    关于选择Linode

    full

    经过一番搜索,发现Linode东京机房是国内最受欢迎的,有不少博文都大赞东京机房的神速。我当时没考虑使用国内的VPS,原因两点:一是国内的质量信不过(无论是售后,技术等等),二是感觉在国内几乎找不到xen的VPS(或者母机系统是Linux的)。于是购买了Linode以后仔细部署了很多安全与优化措施,当时觉得至少两三年不会再动了。

    ...

    READ ALL

  • MySQL设置root密码

    MySQL安装好了以后,默认只有root用户密码为空,这时候我们需要给root用户设置密码保证数据库操作安全,MySQL对于用户的权限与密码都存在mysql库中的user表,最终只要能操作user表,就能修改任何人的权限与密码

    有两种方式设置root密码

    第一种

    /usr/local/mysql/bin/mysqladmin -u root password "123456"
    

    第二种

    update user set password=password('123456') where User='root';
    

    ...

    READ ALL

  • 安装imagick提示[imagick_class.lo] Error 1解决方法

    安装imagick

    [root@**]# tar -xzvf imagick-3.0.1.tgz
    [root@**]# cd imagick-3.0.1
    [root@**]# /usr/local/php/bin/phpize
    [root@**]# ./configure --with-php-config=/usr/local/php/bin/php-config --with-imagick=/usr/local/imagemagick
    [root@**]# make
    

    结果make的时候报错提示

    [imagick_class.lo] Error 1

    ...

    READ ALL

  • PHP警告date() It is not safe to rely on the system

    近来总是有系统邮件提示,开始没在意,后来不断提示就看了一下。提示以下信息

    PHP Warning: date(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Chongqing' for 'CST/8.0/no DST' instead in /data0/htdocs/www.qttc.net/function/function.php on line 542

    ...

    READ ALL

  • 谈谈Web目录部署的一些观点

    本文所针对的对象是那些热爱WEB技术,喜欢动手建站的朋友们。使用PHP框架,开源程序的不推荐,当然Smarty模板的可以留下看看。

    为什么

    也许,这个问题都是很多刚学习的程序员喜欢问的一个问题。如果你没有独立建过一个完整的网站,或者没有应对上千万PV压力的站点经营管理过那么你就不会感觉到目录部署的至关重要。可能很多童鞋认为只要程序写的好,目录都不是问题,程序才是核心。

    其实Web项目目录部署也算是程序设计的一个重要部分,一个好Web目录部署不仅在维护、管理、拓展方便都灵活,对于数据量多的站点更是一种程序设计思想。因此在开始一个Web项目之前,目录的部署应该是首要考虑的。

    ...

    READ ALL

  • JavaScript位置location对象操作例子

    在JavaScript中,使用location对象可以通过很多方式来改变浏览器的位置。看看W3C上的描述location的一句话

    Note: There is no public standard that applies to the location object, but all major browsers support it.

    大意是location对象没有标准定义,但主流浏览器都支持

    看看以下例子看你掌握了几种

    /**
     * JavaScript位置location对象操作例子
     * 琼台博客 www.qttc.net
     */
     
    // 假设初始URL为 https://www.qttc.net/lee/
     
    // 将URL修改为 https://www.qttc.net/lee/#top
    location.hash = '#top';
     
    // 将URL修改为 https://www.qttc.net/lee/?q=php
    location.search = '?q=php';
     
    // 将URL修改为 https://www.lizhong.me/lee/
    location.hostname = 'www.lizhong.me';
     
    // 将URL修改为 https://www.qttc.net/dir/
    location.pathname = 'dir';
     
    // 将URL修改为 https://www.qttc.net:8888/lee/
    location.port = 8888;
    

    ...

    READ ALL

  • MySQL替换函数replace

    一般对于博客程序来说,使用替换语句也许是最有用的一种维护方式了,特别是对于所有博文内容里有统一修改的地方。由于我的博客站点在upload图片上传目录下需要按年建目录,这倒不是什么难点,可是之前上传的图片都要移动到相应的目录里,这样的话博文里所有的图片URL都要改变。

    比如图片a.jpg原来在upload路径下,博文里的地址是

    https://www.qttc.net/static/upload/a.jpg

    现在要把a.jpg移动到upload下的2012文件夹里,那么博文里的地址就需要改变成

    https://www.qttc.net/static/upload/2012/a.jpg

    ...

    READ ALL

  • JavaScript私有变量

    JavaScript没有私有变量这一说,所以实际在开发过程可能很容易就出现变量被破坏是常有的事

    但我们可以通过闭包的方式来解决这个问题,如以下例子

    var instance = (function(){
      var private = {}
    
      return {
        setValue: function(key, val){
          private[key] = val
        },
    
        getValue: function(key) {
          return private[key]
        }
      }
    })()
    
    // Usage
    instance.setValue('name', 'qttc')
    
    var name = instance.getValue('name')
    
    console.log(name) // Output: qttc
    

    ...

    READ ALL