服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C/C++仿华容道小游戏

C/C++仿华容道小游戏

2021-03-25 11:21__lurenjia__ C/C++

这篇文章主要介绍了C/C++仿华容道小游戏的相关资料,模仿实现华容道游戏,感兴趣的朋友可以参考一下

本文实例介绍了C++模仿华容道小游戏实现代码,分享给大家供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
 
#define maxnum 16
#define colnum 4
 
bool numexists(int *numbers, int length, int num);
int getnumber(int **numbers, int randIndex, int *length);
int* initnumbers(void);
void swap(int **a, int **b);
 
int main(int argc, char *argv[])
{
  int *tempnumbers = initnumbers();
  int *randnumbers = initnumbers();
  int **numbers;
  numbers = malloc(maxnum * sizeof(int*));
  //bool a = numexists(numbers, 16, 3);
  //printf("a=%d\n", a);
 
  int length = maxnum;
  srand((unsigned)time(NULL));
  for (int i = 0; i < maxnum; i++)
  {
    int temp = getnumber(&tempnumbers, rand() % length, &length);
    randnumbers[i] = temp;
    numbers[temp] = &randnumbers[i];
    //printf("%d%c", temp, (i + 1) % colnum == 0?'\n':'\t');
  }
  if (tempnumbers != NULL)
    free(tempnumbers);
  while (true)
  {
    system("clear");
    for (int i = 0; i < maxnum; i++)
      printf("%d%c", randnumbers[i], (i + 1) % colnum == 0?'\n':'\t');
 
    printf("move number/ invalid num==exit: ");
    int i;
    if (!scanf("%d", &i))
    {
      printf("game over\n");
      break;
    }
    if (i >= maxnum || i <0)
    {
      printf("sorry, i can't find %d\n", i);
      break;
    }
    unsigned char sign = abs(numbers[i] - numbers[0]);
    switch (sign)
    {
      case 1:
      case 4: swap(&numbers[0], &numbers[i]); break;
    }
 
  }
  if (numbers != NULL)
    free(numbers);
  if (randnumbers != NULL)
    free(randnumbers);
}
 
void swap(int **a, int **b)
{
  int *templocation = *a;
  int tempvalue = **a;
  **a = **b;
  **b = tempvalue;
  *a = *b;
  *b = templocation;
}
 
int* initnumbers(void)
{
  int *numbers = malloc(maxnum * sizeof(int));
  for (int i = 0; i < maxnum; i++)
    numbers[i] = i;
  return numbers;
}
 
int getnumber(int **numbers, int randIndex, int *length)
{
  int result = (*numbers)[randIndex];
  (*numbers)[randIndex] = (*numbers)[--(*length)];
  int *temp = realloc(*numbers, (*length) * sizeof(int));
  *numbers = temp;
  return result;
}
 
bool _numexists(int *numbers, int start, int end, int num)
{
  printf("start: %d, end: %d, num: %d\n", start, end, num);
  if (start == end)
    return numbers[start] == num;
  else
  {
    int middle = (start+end) / 2;
    if (numbers[middle] == num)
      return true;
    else if (numbers[middle] > num)
      return _numexists(numbers, start, middle-1, num);
    else
      return _numexists(numbers, middle+1, end, num);
  }
}
bool numexists(int *numbers, int length, int num)
{
  return _numexists(numbers, 0, length-1, num); 
}

希望本文对大家学习C++程序设计有所帮助。

延伸 · 阅读

精彩推荐