December 1, 20212275 words

C程序设计

This was a project done by me in 2021, just starting in C and using almost a week (5 full days) coding almost all day long without LLMs.

大作业 by 陈加木

alt text

游戏介绍

Generals.io 是一个最近国内很流行的 io 游戏,游戏由普林斯顿大学的华人 victor zhou 设计。进入到网址 generals.io 就可以玩,有几种模式,包括 ffa,1v1,2v2,custom game。1v1 游戏就是双人对战,双人要同时在不同电脑上进入到游戏。

主要规则是这样的,每个人有一个家,家里每秒钟兵力会+1,同时每一步玩家可以移动最多两次自己的兵,去占地,吃城堡等。地图是标准的长方形地图,上面有几种类型,家,山,城堡,对方的家,地。山就是兵不能到的地方。城堡可以用 40-50 兵左右吃掉,然后就归自己所有了,归自己所有的城堡每秒兵加一,但是别人也可以占领自己的城堡,这样就是别人的并加一了。地就是可以走到的地方,兵每 25 秒才+1。如果我方兵碰到了对方兵,(就是吃到了对方的地或者城堡),如果我兵多,那么就能吃掉对方,地或者城堡也归我方,但兵力会损失,损失的值就是对方那个地上的兵力。如果我方兵少,就吃不掉,但对方那个地或者城堡上的兵力会对应的减去我方的兵力。FFA 就是每个人对战多人。 文字难以描述清,上 generals.io 网站玩玩吧!

程序功能

我自己在 2021 年的 4 月份,开始接触到这个游戏。这是因为我高中班里经常喜欢一起玩。这个游戏非常的好玩,我也非常的沉迷。Generals.io 现在里面还有当时一些对战记录。

我决定自己用 C 语言和 easyx 库写出来 Generals.io 这个游戏,我花了很长的时间和很多的精力,写了个 1v1 的程序。由于水平不够,现在只能人机对战,然而电脑只能乱走,所以基本上就是单人游戏了。

我的程序主要功能

  1. 播放音乐(可以自己随便选择)
  2. 可以设置地图的宽度,高度,和游戏的速度,城堡的数量,山的数量
  3. 有自我介绍,和 2048 游戏嵌在里面
  4. 可以自己命名玩家
    5 .实现 generals.io 人机对战
  5. 有游戏的回放
  6. 游戏里面的功能:
  • 可以吃城堡,占地; 自己的家被掏掉了会 exit;
  • wasd 上下左右移动;
  • 鼠标点击的方块是即将操作的方块;
  • 双击或点击选中的方块或者按 z 后移动实现分半;
  • q 清除所有操作;
  • e 撤销上次操作;
  • 方块内的数字表示这个格子上玩家的士兵数;
  • 移动时士兵总量不改变,每个格子至少需要 1 个兵镇守(除了被对方撞了但没有被对方占领的情况);
  • 每回合移动两步;
  • 每 25 回合每个自己占领的地上增加一个兵;
  • 每一回合大本营或兵营增加一个兵;
  • 兵与兵的相碰规则:遵循作差不变原则;
  • 占领对方大本营即算胜利;

编程思路

  1. 搞两个数组 a[100][100],b[100][100]把地图存的放进去
  2. 每一步检查如果是家或者城堡就兵++
  3. 如果对方或者自己被吃了就 exit
  4. 播放音乐
    就是点击进去,检测鼠标是否点击,然后播放
  5. 检测键盘是否点击了,如果是就执行操作

用到的知识点

  • 字符串操作 strlen
  • easyx 的图片操作
  • 导入音乐
  • 读入鼠标信息
  • clock 函数计时
  • rand 函数设计家的位置
  • 队列数据结构的使用
  • 用到了 easyx 里面的 putimage 函数 putimage 函数要注意 resize,否则先放了大的图片,然后再放小的图片会出问题
  • 用到了 easyx 里的 mcisendstring 函数,这个就是播放音乐的。
  • 用到了 easyx 里面的 outtextxy 函数,和 settextstyle,solidrectangele 函数。solidrectangle 作用是清除每一步后面的残留。settextstyle 函数就是把字设置格式
  • 还用到了那些 beginbatchdraw,flushbatchdraw 等,initgraph,closegraph。
  • 这些是操作画面的,开始图像的。
    颜色
    EasyX 使用 24bit 真彩色,不支持调色板模式。

解题思路

alt text

I made a bot.

I used a two-dimensional array to save the map.

There are a few points which is really hard for me. Especially in printing the map. I used a variable t to print the lines for the map. Then on each i,j of the map, I just go to i50 and j50 and print the corresponding thing, ie, castle, home, or whatever. Then each turn, I used this way clock()-clock()>singletime, which is true is it is more than singletime since last turn. This way, I can increase the number of armies in the castle and home in the map each turn and not go wrong.

Each turn there is a computer player.

The computer player move randomly. I haven’t thought of any way to win, though, since I am not an expert yet. The computer player just puts what he goes into a queue called kkk1(), and function go2(), which can help to pop from the queue and move the armies. Plus, also the music color is different every time you enter the program. This is made by setting it RGB (random, random, random), which I used the rand function in the corresponding library.

In the setname() function I printed a rectangle and used the function getch(), so that each time you input a character it will show on the screen as followed. However, the name must not be too lone. I set the name originally as anonymous. Then you can change your username the way you want.

Every half second, I print a | in the box, which makes it seems like the normal input. You can see my profile on the top left corner. You can enter the 2048 game also.

I used getmousemsg and mousehit function. If the left button of the mouse is hit, then I will detect where it is and choose to operate.

I used the image from Generals.io. When putting image, I use resize the image so that the image will not go bad it its lengths and heights are changed.

Now I print the turns and two players on top of the screen so that you can see how your enemy, in this case, the stupid computer program, is performing.
The white line outside of a block is made by going to the corresponding line and printing a rectangle.

And I used variable px and py for you to move your army on the map freely.

If you move your arm the number of it is detected from the two dimensional array a[100][100].
Then if you collide with the enemy or moved into a castle it is counted and determined whether or not you can eat them.

When the M,N, height weight, of the map is too big, I used a small t, the weight of the square of each block to print out the map. Else if M or N is too big, t became bigger.

Using rand to determine the density of Castle and Mountain.

So its basically just first rand()%n, and then you count down the possibility, and divide it by n/n+1. First determine where your home and enemy’s home is so that they will not collide. If your home is absent it is a big catastrophe.

有一个库叫 bits/stdc+还有就是 easyx 拓展库

So basically, I just "cloned" the game online. There are a few more points I added to the program, though.
First, remember in the game how you can’t change your name. Well, in my program, you can change your name to anything multiple times.

What’s more, I created a setting where you can set all the moods for the game beforehand, You can set the map at any size you want, and set the speed. If you set the height and weight too high, you’d expect to see a pretty small map. Else if you set them too low, you would expect to see a map where you home and armies are bigger. I made this all in my program, in the t variable.

Speed: you can set the speed at any rate you want. You can choose to play the game very fast or very slow.
Castle and Mountain: you can set the mountain density high. When once I set the city density so high that all around me there’s nothing but castle! It is very fun and indeed a new experience for Generals players.

Additionally, you can listen to music! You can listen to One Direction while playing the game. If you don’t want to listen anymore, just click the music button and mute.

From pop songs to opera, you ‘d expect to listen to the most popular songs in the past decade in my program.
Replay: I used a three-dimension array to put all I need in it. Each step the two-dimensional array will be put in a[turn][i][j]. when you click the button on the left top side, you can easily go and see the replay. Note that, however, you cannot enter at the beginning. This is because that you haven’t played the game, and that the three-dimensional array is left empty.

I relied largely on the book K&R(Brian Kernighan and Dennis Ritchie’s C Programming Language). I think it is a great book for me to study C language for it taught me how to make my code compact and all the functions in libraries. Some other books from China are not so good, they teach you how to program but nothing else.

So the programs have been completed. In my personal opinion, they are very easy. Anyone who had read Kernighan and Ritchie’s C Programming language can do it. There are some mistakes in the problem, I think, in which there is no instructions for what kind of numbers I should input and how to define pi, etc. There is no use of arrays in them, nor pointers or data structures. They are just basic calculating problems designed for new learners. In these programs I used a lot of functions, which I think is unnecessary. But because when I first write the program, I wrote them all together so functions are essential for debugging and making the program look clearer and readable. You can see my source code in the txt which is also in the file. I prefer no instructions for input for they make your program seem much longer and less beautiful. There may be some errors in the program because bitsstd is new, and also there is the problem with characters. Some IDEs can’t recognize Chinese symbols. Coding is very interesting. I hope I can learn much more than this from the C programming class and I hope that one day I can qualify for an excellent programmer.

Loading...




Loading...